import {z} from 'zod' export type GetApiTokenRecordsRecordIdArgs = {recordId: string, includeVolatileInfo?: boolean | undefined, includeDetokenizations?: boolean | undefined}; export type GetApiTokenRecordsRecordIdOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTokenRecordsRecordIdResponse = z.object({id: z.string().optional(), type: z.string().optional(), fingerprint: z.string().optional(), fields: z.array(z.object({alias: z.string().optional(), recordId: z.string().optional(), recordType: z.string().optional(), fieldType: z.string().optional(), displayableValue: z.string().nullable().optional(), allowedDetokenizationCount: z.number().int().nullable().optional(), allowedSdkRevealCount: z.number().int().nullable().optional(), hasValue: z.boolean().nullable().optional(), maximumExpirationDate: z.string().nullable().optional(), detokenizations: z.array(z.object({lastAttemptedAt: z.string().optional(), count: z.number().int().optional(), type: z.enum(["detokenization", "sdkReveal"]).optional()})).nullable().optional()})).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get record and its fields by ID * * GET /token/records/{recordId} */ export const getApiTokenRecordsRecordId = async (args: GetApiTokenRecordsRecordIdArgs, options?: GetApiTokenRecordsRecordIdOptions) => { const {baseUrl = "https://api.staging.payrails.io", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeVolatileInfo != null) query.append("includeVolatileInfo", String(args.includeVolatileInfo)) if (args.includeDetokenizations != null) query.append("includeDetokenizations", String(args.includeDetokenizations)) if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/token/records/${encodeURIComponent(String(args.recordId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiTokenRecordsRecordIdResponse, await res.json()) };