UNPKG

@iotize/cli

Version:
69 lines 3.09 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()); }); }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios = __importStar(require("axios")); const ui_1 = require("../../ui"); function createHttpClient(url) { const client = axios.default.create({ baseURL: url, timeout: 10000 }); client.interceptors.request.use(config => { const cliConfig = global.configProvider.config(); const accessToken = cliConfig.get('cloud.session.accessToken', null); if (accessToken) { config.headers["Authorization"] = "Bearer " + accessToken; } return config; }, (error) => __awaiter(this, void 0, void 0, function* () { throw error; })); client.interceptors.response.use(response => { return response; }, error => { var _a, _b, _c, _d; if (error.response.status === 401) { if (error.request.path.indexOf('/oauth/') < 0) { ui_1.display.warn(`Use command "cloud login" to login to IoTize Cloud before performing this command.`); } } if ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) { throw new Error(`HTTP error ${error.response.status}: ${(_d = (_c = error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message}.`); } return error; }); return client; } exports.createHttpClient = createHttpClient; class CloudAuthApi { constructor() { this.authClient = createHttpClient('https://api.cloud.iotize.com'); // TODO config } login(username, password) { return __awaiter(this, void 0, void 0, function* () { const body = { grant_type: 'client_credentials', username, password }; return (yield this.authClient.post('oauth/token', body)).data; }); } } exports.CloudAuthApi = CloudAuthApi; //# sourceMappingURL=cloud-auth-api.js.map