@n1k1t/mock-server
Version:
Powerful util to setup mocks over HTTP APIs
88 lines • 3.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HistoryStorage = void 0;
const models_1 = require("../../models");
const expectations_1 = require("../../../expectations");
const model_1 = require("./model");
const config_1 = __importDefault(require("../../../config"));
class HistoryStorage extends Map {
constructor(configuration) {
super();
this.configuration = configuration;
this.stack = [];
}
/** Registers history item */
register(predicate) {
const history = predicate instanceof model_1.History ? predicate : model_1.History.build({
status: 'registered',
group: this.configuration.group,
snapshot: predicate.snapshot,
});
this.set(history.id, history);
if (this.stack.push(history.id) > (this.configuration.limit ?? config_1.default.get('history').limit)) {
this.delete(this.stack.shift());
}
return history;
}
/** Removes history item from storage and marks it as `unregistred` */
unregister(history) {
if (history) {
this.delete(history.switchStatus('unregistered').id);
this.stack.splice(this.stack.indexOf(history.id), 1);
}
return this;
}
/** Injects and registers history items from plain */
inject(list) {
const fake = models_1.Provider.build({ group: this.configuration.group });
list.forEach((history) => this.register(model_1.History.build({
id: history.id,
group: history.group,
status: history.status,
timestamp: history.timestamp,
duration: history.duration,
...(history.expectation && { expectation: expectations_1.Expectation.build(history.expectation) }),
snapshot: models_1.RequestContextSnapshot.build({
transport: history.snapshot.transport,
event: history.snapshot.event,
flags: history.snapshot.flags,
cache: history.snapshot.cache,
state: history.snapshot.state,
error: history.snapshot.error,
incoming: history.snapshot.incoming,
outgoing: history.snapshot.outgoing,
messages: history.snapshot.messages,
seed: history.snapshot.seed,
storage: fake.storages.containers,
...(history.snapshot.forwarded && {
forwarded: {
messages: history.snapshot.forwarded.messages,
incoming: Object.assign(history.snapshot.forwarded.incoming, {
dataRaw: history.snapshot.forwarded.incoming.dataRaw
? Buffer.from(history.snapshot.forwarded.incoming.dataRaw)
: undefined,
}),
...(history.snapshot.forwarded.outgoing && {
outgoing: Object.assign(history.snapshot.forwarded.outgoing, {
dataRaw: history.snapshot.forwarded.outgoing.dataRaw
? Buffer.from(history.snapshot.forwarded.outgoing.dataRaw)
: undefined,
}),
}),
},
}),
})
})));
return this;
}
/** Clears storage */
clear() {
this.stack = [];
super.clear();
}
}
exports.HistoryStorage = HistoryStorage;
//# sourceMappingURL=storage.js.map