import {z} from 'zod' export type ConsentRequestBody = {scopes: Array<'account_details' | 'holder_information' | 'transactions_details'>, from_date?: string | undefined, to_date?: (string | null) | undefined, period_days?: number | undefined}; export type AttemptRequestBody = {fetch_scopes?: (Array<'accounts' | 'accounts_without_balance' | 'holder_info' | 'transactions'>) | undefined, from_date?: string | undefined, to_date?: string | undefined, fetched_accounts_notify?: boolean | undefined, custom_fields?: Record | undefined, locale?: string | undefined, include_natures?: (Array | null) | undefined, customer_last_logged_at?: string | undefined, exclude_accounts?: Array | undefined, store_credentials: boolean, unduplication_strategy?: string | undefined, user_present?: boolean | undefined, return_to?: string | undefined}; export type OauthConnectRequestBody = {data: {customer_id: string, country_code: string, provider_code: string, consent: ConsentRequestBody, attempt?: AttemptRequestBody | undefined, daily_refresh?: boolean | undefined, return_connection_id?: boolean | undefined, categorization?: ('none' | 'personal' | 'business') | undefined, include_fake_providers?: boolean | undefined}}; export type CreateApiOauthProvidersCreateArgs = {body: OauthConnectRequestBody}; export type CreateApiOauthProvidersCreateOptions = {baseUrl?: string | undefined, appId?: string | undefined, secret?: string | undefined, fetch?: typeof fetch | undefined}; export const oauthConnectResponse = z.object({data: z.object({connection_id: z.string().optional(), connection_secret: z.string().optional(), attempt_id: z.string().optional(), token: z.string().optional(), expires_at: z.string().optional(), redirect_url: z.string().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 }; /** * Create connection for OAuth provider * * POST /oauth_providers/create */ export const createApiOauthProvidersCreate = async (args: CreateApiOauthProvidersCreateArgs, options?: CreateApiOauthProvidersCreateOptions) => { const {baseUrl = "https://www.saltedge.com/api/v5", fetch: fetchFn = fetch, appId, secret} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (appId !== undefined) headers.set("App-id", appId) if (secret !== undefined) headers.set("Secret", secret) const url = `${baseUrl.replace(/\/+$/, '')}/oauth_providers/create${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(oauthConnectResponse, await res.json()) };