import {z} from 'zod' export type GetApiAccountsAidLocationsArgs = {aid: unknown, limit?: number | undefined, starting_after?: string | undefined, search?: string | undefined, status?: Array | undefined, country?: Array | undefined, phone_number?: Array | undefined, organization_number?: Array | undefined, email?: Array | undefined, type?: Array | undefined}; export type GetApiAccountsAidLocationsOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const salesLocationResponse = z.object({account_id: z.string().min(9).max(9).optional(), id: z.string(), organization_number: z.string().optional(), name: z.string().optional(), business_name: z.string().optional()}); export const getApiAccountsAidLocationsResponse = z.array(salesLocationResponse); 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 }; /** * aid_locations_get * * GET /accounts/{aid}/locations */ export const getApiAccountsAidLocations = async (args: GetApiAccountsAidLocationsArgs, options?: GetApiAccountsAidLocationsOptions) => { const {baseUrl = "https://api.dintero.com/v1", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.search != null) query.append("search", String(args.search)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.country ?? []) query.append("country", String(item)) for (const item of args.phone_number ?? []) query.append("phone_number", String(item)) for (const item of args.organization_number ?? []) query.append("organization_number", String(item)) for (const item of args.email ?? []) query.append("email", String(item)) for (const item of args.type ?? []) query.append("type", String(item)) if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.aid))}/locations${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(getApiAccountsAidLocationsResponse, await res.json()) };