import {z} from 'zod' export type GetApiPersonsPersonIdArgs = {person_id: string, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string}; export type GetApiPersonsPersonIdOptions = {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 }; /** * Get Person * * GET /persons/{person_id} */ export const getApiPersonsPersonId = async (args: GetApiPersonsPersonIdArgs, options?: GetApiPersonsPersonIdOptions) => { 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['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/${encodeURIComponent(String(args.person_id))}${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(brokerAppsApiEndpointsPersonsPersonResponse, await res.json()) };