UNPKG

rest-chronicle

Version:
112 lines (109 loc) 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _utils = require("../utils"); var _chronicle = _interopRequireDefault(require("../chronicle")); var _constants = require("../constants"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } const supertest = _utils.PeerDependency.load('supertest'); function fillParams(url, params) { let filled = url; for (const [key, value] of Object.entries(params)) { filled = filled.replace(`:${key}`, value); } return filled; } class Supertest { constructor(app, chronicle = _chronicle.default) { _defineProperty(this, "with", params => { this._setContext(params); return this._decorate(this._supertest); }); _defineProperty(this, 'before_end', ({ params }) => { // eslint-disable-next-line unicorn/no-this-assignment const that = this; return [function (err, res) { params[0](...arguments); if (!err) { that._process(res); } }]; }); _defineProperty(this, "_proxy", ({ result }) => { if (!this._isContextSet && this._chronicle.clsEnabled) { const clsContext = this._chronicle.getCLSContext(); if (clsContext) this._setContext(clsContext); } return this._decorate(result); }); _defineProperty(this, 'after_get', this._proxy); _defineProperty(this, 'after_patch', this._proxy); _defineProperty(this, 'after_post', this._proxy); _defineProperty(this, 'after_delete', this._proxy); _defineProperty(this, 'after_expect', this._proxy); _utils.PeerDependency.check(supertest); this._chronicle = chronicle; this._app = app; this._supertest = supertest.agent(this._app); // eslint-disable-next-line no-constructor-return return this._decorate(this._supertest); } _decorate(target) { if (target[_constants.isDecorated]) return target; for (const key of ['_with']) { target[key] = this[key]; // eslint-disable-line no-param-reassign } return (0, _utils.decorate)(target, this); } _setContext(context) { this._with = context; this._isContextSet = true; } _process(response) { if (!this._isContextSet || !this._with) return; const action = this._chronicle.action(this._with); this._with = null; this._isContextSet = false; const { request, res } = response; action.request = { url: request.url, headers: request.header, method: request.method, body: request._data }; action.response = { body: response.body, headers: response.header, http: { version: res.httpVersion }, status: { code: response.statusCode, message: res.statusMessage }, type: response.type, charset: response.charset }; } params(params) { if (this._with) { this._with.urlParams = params; this._with.rawUrl = this.url; } this.url = fillParams(this.url, params); return this; } } exports.default = Supertest;