UNPKG

devopness-sdk-js

Version:

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

48 lines (47 loc) 2.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DevopnessApiClient = void 0; const ApiBaseService_1 = require("./services/ApiBaseService"); const ProjectService_1 = require("./services/ProjectService"); const UserService_1 = require("./services/UserService"); class DevopnessApiClient { /** * @todo provide a global onError event? How to make it easy to clients to interact with * this SDK without being dependant themselves on AxiosResponse types? * We should be the only ones concerned about axios and moving from * Axios to another HTTP library should not affect the consumers * of this SDK */ constructor(options) { if (options === undefined) { throw new Error('API services cannot be initialized: Configuration options must be provided'); } if (options.baseUrl == undefined || options.baseUrl == '') { throw new Error('API services cannot be initialized: base URL must be provided'); } ApiBaseService_1.ApiBaseService.configuration = new ApiBaseService_1.Configuration(options); // we'd better initialize the services explicitly, instead of auto initialize them on property // declaration in the beginning of the class, cause some (or all) of them might need constructor // parameters. Furthermore, we ensure all assertions for required parameters (like the // above check for `baseUrl`) are quickly returned to the end user before spending // time loading extra resources this.projects = new ProjectService_1.ProjectService(); this.users = new UserService_1.UserService(); } get accessToken() { return ApiBaseService_1.ApiBaseService.accessToken; } set accessToken(accessToken) { const MIN_TOKEN_LENGHT = 100; /** * As a complete access token validation rule might be too much to be implemented here, we * at least check for min length and return a substring of it to help users identify the * issue when first initializing this SDK from their apps */ if (accessToken && accessToken.length < MIN_TOKEN_LENGHT) { throw new Error(`"${accessToken.substring(0, 10)} ..." doesn't seem to be a valid access token issued by Devopness API.`); } ApiBaseService_1.ApiBaseService.accessToken = accessToken; } } exports.DevopnessApiClient = DevopnessApiClient;