import {z} from 'zod' export type GetApiV2RateShoppersRateShopperIdOptions = {baseUrl?: string | undefined, apiKeys?: string | undefined, fetch?: typeof fetch | undefined}; export const rateShopperService = z.object({service_code: z.string(), carrier_id: z.string()}); export const rateShopperTransitTime = z.object({max_days: z.number().int().nullable().optional(), max_days_international: z.number().int().nullable().optional(), use_deliver_by_date: z.boolean().optional()}); export const rateShopperPriceTolerance = z.object({amount: z.number(), percentage: z.number()}); export const rateShopperServicePreference = z.object({service: rateShopperService.optional(), price_tolerance: rateShopperPriceTolerance.optional()}); export const rateShopperResponse = z.object({rate_shopper_id: z.string(), name: z.string(), services: z.array(rateShopperService), transit_time: rateShopperTransitTime.optional(), service_preference: rateShopperServicePreference.optional(), is_active: z.boolean(), created_at: z.string(), updated_at: z.string()}); 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 rate shopper by ID * * GET /v2/rate_shoppers/{rate_shopper_id} */ export const getApiV2RateShoppersRateShopperId = async (options?: GetApiV2RateShoppersRateShopperIdOptions) => { const {baseUrl = "https://api.shipstation.com", fetch: fetchFn = fetch, apiKeys} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeys !== undefined) headers.set("api-key", apiKeys) const url = `${baseUrl.replace(/\/+$/, '')}/v2/rate_shoppers/${encodeURIComponent(String(args.rate_shopper_id))}${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(rateShopperResponse, await res.json()) };