import {z} from 'zod' export type GetApiApiV1OrdersCarriersArgs = {page?: number | undefined, per_page?: number | undefined}; export type GetApiApiV1OrdersCarriersOptions = {baseUrl: string, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const carrierSchema = z.object({country_code: z.string(), display_label: z.string().nullable(), key: z.string().max(255), carrier_name: z.string().max(255)}); 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 pagedCarrierSchema = z.object({items: z.array(carrierSchema), 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 Carriers * * GET /api/v1/orders/carriers/ */ export const getApiApiV1OrdersCarriers = async (args: GetApiApiV1OrdersCarriersArgs, options: GetApiApiV1OrdersCarriersOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (APIKeyAuth !== undefined) headers.set("X-API-Key", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/orders/carriers/${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(pagedCarrierSchema, await res.json()) };