import {z} from 'zod' export type CountryCode = 'fr-fr' | 'fr-be' | 'es-es' | 'de-de' | 'de-at' | 'it-it' | 'ja-jp' | 'en-au' | 'en-gb' | 'en-us' | 'nl-nl' | 'pt-pt' | 'en-ie' | 'el-gr' | 'sk-sk' | 'sv-se'; export type OrderState = 0 | 10 | 1 | 3 | 8 | 9; export type GetApiWsOrdersArgs = {date_creation?: string | undefined, date_modification?: string | undefined, country_code?: CountryCode | undefined, state?: OrderState | undefined, page?: number | undefined, 'page-size'?: number | undefined}; export type GetApiWsOrdersOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({id: z.number().int().optional(), country: z.string().optional(), city: z.string().optional(), postalCode: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), company: z.string().optional(), phoneNumber: z.string().optional(), state: z.string().optional(), street: z.string().optional(), street2: z.string().optional()}); export const orderState = z.union([z.literal(0), z.literal(10), z.literal(1), z.literal(3), z.literal(8), z.literal(9)]); export const orderLineState = z.union([z.literal(0), z.literal(9), z.literal(8), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7)]); export const returnReason = z.union([z.literal(0), z.literal(1), z.literal(11), z.literal(12), z.literal(13), z.literal(21), z.literal(22), z.literal(23), z.literal(24), z.literal(25)]); export const productCondition = z.union([z.literal(9), z.literal(10), z.literal(11), z.literal(12)]); export const orderLine = z.object({id: z.number().int().optional(), date_creation: z.string().optional(), state: orderLineState.optional(), price: z.string().optional(), shipping_price: z.string().optional(), shipping_delay: z.number().int().optional(), currency: z.string().optional(), return_reason: returnReason.optional(), return_message: z.string().optional(), listing: z.string().optional(), listing_id: z.number().int().optional(), orderline_fee: z.string().optional(), product: z.string().optional(), quantity: z.number().int().optional(), brand: z.string().optional(), product_id: z.number().int().optional(), backcare: z.boolean().optional(), backcare_price: z.number().int().optional(), sales_taxes: z.string().optional(), imei: z.string().optional(), serial_number: z.string().optional(), condition: productCondition.optional()}); export const countryCode = z.enum(["fr-fr", "fr-be", "es-es", "de-de", "de-at", "it-it", "ja-jp", "en-au", "en-gb", "en-us", "nl-nl", "pt-pt", "en-ie", "el-gr", "sk-sk", "sv-se"]); export const order = z.object({order_id: z.number().int().optional(), delivery_mode: z.enum(["HOME_DELIVERY", "COLLECTION_POINT"]).optional(), shipping_address: address.optional(), billing_address: address.optional(), delivery_note: z.string().max(300).optional(), expected_dispatch_date: z.string().optional(), tracking_number: z.string().max(200).optional(), tracking_url: z.string().max(300).optional(), shipper_display: z.string().max(200).optional(), is_backship: z.boolean().optional(), date_creation: z.string().optional(), date_modification: z.string().optional(), date_shipping: z.string().optional(), date_payment: z.string().optional(), state: orderState.optional(), orderlines: z.array(orderLine).optional(), price: z.string().optional(), shipping_price: z.string().optional(), currency: z.string().max(3).optional(), country_code: countryCode.optional(), paypal_reference: z.string().max(17).nullable().optional(), installment_payment: z.boolean().optional(), payment_method: z.string().optional(), sales_taxes: z.string().optional()}); export const getApiWsOrdersResponse = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(order).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 the orders * * GET /ws/orders */ export const getApiWsOrders = async (args: GetApiWsOrdersArgs, options?: GetApiWsOrdersOptions) => { const {baseUrl = "https://preprod.backmarket.co.jp", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date_creation != null) query.append("date_creation", String(args.date_creation)) if (args.date_modification != null) query.append("date_modification", String(args.date_modification)) if (args.country_code != null) query.append("country_code", String(args.country_code)) if (args.state != null) query.append("state", String(args.state)) if (args.page != null) query.append("page", String(args.page)) if (args['page-size'] != null) query.append("page-size", String(args['page-size'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/ws/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(getApiWsOrdersResponse, await res.json()) };