import {z} from 'zod' export type CustomerAccountCheckCategory = 'onboarding' | 'post_onboarding' | 'offboarding'; export type GetApiAccountsAccountIdChecksArgs = {account_id: string, cursor?: string | undefined, limit?: number | undefined, category?: CustomerAccountCheckCategory | undefined, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string}; export type GetApiAccountsAccountIdChecksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const checkType = z.enum(["address_validation", "authenticator_enrollment", "authenticator_reset", "identity_verification", "risk_assessment", "submit_document", "tin_validation", "positions", "cash_balance", "overdue_fees"]); export const customerAccountCheckCategory = z.enum(["onboarding", "post_onboarding", "offboarding"]); export const customerAccountCheckStatus = z.enum(["action_required", "processing", "success", "failure"]); export const completeIdentificationAction = z.object({type: z.literal("complete_identification"), identification: z.string(), person: z.string()}); export const enrollSCAAuthenticatorAction = z.object({type: z.literal("enroll_sca_authenticator")}); export const initiateIdentityVerificationAction = z.object({type: z.literal("initiate_identity_verification"), url: z.string().optional(), video_ident_id: z.string().optional()}); export const pendingCheckAction = z.object({type: z.enum(["proof_of_address", "tax_identification"]), due_date: z.string()}); export const provideSCARecoveryCodeAction = z.object({type: z.literal("provide_recovery_code"), expires_at: z.string().optional()}); export const businessDocumentType = z.enum(["articles_of_association", "commercial_register_extract", "shareholder_list"]); export const submitDocumentAction = z.object({type: z.literal("submit_document"), document: businessDocumentType}); export const action = z.discriminatedUnion("type", [completeIdentificationAction, enrollSCAAuthenticatorAction, initiateIdentityVerificationAction, pendingCheckAction, provideSCARecoveryCodeAction, submitDocumentAction]); export const statusChangeOnboardingCheckStatus = z.object({status: customerAccountCheckStatus, timestamp: z.string()}); export const customerAccountCheckResponse = z.object({type: checkType, category: customerAccountCheckCategory, status: customerAccountCheckStatus, actions: z.array(action), history: z.array(statusChangeOnboardingCheckStatus)}); export const pagination = z.object({next_cursor: z.string().optional()}); export const pageCustomerAccountCheckResponse = z.object({data: z.array(customerAccountCheckResponse), pagination: pagination}); 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 }; /** * Get Account Checks * * GET /accounts/{account_id}/checks */ export const getApiAccountsAccountIdChecks = async (args: GetApiAccountsAccountIdChecksArgs, options?: GetApiAccountsAccountIdChecksOptions) => { const {baseUrl = "https://sandbox.api.lemon.markets/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.category != null) query.append("category", String(args.category)) if (args['LMG-Data-Privacy-Access-Principal'] != null) headers.set("LMG-Data-Privacy-Access-Principal", String(args['LMG-Data-Privacy-Access-Principal'])) if (args['LMG-Data-Privacy-Access-Justification'] != null) headers.set("LMG-Data-Privacy-Access-Justification", String(args['LMG-Data-Privacy-Access-Justification'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/checks${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(pageCustomerAccountCheckResponse, await res.json()) };