import {z} from 'zod' export type IssuingCardId = string; export type Type3dsSecurityPair = {question: string, answer: string}; export type Type3dsLocale = 'en-US' | 'fr-FR'; export type Type3dsPhoneNumber = {country_code: string, number: string}; export type Type3dsPassword = string; export type PatchApiIssuingCardsCardId3dsEnrollmentArgs = {cardId: IssuingCardId, body: {security_pair?: Type3dsSecurityPair | undefined, locale?: Type3dsLocale | undefined, phone_number?: Type3dsPhoneNumber | undefined} | {password?: Type3dsPassword | undefined, locale?: Type3dsLocale | undefined, phone_number?: Type3dsPhoneNumber | undefined}}; export type PatchApiIssuingCardsCardId3dsEnrollmentOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const issuingLink = z.object({href: z.string().max(100), actions: z.array(z.enum(["GET", "POST", "PUT", "DELETE", "PATCH"]).max(100)), types: z.array(z.string().max(100))}); export const schema3dsLink = z.object({self: issuingLink}); export const schema3dsEnrollmentPatchResponse = z.object({_links: schema3dsLink.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 }; /** * Update a card's 3DS details * * PATCH /issuing/cards/{cardId}/3ds-enrollment */ export const patchApiIssuingCardsCardId3dsEnrollment = async (args: PatchApiIssuingCardsCardId3dsEnrollmentArgs, options: PatchApiIssuingCardsCardId3dsEnrollmentOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/issuing/cards/${encodeURIComponent(String(args.cardId))}/3ds-enrollment${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(schema3dsEnrollmentPatchResponse, await res.json()) };