import {z} from 'zod' export type CoverageCityCreate = {city: string, state: string}; export type CoverageZipRangeCreate = {start: string, end: string}; export type PolygonGeometryCreate = {type: 'Polygon', coordinates: Array>>}; export type CoverageAreaCreate = {type: 'STATES' | 'CITIES' | 'ZIP_RANGES' | 'POLYGON', states?: Array | undefined, cities?: Array | undefined, ranges?: Array | undefined, polygon?: PolygonGeometryCreate | undefined}; export type StrategyCarrierFreightRulesCreate = {sla_hours: number, fixed_cost: number, cost_per_kg: number}; export type StrategyCarrierOperatingHourCreate = {day: string, open: string, close: string}; export type StrategyCarrierHolidaysHoursCreate = {open: string, close: string}; export type StrategyCarrierCreate = {carrier_id: string, delivery_type_codes: Array, active?: boolean | undefined, holidays_open?: boolean | undefined, delivery_modalities?: Array | undefined, channel_codes?: Array | undefined, coverage_area?: (CoverageAreaCreate | null) | undefined, freight_rules?: (StrategyCarrierFreightRulesCreate | null) | undefined, operating_hours?: (Array | null) | undefined, holidays_hours?: (StrategyCarrierHolidaysHoursCreate | null) | undefined, metadata?: (Record | null) | undefined}; export type CreateApiApiV1StrategiesStrategyIdCarriersArgs = {strategyId: string, body: StrategyCarrierCreate}; export type CreateApiApiV1StrategiesStrategyIdCarriersOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const strategyCarrierCreateResponse = z.object({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({value: z.object({sla_hours: z.number().optional(), fixed_cost: z.number().optional(), cost_per_kg: z.number().optional()}), inherited: z.boolean()}), operating_hours: z.object({value: z.array(z.object({day: z.string(), open: z.string(), close: z.string()})).optional(), inherited: z.boolean()}), holidays_hours: z.object({open: z.string(), close: z.string()}), carrier_active: z.boolean(), 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()})}), metadata: z.object({}).nullable(), 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 }; /** * Create strategy carrier * * POST /api/v1/strategies/{strategyId}/carriers * * Also accepts auth: bearer */ export const createApiApiV1StrategiesStrategyIdCarriers = async (args: CreateApiApiV1StrategiesStrategyIdCarriersArgs, options?: CreateApiApiV1StrategiesStrategyIdCarriersOptions) => { const {baseUrl = "https://oms-api.tenant.krzlabs.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(strategyCarrierCreateResponse, await res.json()) };