UNPKG

@n1k1t/mock-server

Version:

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

88 lines 3.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Container = void 0; const deepmerge_1 = __importDefault(require("deepmerge")); const rfdc_1 = __importDefault(require("rfdc")); const utils_1 = require("./utils"); const clone = (0, rfdc_1.default)(); class Container { constructor(provided) { this.provided = provided; this.key = this.provided.key; this.group = this.provided.group; this.aliases = this.provided.aliases instanceof Set ? this.provided.aliases : new Set(this.provided.aliases ?? []); /** Seconds */ this.ttl = this.provided.ttl; this.payload = this.provided.payload; this.timestamp = this.provided.timestamp; this.hooks = this.provided.hooks; } /** Updates internal timestamp to increase TTL */ renew() { this.timestamp = Date.now(); return this; } /** Checks expiration by TTL */ checkIsExpired(timestamp = Date.now()) { return (this.provided.timestamp + this.ttl * 1000) < timestamp; } assign(predicate) { const payload = Object.assign(this.payload, typeof predicate === 'function' ? predicate(this.payload) : predicate); return Object.assign(this, { payload }); } merge(predicate) { const payload = (0, deepmerge_1.default)(this.payload, (typeof predicate === 'function' ? predicate(this.payload) : predicate), { arrayMerge: (target, source) => source }); return Object.assign(this, { payload }); } /** Binds this container to another alias key */ bind(key) { const compiled = (0, utils_1.compileContainerKey)(key); this.aliases.add(compiled); this.hooks?.bind?.(compiled, this); return this; } /** Unbinds this container by provided key */ unbind(key) { const compiled = key ? (0, utils_1.compileContainerKey)(key) : this.key; this.aliases.delete(compiled); this.hooks?.unbind?.(compiled, this); return this; } /** Setups hooks to work with storage */ configure(payload) { if (payload.hooks) { this.hooks = payload.hooks; } return this; } clone() { return new Container({ ...this.provided, payload: clone(this.payload) }); } toPlain() { return { key: this.key, ttl: this.ttl, payload: this.payload, }; } toBackup() { return { group: this.group, key: this.key, ttl: this.ttl, timestamp: this.timestamp, payload: this.payload, aliases: [...this.aliases.values()], }; } static build(provided) { return new Container(provided); } } exports.Container = Container; //# sourceMappingURL=model.js.map