import {z} from 'zod' export type GetApiV1CouriersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const vehicleDisplaySize = z.enum(["bike", "motorbike", "cargobike", "car", "suv", "pickup_truck", "van", "truck", "large_van", "extra_large_van"]); export const vehicleInfo = z.object({type: vehicleDisplaySize.nullable().optional(), make: z.string().nullable().optional(), model: z.string().nullable().optional(), licenseplate: z.string().nullable().optional(), color: z.string().nullable().optional(), year: z.string().nullable().optional()}); export const vehicleLocation = z.object({lat: z.number(), lng: z.number()}); export const workRelationship = z.enum(["EMPLOYEE", "CONTRACTOR"]); export const courierResponse = z.object({id: z.string(), externalidentifier: z.string(), firstname: z.string(), lastname: z.string(), phonenumber: z.string(), createdat: z.string(), firstversioncontractids: z.array(z.string()), vehicleinfo: vehicleInfo, courierlocation: vehicleLocation, dateofbirth: z.string().nullable().optional(), workrelationship: workRelationship.nullable().optional(), driverslicense: z.string().nullable().optional(), vehicleids: z.array(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 couriers * * GET /v1/couriers */ export const getApiV1Couriers = async (options?: GetApiV1CouriersOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(courierResponse, await res.json()) };