'use strict'; var renaultApi = require('@remscodes/renault-api'); var drino = require('drino'); var thror = require('thror'); var dayjs = require('dayjs'); var __defProp$3 = Object.defineProperty; var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value); class RenaultSession { constructor(init) { /** * Locale that will be used to format date. * * @default "fr_FR" */ __publicField$3(this, "locale"); /** * Country code that will use as http param for Kamereon. * * @default "FR" */ __publicField$3(this, "country"); /** * Token to use Gigya getJWT API. * * Automatically set when Gigya login API is called and succeed. */ __publicField$3(this, "gigyaToken"); /** * Token to use Kamereon API. * * Automatically set when Gigya getJWT API is called and succeed. */ __publicField$3(this, "token"); /** * Selected person id. * * Automatically set when Gigya getAccountInfo API is called and succeed. */ __publicField$3(this, "personId"); /** * Selected account id. * * To be set in order to be automatically passed into each Kamereon API functions that needs it. * * Otherwise, it needs to be manually passed as function argument using `KamereonClient`. */ __publicField$3(this, "accountId"); /** * Selected vehicle vin. * * To be set in order to be automatically passed into each Kamereon API functions that needs it. * * Otherwise, it needs to be manually passed as function argument using `KamereonClient`. */ __publicField$3(this, "vin"); this.locale = init?.locale ?? "fr_FR"; this.country = init?.country ?? "FR"; } } function fixGigyaResponse(res) { if (isGigyaErrorResponse(res)) throw responseToError(res); return res; } function isGigyaErrorResponse(res) { return (res.body?.statusCode ?? 0) >= 400; } function responseToError({ headers, body, url, statusText }) { return new drino.HttpErrorResponse({ headers, url: `${url}`, status: body?.statusCode ?? 500, statusText, error: { message: body?.errorMessage, details: body?.errorDetails, time: body.time } }); } var __defProp$2 = Object.defineProperty; var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value); class GigyaClient { constructor(init = {}) { /** * The user session. */ __publicField$2(this, "session"); /** @internal */ __publicField$2(this, "httpClient"); const { session = new RenaultSession(), onError } = init; this.session = session; this.httpClient = drino.create({ requestsConfig: { queryParams: { apikey: renaultApi.GigyaApi.KEY }, progress: { download: { inspect: false } } }, interceptors: { beforeConsume: (req) => { const token = this.session.gigyaToken; if (token) req.url.searchParams.set("login_token", token); }, beforeError: (res) => onError?.(res, this.session) } }); } /** * Login to Gigya service. * @param {string} loginID - The user login. * @param {string} password - The user password. */ login(loginID, password) { return this.httpClient.post(renaultApi.GigyaApi.LOGIN_URL, {}, { queryParams: { loginID, password }, wrapper: "response" }).transform((res) => fixGigyaResponse(res)).transform((res) => res.body).check((result) => this.session.gigyaToken = result.sessionInfo?.cookieValue).consume(); } /** * Get account info. */ getAccountInfo() { return this.httpClient.post(renaultApi.GigyaApi.GET_ACCOUNT_INFO_URL, {}, { wrapper: "response" }).transform((res) => fixGigyaResponse(res)).transform((res) => res.body).check((result) => this.session.personId = result.data?.personId).consume(); } /** * Get JWT. * @param {number} [expiration = 900] - The chosen expiration (in milliseconds) of the JWT. */ getJwt(expiration = 900) { return this.httpClient.post(renaultApi.GigyaApi.GET_JWT_URL, {}, { queryParams: { fields: ["data.personId", "data.gigyaDataCenter"], expiration: `${expiration}` }, wrapper: "response" }).transform((res) => fixGigyaResponse(res)).transform((res) => res.body).check((token) => this.session.token = token.id_token).consume(); } /** * Get public info about JWT key. */ getJwtPublicKey() { return this.httpClient.post(renaultApi.GigyaApi.GET_JWT_PUBLIC_KEY_URL, {}, { wrapper: "response" }).transform((res) => fixGigyaResponse(res)).transform((res) => res.body).consume(); } /** * Logout from Gigya service. */ logout() { return this.httpClient.post(renaultApi.GigyaApi.LOGOUT_URL, {}, { wrapper: "response" }).transform((res) => fixGigyaResponse(res)).transform((res) => res.body).consume(); } } function dateFilterToParams({ start, end, period }, locale) { const params = new URLSearchParams({ start: normalizeDate(start, locale, period), end: normalizeDate(end, locale, period) }); if (period) params.set("type", period); return params; } function normalizeDate(date, locale, period = "day") { return formatDate(date, renaultApi.PERIOD_FORMATS[period], locale); } function formatDate(date, format, locale) { return dayjs(date, { format, locale }).toString(); } var __defProp$1 = Object.defineProperty; var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value); class KamereonClient { constructor(init = {}) { /** * The user session. */ __publicField$1(this, "session"); /** @internal */ __publicField$1(this, "httpClient"); const { session = new RenaultSession(), onError } = init; this.session = session; this.httpClient = drino.create({ requestsConfig: { headers: { apikey: renaultApi.KamereonApi.KEY, accept: "application/json", "content-type": "application/vnd.api+json" }, progress: { download: { inspect: false } } }, interceptors: { beforeConsume: (req) => { const token = this.session.token; if (token) req.headers.set("x-gigya-id_token", token); req.url.searchParams.set("country", this.session.country); }, beforeError: (res) => onError?.(res, this.session) } }); } /** * Get user person info. * @param {string?} [personId = the personId stored in the session] - The person id. */ getPerson(personId) { const requiredPersonId = this.getPersonIdOrThrow(personId, "getPerson"); return this.httpClient.get(renaultApi.KamereonApi.PERSON_URL(requiredPersonId)).consume(); } /** * Get user vehicles. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ getAccountVehicles(accountId) { const requiredAccountId = this.getAccountIdOrThrow(accountId, "getAccountVehicles"); return this.httpClient.get(renaultApi.KamereonApi.ACCOUNT_VEHICLES_URL(requiredAccountId)).consume(); } /** * Get user vehicle contracts. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ getVehicleContracts(vin, accountId) { return this.read({ apiUrl: "VEHICLE_CONTRACTS_URL", method: "getVehicleContracts", accountId, vin, queryParams: new URLSearchParams({ locale: this.session.locale, brand: "RENAULT", connectedServicesContracts: "true", warranty: "true", warrantyMaintenanceContracts: "true" }) }); } /** * Get user vehicle details. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ getVehicleDetails(vin, accountId) { return this.read({ apiUrl: "VEHICLE_DETAILS_URL", method: "getVehicleDetails", accountId, vin }); } /** * Get vehicle adapter info. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readAdapter(vin, accountId) { return this.read({ apiUrl: "READ_ADAPTER_URL", method: "readAdapter", accountId, vin }); } /** * Get vehicle battery status. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readBatteryStatus(vin, accountId) { return this.read({ apiUrl: "READ_BATTERY_STATUS_URL", method: "readBatteryStatus", accountId, vin }); } /** * Get vehicle charge history. * @param {DateFilter} filter - The date filter. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readChargeHistory(filter, vin, accountId) { return this.read({ apiUrl: "READ_CHARGE_HISTORY_URL", method: "readChargeHistory", accountId, vin, queryParams: dateFilterToParams(filter, this.session.locale) }); } /** * Get vehicle charge mode. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readChargeMode(vin, accountId) { return this.read({ apiUrl: "READ_CHARGE_MODE_URL", method: "readChargeMode", accountId, vin }); } /** * Get vehicle charges. * @param {Omit} filter - The date filter. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readCharges(filter, vin, accountId) { return this.read({ apiUrl: "READ_CHARGES_URL", method: "readCharges", accountId, vin, queryParams: dateFilterToParams(filter, this.session.locale) }); } /** * Get vehicle charging settings. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readChargingSettings(vin, accountId) { return this.read({ apiUrl: "READ_CHARGING_SETTINGS_URL", method: "readChargingSettings", accountId, vin }); } /** * Get vehicle cockpit. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readCockpit(vin, accountId) { return this.read({ apiUrl: "READ_COCKPIT_URL", method: "readCockpit", accountId, vin }); } /** * Get vehicle hvac history. * @param {DateFilter} filter - The date filter. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readHvacHistory(filter, vin, accountId) { return this.read({ apiUrl: "READ_HVAC_HISTORY_URL", method: "readHvacHistory", accountId, vin, queryParams: dateFilterToParams(filter, this.session.locale) }); } /** * Get vehicle hvac sessions. * @param {Omit} filter - The date filter. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readHvacSessions(filter, vin, accountId) { return this.read({ apiUrl: "READ_HVAC_SESSIONS_URL", method: "readHvacSessions", accountId, vin, queryParams: dateFilterToParams(filter, this.session.locale) }); } /** * Get vehicle hvac status. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readHvacStatus(vin, accountId) { return this.read({ apiUrl: "READ_HVAC_STATUS_URL", method: "readHvacStatus", accountId, vin }); } /** * Get vehicle hvac settings. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readHvacSettings(vin, accountId) { return this.read({ apiUrl: "READ_HVAC_SETTINGS_URL", method: "readHvacSettings", accountId, vin }); } /** * Get vehicle location. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readLocation(vin, accountId) { return this.read({ apiUrl: "READ_LOCATION_URL", method: "readLocation", accountId, vin }); } /** * Get vehicle lock status. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readLockStatus(vin, accountId) { return this.read({ apiUrl: "READ_LOCK_STATUS_URL", method: "readLockStatus", accountId, vin }); } /** * Get vehicle notification settings. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readNotificationSettings(vin, accountId) { return this.read({ apiUrl: "READ_NOTIFICATION_SETTINGS_URL", method: "readNotificationSettings", accountId, vin }); } /** * Get vehicle res state. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ readResState(vin, accountId) { return this.read({ apiUrl: "READ_RES_STATE_URL", method: "readResState", accountId, vin }); } /** * Select vehicle charge mode. * @param {ChargeModeInputs} inputs - The charge mode inputs. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ performChargeMode(inputs, vin, accountId) { const { action } = inputs; return this.perform({ apiUrl: "PERFORM_CHARGE_MODE_URL", method: "performChargeMode", accountId, vin, data: { type: "ChargeMode", attributes: { action } } }); } /** * Set vehicle charge schedule. * @param {ChargeScheduleInputs} inputs - The charge schedules inputs. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ performChargeSchedule(inputs, vin, accountId) { const { schedules } = inputs; return this.perform({ apiUrl: "PERFORM_CHARGE_SCHEDULE_URL", method: "performChargeSchedule", accountId, vin, data: { type: "ChargeSchedule", attributes: { schedules } } }); } /** * Set vehicle hvac schedule. * @param {HvacScheduleInputs} inputs - The hvac schedule inputs. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ performHvacSchedule(inputs, vin, accountId) { const { schedules } = inputs; return this.perform({ apiUrl: "PERFORM_HVAC_SCHEDULE_URL", method: "performHvacSchedule", accountId, vin, data: { type: "HvacSchedule", attributes: { schedules } } }); } /** * Start vehicle hvac. * @param {HvacStartInputs} inputs - The hvac inputs. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ performHvacStart(inputs, vin, accountId) { const { targetTemperature, startDateTime } = inputs; const data = { type: "HvacStart", attributes: { action: "start", targetTemperature } }; if (startDateTime) data.attributes.startDateTime = formatDate(startDateTime, renaultApi.PERIOD_TZ_FORMAT, this.session.locale); return this.perform({ apiUrl: "PERFORM_HVAC_START_URL", method: "performHvacStart", accountId, vin, data }); } /** * Stop vehicle hvac. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ performHvacStop(vin, accountId) { return this.perform({ apiUrl: "PERFORM_HVAC_START_URL", method: "performHvacStop", accountId, vin, data: { type: "HvacStart", attributes: { action: "cancel" } } }); } /** * Start vehicle charging. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ performChargeStart(vin, accountId) { return this.perform({ apiUrl: "PERFORM_CHARGING_START_URL", method: "performChargeStart", accountId, vin, data: { type: "ChargingStart", attributes: { action: "start" } } }); } /** * Stop vehicle charging. * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ performChargeStop(vin, accountId) { return this.perform({ apiUrl: "PERFORM_CHARGING_START_URL", method: "performChargeStop", accountId, vin, data: { type: "ChargingStart", attributes: { action: "stop" } } }); } /** * Start vehicle charging (Dacia Spring ONLY). * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ performChargeResume(vin, accountId) { return this.perform({ apiUrl: "PERFORM_PAUSE_RESUME_URL", method: "performChargeResume", accountId, vin, data: { type: "ChargingStart", attributes: { action: "start" } } }); } /** * Stop vehicle charging (Dacia Spring ONLY). * @param {string?} [vin = the vin stored in the session] - The vehicle vin. * @param {string?} [accountId = the accountId stored in the session] - The account id. */ performChargePause(vin, accountId) { return this.perform({ apiUrl: "PERFORM_PAUSE_RESUME_URL", method: "performChargePause", accountId, vin, data: { type: "ChargingStart", attributes: { action: "stop" } } }); } /** @internal */ read(args) { const { apiUrl, method, accountId, vin, queryParams } = args; const requiredAccountId = this.getAccountIdOrThrow(accountId, method); const requiredVin = this.getVinOrThrow(vin, method); return this.httpClient.get(renaultApi.KamereonApi[apiUrl](requiredAccountId, requiredVin), { queryParams }).consume(); } /** @internal */ perform(args) { const { apiUrl, method, accountId, vin, data } = args; const requiredAccountId = this.getAccountIdOrThrow(accountId, method); const requiredVin = this.getVinOrThrow(vin, method); return this.httpClient.post(renaultApi.KamereonApi[apiUrl](requiredAccountId, requiredVin), { data }).consume(); } /** @internal */ getPersonIdOrThrow(personId, method) { return personId || this.getFromSessionOrThrow("personId", method); } /** @internal */ getAccountIdOrThrow(accountId, method) { return accountId || this.getFromSessionOrThrow("accountId", method); } /** @internal */ getVinOrThrow(vin, method) { return vin || this.getFromSessionOrThrow("vin", method); } /** @internal */ getFromSessionOrThrow(key, method) { const value = this.session[key]; if (!value) thror.emitError("KamereonException", `Cannot ${method} because "${key}" is falsy or not stored in session.`); return value; } } var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); class RenaultClient { constructor(init = {}) { /** * The user session. */ __publicField(this, "session"); /** * The Gigya http client. */ __publicField(this, "gigya"); /** * The Kamereon http client. */ __publicField(this, "kamereon"); const { session = new RenaultSession(), onError } = init; this.session = session; this.gigya = new GigyaClient({ session: this.session, onError }); this.kamereon = new KamereonClient({ session: this.session, onError }); } } exports.GigyaClient = GigyaClient; exports.KamereonClient = KamereonClient; exports.RenaultClient = RenaultClient; exports.RenaultSession = RenaultSession; //# sourceMappingURL=index.cjs.map