import {z} from 'zod' export type VehicleDisplaySize = 'bike' | 'motorbike' | 'cargobike' | 'car' | 'suv' | 'pickup_truck' | 'van' | 'truck' | 'large_van' | 'extra_large_van'; export type VehicleInfo = {type?: (VehicleDisplaySize | null) | undefined, make?: (string | null) | undefined, model?: (string | null) | undefined, licenseplate?: (string | null) | undefined, color?: (string | null) | undefined, year?: (string | null) | undefined}; export type WorkRelationship = 'EMPLOYEE' | 'CONTRACTOR'; export type CreateCourierInputSerializer = {externalidentifier?: (string | null) | undefined, firstname: string, lastname: string, phonenumber: string, vehicleinfo?: (VehicleInfo | null) | undefined, dateofbirth?: (string | null) | undefined, workrelationship?: (WorkRelationship | null) | undefined, driverslicense?: (string | null) | undefined, vehicleids?: (Array | null) | undefined}; export type CreateApiV1CourierArgs = {body: CreateCourierInputSerializer}; export type CreateApiV1CourierOptions = {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 }; /** * Create a courier * * POST /v1/courier */ export const createApiV1Courier = async (args: CreateApiV1CourierArgs, options?: CreateApiV1CourierOptions) => { 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/courier${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(courierResponse, await res.json()) };