UNPKG

domrobot-client

Version:
147 lines 6.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Language = exports.ApiClient = void 0; const otplib = require("otplib"); class ApiClient { static generateClientTransactionId() { return 'DomRobot-' + Math.round(Math.random() * 1000000000); } /** * @param apiUrl url of the API. * @param language default language for future API requests. * @param debugMode whether requests and responses should be printed out. * @param headers Optional HTTP headers to be included in all API requests. */ constructor(apiUrl = ApiClient.API_URL_OTE, language = Language.EN, debugMode = false, headers = {}) { this.apiUrl = apiUrl; this.language = language; this.debugMode = debugMode; this.cookie = null; this.headers = headers; } /** * Makes an API call. * * @param apiMethod The name of the method called in the API. * @param methodParams An object of parameters added to the request. * @param clientTransactionId Id sent with every request to distinguish your api requests in case you need support. * @param language Language for the API request. Default is value of field language. * @param cacheOption HTTP cache options * @param nextjsOptions Next.js options */ callApi(apiMethod_1) { return __awaiter(this, arguments, void 0, function* (apiMethod, methodParams = {}, clientTransactionId = ApiClient.generateClientTransactionId(), language = this.language, cacheOption, nextjsOptions) { var _a; if ('clTRID' in methodParams && clientTransactionId !== null) { methodParams.clTRID = clientTransactionId; } if ('lang' in methodParams) { methodParams.lang = language; } const requestBody = JSON.stringify({ method: apiMethod, params: methodParams, }); let fetchOption = { method: 'POST', headers: new Headers(Object.assign({ 'Content-Type': 'application/json', Cookie: this.cookie, 'User-Agent': `DomRobot/${ApiClient.CLIENT_VERSION} (Node ${process.version})` }, ((_a = this.headers) !== null && _a !== void 0 ? _a : {}))), body: requestBody, cache: cacheOption !== null && cacheOption !== void 0 ? cacheOption : 'default', }; if (nextjsOptions !== undefined) { fetchOption = Object.assign(Object.assign({}, fetchOption), nextjsOptions); } const response = yield fetch(this.apiUrl, fetchOption); if (apiMethod === 'account.login') { this.cookie = response.headers.get('set-cookie'); } const data = yield response.json(); if (this.debugMode) { console.info(`Request (${apiMethod}): ${requestBody}`); console.info(`Response (${apiMethod}): ${JSON.stringify(data)}`); } return data; }); } /** * Performs a login at the API and saves the session for following API calls. * * @param username your username. * @param password your password. * @param sharedSecret */ login(username_1, password_1) { return __awaiter(this, arguments, void 0, function* (username, password, sharedSecret = null) { const loginResult = yield this.callApi('account.login', { user: username, pass: password }); if (loginResult.code === 1000 && 'tfa' in loginResult.resData && loginResult.resData.tfa !== '0') { if (sharedSecret === null) { return Promise.reject('API requests two factor challenge but no shared secret is given. Aborting.'); } const secretCode = otplib.authenticator.generate(sharedSecret); const unlockResult = yield this.callApi('account.unlock', { tan: secretCode }); if (unlockResult.code !== 1000) { return unlockResult; } } return loginResult; }); } /** * Performs a logout at the API and destroys the current session. */ logout() { return __awaiter(this, void 0, void 0, function* () { return yield this.callApi('account.logout').then(() => (this.cookie = null)); }); } getApiUrl() { return this.apiUrl; } getLanguage() { return this.language; } setLanguage(value) { this.language = value; } isDebugMode() { return this.debugMode; } setDebugMode(value) { this.debugMode = value; } getCookie() { return this.cookie; } setCookie(cookie) { this.cookie = cookie; } setHeaders(headers) { this.headers = headers; } getHeaders() { return this.headers; } } exports.ApiClient = ApiClient; ApiClient.CLIENT_VERSION = '3.0.2'; ApiClient.API_URL_LIVE = 'https://api.domrobot.com/jsonrpc/'; ApiClient.API_URL_OTE = 'https://api.ote.domrobot.com/jsonrpc/'; /** * The API response-message language. */ var Language; (function (Language) { Language["EN"] = "en"; Language["DE"] = "de"; Language["ES"] = "es"; })(Language || (exports.Language = Language = {})); //# sourceMappingURL=domrobot.js.map