import {z} from 'zod' export type GetApiApiV1StrategiesStrategyIdCarriersArgs = {strategyId: string, active?: boolean | undefined, carrier_id?: string | undefined, delivery_type_code?: string | undefined, capability?: (Array<'returns' | 'same_day' | 'tracking'>) | undefined, q?: string | undefined, page?: number | undefined, limit?: number | undefined, sortBy?: string | undefined, sortOrder?: string | undefined}; export type GetApiApiV1StrategiesStrategyIdCarriersOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const strategyCarrierListItemResponse = z.object({metadata: z.record(z.string(), z.unknown()).nullable(), id: z.string(), strategy_id: z.string(), carrier_id: z.string(), delivery_type_codes: z.array(z.string()), active: z.boolean(), holidays_open: z.boolean(), delivery_modalities: z.array(z.string()), channel_codes: z.array(z.string()), coverage_area: z.object({}).nullable(), freight_rules: z.object({sla_hours: z.number(), fixed_cost: z.number(), cost_per_kg: z.number()}), operating_hours: z.array(z.object({day: z.string(), open: z.string(), close: z.string()})), holidays_hours: z.object({open: z.string(), close: z.string()}), carrier: z.object({name: z.string(), code: z.string(), capabilities: z.object({tracking: z.boolean().optional(), same_day: z.boolean().optional(), returns: z.boolean().optional()})}), created_at: z.string(), updated_at: z.string()}); export const paginationMetaResponse = z.object({total: z.number(), pages: z.number(), current_page: z.number(), next_page: z.number().nullable(), previous_page: z.number().nullable(), limit: z.number()}); export const strategyCarrierListResponse = z.object({data: z.array(strategyCarrierListItemResponse), meta: paginationMetaResponse}); 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 }; /** * List strategy carriers * * GET /api/v1/strategies/{strategyId}/carriers * * Also accepts auth: bearer */ export const getApiApiV1StrategiesStrategyIdCarriers = async (args: GetApiApiV1StrategiesStrategyIdCarriersArgs, options?: GetApiApiV1StrategiesStrategyIdCarriersOptions) => { const {baseUrl = "https://oms-api.tenant.krzlabs.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.active != null) query.append("active", String(args.active)) if (args.carrier_id != null) query.append("carrier_id", String(args.carrier_id)) if (args.delivery_type_code != null) query.append("delivery_type_code", String(args.delivery_type_code)) for (const item of args.capability ?? []) query.append("capability", String(item)) if (args.q != null) query.append("q", String(args.q)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/strategies/${encodeURIComponent(String(args.strategyId))}/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(strategyCarrierListResponse, await res.json()) };