import {z} from 'zod' export type OrdersAppModelsOrderOrderStatusChoices = 'open' | 'shipped' | 'cancelled' | 'returned' | 'mixed'; export type OrdersAppModelsOrderItemOrderItemStatusChoices = 'open' | 'shipped' | 'cancelled' | 'returned'; export type FulfillmentTypes = 'dropshipping' | 'fulfillment_by_marketplace'; export type GetApiApiV1OrdersArgs = {order_number?: (string | null) | undefined, customer_key?: (string | null) | undefined, carrier_key?: (string | null) | undefined, shop_country?: (string | null) | undefined, order_status?: (OrdersAppModelsOrderOrderStatusChoices | null) | undefined, order_item_status?: (OrdersAppModelsOrderItemOrderItemStatusChoices | null) | undefined, orders_from?: (string | null) | undefined, orders_to?: (string | null) | undefined, fulfillment_type?: (FulfillmentTypes | null) | undefined, page?: number | undefined, per_page?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiApiV1OrdersOptions = {baseUrl: string, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ordersAppModelsOrderItemOrderItemStatusChoices = z.enum(["open", "shipped", "cancelled", "returned"]); export const orderItemSchema = z.object({return_tracking_key: z.string().nullable(), shipment_tracking_key: z.string().nullable(), sku: z.string(), status: ordersAppModelsOrderItemOrderItemStatusChoices, id: z.number().int().nullable().optional(), vat: z.number().optional(), price_without_tax: z.number().int(), price_with_tax: z.number().int()}); export const ordersAppModelsOrderOrderStatusChoices = z.enum(["open", "shipped", "cancelled", "returned", "mixed"]); export const getOrderSchema = z.object({order_items: z.array(orderItemSchema), order_number: z.string(), carrier_key: z.string(), cost_without_tax: z.number().int(), cost_with_tax: z.number().int(), delivery_document_url: z.string(), status: ordersAppModelsOrderOrderStatusChoices, customer_key: z.string().max(255), shop: z.number().int(), created_at: z.string(), updated_at: z.string(), fulfillment_type: z.string().max(255).optional(), payment_method: z.string().max(255).nullable().optional(), billing_street: z.string().max(255), billing_zip_code: z.string().max(255), billing_city: z.string().max(255), billing_country_code: z.string().max(2), billing_recipient_gender: z.string().max(1).nullable().optional(), billing_recipient_first_name: z.string().max(255), billing_recipient_last_name: z.string().max(255), billing_additional: z.string().max(255).nullable().optional(), shipping_street: z.string().max(255).nullable().optional(), shipping_zip_code: z.string().max(255).nullable().optional(), shipping_city: z.string().max(255).nullable().optional(), shipping_country_code: z.string().max(2).nullable().optional(), shipping_additional: z.string().max(255).nullable().optional(), shipping_collection_point_key: z.string().max(255).nullable().optional(), shipping_collection_point_type: z.string().max(255).nullable().optional(), shipping_collection_point_description: z.string().max(255).nullable().optional(), customer_phone: z.string().max(50).nullable().optional(), customer_email: z.string().max(255).nullable().optional(), shipping_recipient_gender: z.string().max(1).nullable().optional(), shipping_recipient_first_name: z.string().max(255), shipping_recipient_last_name: z.string().max(255), currency_code: z.string().max(3)}); export const pagination = z.object({per_page: z.number().int().gt(0), page: z.number().int().gt(0).nullable().optional(), pages: z.number().int().gte(0).nullable().optional(), total: z.number().int().gte(0).nullable().optional(), next: z.string().nullable().optional(), prev: z.string().nullable().optional(), next_cursor: z.string().nullable().optional()}); export const pagedGetOrderSchema = z.object({items: z.array(getOrderSchema), pagination: pagination}); 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 }; /** * Get Orders * * GET /api/v1/orders/ */ export const getApiApiV1Orders = async (args: GetApiApiV1OrdersArgs, options: GetApiApiV1OrdersOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.order_number != null) query.append("order_number", String(args.order_number)) if (args.customer_key != null) query.append("customer_key", String(args.customer_key)) if (args.carrier_key != null) query.append("carrier_key", String(args.carrier_key)) if (args.shop_country != null) query.append("shop_country", String(args.shop_country)) if (args.order_status != null) query.append("order_status", String(args.order_status)) if (args.order_item_status != null) query.append("order_item_status", String(args.order_item_status)) if (args.orders_from != null) query.append("orders_from", String(args.orders_from)) if (args.orders_to != null) query.append("orders_to", String(args.orders_to)) if (args.fulfillment_type != null) query.append("fulfillment_type", String(args.fulfillment_type)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (APIKeyAuth !== undefined) headers.set("X-API-Key", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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(pagedGetOrderSchema, await res.json()) };