import {z} from 'zod' export type BankAccountSubClassificationEnum = 'BANK' | 'PETTY_CASH' | 'CREDIT_CARD'; export type ApiV1BankAccount = {account: string, classification: string, created_ts: string, currency: string, id: string, legacy_id: string, modified_ts: string, name: string, sub_classification: BankAccountSubClassificationEnum}; export type CreateApiBankAccountsArgs = {'X-Wafeq-Idempotency-Key'?: string | undefined, expand?: string | undefined, body: ApiV1BankAccount}; export type CreateApiBankAccountsOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bankAccountSubClassificationEnum = z.enum(["BANK", "PETTY_CASH", "CREDIT_CARD"]); export const apiV1BankAccount = z.object({account: z.string(), classification: z.string(), created_ts: z.string(), currency: z.string(), id: z.string(), legacy_id: z.string(), modified_ts: z.string(), name: z.string(), sub_classification: bankAccountSubClassificationEnum}); 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 bank account * * POST /bank-accounts/ */ export const createApiBankAccounts = async (args: CreateApiBankAccountsArgs, options?: CreateApiBankAccountsOptions) => { const {baseUrl = "https://api.wafeq.com/v1", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Wafeq-Idempotency-Key'] != null) headers.set("X-Wafeq-Idempotency-Key", String(args['X-Wafeq-Idempotency-Key'])) if (args.expand != null) query.append("expand", String(args.expand)) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/bank-accounts/${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(apiV1BankAccount, await res.json()) };