import {z} from 'zod' export type GetApiV2ItemsArgs = {pageSize?: number | undefined, page?: number | undefined, field?: string | undefined, order?: string | undefined, serialId?: string | undefined, categoryId?: string | undefined, type?: ('service' | 'product' | 'hours' | 'expense' | 'equipment' | 'warranty') | undefined, active?: boolean | undefined, inventoryManaged?: boolean | undefined, booking?: boolean | undefined, dateProperty?: string | undefined, dateOperator?: string | undefined, date?: string | undefined, Authorization: string}; export type GetApiV2ItemsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseGenericItemCategoryV2Dto = z.object({id: z.string().optional(), name: z.string().optional()}); export const responseItemV2Dto = z.object({id: z.string().optional(), serialId: z.number().optional(), name: z.string().optional(), description: z.string().optional(), model: z.string().optional(), category: responseGenericItemCategoryV2Dto.nullable().optional(), inventoryManaged: z.boolean().optional(), currentStock: z.number().optional(), cost: z.number().optional(), price: z.number().optional(), type: z.enum(["service", "product", "hours", "expense", "equipment", "warranty"]).optional(), taxable: z.boolean().optional(), discountable: z.boolean().optional(), booking: z.boolean().optional(), bookingPrice: z.number().nullable().optional(), active: z.boolean().optional(), imageUrl: z.string().nullable().optional(), updated: z.string().nullable().optional()}); export const responseItemsV2Dto = z.object({pageSize: z.number().optional(), page: z.number().optional(), totalResults: z.number().optional(), hasMore: z.boolean().optional(), data: z.array(responseItemV2Dto).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 items * * GET /v2/items */ export const getApiV2Items = async (args: GetApiV2ItemsArgs, options?: GetApiV2ItemsOptions) => { 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.field != null) query.append("field", String(args.field)) if (args.order != null) query.append("order", String(args.order)) if (args.serialId != null) query.append("serialId", String(args.serialId)) if (args.categoryId != null) query.append("categoryId", String(args.categoryId)) if (args.type != null) query.append("type", String(args.type)) if (args.active != null) query.append("active", String(args.active)) if (args.inventoryManaged != null) query.append("inventoryManaged", String(args.inventoryManaged)) if (args.booking != null) query.append("booking", String(args.booking)) 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/items${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(responseItemsV2Dto, await res.json()) };