UNPKG

flagpole

Version:

Simple and fast DOM integration, headless or headful browser, and REST API testing framework.

241 lines 6.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpRequest = void 0; const tunnel = require("tunnel"); const FormData = require("form-data"); const form_urlencoded_1 = require("form-urlencoded"); const needle_1 = require("./needle"); const constants_1 = require("../interfaces/constants"); class HttpRequest { constructor(opts) { this._uri = null; this._method = "get"; this._headers = {}; this._cookies = {}; this._verifyCert = false; this._timeout = { open: 10000 }; this._maxRedirects = 10; this._fetched = false; this._customOpts = {}; this.setOptions(opts); } get uri() { return this._uri; } set uri(value) { if (!this.isImmutable) { this._uri = value; } } get method() { return this._method; } set method(value) { if (!this.isImmutable) { this._method = value; } } get headers() { return this._headers; } set headers(value) { if (!this.isImmutable) { this._headers = value; } } get cookies() { return this._cookies; } set cookies(value) { if (!this.isImmutable) { this._cookies = value; } } get auth() { return this._auth; } set auth(value) { if (!this.isImmutable) { this._auth = value; } } get authType() { return this._authType || "auto"; } set authType(value) { if (!this.isImmutable) { this._authType = value; } } get maxRedirects() { return this._maxRedirects; } set maxRedirects(value) { if (!this.isImmutable) { this._maxRedirects = value; } } get timeout() { return this._timeout; } set timeout(value) { if (!this.isImmutable) { this._timeout = value; } } get proxy() { return this._proxy; } set proxy(value) { if (!this.isImmutable) { this._proxy = value; } } get verifyCert() { return this._verifyCert; } set verifyCert(value) { if (!this.isImmutable) { this._verifyCert = value; } } get data() { return this._data; } set data(value) { if (!this.isImmutable) { this._data = value; } } get customOpts() { return this._customOpts; } set customOpts(value) { if (!this.isImmutable) { this._customOpts = value; } } get proxyAgent() { if (this._proxy) { return tunnel.httpOverHttp({ proxy: { host: this._proxy.host, port: this._proxy.port, proxyAuth: `${this._proxy.auth.username}:${this._proxy.auth.password}`, }, }); } } get isImmutable() { return this._fetched; } get outputFile() { return this._outputFile; } set outputFile(value) { if (!this.isImmutable) { this._outputFile = value; } } get options() { return { uri: this._uri, method: this._method, headers: this._headers, cookies: this._cookies, verifyCert: this._verifyCert, proxy: this._proxy, maxRedirects: this._maxRedirects, timeout: this._timeout, auth: this._auth, authType: this._authType, outputFile: this._outputFile, data: this._data, }; } setOptions(opts) { if (!this.isImmutable) { this._uri = opts.uri || this._uri; this._method = opts.method || this._method; this._headers = opts.headers || this._headers; this._cookies = opts.cookies || this._cookies; this._verifyCert = typeof opts.verifyCert === "undefined" ? this._verifyCert : opts.verifyCert; this._proxy = opts.proxy; this._maxRedirects = typeof opts.maxRedirects === "undefined" ? this._maxRedirects : opts.maxRedirects; this._timeout = (() => { if (!opts.timeout) { return this._timeout; } if (typeof opts.timeout == "number") { return { open: opts.timeout, }; } return opts.timeout; })(); this._auth = opts.auth || this._auth; this._customOpts = opts.customOpts || this._customOpts; this._data = opts.data || this._data; this._outputFile = opts.outputFile || this._outputFile; } return this; } setCookie(key, value) { if (!this.isImmutable) { this._cookies[key] = value; } } getCookie(key) { return this._cookies[key]; } setHeader(key, value) { if (!this.isImmutable) { this._headers[key] = value; } } getHeader(key) { return this._headers[key]; } setJsonData(data) { this.setHeader("Content-Type", constants_1.CONTENT_TYPE_JSON); this.data = data; } setFormData(data, isMultipart) { if (data instanceof FormData && !isMultipart) { throw new Error("This format of form data must be multipart."); } this.setHeader("Content-Type", isMultipart ? constants_1.CONTENT_TYPE_FORM_MULTIPART : constants_1.CONTENT_TYPE_FORM); if (data instanceof FormData) { this.data = data; } else if (isMultipart) { const formData = new FormData(); Object.keys(data).forEach((key) => formData.append(key, data[key])); this.data = formData; } else { this.data = form_urlencoded_1.default(data); } return this; } fetch(opts = {}, fetchMethod) { if (this._fetched) { throw new Error("This request was already fetched."); } this._fetched = true; if (this._uri === null) { throw new Error("Invalid URI"); } if (fetchMethod) return fetchMethod(this, opts); return needle_1.fetchWithNeedle(this, opts); } } exports.HttpRequest = HttpRequest; //# sourceMappingURL=http-request.js.map