import {z} from 'zod' export type GetApiProvidersProviderCodeArgs = {provider_code: string, include_payments_fields?: boolean | undefined}; export type GetApiProvidersProviderCodeOptions = {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 providerResponse = z.object({data: provider.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 }; /** * Show a provider * * GET /providers/{provider_code} */ export const getApiProvidersProviderCode = async (args: GetApiProvidersProviderCodeArgs, options?: GetApiProvidersProviderCodeOptions) => { 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.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/${encodeURIComponent(String(args.provider_code))}${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(providerResponse, await res.json()) };