import {z} from 'zod' export type GetApiApiConsentsArgs = {page_size?: number | undefined, page?: number | undefined, omit?: string | undefined, fields?: string | undefined, user_document: string}; export type GetApiApiConsentsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string(); export const createdAt = z.string(); export const enumConsentStatus = z.enum(["AUTHORISED", "AWAITING_AUTHORISATION_CONFIRMATION", "AWAITING_AUTHORISATION", "REJECTED", "EXPIRED", "null"]).nullable(); export const consents = z.object({id: id.optional(), display_name: z.string().max(128).nullable().optional(), document_number: z.string().optional(), belvo_institution_name: z.string().optional(), institution_display_name: z.string().optional(), institution_icon_logo: z.string().optional(), belvo_organization_name: z.string().optional(), link_id: z.string().optional(), user_name: z.string().nullable().optional(), expired_at: z.string().optional(), undefined_consent_expiration: z.boolean().optional(), consent_duration: z.number().int().nullable().optional(), created_at: createdAt.optional(), status: enumConsentStatus.optional(), permissions: z.object({ACCOUNTS: z.array(z.string()).optional(), CREDIT_CARDS: z.array(z.string()).optional(), CREDIT_OPERATIONS: z.array(z.string()).optional(), REGISTER: z.array(z.string()).optional(), RESOURCES: z.array(z.string()).optional(), INVESTMENTS: z.array(z.string()).optional()}).optional()}); export const getApiApiConsentsResponse = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(consents).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 consents * * GET /api/consents/ */ export const getApiApiConsents = async (args: GetApiApiConsentsArgs, options?: GetApiApiConsentsOptions) => { 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.page_size != null) query.append("page_size", String(args.page_size)) if (args.page != null) query.append("page", String(args.page)) if (args.omit != null) query.append("omit", String(args.omit)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.user_document != null) query.append("user_document", String(args.user_document)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/consents/${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(getApiApiConsentsResponse, await res.json()) };