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

217 lines 6.54 kB
// 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. import { Argument } from "./utils/argument"; import { Filter } from "./utils/filter"; import { Pager } from "./utils/pager"; import { Sort } from "./utils/sort"; import { Headers, CustomHeader } from "./utils/headers"; /** * Abstract base class for all Request objects. Developers should * create a subclass of this that implements the generate() method. */ export 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(); /** * Optional custom headers collection */ this.headers = new 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) { this.arguments.push(argument); } else { this.arguments.push(new 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) { this.sorts.push(sort); } else { this.sorts.push(new 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) { this.filters.push(filter); } else { this.filters.push(new 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 CustomHeader) { this.headers.push(header); } else { this.headers.push(new 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) { this.pager = pager; } else { this.pager = new Pager(pager.page, pager.pageSize || 20); } this._usePager = true; return this; } } //# sourceMappingURL=request.js.map