import {z} from 'zod' export type CreateApiDriversImportArgs = {body: Array<{name?: (string | null) | undefined, displayName?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (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 CreateApiDriversImportOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiDriversImportResponse = z.object({success: z.array(z.string()), failed: z.array(z.object({error: z.object({message: z.string()}), driver: z.object({name: z.string().min(1).max(255).nullable().optional(), displayName: z.string().min(1).max(255).nullable().optional(), email: z.string().min(1).max(255).nullable().optional(), phone: z.string().min(1).max(255).nullable().optional(), depots: z.array(z.string()).nullable().optional(), routeOverrides: z.object({startAddress: z.object({addressName: z.string().min(1).max(255).nullable().optional(), addressLineOne: z.string().min(1).max(255).nullable().optional(), addressLineTwo: z.string().min(1).max(255).nullable().optional(), city: z.string().min(1).max(100).nullable().optional(), state: z.string().min(1).max(100).nullable().optional(), zip: z.string().min(1).max(100).nullable().optional(), country: z.string().min(1).max(100).nullable().optional(), latitude: z.number().gte(-90).lte(90).nullable().optional(), longitude: z.number().gte(-180).lte(180).nullable().optional()}).nullable().optional(), endAddress: z.object({addressName: z.string().min(1).max(255).nullable().optional(), addressLineOne: z.string().min(1).max(255).nullable().optional(), addressLineTwo: z.string().min(1).max(255).nullable().optional(), city: z.string().min(1).max(100).nullable().optional(), state: z.string().min(1).max(100).nullable().optional(), zip: z.string().min(1).max(100).nullable().optional(), country: z.string().min(1).max(100).nullable().optional(), latitude: z.number().gte(-90).lte(90).nullable().optional(), longitude: z.number().gte(-180).lte(180).nullable().optional()}).nullable().optional(), startTime: z.object({hour: z.number().int().gte(0).lte(23), minute: z.number().int().gte(0).lte(59)}).nullable().optional(), endTime: z.object({hour: z.number().int().gte(0).lte(23), minute: z.number().int().gte(0).lte(59)}).nullable().optional(), maxStops: z.number().int().gte(0).lte(9007199254740991).nullable().optional(), drivingSpeed: z.enum(["slower", "average", "faster"]).nullable().optional(), deliverySpeed: z.enum(["slower", "average", "faster"]).nullable().optional(), vehicleType: z.enum(["bike", "scooter", "car", "small_truck", "truck", "electric_cargo_bike"]).nullable().optional()}).nullable().optional()})}))}); 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 }; /** * Batch import drivers * * POST /drivers:import */ export const createApiDriversImport = async (args: CreateApiDriversImportArgs, options?: CreateApiDriversImportOptions) => { 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:import${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(createApiDriversImportResponse, await res.json()) };