import {z} from 'zod' export type GetApiEntitiesAddressArgs = {Authorization: string}; export type GetApiEntitiesAddressOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const addressResponse = z.object({id: z.string().optional(), platform_account_id: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const pageInfo = z.object({end_cursor: z.string().optional(), has_next: z.boolean().optional(), has_previous: z.boolean().optional(), start_cursor: z.string().optional()}); export const getApiEntitiesAddressResponse = z.object({id: z.number().optional(), type: z.string().optional(), data: z.array(addressResponse).optional(), page_info: pageInfo.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 }; /** * List Addresses * * GET /entities/address */ export const getApiEntitiesAddress = async (args: GetApiEntitiesAddressArgs, options?: GetApiEntitiesAddressOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/entities/address${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(getApiEntitiesAddressResponse, await res.json()) };