import {z} from 'zod' export type HubLabelKind = 'rack' | 'package'; export type HubLabelPackageIdentity = {orderId: string, itemId: string}; export type HubLabelRequest = {manifestVersion: string, kinds: Array, packages?: (Array | null) | undefined}; export type CreateApiV1HubScanningRoutesRouteIdLabelsArgs = {routeId: string, body: HubLabelRequest}; export type CreateApiV1HubScanningRoutesRouteIdLabelsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const hubLabelMedia = z.object({widthInches: z.literal(4).optional(), heightInches: z.literal(2).optional(), dpi: z.literal(203).optional()}); export const hubLabelKind = z.enum(["rack", "package"]); export const hubLabel = z.object({kind: hubLabelKind, symbology: z.enum(["qr", "code128"]), barcode: z.string(), routeId: z.string(), orderId: z.string().nullable().optional(), itemId: z.string().nullable().optional(), stopNumber: z.number().int().nullable().optional(), zpl: z.string()}); export const hubLabelResponse = z.object({routeId: z.string(), manifestVersion: z.string(), templateVersion: z.string(), media: hubLabelMedia.optional(), labels: z.array(hubLabel)}); 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 }; /** * Generate rack and package labels from a current hub route manifest * * POST /v1/hub-scanning/routes/{route_id}/labels */ export const createApiV1HubScanningRoutesRouteIdLabels = async (args: CreateApiV1HubScanningRoutesRouteIdLabelsArgs, options?: CreateApiV1HubScanningRoutesRouteIdLabelsOptions) => { 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/hub-scanning/routes/${encodeURIComponent(String(args.route_id))}/labels${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(hubLabelResponse, await res.json()) };