UNPKG

@barchart/common-js

Version:
313 lines (307 loc) 15 kB
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 EndpointBuilder_exports = {}; __export(EndpointBuilder_exports, { default: () => EndpointBuilder }); module.exports = __toCommonJS(EndpointBuilder_exports); var assert = __toESM(require("./../../../lang/assert.js")); var import_CredentialsBuilder = __toESM(require("./CredentialsBuilder.js")); var import_ParametersBuilder = __toESM(require("./ParametersBuilder.js")); var import_Endpoint = __toESM(require("./../definitions/Endpoint.js")); var import_ProtocolType = __toESM(require("./../definitions/ProtocolType.js")); var import_VerbType = __toESM(require("./../definitions/VerbType.js")); var import_CompositeErrorInterceptor = __toESM(require("./../interceptors/CompositeErrorInterceptor.js")); var import_CompositeResponseInterceptor = __toESM(require("./../interceptors/CompositeResponseInterceptor.js")); var import_CompositeRequestInterceptor = __toESM(require("./../interceptors/CompositeRequestInterceptor.js")); var import_ErrorInterceptor = __toESM(require("./../interceptors/ErrorInterceptor.js")); var import_ResponseInterceptor = __toESM(require("./../interceptors/ResponseInterceptor.js")); var import_RequestInterceptor = __toESM(require("./../interceptors/RequestInterceptor.js")); class EndpointBuilder { #endpoint; /** * @param {string} name * @param {string=} description */ constructor(name, description) { assert.argumentIsRequired(name, "name", String); assert.argumentIsOptional(description, "description", String); this.#endpoint = new import_Endpoint.default(name, description); } /** * The {@link Endpoint}, given all the information provided thus far. * * @public * @returns {Endpoint} */ get endpoint() { return this.#endpoint; } /** * Sets the verb. * * @public * @param {VerbType} verb * @returns {EndpointBuilder} */ withVerb(verb) { assert.argumentIsRequired(verb, "verb", import_VerbType.default, "VerbType"); this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, verb, this.endpoint.protocol, this.endpoint.host, this.endpoint.port, this.endpoint.path, this.endpoint.query, this.endpoint.headers, this.endpoint.body, this.endpoint.credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Sets the host. * * @public * @param {ProtocolType} protocol * @returns {EndpointBuilder} */ withProtocol(protocol) { assert.argumentIsRequired(protocol, "protocol", import_ProtocolType.default, "ProtocolType"); this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, protocol, this.endpoint.host, this.endpoint.port, this.endpoint.path, this.endpoint.query, this.endpoint.headers, this.endpoint.body, this.endpoint.credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Sets the host. * * @public * @param {string} host * @returns {EndpointBuilder} */ withHost(host) { assert.argumentIsRequired(host, "host", String); this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, host, this.endpoint.port, this.endpoint.path, this.endpoint.query, this.endpoint.headers, this.endpoint.body, this.endpoint.credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Sets the port. * * @public * @param {number} port * @returns {EndpointBuilder} */ withPort(port) { assert.argumentIsRequired(port, "port", Number); this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, this.endpoint.host, port, this.endpoint.path, this.endpoint.query, this.endpoint.headers, this.endpoint.body, this.endpoint.credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Adds a {@link Parameters} collection, describing the request headers, using a callback. * * @public * @param {parametersBuilderCallback} callback * @returns {EndpointBuilder} */ withHeadersBuilder(callback) { assert.argumentIsRequired(callback, "callback", Function); const builder = new import_ParametersBuilder.default(); callback(builder); const headers = builder.parameters; this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, this.endpoint.host, this.endpoint.port, this.endpoint.path, this.endpoint.query, headers, this.endpoint.body, this.endpoint.credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Adds a {@link Parameters} collection, describing the request path, using a callback. * * @public * @param {parametersBuilderCallback} callback * @returns {EndpointBuilder} */ withPathBuilder(callback) { assert.argumentIsRequired(callback, "callback", Function); const builder = new import_ParametersBuilder.default(true); callback(builder); const path = builder.parameters; this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, this.endpoint.host, this.endpoint.port, path, this.endpoint.query, this.endpoint.headers, this.endpoint.body, this.endpoint.credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Adds a {@link Parameters} collection, describing the request querystring, using a callback. * * @public * @param {parametersBuilderCallback} callback * @returns {EndpointBuilder} */ withQueryBuilder(callback) { assert.argumentIsRequired(callback, "callback", Function); const builder = new import_ParametersBuilder.default(); callback(builder); const query = builder.parameters; this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, this.endpoint.host, this.endpoint.port, this.endpoint.path, query, this.endpoint.headers, this.endpoint.body, this.endpoint.credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Adds a {@link Parameters} collection, describing the request body, using a callback. * * @public * @param {parametersBuilderCallback} callback * @returns {EndpointBuilder} */ withBodyBuilder(callback) { assert.argumentIsRequired(callback, "callback", Function); const builder = new import_ParametersBuilder.default(); callback(builder); const body = builder.parameters; this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, this.endpoint.host, this.endpoint.port, this.endpoint.path, this.endpoint.query, this.endpoint.headers, body, this.endpoint.credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Adds a body to the request. * * @public * @param {string=} description - The human-readable description of the request body. * @returns {EndpointBuilder} */ withBody(description) { assert.argumentIsOptional(description, "description", String); return this.withBodyBuilder((bodyBuilder) => { bodyBuilder.withDelegateParameter(description || "request payload", "body", (x) => x); }); } /** * Adds basic authentication to the request. * * @public * @param {string} username * @param {string} password * @returns {EndpointBuilder} */ withBasicAuthentication(username, password) { assert.argumentIsRequired(username, "username", String); assert.argumentIsRequired(password, "password", String); return this.withBasicAuthenticationBuilder((credentialsBuilder) => { credentialsBuilder.withLiteralUsername(username); credentialsBuilder.withLiteralPassword(password); }); } /** * Adds basic authentication to the request, using a callback. * * @public * @param {Function} callback * @returns {EndpointBuilder} */ withBasicAuthenticationBuilder(callback) { assert.argumentIsRequired(callback, "callback", Function); const builder = new import_CredentialsBuilder.default(); callback(builder); const credentials = builder.credentials; this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, this.endpoint.host, this.endpoint.port, this.endpoint.path, this.endpoint.query, this.endpoint.headers, this.endpoint.body, credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Adds a {@link RequestInterceptor}. * * @public * @param {RequestInterceptor} requestInterceptor * @returns {EndpointBuilder} */ withRequestInterceptor(requestInterceptor) { assert.argumentIsRequired(requestInterceptor, "requestInterceptor", import_RequestInterceptor.default, "RequestInterceptor"); let existingRequestInterceptor = this.endpoint.requestInterceptor; let updatedRequestInterceptor; if (existingRequestInterceptor && existingRequestInterceptor !== import_RequestInterceptor.default.EMPTY) { updatedRequestInterceptor = new import_CompositeRequestInterceptor.default(existingRequestInterceptor, requestInterceptor); } else { updatedRequestInterceptor = requestInterceptor; } this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, this.endpoint.host, this.endpoint.port, this.endpoint.path, this.endpoint.query, this.endpoint.headers, this.endpoint.body, this.endpoint.credentials, updatedRequestInterceptor, this.endpoint.responseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Adds a {@link ResponseInterceptor} for successful web service responses. * * @public * @param {ResponseInterceptor} responseInterceptor * @returns {EndpointBuilder} */ withResponseInterceptor(responseInterceptor) { assert.argumentIsRequired(responseInterceptor, "responseInterceptor", import_ResponseInterceptor.default, "ResponseInterceptor"); let existingResponseInterceptor = this.endpoint.responseInterceptor; let updatedResponseInterceptor; if (existingResponseInterceptor && existingResponseInterceptor !== import_ResponseInterceptor.default.EMPTY) { updatedResponseInterceptor = new import_CompositeResponseInterceptor.default(existingResponseInterceptor, responseInterceptor); } else { updatedResponseInterceptor = responseInterceptor; } this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, this.endpoint.host, this.endpoint.port, this.endpoint.path, this.endpoint.query, this.endpoint.headers, this.endpoint.body, this.endpoint.credentials, this.endpoint.requestInterceptor, updatedResponseInterceptor, this.endpoint.errorInterceptor); return this; } /** * Adds a {@link ErrorInterceptor} for handling remote web service errors. * * @public * @param {ErrorInterceptor} errorInterceptor * @returns {EndpointBuilder} */ withErrorInterceptor(errorInterceptor) { assert.argumentIsRequired(errorInterceptor, "errorInterceptor", import_ErrorInterceptor.default, "ErrorInterceptor"); let existingErrorInterceptor = this.endpoint.errorInterceptor; let updatedErrorInterceptor; if (existingErrorInterceptor && existingErrorInterceptor !== import_ErrorInterceptor.default.EMPTY) { updatedErrorInterceptor = new import_CompositeErrorInterceptor.default(existingErrorInterceptor, errorInterceptor); } else { updatedErrorInterceptor = errorInterceptor; } this.#endpoint = new import_Endpoint.default(this.endpoint.name, this.endpoint.description, this.endpoint.verb, this.endpoint.protocol, this.endpoint.host, this.endpoint.port, this.endpoint.path, this.endpoint.query, this.endpoint.headers, this.endpoint.body, this.endpoint.credentials, this.endpoint.requestInterceptor, this.endpoint.responseInterceptor, updatedErrorInterceptor); return this; } /** * Factory function for creating an {@link EndpointBuilder} instance. * * @public * @static * @param {string} name * @param {string=} description * @returns {EndpointBuilder} */ static for(name, description) { return new EndpointBuilder(name, description); } /** * Returns a string representation. * * @public * @returns {string} */ toString() { return "[EndpointBuilder]"; } } { 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; }