import {z} from 'zod' export type CardNetwork = 'Visa' | 'Mastercard' | 'AmericanExpress' | 'JCB' | 'DinersClub' | 'Discover' | 'CartesBancaires' | 'UnionPay' | 'Interac' | 'RuPay' | 'Maestro' | 'Star' | 'Pulse' | 'Accel' | 'Nyce' | 'Prop' | 'PrivateLabel' | 'Dinacard'; export type Card = {card_number: string, card_exp_month: string, card_exp_year: string, card_holder_name: string, card_cvc: string, card_issuer?: (string | null) | undefined, card_network?: (CardNetwork | null) | undefined, card_type?: (string | null) | undefined, card_issuing_country?: (string | null) | undefined, card_issuing_country_code?: (string | null) | undefined, bank_code?: (string | null) | undefined, nick_name?: (string | null) | undefined}; export type StoreDataReq = {merchant_id?: string | undefined, merchant_customer_id?: string | undefined, requester_card_reference?: string | undefined, card?: Card | undefined, enc_card_data?: string | undefined}; export type JWEReq = {header?: string | undefined, iv?: string | undefined, encrypted_payload?: string | undefined, tag?: string | undefined, encrypted_key?: string | undefined}; export type CreateApiDataAddArgs = {'x-tenant-id'?: string | undefined, body: StoreDataReq | JWEReq}; export type CreateApiDataAddOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const storeDataRes = z.object({status: z.literal("Ok").optional(), payload: z.object({card_reference: z.string().optional()}).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 createApiDataAddResponse = z.union([storeDataRes, 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 }; /** * Add Data in Locker * * POST /data/add */ export const createApiDataAdd = async (args: CreateApiDataAddArgs, options: CreateApiDataAddOptions) => { 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/add${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(createApiDataAddResponse, await res.json()) };