import {z} from 'zod' export type GetApiApiV1MeLegalEntitiesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({line1: z.string(), line2: z.string().nullable().optional(), city: z.string(), state: z.string().nullable().optional(), postalCode: z.string(), country: z.string()}).nullable(); export const legalEntity = z.object({id: z.string(), optionId: z.string(), type: z.string(), name: z.string(), extension: z.string(), nameStartsWithExtension: z.boolean(), formationDate: z.string().nullable().optional(), registrationDate: z.string().nullable().optional(), dissolutionDate: z.string().nullable().optional(), residentPermitNumber: z.string().nullable(), principalOfficeAddress: address.optional(), createdAt: z.string().optional()}); export const getApiApiV1MeLegalEntitiesResponse = z.object({data: z.array(legalEntity)}); 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 }; /** * List consented legal entities * * GET /api/v1/me/legal-entities */ export const getApiApiV1MeLegalEntities = async (options?: GetApiApiV1MeLegalEntitiesOptions) => { const {baseUrl = "https://portal.eprospera.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/me/legal-entities${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(getApiApiV1MeLegalEntitiesResponse, await res.json()) };