import {z} from 'zod' export type RetrieveDataReq = {merchant_id?: string | undefined, merchant_customer_id?: string | undefined, card_reference?: string | undefined}; export type JWEReq = {header?: string | undefined, iv?: string | undefined, encrypted_payload?: string | undefined, tag?: string | undefined, encrypted_key?: string | undefined}; export type CreateApiDataRetrieveArgs = {'x-tenant-id'?: string | undefined, body: RetrieveDataReq | JWEReq}; export type CreateApiDataRetrieveOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const cardNetwork = z.enum(["Visa", "Mastercard", "AmericanExpress", "JCB", "DinersClub", "Discover", "CartesBancaires", "UnionPay", "Interac", "RuPay", "Maestro", "Star", "Pulse", "Accel", "Nyce", "Prop", "PrivateLabel", "Dinacard"]); export const card = z.object({card_number: z.string(), card_exp_month: z.string(), card_exp_year: z.string(), card_holder_name: z.string(), card_cvc: z.string(), card_issuer: z.string().nullable().optional(), card_network: cardNetwork.nullable().optional(), card_type: z.string().nullable().optional(), card_issuing_country: z.string().nullable().optional(), card_issuing_country_code: z.string().nullable().optional(), bank_code: z.string().nullable().optional(), nick_name: z.string().nullable().optional()}); export const retrieveRes = z.union([z.object({card: card.optional()}), z.object({enc_card_data: z.string().optional()})]); export const retrieveDataRes = z.object({status: z.literal("Ok").optional(), payload: retrieveRes.optional()}); export const jWERes = z.object({header: z.string().optional(), iv: z.string().optional(), encrypted_payload: z.string().optional(), tag: z.string().optional(), encrypted_key: z.string().optional()}); export const createApiDataRetrieveResponse = z.union([retrieveDataRes, jWERes]); 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 }; /** * Retrieve Data from Locker * * POST /data/retrieve */ export const createApiDataRetrieve = async (args: CreateApiDataRetrieveArgs, options: CreateApiDataRetrieveOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-tenant-id'] != null) headers.set("x-tenant-id", String(args['x-tenant-id'])) const url = `${baseUrl.replace(/\/+$/, '')}/data/retrieve${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiDataRetrieveResponse, await res.json()) };