UNPKG

@cpanel/api

Version:

cPanel API JavaScript and TypeScript interface libraries. This library provides a set of classes for calling cPanel WHM API 1 and UAPI calls. The classes hide much of the complexity of these APIs behind classes the abstract the underlying variances betwee

160 lines 5.82 kB
"use strict"; // MIT License // // Copyright 2021 cPanel L.L.C. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. Object.defineProperty(exports, "__esModule", { value: true }); exports.WhmApiTokenHeader = exports.WhmApiTokenMismatchError = exports.WhmApiTokenInvalidError = exports.CpanelApiTokenHeader = exports.CpanelApiTokenMismatchError = exports.CpanelApiTokenInvalidError = exports.CustomHeader = exports.Headers = void 0; /** * HTTP Headers Collection Abstraction * * The abstraction is an adapter to allow easy transformation of the headers array * into various formats for external HTTP libraries. */ class Headers { /** * Create the adapter. * * @param headers - List of headers. */ constructor(headers = []) { this.headers = headers; } /** * Push a header into the collection. * * @param header - A header to add to the collection */ push(header) { this.headers.push(header); } /** * Iterator for the headers collection. * * @param fn - Transform for the forEach * @param thisArg - Optional reference to `this` to apply to the transform function. */ forEach(fn, thisArg) { this.headers.forEach(fn, thisArg); } /** * Retrieve the headers as an array of Headers */ toArray() { const copy = []; this.headers.forEach((h) => copy.push({ name: h.name, value: h.value })); return copy; } /** * Retrieve the headers as an object */ toObject() { return this.headers.reduce((o, header) => { o[header.name] = header.value; return o; }, {}); } } exports.Headers = Headers; class CustomHeader { constructor(_header) { this._header = _header; } get name() { return this._header.name; } get value() { return this._header.value; } } exports.CustomHeader = CustomHeader; class CpanelApiTokenInvalidError extends Error { constructor(m) { super(m); this.name = "CpanelApiTokenInvalidError"; // Set the prototype explicitly. This fixes unit tests. Object.setPrototypeOf(this, CpanelApiTokenInvalidError.prototype); } } exports.CpanelApiTokenInvalidError = CpanelApiTokenInvalidError; class CpanelApiTokenMismatchError extends Error { constructor(m) { super(m); this.name = "CpanelApiTokenMismatchError"; // Set the prototype explicitly. This fixes unit tests. Object.setPrototypeOf(this, CpanelApiTokenMismatchError.prototype); } } exports.CpanelApiTokenMismatchError = CpanelApiTokenMismatchError; class CpanelApiTokenHeader extends CustomHeader { constructor(token, user) { if (!token) { throw new CpanelApiTokenInvalidError("You must pass a valid token to the constructor."); } if (!user && !/^.+[:]/.test(token)) { throw new CpanelApiTokenInvalidError("You must pass a cPanel username associated with the cPanel API token."); } if (!user && !/[:].+$/.test(token)) { throw new CpanelApiTokenInvalidError("You must pass a valid cPanel API token."); } super({ name: "Authorization", value: `cpanel ${user ? user + ":" : ""}${token}`, }); } } exports.CpanelApiTokenHeader = CpanelApiTokenHeader; class WhmApiTokenInvalidError extends Error { constructor(m) { super(m); this.name = "WhmApiTokenInvalidError"; // Set the prototype explicitly. This fixes unit tests. Object.setPrototypeOf(this, WhmApiTokenInvalidError.prototype); } } exports.WhmApiTokenInvalidError = WhmApiTokenInvalidError; class WhmApiTokenMismatchError extends Error { constructor(m) { super(m); this.name = "WhmApiTokenMismatchError"; // Set the prototype explicitly. This fixes unit tests. Object.setPrototypeOf(this, WhmApiTokenMismatchError.prototype); } } exports.WhmApiTokenMismatchError = WhmApiTokenMismatchError; class WhmApiTokenHeader extends CustomHeader { constructor(token, user) { if (!token) { throw new WhmApiTokenInvalidError("You must pass a valid token to the constructor."); } if (!user && !/^.+:/.test(token)) { throw new WhmApiTokenInvalidError("You must pass a WHM username associated with the WHM API token."); } if (!user && !/:.+$/.test(token)) { throw new WhmApiTokenInvalidError("You must pass a valid WHM API token."); } super({ name: "Authorization", value: `whm ${user ? user + ":" : ""}${token}`, }); } } exports.WhmApiTokenHeader = WhmApiTokenHeader; //# sourceMappingURL=headers.js.map