import {z} from 'zod' export type Expiration = {year?: number | undefined, month?: number | undefined}; export type Segment = 'SEGMENT_A' | 'SEGMENT_B' | 'SEGMENT_C' | 'SEGMENT_D' | 'SEGMENT_E' | 'SEGMENT_F'; export type PinStatus = 'D' | 'W' | 'S'; export type DigitalLayoutCode = string; export type ScheduledClosingType = 'TIMEBASED'; export type ScheduledClosingTime = string; export type ScheduledClosing = {type?: ScheduledClosingType | undefined, time?: ScheduledClosingTime | undefined}; export type KeyValuePair = {key: string, value: string}; export type AdditionalValues = Array; export type ProductCodeInRequest = string; export type CreateDebitResourceBody = {expiration?: Expiration | undefined, segment?: Segment | undefined, pinStatus?: PinStatus | undefined, digitalLayoutCode?: DigitalLayoutCode | undefined, scheduledClosing?: ScheduledClosing | undefined, additionalValues?: AdditionalValues | undefined, encryptedData?: string | undefined, productCode?: ProductCodeInRequest | undefined}; export type CreateApiV4DEBITCustomerCustomerIdArgs = {auditUser: string, customerId: unknown, body: CreateDebitResourceBody}; export type CreateApiV4DEBITCustomerCustomerIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const resourceResponse = z.object({description: z.string().optional(), id: z.string().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 }; /** * Create debit account * * POST /v4/DEBIT/customer/{customerId} */ export const createApiV4DEBITCustomerCustomerId = async (args: CreateApiV4DEBITCustomerCustomerIdArgs, options: CreateApiV4DEBITCustomerCustomerIdOptions) => { 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.auditUser != null) query.append("auditUser", String(args.auditUser)) const url = `${baseUrl.replace(/\/+$/, '')}/v4/DEBIT/customer/${encodeURIComponent(String(args.customerId))}${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(resourceResponse, await res.json()) };