UNPKG

@n1k1t/mock-server

Version:

The ultimate toolkit to intercept, transform, and simulate HTTP/WS traffic with type-safe expectations

122 lines 4.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.History = void 0; const uuid_1 = require("uuid"); const lodash_1 = __importDefault(require("lodash")); class History { constructor(configuration) { this.configuration = configuration; this.id = this.configuration.id ?? (0, uuid_1.v4)(); this.timestamp = this.configuration.timestamp ?? Date.now(); this.group = this.configuration.group; this.snapshot = this.configuration.snapshot; this.status = this.configuration.status ?? 'unregistered'; this.meta = this.configuration.meta ?? { tags: { transport: this.snapshot.transport, incoming: { path: this.snapshot.incoming.path, method: this.snapshot.incoming.method, }, outgoing: { status: this.snapshot.outgoing.status, }, forward: { url: this.expectation?.schema.forward?.baseUrl ?? this.expectation?.schema.forward?.url, }, }, metrics: { duration: 0, }, }; this.expectation = this.configuration.expectation; } /** Actualizes internal snapshot with provided */ actualize(snapshot) { this.snapshot.assign(snapshot.omit(['incoming', 'forwarded'])); if (snapshot.forwarded) { this.meta.tags.forward = { url: snapshot.forwarded.schema.baseUrl ?? snapshot.forwarded.schema.url, }; this.snapshot.assign({ forwarded: { schema: snapshot.forwarded.schema, incoming: lodash_1.default.omit(snapshot.forwarded.incoming, ['stream']), messages: snapshot.forwarded.messages, ...(snapshot.forwarded.outgoing && { outgoing: lodash_1.default.omit(snapshot.forwarded.outgoing, ['stream']), }), }, }); } if (snapshot.seed !== undefined) { this.meta.tags.seed = snapshot.seed; } return this.mark(); } /** Switched status and updates duration */ switch(status) { return Object.assign(this.mark(), { status }); } is(status) { return this.status === status; } complete() { if (this.snapshot.container) { this.snapshot.container = this.snapshot.container.clone(); } if (this.snapshot.cache.hasRead || this.snapshot.cache.hasWritten) { this.meta.tags.cache = { hasWritten: this.snapshot.cache.hasWritten, hasRead: this.snapshot.cache.hasRead, }; } this.meta.tags.error = this.snapshot.error; this.meta.tags.outgoing = { status: this.snapshot.outgoing.status, }; return this.switch('completed'); } assign(payload) { return Object.assign(this.mark(), payload); } /** Updates duration based on timestamp */ mark() { if (this.is('completed')) { return this; } this.meta.metrics.duration = Date.now() - this.timestamp; return this; } toPlain() { return { format: 'plain', id: this.id, timestamp: this.timestamp, group: this.group, status: this.status, meta: this.meta, snapshot: this.snapshot.toPlain(), expectation: this.expectation?.toPlain(), }; } toCompact() { return { format: 'compact', id: this.id, timestamp: this.timestamp, group: this.group, status: this.status, meta: this.meta, expectation: this.expectation?.toCompact(), }; } static build(configuration) { return new History(configuration); } } exports.History = History; //# sourceMappingURL=model.js.map