UNPKG

devopness-sdk-js

Version:

Devopness API JS/TS SDK - Painless essential DevOps to everyone

128 lines (127 loc) 4.83 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiBaseService = exports.Configuration = void 0; const axios_1 = __importDefault(require("axios")); const Exceptions_1 = require("../common/Exceptions"); class Configuration { constructor(options) { this.baseUrl = "https://api.devopness.com"; this.apiKey = options.apiKey; this.baseUrl = options.baseUrl || this.baseUrl; } } exports.Configuration = Configuration; class ApiBaseService { constructor() { // public onTokenExpired: () => void; // private static SDK_VERSION = '0.1'; this.defaultAxiosSettings = { timeout: 30000, responseType: 'json', headers: { common: { "Content-Type": "application/json", Accept: "application/json", }, }, withCredentials: false }; if (ApiBaseService.configuration == undefined) { throw new Exceptions_1.ArgumentNullException('configuration'); } const settings = this.defaultAxiosSettings; settings.baseURL = ApiBaseService.configuration.baseUrl; this.api = axios_1.default.create(settings); this.setupAxiosInterceptors(); } setupAxiosInterceptors() { this.setupAxiosRequestInterceptors(); this.setupAxiosResponseInterceptors(); } setupAxiosRequestInterceptors() { this.api.interceptors.request.use((config) => { if (ApiBaseService._accessToken) { config.headers.Authorization = `Bearer ${ApiBaseService._accessToken}`; } else { delete config.headers.Authorization; } return config; }, (error) => { throw error; }); } setupAxiosResponseInterceptors() { this.api.interceptors.response.use((response) => { return response; }, (error) => { throw new Exceptions_1.ApiError(error); }); } static get accessToken() { return ApiBaseService._accessToken; } static set accessToken(value) { ApiBaseService._accessToken = value; } // TO DO: define events to notify the external world that a token has expired // so the consumer app can invoke refresh-token // so a web app might decide to redirect the user to login page or set // a or should we add an event parameter allowing `refresh: false` to be set to // true and we trigger refresh? // public tokenExpired(): void { // if (this.onTokenExpired) { // this.onTokenExpired(); // } // } post(endpoint, data) { return __awaiter(this, void 0, void 0, function* () { return this.api.post(endpoint, data); // try { // const response = await this.api.post<T, R>(endpoint, data); // const result = response.data; // return result; // } catch (exception) { // if (exception && exception.response) { // const axiosError = exception as AxiosError<any>; // return axiosError.response.data; // } // throw err; // } // }; }); } put(endpoint, data) { return __awaiter(this, void 0, void 0, function* () { return this.api.post(endpoint, data); }); } delete(endpoint) { /** * @todo: why not return `response.data` from here instead of AxiosResponse<T>? */ return this.api.get(this.api.defaults.baseURL + endpoint); } get(endpoint) { return this.api.get(this.api.defaults.baseURL + endpoint); } success(response) { return response.data; } error(error) { throw error; } } exports.ApiBaseService = ApiBaseService;