UNPKG

@n1k1t/mock-server

Version:

Powerful util to setup mocks over HTTP APIs

88 lines 3.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestContextSnapshot = void 0; const rfdc_1 = __importDefault(require("rfdc")); const lodash_1 = __importDefault(require("lodash")); const clone = (0, rfdc_1.default)(); class RequestContextSnapshot { constructor(configuration) { this.configuration = configuration; this.transport = this.configuration.transport; this.event = this.configuration.event; this.flags = this.configuration.flags; this.storage = this.configuration.storage; this.cache = this.configuration.cache ?? { isEnabled: false }; this.state = this.configuration.state ?? {}; this.incoming = this.configuration.incoming; this.outgoing = this.configuration.outgoing; this.forwarded = this.configuration.forwarded; this.messages = this.configuration.messages; this.container = this.configuration.container; this.seed = this.configuration.seed; } assign(payload) { return Object.assign(this, payload); } pick(keys) { return lodash_1.default.pick(this, keys); } omit(keys) { return lodash_1.default.omit(this, keys); } unset(keys) { keys.forEach((key) => lodash_1.default.unset(this, key)); return this; } clone() { return RequestContextSnapshot.build({ ...lodash_1.default.omit(this, ['incoming', 'outgoing']), incoming: Object.assign(clone(lodash_1.default.omit(this.incoming, ['stream', 'dataRaw'])), { stream: this.incoming.stream, dataRaw: this.incoming.dataRaw?.subarray(), }), outgoing: Object.assign(clone(lodash_1.default.omit(this.outgoing, ['stream', 'dataRaw'])), { stream: this.outgoing.stream, dataRaw: this.outgoing.dataRaw?.subarray(), }), }); } toPlain() { return { transport: this.transport, event: this.event, flags: this.flags, state: this.state, cache: this.cache, seed: this.seed, container: this.container?.toPlain(), incoming: Object.assign(lodash_1.default.omit(this.incoming, ['stream']), { dataRaw: this.incoming.dataRaw?.toString(), }), outgoing: Object.assign(lodash_1.default.omit(this.outgoing, ['stream']), { dataRaw: this.outgoing.dataRaw?.toString(), }), ...(this.forwarded && { forwarded: Object.assign(lodash_1.default.omit(this.forwarded, ['incoming', 'outgoing']), { incoming: Object.assign(lodash_1.default.omit(this.forwarded.incoming, ['dataRaw']), { dataRaw: this.forwarded.incoming.dataRaw?.toString(), }), ...(this.forwarded.outgoing && { outgoing: Object.assign(lodash_1.default.omit(this.forwarded.outgoing, ['dataRaw']), { dataRaw: this.forwarded.outgoing.dataRaw?.toString(), }), }), }), }), messages: this.messages, error: this.error, }; } static build(configuration) { return new RequestContextSnapshot(configuration); } } exports.RequestContextSnapshot = RequestContextSnapshot; //# sourceMappingURL=snapshot.js.map