import {z} from 'zod' export type GetApiProvidersArgs = {from_id?: (string | null) | undefined, from_date?: (string | null) | undefined, country_code?: (string | null) | undefined, mode?: ('oauth' | 'web' | 'api' | 'file') | undefined, include_fake_providers?: boolean | undefined, include_provider_fields?: boolean | undefined, provider_key_owner?: ('client' | 'saltedge') | undefined, include_payments_fields?: boolean | undefined}; export type GetApiProvidersOptions = {baseUrl?: string | undefined, appId?: string | undefined, secret?: string | undefined, fetch?: typeof fetch | undefined}; export const provider = z.object({id: z.string(), code: z.string(), name: z.string(), mode: z.string(), status: z.string(), automatic_fetch: z.boolean(), dynamic_registration_code: z.string().optional(), group_code: z.string().optional(), group_name: z.string().optional(), hub: z.string().optional(), customer_notified_on_sign_in: z.boolean(), interactive: z.boolean(), identification_mode: z.enum(["client", "saltedge"]), instruction: z.string(), home_url: z.string(), login_url: z.string(), logo_url: z.string(), country_code: z.string(), refresh_timeout: z.number().int(), holder_info: z.array(z.string()), max_consent_days: z.number().int(), created_at: z.string(), updated_at: z.string(), timezone: z.string(), max_interactive_delay: z.number().int(), optional_interactivity: z.boolean(), regulated: z.boolean(), max_fetch_interval: z.number().int(), custom_pendings: z.boolean().optional(), custom_pendings_period: z.number().int().optional(), supported_fetch_scopes: z.array(z.string()), supported_account_extra_fields: z.array(z.string()), supported_transaction_extra_fields: z.array(z.string()), supported_account_natures: z.array(z.string()), supported_account_types: z.array(z.enum(["personal", "business"])), identification_codes: z.array(z.string()), bic_codes: z.array(z.string()), supported_iframe_embedding: z.boolean(), payment_templates: z.array(z.string()), supported_payment_fields: z.object({}).optional(), required_payment_fields: z.object({}).optional()}); export const metaObject = z.object({next_id: z.string().optional(), next_page: z.string().optional()}); export const providersResponse = z.object({data: z.array(provider).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 providers * * GET /providers */ export const getApiProviders = async (args: GetApiProvidersArgs, options?: GetApiProvidersOptions) => { 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.from_date != null) query.append("from_date", String(args.from_date)) if (args.country_code != null) query.append("country_code", String(args.country_code)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.include_fake_providers != null) query.append("include_fake_providers", String(args.include_fake_providers)) if (args.include_provider_fields != null) query.append("include_provider_fields", String(args.include_provider_fields)) if (args.provider_key_owner != null) query.append("provider_key_owner", String(args.provider_key_owner)) if (args.include_payments_fields != null) query.append("include_payments_fields", String(args.include_payments_fields)) if (appId !== undefined) headers.set("App-id", appId) if (secret !== undefined) headers.set("Secret", secret) const url = `${baseUrl.replace(/\/+$/, '')}/providers${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(providersResponse, await res.json()) };