import {z} from 'zod' export type GetApiEcommerceOrdersOrderIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiEcommerceOrdersOrderIdResponse = z.object({data: z.object({_id: z.string().optional(), order: z.object({__v: z.number().optional(), _id: z.string().optional(), address: z.object({__v: z.number().optional(), _id: z.string().optional(), addressLine1: z.string().optional(), addressLine2: z.string().optional(), city: z.string().optional(), country: z.string().optional(), createdAt: z.string().optional(), owner: z.string().optional(), pincode: z.string().optional(), state: z.string().optional(), updatedAt: z.string().optional()}).optional(), coupon: z.object({_id: z.string().optional(), couponCode: z.string().optional(), name: z.string().optional()}).optional(), createdAt: z.string().optional(), customer: z.object({_id: z.string().optional(), email: z.string().optional(), username: z.string().optional()}).optional(), discountedOrderPrice: z.number().optional(), isPaymentDone: z.boolean().optional(), items: z.array(z.object({_id: z.string().optional(), product: z.object({__v: z.number().optional(), _id: z.string().optional(), category: z.string().optional(), createdAt: z.string().optional(), description: z.string().optional(), mainImage: z.object({_id: z.string().optional(), localPath: z.string().optional(), url: z.string().optional()}).optional(), name: z.string().optional(), owner: z.string().optional(), price: z.number().optional(), stock: z.number().optional(), subImages: z.array(z.object({_id: z.string().optional(), localPath: z.string().optional(), url: z.string().optional()})).optional(), updatedAt: z.string().optional()}).optional(), quantity: z.number().optional()})).optional(), orderPrice: z.number().optional(), paymentId: z.string().optional(), paymentProvider: z.string().optional(), status: z.string().optional(), updatedAt: z.string().optional()}).optional()}).optional(), message: z.string().optional(), statusCode: z.number().optional(), success: z.boolean().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 }; /** * Get order by id * * GET /ecommerce/orders/{orderId} */ export const getApiEcommerceOrdersOrderId = async (options?: GetApiEcommerceOrdersOrderIdOptions) => { const {baseUrl = "https://api.freeapi.app/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/ecommerce/orders/${encodeURIComponent(String(args.orderId))}${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(getApiEcommerceOrdersOrderIdResponse, await res.json()) };