UNPKG

sensai

Version:

Because even AI needs a master

47 lines (46 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { /** * A guard is a function that wraps a handler and adds input/output * validation and other features. */ default: function() { return _default; }, getHandlerOptions: function() { return getHandlerOptions; } }); const _schema = require("./schema"); // symbol use to add properties to handler const symbol = Symbol("guard"); function _default(handler, options) { return hideProperties(async function(props, ...args) { const data = options?.input ? await (0, _schema.validateAndCoerce)(props, options.input) : props; return await handler.call(this, data, ...args); }, options || {}); } const getHandlerOptions = (fn)=>{ return fn[symbol]; }; /** * Guard needs to return a function so we can import a route, a tool, * etc in a test suite and execute code directly. This is why we adding * the guard options (used for compilation and run time) as non-emurable * properties to the function. */ const hideProperties = (fn, value)=>{ return Object.defineProperty(fn, symbol, { value, writable: false, enumerable: false, configurable: false }); };