import {z} from 'zod' export type RemotePreconditioning = {airConditioning: {immediate?: boolean | undefined, programs?: (Array<{recurrence?: ('None' | 'Daily') | undefined, start: string, occurence?: ({day: Array<'Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat' | 'Sun'>}) | undefined, slot?: number | undefined, enabled?: boolean | undefined, actionsType?: ('Delete' | 'Set') | undefined}>) | undefined}}; export type RemoteSetImmobilization = {activate: boolean}; export type RemoteDoorsState = {state: 'Unlocked' | 'Locked', forced?: boolean | undefined}; export type RemoteHorn = {state: 'Activated' | 'Unactivated'}; export type RemoteCharging = {nextDelayedTime?: string | undefined, schedule?: ({nextDelayedTime?: string | undefined, programs?: (Array<{start: string, occurence?: ({day: Array<'Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat' | 'Sun'>}) | undefined, enabled?: boolean | undefined, slot?: number | undefined, end?: string | undefined}>) | undefined}) | undefined, immediate?: boolean | undefined, preferences?: ({level?: ('Level1' | 'Level2' | 'Level3' | 'Level4' | 'Level5') | undefined, type?: ('Partial' | 'Full') | undefined}) | undefined}; export type RemoteStolen = {stolen?: boolean | undefined, trackingPeriod?: ({running?: number | undefined, shutdown?: number | undefined}) | undefined, immobilization?: {activate: boolean} | undefined}; export type RemoteLights = {on: boolean}; export type RemoteWakeUp = {action?: string | undefined}; export type Point = {coordinates: Array, type: 'Point'}; export type RemoteNavigation = {driverApproval?: boolean | undefined, positions: Array}; export type Remote = {label?: string | undefined, preconditioning?: RemotePreconditioning | undefined, immobilization?: RemoteSetImmobilization | undefined, door?: RemoteDoorsState | undefined, horn?: RemoteHorn | undefined, charging?: RemoteCharging | undefined, stolen?: RemoteStolen | undefined, lights?: RemoteLights | undefined, wakeUp?: RemoteWakeUp | undefined, navigation?: RemoteNavigation | undefined}; export type CreateApiFleetsFidVehiclesVidCallbacksCbidRemotesArgs = {fid: string, vid: string, cbid: string, body: Remote}; export type CreateApiFleetsFidVehiclesVidCallbacksCbidRemotesOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, ClientId?: string | undefined, fetch?: typeof fetch | undefined}; export const url = z.string(); export const link = z.object({href: url, templated: z.boolean().optional(), type: z.string().optional(), deprecation: z.string().optional(), name: z.string().optional(), title: z.string().optional(), profile: z.string().optional(), hreflang: z.string().optional()}); export const remoteActionId = z.string(); export const remoteType = z.enum(["ThermalPreconditioning", "ElectricBatteryChargingRequest", "Horn", "Doors", "Lights", "Immobilization", "Stolen", "WakeUp", "Navigation"]); export const remotePostResponse = z.object({_links: z.object({fleet: link.optional(), callback: link.optional(), remote: link.optional()}).optional(), remoteActionId: remoteActionId.optional(), type: remoteType.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 }; /** * Send a remote action to a vehicle. * * POST /fleets/{fid}/vehicles/{vid}/callbacks/{cbid}/remotes */ export const createApiFleetsFidVehiclesVidCallbacksCbidRemotes = async (args: CreateApiFleetsFidVehiclesVidCallbacksCbidRemotesArgs, options?: CreateApiFleetsFidVehiclesVidCallbacksCbidRemotesOptions) => { const {baseUrl = "https://api-cert.groupe-psa.com/connectedcar/v3", fetch: fetchFn = fetch, BasicAuth, ClientId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/hal+json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) if (ClientId !== undefined) query.set("client_id", ClientId) const url = `${baseUrl.replace(/\/+$/, '')}/fleets/${encodeURIComponent(String(args.fid))}/vehicles/${encodeURIComponent(String(args.vid))}/callbacks/${encodeURIComponent(String(args.cbid))}/remotes${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(remotePostResponse, await res.json()) };