@iamkenos/iris
Version:
Test API endpoints with Axios & Jest using a collection of custom matchers and built-in utility functions.
44 lines • 1.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Request = void 0;
const axios_1 = __importDefault(require("axios"));
class Request {
url;
delay = { pre: 0, post: 0 };
spec;
constructor(url, spec) {
this.url = url;
this.spec = { url: this.url, validateStatus: () => true, ...spec, data: spec.body };
}
setPreDelay(ms) {
this.delay.pre = ms;
return this;
}
setPostDelay(ms) {
this.delay.post = ms;
return this;
}
async send() {
await new Promise(resolve => setTimeout(resolve, this.delay.pre));
let response;
try {
/** measure response time in nanos with [`process.hrtime.bigint()`](https://nodejs.org/api/process.html#process_process_hrtime_bigint) */
const start = process.hrtime.bigint();
response = await (0, axios_1.default)(this.url, this.spec);
response.time = Math.round(Number(process.hrtime.bigint() - start) / 1000000);
response.request = this.spec;
response.body = response.data;
}
catch (err) {
console.error(`Fetch ${this.url} returned an error.`, err);
throw err;
}
await new Promise(resolve => setTimeout(resolve, this.delay.post));
return response;
}
}
exports.Request = Request;
//# sourceMappingURL=request.js.map