UNPKG

fast-controllers

Version:

class based controllers, and implied folder route mapping for Fastify

60 lines (59 loc) 2.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.METHODS = void 0; const FastControllerException_MethodHandlerNotDefined_1 = __importDefault(require("./exceptions/FastControllerException_MethodHandlerNotDefined")); exports.METHODS = ['delete', 'get', 'head', 'patch', 'post', 'put', 'options', 'search', 'trace', 'propfind', 'proppatch', 'mkcol', 'copy', 'move', 'lock', 'unlock']; class FastController { url = ''; schema; websocket = false; method; handler = (requestOrConn, replyOrRequest) => { const request = requestOrConn; const reply = replyOrRequest; if (typeof this[request.method.toLowerCase()] === 'function') { return this[request.method.toLocaleLowerCase()](request, reply); } else if (typeof this.handle === 'function') { return this.handle(request, reply); } }; static scope = 'unsecured'; _instance; params; constructor(i, route) { this._instance = i; this.url = route; const methods = new Array(); exports.METHODS.forEach(method => { if (typeof this[method] === 'function') { methods.push(method.toUpperCase()); } }); this.method = methods; if (this.method.length === 0 && typeof this.handle !== 'function') { throw new FastControllerException_MethodHandlerNotDefined_1.default(this.constructor['name']); } this.init(); } init() { } set instance(i) { this._instance = i; } get instance() { return this._instance; } get methods() { return Array.isArray(this.method) ? this.method : [this.method]; } preValidation = (request, reply, done) => { if (this.onPreValidation !== undefined && typeof this.onPreValidation === 'function') { return this.onPreValidation(request, reply, done); } done(); }; } exports.default = FastController;