import {z} from 'zod' export type GetApiV1RoutesNearbyCouriersArgs = {lat: number, lng: number, limit?: number | undefined, store_location_id?: (string | null) | undefined, exclude_route_ids?: (string | null) | undefined, max_location_age_minutes?: number | undefined}; export type GetApiV1RoutesNearbyCouriersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const routeStatus = z.enum(["created", "assigned", "started", "in_progress", "completed", "failed", "canceled"]); export const nearbyCourier = z.object({courier_id: z.string(), courier_name: z.string().nullable(), route_id: z.string(), route_name: z.string().nullable(), route_status: routeStatus, lat: z.number(), lng: z.number(), captured_at: z.string(), distance_meters: z.number().int()}); export const nearbyCouriersResponse = z.object({couriers: z.array(nearbyCourier)}); 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 }; /** * Find Nearby Couriers * * GET /v1/routes/nearby-couriers */ export const getApiV1RoutesNearbyCouriers = async (args: GetApiV1RoutesNearbyCouriersArgs, options?: GetApiV1RoutesNearbyCouriersOptions) => { 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.lat != null) query.append("lat", String(args.lat)) if (args.lng != null) query.append("lng", String(args.lng)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.store_location_id != null) query.append("store_location_id", String(args.store_location_id)) if (args.exclude_route_ids != null) query.append("exclude_route_ids", String(args.exclude_route_ids)) if (args.max_location_age_minutes != null) query.append("max_location_age_minutes", String(args.max_location_age_minutes)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/routes/nearby-couriers${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(nearbyCouriersResponse, await res.json()) };