import {z} from 'zod' export type GetApiV2ClientsArgs = {pageSize?: number | undefined, page?: number | undefined, searchQuery?: string | undefined, isDue?: ('PastDue' | 'Due') | undefined, tags?: string | undefined, field?: ('serialId' | 'firstName' | 'fullName' | 'clientCompany' | 'primaryPhone') | undefined, order?: ('ASC' | 'DESC') | undefined, dateProperty?: 'created' | undefined, dateOperator?: ('prev' | 'next' | 'between') | undefined, date?: string | undefined, Authorization: string}; export type GetApiV2ClientsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseAllClientContactV2Dto = z.object({firstName: z.string().optional(), lastName: z.string().optional(), fullName: z.string().optional(), email: z.string().optional(), primaryPhone: z.string().optional(), secondaryPhone: z.string().optional(), country: z.string().optional(), state: z.string().optional(), city: z.string().optional(), zipcode: z.string().optional(), address: z.string().optional(), role: z.string().optional(), note: z.string().optional()}); export const responseAllClientV2Dto = z.object({id: z.string().optional(), serialId: z.number().optional(), firstName: z.string().optional(), lastName: z.string().optional(), fullName: z.string().optional(), companyName: z.string().optional(), email: z.string().optional(), primaryExt: z.string().optional(), primaryPhone: z.string().optional(), secondaryExt: z.string().optional(), secondaryPhone: z.string().optional(), country: z.string().optional(), state: z.string().optional(), city: z.string().optional(), zipcode: z.string().optional(), unit: z.string().optional(), address: z.string().optional(), fullAddress: z.string().optional(), clientContacts: z.array(responseAllClientContactV2Dto).optional(), created: z.string().optional()}); export const responseClientsV2Dto = z.object({pageSize: z.number().optional(), page: z.number().optional(), totalResults: z.number().optional(), hasMore: z.boolean().optional(), data: z.array(responseAllClientV2Dto).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 }; /** * Retrieve a list of Clients * * GET /v2/clients */ export const getApiV2Clients = async (args: GetApiV2ClientsArgs, options?: GetApiV2ClientsOptions) => { const {baseUrl = "https://api.workiz.com/crm/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.searchQuery != null) query.append("searchQuery", String(args.searchQuery)) if (args.isDue != null) query.append("isDue", String(args.isDue)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.field != null) query.append("field", String(args.field)) if (args.order != null) query.append("order", String(args.order)) if (args.dateProperty != null) query.append("dateProperty", String(args.dateProperty)) if (args.dateOperator != null) query.append("dateOperator", String(args.dateOperator)) if (args.date != null) query.append("date", String(args.date)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/clients${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(responseClientsV2Dto, await res.json()) };