UNPKG

hypershield

Version:

Middleware suite for high-performance and resilient APIs

73 lines 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HyperShield = void 0; class HyperShield { constructor(config) { this.initialized = false; this.config = this.validateConfig(config || {}); } validateConfig(config) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; return { compression: { enabled: (_b = (_a = config.compression) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : false, type: 'gzip', level: (_d = (_c = config.compression) === null || _c === void 0 ? void 0 : _c.level) !== null && _d !== void 0 ? _d : 6, threshold: (_f = (_e = config.compression) === null || _e === void 0 ? void 0 : _e.threshold) !== null && _f !== void 0 ? _f : 1024 }, cache: { enabled: (_h = (_g = config.cache) === null || _g === void 0 ? void 0 : _g.enabled) !== null && _h !== void 0 ? _h : false, provider: (_k = (_j = config.cache) === null || _j === void 0 ? void 0 : _j.provider) !== null && _k !== void 0 ? _k : 'memory', ttl: (_m = (_l = config.cache) === null || _l === void 0 ? void 0 : _l.ttl) !== null && _m !== void 0 ? _m : 3600 }, metrics: { enabled: (_p = (_o = config.metrics) === null || _o === void 0 ? void 0 : _o.enabled) !== null && _p !== void 0 ? _p : false, path: (_r = (_q = config.metrics) === null || _q === void 0 ? void 0 : _q.path) !== null && _r !== void 0 ? _r : '/metrics' } }; } initialize() { if (this.initialized) { return; } this.initialized = true; } compression(_options) { var _a, _b; if (!this.initialized) { throw new Error('HyperShield must be initialized before using compression'); } const compressionEnabled = (_b = (_a = this.config.compression) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : false; return (_req, _res, next) => { if (!compressionEnabled) { next(); return; } next(); }; } cache(_options) { if (!this.initialized) { throw new Error('HyperShield must be initialized before using cache'); } return (_req, _res, next) => { next(); }; } metrics(_options) { if (!this.initialized) { throw new Error('HyperShield must be initialized before using metrics'); } return (_req, _res, next) => { next(); }; } getMetrics() { if (!this.initialized) { throw new Error('HyperShield must be initialized before getting metrics'); } return Promise.resolve(''); } } exports.HyperShield = HyperShield; //# sourceMappingURL=index.js.map