import {z} from 'zod' export type CreateSessionRequest = {supported_financial_institution_aggregators: Array<'akoya' | 'basiq' | 'belvo' | 'finicity' | 'finverse' | 'flinks' | 'mono' | 'mx' | 'plaid' | 'saltedge' | 'snaptrade' | 'sophtron' | 'teller' | 'truelayer'>, products: Array<'account_details' | 'balance' | 'ownership' | 'transactions' | 'investments' | 'assets'>, country_codes?: (Array<'AE' | 'AM' | 'AR' | 'AT' | 'AU' | 'BD' | 'BE' | 'BG' | 'BH' | 'BM' | 'BN' | 'BR' | 'BW' | 'BY' | 'CA' | 'CH' | 'CI' | 'CL' | 'CM' | 'CN' | 'CO' | 'CZ' | 'DE' | 'DO' | 'DZ' | 'EC' | 'EG' | 'ES' | 'FI' | 'FK' | 'FR' | 'GB' | 'GG' | 'GH' | 'GM' | 'GR' | 'HK' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IT' | 'JE' | 'JO' | 'JP' | 'KE' | 'KH' | 'KR' | 'KW' | 'LA' | 'LB' | 'LK' | 'LT' | 'LU' | 'MC' | 'MD' | 'MK' | 'MO' | 'MT' | 'MU' | 'MV' | 'MX' | 'MY' | 'NG' | 'NL' | 'NP' | 'NZ' | 'OM' | 'PE' | 'PH' | 'PK' | 'PL' | 'PT' | 'QA' | 'RO' | 'RU' | 'SA' | 'SE' | 'SG' | 'SK' | 'SL' | 'TH' | 'TR' | 'TW' | 'TZ' | 'UA' | 'UG' | 'US' | 'UY' | 'VN' | 'ZA' | 'ZM' | 'ZW'>) | undefined, entity: {id: string, name?: string | undefined, email?: string | undefined, phone?: string | undefined}, access_token?: string | undefined, is_web_view?: boolean | undefined}; export type CreateApiV1SessionArgs = {'Fuse-Client-Id': string, 'Fuse-Api-Key': string, 'Plaid-Client-Id'?: string | undefined, 'Plaid-Secret'?: string | undefined, 'Teller-Application-Id'?: string | undefined, 'Teller-Certificate'?: string | undefined, 'Teller-Private-Key'?: string | undefined, 'Teller-Token-Signing-Key'?: string | undefined, 'Teller-Signing-Secret'?: string | undefined, 'Mx-Client-Id'?: string | undefined, 'Mx-Api-Key'?: string | undefined, 'Snaptrade-Client-Id'?: string | undefined, 'Snaptrade-Consumer-Key'?: string | undefined, 'Flinks-Ca-Customer-Id'?: string | undefined, 'Flinks-Us-Customer-Id'?: string | undefined, 'Flinks-Us-Instance-Id'?: string | undefined, 'Flinks-Ca-Instance-Id'?: string | undefined, 'Mono-Public-Key'?: string | undefined, 'Mono-Secret-Key'?: string | undefined, 'Mono-Webhook-Secret'?: string | undefined, 'TrueLayer-Client-Id'?: string | undefined, 'TrueLayer-Client-Secret'?: string | undefined, 'Finverse-Client-Id'?: string | undefined, 'Finverse-Client-Secret'?: string | undefined, 'Belvo-Secret-Id'?: string | undefined, 'Belvo-Secret-Password'?: string | undefined, 'Finicity-Partner-Secret'?: string | undefined, 'Finicity-Partner-Id'?: string | undefined, 'Finicity-App-Key'?: string | undefined, 'Basiq-Api-Key'?: string | undefined, 'Akoya-Client-Id'?: string | undefined, 'Akoya-Client-Secret'?: string | undefined, 'SaltEdge-App-Id'?: string | undefined, 'SaltEdge-App-Secret'?: string | undefined, body: CreateSessionRequest}; export type CreateApiV1SessionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createSessionResponse = z.object({expiration: z.string(), client_secret: z.string(), request_id: z.string()}); 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 }; /** * Creates a session that returns a client_secret which is required as a parameter when initializing the Fuse SDK. * * POST /v1/session */ export const createApiV1Session = async (args: CreateApiV1SessionArgs, options?: CreateApiV1SessionOptions) => { const {baseUrl = "https://sandbox-api.letsfuse.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Fuse-Client-Id'] != null) headers.set("Fuse-Client-Id", String(args['Fuse-Client-Id'])) if (args['Fuse-Api-Key'] != null) headers.set("Fuse-Api-Key", String(args['Fuse-Api-Key'])) if (args['Plaid-Client-Id'] != null) headers.set("Plaid-Client-Id", String(args['Plaid-Client-Id'])) if (args['Plaid-Secret'] != null) headers.set("Plaid-Secret", String(args['Plaid-Secret'])) if (args['Teller-Application-Id'] != null) headers.set("Teller-Application-Id", String(args['Teller-Application-Id'])) if (args['Teller-Certificate'] != null) headers.set("Teller-Certificate", String(args['Teller-Certificate'])) if (args['Teller-Private-Key'] != null) headers.set("Teller-Private-Key", String(args['Teller-Private-Key'])) if (args['Teller-Token-Signing-Key'] != null) headers.set("Teller-Token-Signing-Key", String(args['Teller-Token-Signing-Key'])) if (args['Teller-Signing-Secret'] != null) headers.set("Teller-Signing-Secret", String(args['Teller-Signing-Secret'])) if (args['Mx-Client-Id'] != null) headers.set("Mx-Client-Id", String(args['Mx-Client-Id'])) if (args['Mx-Api-Key'] != null) headers.set("Mx-Api-Key", String(args['Mx-Api-Key'])) if (args['Snaptrade-Client-Id'] != null) headers.set("Snaptrade-Client-Id", String(args['Snaptrade-Client-Id'])) if (args['Snaptrade-Consumer-Key'] != null) headers.set("Snaptrade-Consumer-Key", String(args['Snaptrade-Consumer-Key'])) if (args['Flinks-Ca-Customer-Id'] != null) headers.set("Flinks-Ca-Customer-Id", String(args['Flinks-Ca-Customer-Id'])) if (args['Flinks-Us-Customer-Id'] != null) headers.set("Flinks-Us-Customer-Id", String(args['Flinks-Us-Customer-Id'])) if (args['Flinks-Us-Instance-Id'] != null) headers.set("Flinks-Us-Instance-Id", String(args['Flinks-Us-Instance-Id'])) if (args['Flinks-Ca-Instance-Id'] != null) headers.set("Flinks-Ca-Instance-Id", String(args['Flinks-Ca-Instance-Id'])) if (args['Mono-Public-Key'] != null) headers.set("Mono-Public-Key", String(args['Mono-Public-Key'])) if (args['Mono-Secret-Key'] != null) headers.set("Mono-Secret-Key", String(args['Mono-Secret-Key'])) if (args['Mono-Webhook-Secret'] != null) headers.set("Mono-Webhook-Secret", String(args['Mono-Webhook-Secret'])) if (args['TrueLayer-Client-Id'] != null) headers.set("TrueLayer-Client-Id", String(args['TrueLayer-Client-Id'])) if (args['TrueLayer-Client-Secret'] != null) headers.set("TrueLayer-Client-Secret", String(args['TrueLayer-Client-Secret'])) if (args['Finverse-Client-Id'] != null) headers.set("Finverse-Client-Id", String(args['Finverse-Client-Id'])) if (args['Finverse-Client-Secret'] != null) headers.set("Finverse-Client-Secret", String(args['Finverse-Client-Secret'])) if (args['Belvo-Secret-Id'] != null) headers.set("Belvo-Secret-Id", String(args['Belvo-Secret-Id'])) if (args['Belvo-Secret-Password'] != null) headers.set("Belvo-Secret-Password", String(args['Belvo-Secret-Password'])) if (args['Finicity-Partner-Secret'] != null) headers.set("Finicity-Partner-Secret", String(args['Finicity-Partner-Secret'])) if (args['Finicity-Partner-Id'] != null) headers.set("Finicity-Partner-Id", String(args['Finicity-Partner-Id'])) if (args['Finicity-App-Key'] != null) headers.set("Finicity-App-Key", String(args['Finicity-App-Key'])) if (args['Basiq-Api-Key'] != null) headers.set("Basiq-Api-Key", String(args['Basiq-Api-Key'])) if (args['Akoya-Client-Id'] != null) headers.set("Akoya-Client-Id", String(args['Akoya-Client-Id'])) if (args['Akoya-Client-Secret'] != null) headers.set("Akoya-Client-Secret", String(args['Akoya-Client-Secret'])) if (args['SaltEdge-App-Id'] != null) headers.set("SaltEdge-App-Id", String(args['SaltEdge-App-Id'])) if (args['SaltEdge-App-Secret'] != null) headers.set("SaltEdge-App-Secret", String(args['SaltEdge-App-Secret'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/session${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(createSessionResponse, await res.json()) };