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

221 lines 6.77 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.Request = void 0; const argument_1 = require("./utils/argument"); const filter_1 = require("./utils/filter"); const pager_1 = require("./utils/pager"); const sort_1 = require("./utils/sort"); const headers_1 = require("./utils/headers"); /** * Abstract base class for all Request objects. Developers should * create a subclass of this that implements the generate() method. */ class Request { /** * Use the pager only if true. */ get usePager() { return this._usePager; } /** * Create a new request. * * @param init Optional request object used to initialize this object. */ constructor(init) { /** * Namespace where the API call lives * @type {string} */ this.namespace = ""; /** * Method name of the API call. * @type {string} */ this.method = ""; /** * Optional list of arguments for the API call. * @type {IArgument[]} */ this.arguments = []; /** * Optional list of sorting rules to pass to the API call. */ this.sorts = []; /** * Optional list of filter rules to pass to the API call. */ this.filters = []; /** * Optional list of columns to include with the response to the API call. */ this.columns = []; /** * Optional pager rule to pass to the API. */ this.pager = new pager_1.Pager(); /** * Optional custom headers collection */ this.headers = new headers_1.Headers(); this._usePager = false; /** * Default configuration object. */ this.defaultConfig = { analytics: false, json: false, }; /** * Optional configuration information */ this.config = this.defaultConfig; if (init) { this.method = init.method; if (init.namespace) { this.namespace = init.namespace; } if (init.arguments) { init.arguments.forEach((argument) => { this.addArgument(argument); }); } if (init.sorts) { init.sorts.forEach((sort) => { this.addSort(sort); }); } if (init.filters) { init.filters.forEach((filter) => { this.addFilter(filter); }); } if (init.columns) { init.columns.forEach((column) => this.addColumn(column)); } if (init.pager) { this.paginate(init.pager); } if (init.config) { this.config = init.config; } else { this.config = this.defaultConfig; } if (init.headers) { init.headers.forEach((header) => { this.addHeader(header); }); } } } /** * Add an argument to the request. * * @param argument * @return Updated Request object. */ addArgument(argument) { if (argument instanceof argument_1.Argument) { this.arguments.push(argument); } else { this.arguments.push(new argument_1.Argument(argument.name, argument.value)); } return this; } /** * Add sorting rule to the request. * * @param sort Sort object with sorting information. * @return Updated Request object. */ addSort(sort) { if (sort instanceof sort_1.Sort) { this.sorts.push(sort); } else { this.sorts.push(new sort_1.Sort(sort.column, sort.direction, sort.type)); } return this; } /** * Add a filter to the request. * * @param filter Filter object with filter information. * @return Updated Request object. */ addFilter(filter) { if (filter instanceof filter_1.Filter) { this.filters.push(filter); } else { this.filters.push(new filter_1.Filter(filter.column, filter.operator, filter.value)); } return this; } /** * Add a column to include in the request. If no columns are specified, all columns are retrieved. * * @param name Name of a column * @return Updated Request object. */ addColumn(column) { this.columns.push(column); return this; } /** * Add a custom http header to the request * * @param name Name of a column * @return Updated Request object. */ addHeader(header) { if (header instanceof headers_1.CustomHeader) { this.headers.push(header); } else { this.headers.push(new headers_1.CustomHeader(header)); } return this; } /** * Set the pager setting for the request. * * @param pager Pager object with pagination information. * @return Updated Request object. */ paginate(pager) { if (pager instanceof pager_1.Pager) { this.pager = pager; } else { this.pager = new pager_1.Pager(pager.page, pager.pageSize || 20); } this._usePager = true; return this; } } exports.Request = Request; //# sourceMappingURL=request.js.map