import {z} from 'zod' export type GetApiV2OrdersReturnsArgs = {order_ids?: Array | undefined, statuses?: (Array<'REQUEST_INITIATED' | 'REQUEST_DECLINED' | 'OPENED' | 'RECEIVED' | 'CLOSED'>) | undefined, updated_from: string, page_token?: string | undefined, limit?: number | undefined}; export type GetApiV2OrdersReturnsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const origin = z.object({channel_id: z.string(), channel_name: z.string().optional(), channel_store_id: z.string().optional(), channel_store_name: z.string().optional(), sub_channel_code: z.string().optional(), sub_channel_name: z.string().optional()}); export const returnLineResponse = z.object({order_line_id: z.string(), product_id: z.string().optional(), quantity: z.number().int(), reason: z.enum(["WRONG_ITEM", "MISSING_ITEM", "DEFECTIVE_ITEM", "DAMAGED_ITEM", "UNWANTED_ITEM", "OTHER"])}); export const returnResponse = z.object({address: z.object({city: z.string(), country: z.string().optional(), country_iso_code: z.string(), state: z.string().optional(), street: z.string().optional(), street_additional_info: z.string().optional(), zip_code: z.string().optional()}).optional(), channel_return_id: z.string(), channel_updated_at: z.string(), created_at: z.string(), id: z.string(), label_url: z.string().optional(), method: z.string().optional(), order_id: z.string(), origin: origin, rejection_reason: z.enum(["RETURN_WINDOW_CLOSED", "NOT_ELIGIBLE_FOR_RETURN", "FINAL_SALES", "OTHER"]).optional(), return_lines: z.array(returnLineResponse), rma_id: z.string().optional(), status: z.enum(["REQUEST_INITIATED", "REQUEST_DECLINED", "OPENED", "RECEIVED", "CLOSED", "CHANNEL_SPECIFIC"]), tracking: z.object({carrier_name: z.string().optional(), tracking_number: z.string().optional(), tracking_url: z.string().optional()}).optional(), updated_at: z.string().optional()}); export const returnsPageResponse = z.object({next_page_token: z.string().min(1).optional(), previous_page_token: z.string().min(1).optional(), data: z.array(returnResponse)}); 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 }; /** * v2-listReturns - List Mirakl Connect returns * * GET /v2/orders/returns */ export const getApiV2OrdersReturns = async (args: GetApiV2OrdersReturnsArgs, options?: GetApiV2OrdersReturnsOptions) => { const {baseUrl = "https://miraklconnect.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.order_ids ?? []) query.append("order_ids", String(item)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) if (args.updated_from != null) query.append("updated_from", String(args.updated_from)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.limit != null) query.append("limit", String(args.limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/orders/returns${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(returnsPageResponse, await res.json()) };