import {z} from 'zod' export type AddressRequest = {line_1: string, line_2?: string | undefined, postal_code: string, city: string, country: string}; export type BusinessPersonPlaceOfBirthRequest = {city?: string | undefined, country: string}; export type TaxResidency = {country: string, tax_identification_number?: string | undefined}; export type BrokerAppsApiEndpointsAgreementsAgreementAcceptanceRequest = {agreement_id: string, accepted_at: string}; export type PersonRequest = {firstname: string, lastname: string, date_of_birth: string, registered_address: AddressRequest, nationalities?: Array | undefined, email?: string | undefined, phone_number?: string | undefined, place_of_birth?: BusinessPersonPlaceOfBirthRequest | undefined, tax_residencies?: Array | undefined, accepted_agreements: Array}; export type CreateApiPersonsArgs = {'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string, body: PersonRequest}; export type CreateApiPersonsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({line_1: z.string().max(128), line_2: z.string().max(128).optional(), postal_code: z.string().max(50), city: z.string().max(150), country: z.string().min(2).max(2)}); export const placeOfBirthRequest = z.object({city: z.string().max(255), country: z.string().min(2).max(2)}); export const taxResidency = z.object({country: z.string().min(2).max(2), tax_identification_number: z.string().min(1).max(255).optional()}); export const brokerAppsApiEndpointsPersonsPersonResponse = z.object({id: z.string(), firstname: z.string(), lastname: z.string(), date_of_birth: z.string(), registered_address: address, nationalities: z.array(z.string().min(2).max(2)).optional(), email: z.string().optional(), phone_number: z.string().optional(), place_of_birth: placeOfBirthRequest.optional(), tax_residencies: z.array(taxResidency).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 Person * * POST /persons */ export const createApiPersons = async (args: CreateApiPersonsArgs, options?: CreateApiPersonsOptions) => { 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(/\/+$/, '')}/persons${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(brokerAppsApiEndpointsPersonsPersonResponse, await res.json()) };