import {z} from 'zod' export type OrdersApiLineItemSchema = {id: string, productId: string, variantId?: (string | null) | undefined, title: string, sku?: (string | null) | undefined, quantity: number, price?: (number | null) | undefined, imageUrl?: (string | null) | undefined, discount?: (number | null) | undefined}; export type OrdersApiCustomerSchema = {id?: (string | null) | undefined, email?: (string | null) | undefined, phoneNumber?: (string | null) | undefined, name?: (string | null) | undefined, firstName?: (string | null) | undefined, lastName?: (string | null) | undefined}; export type OrdersApiShippingAddressSchema = {address1?: (string | null) | undefined, address2?: (string | null) | undefined, city?: (string | null) | undefined, province?: (string | null) | undefined, country?: (string | null) | undefined, postalCode?: (string | null) | undefined}; export type OrdersApiShippingSchema = {method?: (string | null) | undefined, cost?: (number | null) | undefined, address?: OrdersApiShippingAddressSchema | undefined}; export type OrdersApiBillingAddressSchema = {address1?: (string | null) | undefined, address2?: (string | null) | undefined, city?: (string | null) | undefined, province?: (string | null) | undefined, country?: (string | null) | undefined, postalCode?: (string | null) | undefined}; export type OrdersApiCreateOrderRequestSchema = {orderId: string, createdAt: string, lineItems: Array, customer?: OrdersApiCustomerSchema | undefined, shipping?: OrdersApiShippingSchema | undefined, totalPrice?: (number | null) | undefined, subtotalPrice?: (number | null) | undefined, totalTax?: (number | null) | undefined, totalDiscount?: (number | null) | undefined, billingAddress?: OrdersApiBillingAddressSchema | undefined, tags?: (Array | null) | undefined, currencyCode?: (string | null) | undefined, locationId?: (string | null) | undefined}; export type CreateApiStoresStoreIdOrdersArgs = {body: OrdersApiCreateOrderRequestSchema}; export type CreateApiStoresStoreIdOrdersOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ordersApiCreateOrderResponseSchema = z.object({orderId: z.string(), externalOrderId: z.string()}); 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 }; /** * Create Order * * POST /stores/{storeId}/orders */ export const createApiStoresStoreIdOrders = async (args: CreateApiStoresStoreIdOrdersArgs, options?: CreateApiStoresStoreIdOrdersOptions) => { const {baseUrl = "https://api.getredo.com/v2.2", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/stores/${encodeURIComponent(String(args.storeId))}/orders${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(ordersApiCreateOrderResponseSchema, await res.json()) };