import {z} from 'zod' export type GetApiConsentsArgs = {from_id?: string | undefined, connection_id?: string | undefined, customer_id?: string | undefined}; export type GetApiConsentsOptions = {baseUrl?: string | undefined, appId?: string | undefined, secret?: string | undefined, fetch?: typeof fetch | undefined}; export const consent = z.object({id: z.string(), connection_id: z.string(), customer_id: z.string(), scopes: z.array(z.enum(["account_details", "holder_information", "transactions_details"])), period_days: z.number().int(), expires_at: z.string(), from_date: z.string(), to_date: z.string(), status: z.enum(["active", "expired", "revoked"]), collected_by: z.enum(["client", "saltedge"]), revoked_at: z.string(), revoke_reason: z.enum(["expired", "client", "provider", "saltedge"]), created_at: z.string(), updated_at: z.string()}); export const metaObject = z.object({next_id: z.string().optional(), next_page: z.string().optional()}); export const consentsResponse = z.object({data: z.array(consent).optional(), meta: metaObject.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 of consents * * GET /consents */ export const getApiConsents = async (args: GetApiConsentsArgs, options?: GetApiConsentsOptions) => { const {baseUrl = "https://www.saltedge.com/api/v5", fetch: fetchFn = fetch, appId, secret} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from_id != null) query.append("from_id", String(args.from_id)) if (args.connection_id != null) query.append("connection_id", String(args.connection_id)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (appId !== undefined) headers.set("App-id", appId) if (secret !== undefined) headers.set("Secret", secret) const url = `${baseUrl.replace(/\/+$/, '')}/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(consentsResponse, await res.json()) };