import {z} from 'zod' export type GetApiApiConsentsIdArgs = {id: string}; export type GetApiApiConsentsIdOptions = {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 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 }; /** * Get a consent's details * * GET /api/consents/{id}/ */ export const getApiApiConsentsId = async (args: GetApiApiConsentsIdArgs, options?: GetApiApiConsentsIdOptions) => { 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 (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/consents/${encodeURIComponent(String(args.id))}/${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(consents, await res.json()) };