import {z} from 'zod' export type IssuingReference = string; export type IssuingEntityId = string; export type IssuingEmail = string; export type IssuingPhoneNumber = {country_code?: string | undefined, number?: string | undefined}; export type IssuingAddress = {address_line1: string, address_line2?: string | undefined, city: string, state?: string | undefined, zip: string, country: string}; export type AddCardholderRequest = {type: string, reference?: IssuingReference | undefined, entity_id: IssuingEntityId, first_name: string, middle_name?: string | undefined, last_name: string, email?: IssuingEmail | undefined, phone_number?: IssuingPhoneNumber | undefined, date_of_birth?: string | undefined, billing_address: IssuingAddress, residency_address?: IssuingAddress | undefined}; export type CreateApiIssuingCardholdersArgs = {body: AddCardholderRequest}; export type CreateApiIssuingCardholdersOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const issuingCardholderId = z.string().min(30).max(30); export const issuingClientId = z.string().min(30).max(30); export const issuingEntityId = z.string().min(30).max(30); export const issuingReference = z.string().max(256); 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 issuingCardholderLinks = z.object({self: issuingLink, cards: issuingLink.optional()}); export const addCardholderResponse = z.object({id: issuingCardholderId, client_id: issuingClientId, entity_id: issuingEntityId, type: z.literal("individual"), status: z.enum(["active", "pending", "restricted", "requirements_due", "inactive", "rejected"]), reference: issuingReference.optional(), created_date: z.string().optional(), last_modified_date: z.string().optional(), _links: issuingCardholderLinks}); 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 }; /** * Create a cardholder * * POST /issuing/cardholders */ export const createApiIssuingCardholders = async (args: CreateApiIssuingCardholdersArgs, options: CreateApiIssuingCardholdersOptions) => { 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/cardholders${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(addCardholderResponse, await res.json()) };