UNPKG

@decaf-ts/decorator-validation

Version:
95 lines 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.modelBaseDecorator = modelBaseDecorator; exports.model = model; exports.hashedBy = hashedBy; exports.serializedBy = serializedBy; const construction_1 = require("./construction.cjs"); const constants_1 = require("./../utils/constants.cjs"); const Model_1 = require("./Model.cjs"); const decoration_1 = require("@decaf-ts/decoration"); /** * @description Base decorator for model classes. * @summary This decorator wraps the original constructor to bind the Model prototype, run a builder function, and register the model. * @param {any} original The original constructor of the class. * @return {any} The new constructor with added model functionality. * @function modelBaseDecorator * @memberOf module:decorator-validation */ function modelBaseDecorator(original) { // the new constructor behaviour const newConstructor = function (...args) { const instance = (0, construction_1.construct)(original, ...args); (0, construction_1.bindModelPrototype)(instance); // run a builder function if defined with the first argument (The ModelArg) const builder = Model_1.Model.getBuilder(); if (builder) builder(instance, args.length ? args[0] : undefined); return instance; }; // copy prototype so instanceof operator still works newConstructor.prototype = original.prototype; // Sets the proper constructor name for type verification Object.defineProperty(newConstructor, "name", { writable: false, enumerable: true, configurable: false, value: original.prototype.constructor.name, }); (0, decoration_1.metadata)(constants_1.ModelKeys.CONSTRUCTOR, original)(newConstructor); Model_1.Model.register(newConstructor, original.name); // return new constructor (will override original) return newConstructor; } /** * @summary Defines a class as a Model class * @description * * - Registers the class under the model registry so it can be easily rebuilt; * - Overrides the class constructor; * - Runs the global {@link ModelBuilderFunction} if defined; * * @function model * * @category Class Decorators */ function model() { const key = constants_1.ModelKeys.MODEL; return decoration_1.Decoration.for(key).define(modelBaseDecorator).apply(); } /** * @summary Defines the hashing algorithm to use on the model * @description * * - Registers the class under the model registry so it can be easily rebuilt; * - Overrides the class constructor; * - Runs the global {@link ModelBuilderFunction} if defined; * * @param {string} algorithm the algorithm to use * * @function hashedBy * * @category Class Decorators */ function hashedBy(algorithm, ...args) { return (0, decoration_1.metadata)(constants_1.ModelKeys.HASHING, { algorithm: algorithm, args: args, }); } /** * @summary Defines the serialization algorithm to use on the model * * @param {string} serializer the algorithm to use * * @function serializedBy * * @category Class Decorators */ function serializedBy(serializer, ...args) { return (0, decoration_1.metadata)(constants_1.ModelKeys.SERIALIZATION, { serializer: serializer, args: args, }); } //# sourceMappingURL=decorators.js.map