UNPKG

@qaflag/core

Version:

Base requirements for the QA Flag library

137 lines 5.38 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _HttpRequest_method, _HttpRequest_path, _HttpRequest_persona; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpRequest = void 0; const uri_1 = require("../utils/uri"); const cookies_1 = require("../utils/cookies"); const persona_1 = require("../persona/persona"); class HttpRequest { constructor(opts, persona) { this.opts = opts; _HttpRequest_method.set(this, 'get'); _HttpRequest_path.set(this, '/'); _HttpRequest_persona.set(this, void 0); this.uri = opts.uri || 'GET /'; __classPrivateFieldSet(this, _HttpRequest_persona, persona, "f"); } get responseType() { return this.opts.responseType || 'json'; } set responseType(type) { this.opts.responseType = type; } get url() { return new URL(this.path, this.baseUrl); } get uri() { return `${__classPrivateFieldGet(this, _HttpRequest_method, "f")} ${__classPrivateFieldGet(this, _HttpRequest_path, "f")}`; } set uri(value) { const uri = (0, uri_1.parseUri)(value); __classPrivateFieldSet(this, _HttpRequest_method, uri.method, "f"); __classPrivateFieldSet(this, _HttpRequest_path, uri.path, "f"); } get method() { return __classPrivateFieldGet(this, _HttpRequest_method, "f"); } set method(value) { __classPrivateFieldSet(this, _HttpRequest_method, value, "f"); } get path() { if (!this.opts.pathArgs) return __classPrivateFieldGet(this, _HttpRequest_path, "f"); let path = __classPrivateFieldGet(this, _HttpRequest_path, "f"); this.opts.pathArgs.forEach(([key, value]) => { path = path.replace(`{${key}}`, String(value)); }); return path; } set path(value) { __classPrivateFieldSet(this, _HttpRequest_path, value, "f"); } get isPathFullUrl() { return /^[a-z]+:\/\//i.test(__classPrivateFieldGet(this, _HttpRequest_path, "f")); } get baseUrl() { if (this.isPathFullUrl) return undefined; return this.opts.baseUrl; } get timeout() { return this.opts.timeout || 10000; } set persona(persona) { __classPrivateFieldSet(this, _HttpRequest_persona, persona, "f"); } get persona() { return __classPrivateFieldGet(this, _HttpRequest_persona, "f") || new persona_1.DefaultUser(); } get pathArgs() { return this.opts.pathArgs || []; } set pathArgs(value) { this.opts.pathArgs = value; } get proxy() { var _a; return this.opts.proxy || ((_a = this.persona) === null || _a === void 0 ? void 0 : _a.proxy); } get userAgent() { return (this.opts.userAgent || (this.opts.headers && this.opts.headers['user-agent']) || 'QA Flag'); } get cookies() { var _a; return [ ...(0, cookies_1.getCookieArray)((_a = this.persona) === null || _a === void 0 ? void 0 : _a.cookies), ...(0, cookies_1.getCookieArray)(this.opts.cookies), ]; } get headers() { var _a; const headers = [ ...(((_a = this.persona) === null || _a === void 0 ? void 0 : _a.headers) || []), ...(this.opts.headers || []), { key: 'user-agent', value: this.userAgent }, ]; return headers; } set headers(headers) { this.opts.headers = headers; } get bearerToken() { var _a; return this.opts.bearerToken || ((_a = this.persona) === null || _a === void 0 ? void 0 : _a.bearerToken); } get auth() { var _a; return this.opts.auth || ((_a = this.persona) === null || _a === void 0 ? void 0 : _a.basicAuthentication); } get maxRedirects() { return this.opts.maxRedirects || 5; } get data() { return this.opts.data || null; } get responseEncoding() { return this.opts.responseEncoding || 'utf8'; } get queryString() { return this.opts.queryString || {}; } } exports.HttpRequest = HttpRequest; _HttpRequest_method = new WeakMap(), _HttpRequest_path = new WeakMap(), _HttpRequest_persona = new WeakMap(); //# sourceMappingURL=http-request.js.map