import {z} from 'zod' export type ScopeEnum = 'vehicle:all' | 'vehicle:charging_history' | 'vehicle:driving_behavior' | 'vehicle:location_history' | 'vehicle:telemetry' | 'vehicle:information' | 'vehicle:commands' | 'battery:all' | 'battery:telemetry' | 'geofences:all' | 'geofences:read' | 'geofences:write'; export type ConsentFlowLocales = 'en-GB' | 'de-DE' | 'fr-FR' | 'nl-NL'; export type ConsentFlowIntegration = 'BMW_ENERGY_API'; export type MakeEnum = 'ABARTH' | 'ALFA ROMEO' | 'AUDI' | 'BMW' | 'CHRYSLER' | 'CITROEN' | 'CUPRA' | 'DODGE' | 'DS AUTOMOBILES' | 'FIAT' | 'FORD' | 'HYUNDAI' | 'JEEP' | 'KIA' | 'LANCIA' | 'LEXUS' | 'MASERATI' | 'MERCEDES BENZ' | 'MINI' | 'NISSAN' | 'OPEL' | 'PEUGEOT' | 'POLESTAR' | 'RAM' | 'RENAULT' | 'SEAT' | 'SKODA' | 'TESLA' | 'TOYOTA' | 'VAUXHALL' | 'VOLKSWAGEN' | 'VOLVO'; export type DeviceTypeEnum = 'VEHICLE' | 'BATTERY' | 'CHARGER' | 'INVERTER' | 'HEAT_PUMP'; export type OEMRegion = 'NORTH_AMERICA' | 'EUROPE'; export type AccountConnectPayload = {returnUrl?: (string | null) | undefined, scopes: Array, locale?: (ConsentFlowLocales | null) | undefined, integrationMethod?: (ConsentFlowIntegration | null) | undefined, chargerLocationLatitude?: (number | string | null) | undefined, chargerLocationLongitude?: (number | string | null) | undefined, countryCode?: (string | null) | undefined, makes?: (Array | null) | undefined, deviceTypes: Array, urlExpiration?: (number | null) | undefined, region?: (OEMRegion | null) | undefined, vin?: (string | null) | undefined, model?: (string | null) | undefined, modelYear?: (number | null) | undefined}; export type CreateApiV1AccountsConnectArgs = {body: AccountConnectPayload}; export type CreateApiV1AccountsConnectOptions = {baseUrl?: string | undefined, OAuth2ClientCredentialsBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const accountConnect = z.object({authenticationUrl: z.string()}); 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 }; /** * Action Connect * * POST /v1/accounts:connect */ export const createApiV1AccountsConnect = async (args: CreateApiV1AccountsConnectArgs, options?: CreateApiV1AccountsConnectOptions) => { const {baseUrl = "https://api.volteras.com", fetch: fetchFn = fetch, OAuth2ClientCredentialsBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2ClientCredentialsBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentialsBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts:connect${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(accountConnect, await res.json()) };