import {z} from 'zod' export type GetApiCustomFieldsArgs = {limit?: number | undefined, starting_after?: string | undefined, sort_order?: ('asc' | 'desc') | undefined, start_date?: string | undefined, end_date?: string | undefined, key?: string | undefined, object_type?: (Array<'payment_order' | 'expected_payment' | 'transaction' | 'direct_debit_mandate' | 'internal_account' | 'external_account' | 'connected_account'>) | undefined, type?: ('select' | 'text') | undefined}; export type GetApiCustomFieldsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const customFieldValuesCreation = z.object({key: z.string(), name: z.string()}); export const customFieldDetails = z.object({key: z.string(), name: z.string(), type: z.enum(["select", "text"]), object_types: z.array(z.enum(["payment_order", "expected_payment", "transaction", "direct_debit_mandate", "internal_account", "external_account", "connected_account"])), values: z.array(customFieldValuesCreation), created_at: z.string()}); export const customFieldSearchResult = z.object({records: z.array(customFieldDetails).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 }; /** * List custom fields * * GET /custom_fields */ export const getApiCustomFields = async (args: GetApiCustomFieldsArgs, options?: GetApiCustomFieldsOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.key != null) query.append("key", String(args.key)) for (const item of args.object_type ?? []) query.append("object_type", String(item)) if (args.type != null) query.append("type", String(args.type)) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/custom_fields${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(customFieldSearchResult, await res.json()) };