@emartech/escher-request
Version: 
Requests with Escher authentication
103 lines (102 loc) • 3.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EscherRequestOption = void 0;
const MEGA_BYTE = 1024 * 1024;
class EscherRequestOption {
    static createForInternalApi(host, rejectUnauthorized) {
        return this.create(host, '/api/v2/internal', rejectUnauthorized);
    }
    static createForServiceApi(host, rejectUnauthorized) {
        return this.create(host, '/api/services', rejectUnauthorized);
    }
    static create(host, prefix = '', rejectUnauthorized = true) {
        let options = {};
        if (typeof host === 'object') {
            options = host;
            host = options.host || '';
        }
        else {
            options.rejectUnauthorized = rejectUnauthorized;
        }
        options.prefix = prefix;
        return new EscherRequestOption(host, options);
    }
    constructor(host, options) {
        this.secure = true;
        this.port = 443;
        this.host = '';
        this.rejectUnauthorized = true;
        this.headers = [];
        this.prefix = '';
        this.timeout = 15000;
        this.allowEmptyResponse = false;
        this.maxContentLength = 10 * MEGA_BYTE;
        this.keepAlive = false;
        this.credentialScope = '';
        this.retryConfig = null;
        this.secure = options.secure !== false;
        this.port = options.port || 443;
        this.host = host;
        this.rejectUnauthorized = options.rejectUnauthorized !== false;
        this.prefix = '';
        this.timeout = options.timeout || 15000;
        this.allowEmptyResponse = false;
        this.maxContentLength = options.maxContentLength || 10 * MEGA_BYTE;
        this.keepAlive = !!options.keepAlive;
        this.retryConfig = options.retryConfig || null;
        Object.assign(this, options || {});
        this.setHeader(['content-type', 'application/json']);
        this.setHeader(['host', host]);
    }
    setToSecure(port, rejectUnauthorized) {
        this.port = port || 443;
        this.secure = true;
        this.rejectUnauthorized = rejectUnauthorized;
    }
    setToUnsecure(port) {
        this.port = port || 80;
        this.secure = false;
    }
    setHost(host) {
        this.host = host;
    }
    setPort(port) {
        this.port = port;
    }
    setHeader(headerToSet) {
        this.headers = this.headers
            .filter(existingHeader => existingHeader[0] !== headerToSet[0])
            .concat([headerToSet]);
    }
    getHeader(name) {
        const result = this.headers.find((header) => {
            return header[0].toLowerCase() === name.toLowerCase();
        });
        return result ? result[1] : null;
    }
    setTimeout(timeout) {
        this.timeout = timeout;
    }
    getTimeout() {
        return this.timeout;
    }
    toHash() {
        const hash = {
            port: this.port,
            host: this.host,
            headers: this.headers.slice(0),
            prefix: this.prefix,
            timeout: this.timeout,
            maxContentLength: this.maxContentLength,
            retryConfig: this.retryConfig
        };
        if (!this.rejectUnauthorized) {
            hash.rejectUnauthorized = false;
        }
        if (this.allowEmptyResponse) {
            hash.allowEmptyResponse = true;
        }
        return hash;
    }
}
exports.EscherRequestOption = EscherRequestOption;