UNPKG

gypsum

Version:

Simple and easy lightweight typescript server side framework on Node.js.

60 lines 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Validall = require("validall"); const types_1 = require("../types"); const defaults = { secure: false, authorize: false, private: false, internal: false, servicesOptions: {}, apiType: types_1.API_TYPES.ALL, domain: 1, before: [], after: [] }; const schemaOptions = { root: '', required: false, filter: true, strict: false, throwMode: false, traceError: false }; function MODEL(options = {}) { return function (Target) { if (Target.__proto__.prototype) { let proto = Object.getOwnPropertyNames(Target.__proto__.prototype).slice(1); for (let i = 0; i < proto.length; i++) { let method = Target.__proto__.prototype[proto[i]]; if (typeof method === 'function' && (method.isService || method.isHook)) { let attrs = Object.create(Target.prototype[proto[i]]); Target.prototype[proto[i]] = function (...args) { return method.apply(this, args); }; for (var key in attrs) Target.prototype[proto[i]][key] = attrs[key]; } } } for (let prop in defaults) { if (options[prop] === undefined) { if (Target.prototype[`__${prop}`] === undefined) options[prop] = defaults[prop]; else options[prop] = Target.prototype[`__${prop}`]; } } if (options.schema) options.schema = new Validall.Schema(options.schema, Object.assign(schemaOptions, { name: Target.name.toLowerCase() })); for (let prop in options) { if (options.hasOwnProperty(prop)) Object.defineProperty(Target.prototype, `__${prop}`, { get: function () { return options[prop]; }, enumerable: true }); } }; } exports.MODEL = MODEL; //# sourceMappingURL=model.js.map