import {z} from 'zod' export type GetApiV1ShiftsArgs = {startTime: string, endTime: string, storeLocationId?: (string | null) | undefined, externalStoreLocationId?: (string | null) | undefined}; export type GetApiV1ShiftsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const breakConfiguration = z.object({timeWindow: z.array(z.unknown()).nullable().optional(), breakDuration: z.number().int(), maxWorkingTime: z.number().int().nullable().optional()}); export const breakConfig = z.object({anchor: z.literal("shift_start").nullable().optional(), preferOffRoute: z.boolean().optional()}); export const vehicleConfig = z.object({weight: z.number().int().nullable().optional(), height: z.number().int().nullable().optional(), length: z.number().int().nullable().optional(), width: z.number().int().nullable().optional(), axlecount: z.number().int().nullable().optional(), trailercount: z.number().int().nullable().optional(), weightunit: z.enum(["kg", "lbs"]).nullable().optional(), lengthunit: z.enum(["meter", "in"]).nullable().optional(), maxstops: z.number().int().nullable().optional(), maxdistance: z.number().int().nullable().optional(), maxduration: z.number().int().nullable().optional(), speedfactor: z.number().nullable().optional(), capabilities: z.array(z.string()).nullable().optional(), fixedcost: z.number().nullable().optional(), costperunitdistance: z.number().nullable().optional(), costperunittime: z.number().nullable().optional(), vehicleprofile: z.string().nullable().optional(), minordersperroute: z.number().int().nullable().optional(), parkingtime: z.number().nullable().optional()}); export const optimizationParams = z.object({serviceTimeRelativeEfficiency: z.number().nullable().optional(), plannedBreaks: z.array(breakConfiguration).nullable().optional(), breakConfig: breakConfig.nullable().optional(), departureDelay: z.number().int().nullable().optional(), shiftEndServiceTimeMinutes: z.number().int().nullable().optional(), vehicleConfig: vehicleConfig.nullable().optional()}); export const shiftResponse = z.object({id: z.string(), name: z.string(), plannedStart: z.string(), plannedEnd: z.string(), plannedBreakDuration: z.number().int().nullable().optional(), shiftMetadata: z.record(z.string(), z.unknown()).nullable().optional(), optimizationParams: optimizationParams.nullable().optional(), status: z.string(), storeLocationId: z.string().nullable().optional(), externalStoreLocationId: z.string().nullable().optional(), zoneIds: z.array(z.string()).nullable().optional(), externalZoneIds: z.array(z.string()).nullable().optional(), courierId: z.string().nullable().optional(), vehicleId: z.string().nullable().optional(), createdAt: z.string(), updatedAt: 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 all shifts * * GET /v1/shifts */ export const getApiV1Shifts = async (args: GetApiV1ShiftsArgs, options?: GetApiV1ShiftsOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.storeLocationId != null) query.append("storeLocationId", String(args.storeLocationId)) if (args.externalStoreLocationId != null) query.append("externalStoreLocationId", String(args.externalStoreLocationId)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/shifts${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(shiftResponse, await res.json()) };