import {z} from 'zod' export type AcceptAgreementRequest = {person: string, agreement: string, accepted_at: string}; export type CreateApiAccountsAccountAcceptAgreementArgs = {account: string, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string, body: AcceptAgreementRequest}; export type CreateApiAccountsAccountAcceptAgreementOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bankAccountResponse = z.object({iban: z.string(), bic: z.string(), name: z.string().optional()}); export const personCustomerResponse = z.object({type: z.literal("person"), id: z.string(), person: z.string()}); export const businessCustomerResponse = z.object({type: z.literal("business"), id: z.string(), business: z.string()}); export const customerAccountStatus = z.enum(["closed", "created", "opened", "rejected"]); export const statusChangeCustomerAccountStatus = z.object({status: customerAccountStatus, timestamp: z.string()}); export const brokerAppsApiEndpointsAccountsSecuritiesAccountResponse = z.object({number: z.string()}); export const acceptedAgreementResponse = z.object({agreement: z.string(), accepted_at: z.string(), person: z.string()}); export const acceptAgreementResponse = z.object({id: z.string(), cash_account: bankAccountResponse.optional(), customer: z.union([personCustomerResponse, businessCustomerResponse]), declaration_of_acting_on_own_account: z.boolean(), history: z.array(statusChangeCustomerAccountStatus), reference_account: bankAccountResponse.optional(), securities_account: brokerAppsApiEndpointsAccountsSecuritiesAccountResponse.optional(), status: customerAccountStatus, metadata: z.record(z.string(), z.string()).optional(), agreements: z.array(acceptedAgreementResponse)}); 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 }; /** * Accept Agreement * * POST /accounts/{account}/accept_agreement */ export const createApiAccountsAccountAcceptAgreement = async (args: CreateApiAccountsAccountAcceptAgreementArgs, options?: CreateApiAccountsAccountAcceptAgreementOptions) => { const {baseUrl = "https://sandbox.api.lemon.markets/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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))}/accept_agreement${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(acceptAgreementResponse, await res.json()) };