@n1k1t/mock-server
Version:
Powerful util to setup mocks over HTTP APIs
60 lines • 2.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.History = void 0;
const uuid_1 = require("uuid");
const utils_1 = require("../../../utils");
class History {
constructor(configuration) {
this.configuration = configuration;
this.id = (0, uuid_1.v4)();
this.messagesCounter = (0, utils_1.buildCounter)();
this.group = this.configuration.group;
this.snapshot = this.configuration.snapshot;
this.status = (0, utils_1.cast)('unregistered');
this.timestamp = this.configuration.timestamp ?? Date.now();
this.duration = 0;
}
pushMessage(location, data) {
this.snapshot.messages.push({ location, data, id: this.messagesCounter(), timestamp: Date.now() });
return this.mark();
}
actualizeSnapshot(snapshot) {
this.snapshot.assign(snapshot.omit(['incoming', 'forwarded', 'messages']));
return this.mark();
}
switchStatus(status) {
return Object.assign(this.mark(), { status });
}
hasStatus(status) {
return this.status === status;
}
complete() {
if (this.snapshot.container) {
this.snapshot.container = this.snapshot.container.clone();
}
return this.switchStatus('completed');
}
assign(payload) {
return Object.assign(this.mark(), payload);
}
/** Updates duration property base on timestamp */
mark() {
return this.hasStatus('completed') ? this : Object.assign(this, { duration: Date.now() - this.timestamp });
}
toPlain() {
return {
id: this.id,
timestamp: this.timestamp,
duration: this.duration,
group: this.group,
status: this.status,
snapshot: this.snapshot.toPlain(),
expectation: this.expectation?.toPlain(),
};
}
static build(configuration) {
return new History(configuration);
}
}
exports.History = History;
//# sourceMappingURL=model.js.map