import {z} from 'zod' export type CreateApiDepotsImportArgs = {body: Array<{name: string, 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}, startTime: {hour: number, minute: number}, 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, endTime?: ({hour: number, minute: number} | null) | undefined, defaultTimeAtStop?: (number | null) | undefined, maxStops?: (number | null) | undefined, vehicleType?: ('bike' | 'scooter' | 'car' | 'small_truck' | 'truck' | 'electric_cargo_bike' | null) | undefined, roadSide?: ('any' | 'left_only' | 'right_only' | null) | undefined, roundTrip?: (boolean | null) | undefined}}>}; export type CreateApiDepotsImportOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiDepotsImportResponse = z.object({success: z.array(z.string()), failed: z.array(z.object({error: z.object({message: z.string()}), depot: z.object({name: z.string().min(1).max(2048), 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()}), startTime: z.object({hour: z.number().int().gte(0).lte(23), minute: z.number().int().gte(0).lte(59)}), 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(), endTime: z.object({hour: z.number().int().gte(0).lte(23), minute: z.number().int().gte(0).lte(59)}).nullable().optional(), defaultTimeAtStop: z.number().nullable().optional(), maxStops: z.number().nullable().optional(), vehicleType: z.enum(["bike", "scooter", "car", "small_truck", "truck", "electric_cargo_bike"]).nullable().optional(), roadSide: z.enum(["any", "left_only", "right_only"]).nullable().optional(), roundTrip: z.boolean().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 depots * * POST /depots:import */ export const createApiDepotsImport = async (args: CreateApiDepotsImportArgs, options?: CreateApiDepotsImportOptions) => { 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(/\/+$/, '')}/depots: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(createApiDepotsImportResponse, await res.json()) };