UNPKG

silvie

Version:

Typescript Back-end Framework

131 lines (123 loc) 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.allowUpload = allowUpload; exports.arrayUpload = arrayUpload; exports.default = void 0; exports.multipleUpload = multipleUpload; exports.preventUpload = preventUpload; exports.route = route; exports.singleUpload = singleUpload; exports.withMiddleware = withMiddleware; var _server = _interopRequireDefault(require("../server")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class Controller {} /** * Register class method as HTTP route * @param method Defines HTTP verb * @param url Specifies the url for accessing route */ exports.default = Controller; function route(method, url) { const methodName = method.toLowerCase(); return (target, key, descriptor) => { _server.default.registerRoute(methodName, url, descriptor.middlewares, descriptor.upload, descriptor.value.bind(target)); }; } /** * Assigns one or more middlewares to a route * @param middlewareNames */ function withMiddleware(...middlewareNames) { return (target, key, descriptor) => { const func = descriptor; func.middlewares = middlewareNames; return func; }; } /** * Allow upload for a single file with specified field name * @param fieldName */ function singleUpload(fieldName) { return (target, key, descriptor) => { const func = descriptor; if (func.upload) { throw new Error('Cannot apply more than one upload rule'); } func.upload = { action: 'single', options: fieldName }; return func; }; } /** * Allow upload for multiple files with specified field name * @param fieldName * @param maxCount */ function arrayUpload(fieldName, maxCount) { return (target, key, descriptor) => { const func = descriptor; if (func.upload) { throw new Error('Cannot apply more than one upload rule'); } func.upload = { action: 'array', options: { fieldName, maxCount } }; return func; }; } /** * Allow upload for multiple field names * @param fields */ function multipleUpload(...fields) { return (target, key, descriptor) => { const func = descriptor; if (func.upload) { throw new Error('Cannot apply more than one upload rule'); } func.upload = { action: 'multiple', options: fields }; return func; }; } /** * Allow upload without any restrictions */ function allowUpload() { return (target, key, descriptor) => { const func = descriptor; if (func.upload) { throw new Error('Cannot apply more than one upload rule'); } func.upload = { action: 'any' }; return func; }; } /** * Block any file upload */ function preventUpload() { return (target, key, descriptor) => { const func = descriptor; if (func.upload) { throw new Error('Cannot apply more than one upload rule'); } func.upload = { action: 'block' }; return func; }; }