import {z} from 'zod' export type GetApiShopsShopIdOrdersArgs = {shopId: number, pageToken?: string | undefined}; export type GetApiShopsShopIdOrdersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.object({currencyCode: z.string().optional(), defaultFractionDigits: z.number().int().optional(), numericCode: z.number().int().optional(), symbol: z.string().optional(), displayName: z.string().optional(), numericCodeAsString: z.string().optional()}); export const shopAddressDetail = z.object({name1: z.string().optional(), name2: z.string().optional(), company: z.string().optional(), address1: z.string().optional(), address2: z.string().optional(), address3: z.string().optional(), zip: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), phone: z.string().optional(), email: z.string().optional()}); export const shopPositionDetail = z.object({positionIdentifier: z.string().optional(), productIdentifier: z.string().optional(), variantIdentifier: z.string().optional(), name: z.string().optional(), sku: z.string().optional(), quantity: z.number().int().optional(), unitWeight: z.number().optional(), unitNetPrice: z.number().optional(), unitGrossPrice: z.number().optional()}); export const shopOrderSummary = z.object({orderIdentifier: z.string().optional(), orderReference: z.string().optional(), orderStatus: z.enum(["PENDING", "PROCESSING", "COMPLETED", "CANCELLED"]).optional(), orderStatusIdentifier: z.string().optional(), orderStatusName: z.string().optional(), orderDate: z.string().optional(), currency: currency.optional(), deliveryAddress: shopAddressDetail.optional(), invoiceAddress: shopAddressDetail.optional(), positions: z.array(shopPositionDetail).optional()}); export const shopOrdersGetResult = z.object({orders: z.array(shopOrderSummary).optional(), nextPageToken: z.string().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 }; /** * Search for orders in a shop * * GET /shops/{shopId}/orders */ export const getApiShopsShopIdOrders = async (args: GetApiShopsShopIdOrdersArgs, options?: GetApiShopsShopIdOrdersOptions) => { const {baseUrl = "https://api.quivo.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) const url = `${baseUrl.replace(/\/+$/, '')}/shops/${encodeURIComponent(String(args.shopId))}/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(shopOrdersGetResult, await res.json()) };