import {z} from 'zod' 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 RefreshSessionRequestBody = {data: {connection_id: string, attempt?: AttemptRequestBody | undefined, daily_refresh?: boolean | undefined, return_connection_id?: boolean | undefined, provider_modes?: (Array<'oauth' | 'web' | 'api' | 'file'>) | undefined, javascript_callback_type?: ('iframe' | 'external_saltbridge' | 'external_notify' | 'post_message' | null) | undefined, categorization?: ('none' | 'personal' | 'business') | undefined, lost_connection_notify?: boolean | undefined, include_fake_providers?: boolean | undefined, return_error_class?: boolean | undefined, theme?: ('default' | 'dark') | undefined, connect_template?: string | undefined, show_connect_overview?: boolean | undefined}}; export type CreateApiConnectSessionsRefreshArgs = {body: RefreshSessionRequestBody}; export type CreateApiConnectSessionsRefreshOptions = {baseUrl?: string | undefined, appId?: string | undefined, secret?: string | undefined, fetch?: typeof fetch | undefined}; export const connectSessionResponse = z.object({data: z.object({expires_at: z.string().optional(), connect_url: 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 }; /** * Create Connect session to refresh * * POST /connect_sessions/refresh */ export const createApiConnectSessionsRefresh = async (args: CreateApiConnectSessionsRefreshArgs, options?: CreateApiConnectSessionsRefreshOptions) => { 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(/\/+$/, '')}/connect_sessions/refresh${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(connectSessionResponse, await res.json()) };