import {z} from 'zod' export type PatchApiDriversDriverIdArgs = {driverId: string, body: {name?: (string | null) | undefined, displayName?: (string | null) | undefined, depots?: (Array | null) | undefined, routeOverrides?: ({startAddress?: ({addressName?: (string | null) | undefined, addressLineOne?: (string | null) | undefined, addressLineTwo?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, zip?: (string | null) | undefined, country?: (string | null) | undefined, latitude?: (number | null) | undefined, longitude?: (number | null) | undefined} | null) | undefined, endAddress?: ({addressName?: (string | null) | undefined, addressLineOne?: (string | null) | undefined, addressLineTwo?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, zip?: (string | null) | undefined, country?: (string | null) | undefined, latitude?: (number | null) | undefined, longitude?: (number | null) | undefined} | null) | undefined, startTime?: ({hour: number, minute: number} | null) | undefined, endTime?: ({hour: number, minute: number} | null) | undefined, maxStops?: (number | null) | undefined, drivingSpeed?: ('slower' | 'average' | 'faster' | null) | undefined, deliverySpeed?: ('slower' | 'average' | 'faster' | null) | undefined, vehicleType?: ('bike' | 'scooter' | 'car' | 'small_truck' | 'truck' | 'electric_cargo_bike' | null) | undefined} | null) | undefined}}; export type PatchApiDriversDriverIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const driverIdSchema = z.string(); export const depotIdSchema = z.string(); export const timeOfDaySchema = z.object({hour: z.number().int().gte(0).lte(23), minute: z.number().int().gte(0).lte(59)}); export const driverSchema = z.object({id: driverIdSchema, name: z.string().nullable(), email: z.string().nullable(), phone: z.string().nullable(), displayName: z.string().nullable(), active: z.boolean(), depots: z.array(depotIdSchema), routeOverrides: z.object({startTime: timeOfDaySchema.nullable(), endTime: timeOfDaySchema.nullable(), startAddress: z.object({address: z.string(), addressLineOne: z.string(), addressLineTwo: z.string(), latitude: z.number().gte(-90).lte(90).nullable(), longitude: z.number().gte(-180).lte(180).nullable(), placeId: z.string().nullable(), placeTypes: z.array(z.string())}).nullable(), endAddress: z.object({address: z.string(), addressLineOne: z.string(), addressLineTwo: z.string(), latitude: z.number().gte(-90).lte(90).nullable(), longitude: z.number().gte(-180).lte(180).nullable(), placeId: z.string().nullable(), placeTypes: z.array(z.string())}).nullable(), maxStops: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable(), drivingSpeed: z.enum(["slower", "average", "faster"]), deliverySpeed: z.enum(["slower", "average", "faster"]), vehicleType: z.enum(["bike", "scooter", "car", "small_truck", "truck", "electric_cargo_bike"]).nullable()})}); 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 }; /** * Update a driver * * PATCH /drivers/{driverId} */ export const patchApiDriversDriverId = async (args: PatchApiDriversDriverIdArgs, options?: PatchApiDriversDriverIdOptions) => { const {baseUrl = "https://api.spoke.com/public/v1", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/drivers/${encodeURIComponent(String(args.driverId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(driverSchema, await res.json()) };