import {z} from 'zod' export type EnumCollectionInstructionStatus = 'PROCESSING' | 'COMPLETED' | 'CANCELLED'; export type GetApiApiCollectionInstructionsArgs = {status?: EnumCollectionInstructionStatus | undefined, consent_id?: string | undefined, created_after?: string | undefined, created_before?: string | undefined}; export type GetApiApiCollectionInstructionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const enumCollectionInstructionStatus = z.enum(["PROCESSING", "COMPLETED", "CANCELLED"]); export const collectionInstructionSummary = z.object({id: z.string(), link_id: z.string(), consent_id: z.string().nullable(), status: enumCollectionInstructionStatus, created_at: z.string(), updated_at: z.string()}); export const getApiApiCollectionInstructionsResponse = z.array(collectionInstructionSummary); 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 collection instructions * * GET /api/collection-instructions/ */ export const getApiApiCollectionInstructions = async (args: GetApiApiCollectionInstructionsArgs, options?: GetApiApiCollectionInstructionsOptions) => { const {baseUrl = "https://sandbox.belvo.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.consent_id != null) query.append("consent_id", String(args.consent_id)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/collection-instructions/${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(getApiApiCollectionInstructionsResponse, await res.json()) };