rest-chronicle
Version:
autodocumentate rest api
111 lines (109 loc) • 3.73 kB
JavaScript
"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(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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;