import {z} from 'zod' export type SearchCustomerRequest = {limit?: number | undefined, before?: (string | null) | undefined, after?: (string | null) | undefined}; export type CreateApiCustomersSearchArgs = {version: string | null, body: SearchCustomerRequest}; export type CreateApiCustomersSearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({address1: z.string().max(100).nullable().optional(), address2: z.string().max(100).nullable().optional(), address3: z.string().max(100).nullable().optional(), address4: z.string().max(100).nullable().optional(), city: z.string().max(100).nullable().optional(), state: z.string().max(100).nullable().optional(), postcode: z.string().max(50).nullable().optional(), countryCode: z.string().max(10), locality: z.string().max(100).nullable().optional(), county: z.string().max(100).nullable().optional()}); export const customerFull = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), shippingAddress: address.nullable().optional(), billingAddress: address.nullable().optional(), emailAddress: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), vatNumber: z.string().nullable().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(), documentVersion: z.number().int().optional()}); export const pagedResultOfCustomer = z.object({data: z.array(customerFull).nullable().optional(), before: z.string().nullable().optional(), after: z.string().nullable().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 all customers * * POST /customers/search */ export const createApiCustomersSearch = async (args: CreateApiCustomersSearchArgs, options?: CreateApiCustomersSearchOptions) => { const {baseUrl = "https://api.dojo.tech", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.version != null) headers.set("version", String(args.version)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/customers/search${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(pagedResultOfCustomer, await res.json()) };