import {z} from 'zod' export type GetApiV1RoutesArgs = {limit?: number | undefined, offset?: number | undefined, sortDirection?: string | undefined, startDate?: (string | null) | undefined, endDate?: (string | null) | undefined, tags?: (string | null) | undefined, search?: (string | null) | undefined, isDispatched?: (boolean | null) | undefined, dispatchStrategyId?: (string | null) | undefined, pickupStoreLocationIds?: (string | null) | undefined, dropoffStoreLocationIds?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, country?: (string | null) | undefined}; export type GetApiV1RoutesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const routeListCourierRead = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional()}); export const routeListVehicleRead = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional()}); export const routeListRead = z.object({id: z.string(), externalId: z.string().nullable().optional(), name: z.string().nullable().optional(), status: z.string().nullable().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(), portalUrl: z.string().nullable().optional(), providerName: z.string().nullable().optional(), courier: routeListCourierRead.nullable().optional(), vehicle: routeListVehicleRead.nullable().optional(), stopCount: z.number().int().nullable().optional(), orderIds: z.array(z.string()).nullable().optional(), distanceMeters: z.number().int().nullable().optional(), durationSeconds: z.number().int().nullable().optional()}); export const routeListPageRead = z.object({results: z.array(routeListRead), totalCount: z.number().int(), limit: z.number().int(), offset: z.number().int()}); 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/Search Routes * * GET /v1/routes */ export const getApiV1Routes = async (args: GetApiV1RoutesArgs, options?: GetApiV1RoutesOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.search != null) query.append("search", String(args.search)) if (args.isDispatched != null) query.append("isDispatched", String(args.isDispatched)) if (args.dispatchStrategyId != null) query.append("dispatchStrategyId", String(args.dispatchStrategyId)) if (args.pickupStoreLocationIds != null) query.append("pickupStoreLocationIds", String(args.pickupStoreLocationIds)) if (args.dropoffStoreLocationIds != null) query.append("dropoffStoreLocationIds", String(args.dropoffStoreLocationIds)) if (args.city != null) query.append("city", String(args.city)) if (args.state != null) query.append("state", String(args.state)) if (args.country != null) query.append("country", String(args.country)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/routes${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(routeListPageRead, await res.json()) };