import {z} from 'zod' export type GetApiOrdersOptions = {baseUrl?: string | undefined, accountId?: string | undefined, authToken?: string | undefined, fetch?: typeof fetch | undefined}; export const order = z.object({id: z.string().optional(), accountId: z.string().optional(), orderNumber: z.string().optional(), dueDate: z.string().nullable().optional(), subtotal: z.number().nullable().optional(), tax: z.number().nullable().optional(), total: z.number().nullable().optional(), taxName: z.string().nullable().optional(), taxRate: z.number().nullable().optional(), workflowState: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), canBeCompleted: z.boolean().optional(), canBeIgnored: z.boolean().optional(), completed: z.boolean().optional(), ignored: z.boolean().optional(), companyName: z.string().nullable().optional(), createdById: z.string().nullable().optional(), updatedById: z.string().nullable().optional(), legacy: z.object({id: z.string().optional()}).optional(), lineItems: z.array(z.object({id: z.string().optional(), lineNumber: z.number().int().nullable().optional(), position: z.number().int().optional(), name: z.string().nullable().optional(), description: z.string().nullable().optional(), quantity: z.number().nullable().optional(), price: z.number().nullable().optional(), totalPrice: z.number().nullable().optional(), workflowState: z.string().nullable().optional(), jobStale: z.boolean().nullable().optional(), jobId: z.string().nullable().optional(), productId: z.string().nullable().optional(), job: z.object({id: z.string().optional(), workflowState: z.string().nullable().optional()}).optional()})).optional()}); export const getApiOrdersResponse = z.array(order); 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 in-process orders * * GET /orders */ export const getApiOrders = async (options?: GetApiOrdersOptions) => { const {baseUrl = "https://api.shopvox.com/v1", fetch: fetchFn = fetch, accountId, authToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (accountId !== undefined) query.set("account_id", accountId) if (authToken !== undefined) query.set("authToken", authToken) const url = `${baseUrl.replace(/\/+$/, '')}/orders${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(getApiOrdersResponse, await res.json()) };