import {z} from 'zod' export type DispatchQuoteInput = {providerId: string, totalPriceCents: number}; export type DispatchRuleContextInput = {packageValueCents?: (number | null) | undefined, itemsCount?: (number | null) | undefined, pickupTime?: (string | null) | undefined, dropoffTime?: (string | null) | undefined, drivingDistance?: (number | null) | undefined, tags?: (Array | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, pickupStoreLocationId?: (string | null) | undefined, dropoffStoreLocationId?: (string | null) | undefined, packageHeight?: (number | null) | undefined, packageWidth?: (number | null) | undefined, packageDepth?: (number | null) | undefined, packageWeight?: (number | null) | undefined, totalWeight?: (number | null) | undefined, packageRequirements?: (Array | null) | undefined, packageDeliveryMode?: (string | null) | undefined, packageTipCents?: (number | null) | undefined, numberOfPackages?: number | undefined, isBatch?: boolean | undefined}; export type EvaluateDispatchInput = {pickupAddress?: (string | null) | undefined, pickupLat?: (number | null) | undefined, pickupLng?: (number | null) | undefined, pickupAddressCity?: (string | null) | undefined, pickupAddressState?: (string | null) | undefined, pickupAddressZip?: (string | null) | undefined, pickupAddressCountry?: (string | null) | undefined, pickupBusinessName?: (string | null) | undefined, dropoffAddress?: (string | null) | undefined, dropoffLat?: (number | null) | undefined, dropoffLng?: (number | null) | undefined, dropoffAddressCity?: (string | null) | undefined, dropoffAddressState?: (string | null) | undefined, dropoffAddressZip?: (string | null) | undefined, dropoffAddressCountry?: (string | null) | undefined, dropoffBusinessName?: (string | null) | undefined, quotes: Array, context?: (DispatchRuleContextInput | null) | undefined, optionsGroupId?: (string | null) | undefined}; export type CreateApiV1DispatchEvaluateArgs = {body: EvaluateDispatchInput}; export type CreateApiV1DispatchEvaluateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dispatchRuleResult = z.object({matchedRuleId: z.string().nullable().optional(), matchedRuleLabel: z.string().nullable().optional(), matchedRuleCondition: z.string().nullable().optional(), optionsGroupId: z.string().nullable().optional(), optionsGroupLabel: z.string().nullable().optional()}); export const rankedProvider = z.object({providerId: z.string(), parentProviderId: z.string(), providerName: z.string(), probability: z.number(), rank: z.number().int()}); export const evaluateDispatchOutput = z.object({dispatchRule: dispatchRuleResult.nullable().optional(), rankedProviders: z.array(rankedProvider), strategy: z.string().nullable().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 }; /** * Evaluate dynamic dispatch * * POST /v1/dispatch/evaluate */ export const createApiV1DispatchEvaluate = async (args: CreateApiV1DispatchEvaluateArgs, options?: CreateApiV1DispatchEvaluateOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/dispatch/evaluate${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(evaluateDispatchOutput, await res.json()) };