import {z} from 'zod' export type GetApiDepotsDepotIdArgs = {depotId: string}; export type GetApiDepotsDepotIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const depotIdSchema = z.string(); export const depotSchema = z.object({id: depotIdSchema, name: z.string(), routeOverrides: z.object({startTime: z.object({hour: z.number().int().gte(0).lte(23), minute: z.number().int().gte(0).lte(59)}), startAddress: z.object({address: z.string().nullable(), addressLineOne: z.string().nullable(), addressLineTwo: z.string().nullable(), latitude: z.number().nullable(), longitude: z.number().nullable(), placeId: z.string().nullable(), countryCode: z.string().nullable()}), defaultTimeAtStop: z.number().nullable().optional(), endAddress: z.object({address: z.string().nullable(), addressLineOne: z.string().nullable(), addressLineTwo: z.string().nullable(), latitude: z.number().nullable(), longitude: z.number().nullable(), placeId: z.string().nullable(), countryCode: z.string().nullable()}).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().nullable().optional(), roadSide: z.enum(["any", "left_only", "right_only"]).nullable().optional(), vehicleType: z.enum(["bike", "scooter", "car", "small_truck", "truck", "electric_cargo_bike"]).nullable().optional(), roundTrip: z.boolean().nullable().optional()}).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 }; /** * Retrieve a depot * * GET /depots/{depotId} */ export const getApiDepotsDepotId = async (args: GetApiDepotsDepotIdArgs, options?: GetApiDepotsDepotIdOptions) => { const {baseUrl = "https://api.spoke.com/public/v1", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/depots/${encodeURIComponent(String(args.depotId))}${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(depotSchema, await res.json()) };