@barchart/common-js
Version:
Library of common JavaScript utilities
284 lines (278 loc) • 8.43 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Endpoint_exports = {};
__export(Endpoint_exports, {
default: () => Endpoint
});
module.exports = __toCommonJS(Endpoint_exports);
var is = __toESM(require("./../../../lang/is.js"));
var import_Parameters = __toESM(require("./Parameters.js"));
var import_ProtocolType = __toESM(require("./ProtocolType.js"));
var import_VerbType = __toESM(require("./VerbType.js"));
var import_ErrorInterceptor = __toESM(require("./../interceptors/ErrorInterceptor.js"));
var import_RequestInterceptor = __toESM(require("./../interceptors/RequestInterceptor.js"));
var import_ResponseInterceptor = __toESM(require("./../interceptors/ResponseInterceptor.js"));
class Endpoint {
#name;
#description;
#verb;
#protocol;
#host;
#port;
#path;
#query;
#headers;
#body;
#credentials;
#requestInterceptor;
#responseInterceptor;
#errorInterceptor;
/**
* @param {string=} name
* @param {string=} description
* @param {VerbType=} verb
* @param {ProtocolType=} protocol
* @param {string=} host
* @param {number=} port
* @param {Parameters=} path
* @param {Parameters=} query
* @param {Parameters=} headers
* @param {Parameters=} body
* @param {Credentials=} credentials
* @param {RequestInterceptor=} requestInterceptor
* @param {ResponseInterceptor=} responseInterceptor
* @param {ErrorInterceptor=} errorInterceptor
*/
constructor(name, description, verb, protocol, host, port, path, query, headers, body, credentials, requestInterceptor, responseInterceptor, errorInterceptor) {
this.#name = name || null;
this.#description = description || null;
this.#verb = verb || import_VerbType.default.GET;
this.#protocol = protocol || import_ProtocolType.default.HTTPS;
this.#host = host || null;
this.#port = port || this.#protocol.defaultPort;
this.#path = path || new import_Parameters.default();
this.#query = query || new import_Parameters.default();
this.#headers = headers || new import_Parameters.default();
this.#body = body || new import_Parameters.default();
this.#credentials = credentials || null;
this.#requestInterceptor = requestInterceptor || import_RequestInterceptor.default.EMPTY;
this.#responseInterceptor = responseInterceptor || import_ResponseInterceptor.default.EMPTY;
this.#errorInterceptor = errorInterceptor || import_ErrorInterceptor.default.EMPTY;
}
/**
* The name of the endpoint (used for internal purposes only).
*
* @public
* @returns {string}
*/
get name() {
return this.#name;
}
/**
* A description of the action performed by the endpoint, suitable for display to users.
*
* @public
* @returns {string}
*/
get description() {
return this.#description;
}
/**
* The verb to use when making the request.
*
* @public
* @returns {VerbType}
*/
get verb() {
return this.#verb;
}
/**
* The protocol to use with the endpoint.
*
* @public
* @returns {ProtocolType}
*/
get protocol() {
return this.#protocol;
}
/**
* The host of the endpoint.
*
* @public
* @returns {string}
*/
get host() {
return this.#host;
}
/**
* The host of the endpoint.
*
* @public
* @returns {number}
*/
get port() {
return this.#port;
}
/**
* The path definition of the endpoint.
*
* @public
* @returns {Parameters}
*/
get path() {
return this.#path;
}
/**
* The query definition of the endpoint.
*
* @public
* @returns {Parameters}
*/
get query() {
return this.#query;
}
/**
* The header definition of the endpoint.
*
* @public
* @returns {Parameters}
*/
get headers() {
return this.#headers;
}
/**
* The body definition of the endpoint.
*
* @public
* @returns {Parameters}
*/
get body() {
return this.#body;
}
/**
* Credentials for the request.
*
* @public
* @return {Credentials}
*/
get credentials() {
return this.#credentials;
}
/**
* The request interceptor of the endpoint.
*
* @public
* @returns {RequestInterceptor|null}
*/
get requestInterceptor() {
return this.#requestInterceptor;
}
/**
* The response interceptor of the endpoint.
*
* @public
* @returns {ResponseInterceptor|null}
*/
get responseInterceptor() {
return this.#responseInterceptor;
}
/**
* The error interceptor of the endpoint.
*
* @public
* @returns {ErrorInterceptor|null}
*/
get errorInterceptor() {
return this.#errorInterceptor;
}
/**
* Throws an {@link Error} if the instance is invalid.
*
* @public
*/
validate() {
if (!(this.protocol instanceof import_ProtocolType.default)) {
throw new Error("Endpoint protocol must be an instance of ProtocolType.");
}
if (!is.string(this.#host) || this.#host.length === 0) {
throw new Error("Endpoint host is invalid.");
}
if (!is.integer(this.#port) || this.#port < 0 || this.#port > 65535) {
throw new Error("Endpoint port range is invalid.");
}
if (!(this.path instanceof import_Parameters.default)) {
throw new Error("The path must be a Parameters collection.");
}
this.path.validate();
if (!(this.query instanceof import_Parameters.default)) {
throw new Error("The query must be a Parameters collection.");
}
this.query.validate();
if (!(this.headers instanceof import_Parameters.default)) {
throw new Error("The headers must be a Parameters collection.");
}
this.headers.validate();
if (!(this.body instanceof import_Parameters.default)) {
throw new Error("The body must be a Parameters collection.");
}
this.body.validate();
if (this.credentials) {
this.credentials.validate();
}
if (this.requestInterceptor && !(this.requestInterceptor instanceof import_RequestInterceptor.default)) {
throw new Error("Endpoint request interceptor must be an instance of RequestInterceptor.");
}
if (this.responseInterceptor && !(this.responseInterceptor instanceof import_ResponseInterceptor.default)) {
throw new Error("Endpoint response interceptor must be an instance of ResponseInterceptor.");
}
if (this.errorInterceptor && !(this.errorInterceptor instanceof import_ErrorInterceptor.default)) {
throw new Error("Endpoint error interceptor must be an instance of ErrorInterceptor.");
}
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return `[Endpoint (name=${this.#name})]`;
}
}
{
const cjsExports = module.exports;
const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports;
if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) {
Object.keys(cjsExports).forEach((key) => {
if (key !== 'default' && key !== '__esModule') {
cjsDefaultExport[key] = cjsExports[key];
}
});
}
module.exports = cjsDefaultExport;
}