import {z} from 'zod' export type GetApiV1ResourceownerVehicles3StatusOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const vehicleStatusModel = z.object({VehicleId: z.number().int().optional(), LastPositionUtc: z.string().optional(), LastKnownLatitude: z.number().optional(), LastKnownLongitude: z.number().optional(), IsDriving: z.boolean().optional(), MakeImageUrl: z.string().optional(), ParkedForNumberOfSeconds: z.number().int().optional(), DrivingForNumberOfSeconds: z.number().int().optional(), LastKnownFormattedAddress: z.string().optional(), LastKnownStreetAddress: z.string().optional(), LastKnownCity: z.string().optional(), LastTripId: z.number().int().optional(), LastLocationType: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional(), CategoryColor: z.number().int().optional(), Heading: z.number().int().optional()}); export const getApiV1ResourceownerVehicles3StatusResponse = z.array(vehicleStatusModel); 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 }; /** * Get position and status of all vehicles that the user has access to * * GET /v1/resourceowner/vehicles3/status */ export const getApiV1ResourceownerVehicles3Status = async (options: GetApiV1ResourceownerVehicles3StatusOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/resourceowner/vehicles3/status${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(getApiV1ResourceownerVehicles3StatusResponse, await res.json()) };