import {z} from 'zod' export type GetApiShipmentsShipmentIdRatesArgs = {ShipmentId: string, page?: number | undefined, results?: number | undefined, 'SHIPPO-API-VERSION'?: string | undefined}; export type GetApiShipmentsShipmentIdRatesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseMessage = z.object({source: z.string().optional(), code: z.string().optional(), text: z.string().optional()}); export const responseMessageList = z.array(responseMessage); export const serviceLevelWithParent = z.object({name: z.string().optional(), terms: z.string().optional(), token: z.string().optional(), extended_token: z.string().optional(), parent_servicelevel: z.object({name: z.string().optional(), terms: z.string().optional(), token: z.string().optional(), extended_token: z.string().optional()}).optional()}); export const rate = z.object({amount: z.string(), amount_local: z.string(), currency: z.string(), currency_local: z.string(), arrives_by: z.string().optional(), attributes: z.array(z.enum(["BESTVALUE", "CHEAPEST", "FASTEST"])), carrier_account: z.string(), duration_terms: z.string().optional(), estimated_days: z.number().int().optional(), included_insurance_price: z.string().optional(), messages: responseMessageList.optional(), object_created: z.string(), object_id: z.string(), object_owner: z.string(), provider: z.string(), provider_image_75: z.string().optional(), provider_image_200: z.string().optional(), servicelevel: serviceLevelWithParent, shipment: z.string(), test: z.boolean().optional(), zone: z.string().optional()}); export const ratePaginatedList = z.object({next: z.string().optional(), previous: z.string().optional(), results: z.array(rate).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 }; /** * Retrieve shipment rates * * GET /shipments/{ShipmentId}/rates */ export const getApiShipmentsShipmentIdRates = async (args: GetApiShipmentsShipmentIdRatesArgs, options?: GetApiShipmentsShipmentIdRatesOptions) => { const {baseUrl = "https://api.goshippo.com", fetch: fetchFn = fetch} = 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.results != null) query.append("results", String(args.results)) if (args['SHIPPO-API-VERSION'] != null) headers.set("SHIPPO-API-VERSION", String(args['SHIPPO-API-VERSION'])) const url = `${baseUrl.replace(/\/+$/, '')}/shipments/${encodeURIComponent(String(args.ShipmentId))}/rates${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(ratePaginatedList, await res.json()) };