UNPKG

@n1k1t/mock-server

Version:

Powerful util to setup mocks over HTTP APIs

118 lines 5.23 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestContext = void 0; const lodash_1 = __importDefault(require("lodash")); const rxjs_1 = require("rxjs"); const value_1 = require("@n1k1t/typebox/value"); const snapshot_1 = require("./snapshot"); const meta_1 = require("../../../meta"); const utils_1 = require("../../../utils"); __exportStar(require("./snapshot"), exports); __exportStar(require("./types"), exports); __exportStar(require("./utils"), exports); class RequestContext { constructor(provider, configuration) { this.provider = provider; this.configuration = configuration; this.status = (0, utils_1.cast)('registered'); this.streams = { incoming: new rxjs_1.ReplaySubject(Infinity, 5000), outgoing: new rxjs_1.ReplaySubject(Infinity, 5000), }; this.transport = this.configuration.transport; this.event = this.configuration.event; this.flags = {}; this.timestamp = Date.now(); this.meta = meta_1.metaStorage.generate(); this.streams.incoming.subscribe({ error: () => null, next: (data) => this.history?.pushMessage('incoming', data) }); this.streams.outgoing.subscribe({ error: () => null, next: (data) => this.history?.pushMessage('outgoing', data) }); } switchStatus(status) { this.status = status; return this; } hasStatuses(status) { return status.includes(this.status); } /** Compiles snapshot of own payload to work with expectations */ compileSnapshot() { const snapshot = snapshot_1.RequestContextSnapshot.build({ transport: this.transport, event: this.event, flags: this.flags, incoming: this.incoming, outgoing: this.outgoing ?? { type: this.incoming.type, status: 0, headers: {} }, storage: this.provider.storages.containers, cache: { isEnabled: this.provider.server.databases.redis !== null }, }); snapshot.incoming.stream = this.streams.incoming.asObservable(); snapshot.outgoing.stream = this.streams.outgoing.asObservable(); return snapshot; } ; /** Compiles history model with own snapshot for GUI */ compileHistory() { return this.provider.storages.history.register({ timestamp: this.timestamp, snapshot: this.compileSnapshot().assign({ messages: [] }).clone(), }); } /** Compiles cache configuration using own snapshot and expectation */ compileCacheConfiguration() { if (!this.snapshot.cache.isEnabled || !this.expectation?.forward?.cache) { return { isEnabled: false }; } const payload = this.snapshot.cache.key ?? lodash_1.default.pick(this.snapshot.incoming, ['path', 'method', 'data', 'query']); const prefix = this.snapshot.cache.prefix ?? this.expectation?.forward?.cache?.prefix; const key = typeof payload === 'object' ? value_1.Value.Hash(payload).toString() : String(payload); const ttl = this.snapshot.cache.ttl ?? this.expectation.forward.cache.ttl ?? 3600; return { prefix, ttl, key: `${prefix ?? ''}${key}`, isEnabled: true }; } /** Provides payload parts into context */ assign(payload) { return Object.assign(this, payload); } /** Marks context as skipped to prevent further handling in executors */ skip() { return this.switchStatus('skipped'); } /** Marks context as handling */ handle() { return this.switchStatus('handling'); } /** Marks context as completed, completes streams, provides outgoing payload from the own snapshot and publishes history */ complete() { if (this.hasStatuses(['completed'])) { return this; } this.streams.incoming.complete(); this.streams.outgoing.complete(); if (!this.outgoing) { this.outgoing = this.snapshot.outgoing; } if (this.history?.hasStatus('pending')) { this.history.actualizeSnapshot(this.snapshot.assign({ outgoing: this.outgoing })).complete(); this.provider.server.exchanges.io.publish('history:updated', this.history.toPlain()); } return this.switchStatus('completed'); } } exports.RequestContext = RequestContext; //# sourceMappingURL=index.js.map