@mvx/mvc
Version:
@mvx/mvc is mvc framework on server, base on koa, @tsdi frameworks
246 lines (244 loc) • 7.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Put = exports.Post = exports.Patch = exports.Delete = exports.Get = exports.Options = exports.Head = exports.Route = exports.createRouteDecorator = exports.Middleware = exports.Cors = exports.createCorsDecorator = exports.Controller = exports.Authorization = exports.MvcModule = void 0;
var tslib_1 = require("tslib");
var ioc_1 = require("@tsdi/ioc");
var boot_1 = require("@tsdi/boot");
var exps_1 = require("./exps");
var RequestMethod_1 = require("./RequestMethod");
/**
* MvcModule Decorator, definde class as mvc module.
*
* @MvcModule
*/
exports.MvcModule = boot_1.createDIModuleDecorator('MvcModule', null, function (metadata) {
// static main.
if (ioc_1.isClass(metadata.type) && ioc_1.isFunction(metadata.type['main'])) {
setTimeout(function () {
metadata.type['main'](metadata);
}, 100);
}
return metadata;
});
/**
* Authorization decorator, define class or method need auth check.
*
* @Authorization
*/
exports.Authorization = ioc_1.createClassMethodDecorator('Authorization', [
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isArray(arg)) {
ctx.metadata.middlewares = arg;
ctx.next(next);
}
else {
ctx.metadata.role = arg;
ctx.next(next);
}
},
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isString(arg)) {
ctx.metadata.role = arg;
}
}
]);
/**
* Controller decorator, define the class as mvc controller.
* @Controller
*/
exports.Controller = ioc_1.createClassDecorator('Controller', [
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isString(arg)) {
ctx.metadata.routePrefix = arg;
ctx.next(next);
}
},
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isArray(arg)) {
ctx.metadata.middlewares = arg;
ctx.next(next);
}
else {
ctx.next(next, false);
}
}
], function (meta) {
if (!exps_1.routeSart.test(meta.routePrefix)) {
meta.routePrefix = '/' + meta.routePrefix;
}
}, true);
/**
* Cors Decorator, define controller class or controller method support cors.
* @Cors
* @param name
* @param actions
* @param metadataExtends
*/
function createCorsDecorator(name, actions, metadataExtends) {
return ioc_1.createClassMethodDecorator(name, tslib_1.__spreadArrays((actions || []), [
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isString(arg)) {
ctx.metadata.allowMethods = arg;
ctx.next(next);
}
else if (ioc_1.isArray(arg)) {
var allowMethods = arg;
ctx.metadata.allowMethods = allowMethods.filter(function (m) { return !ioc_1.isUndefined(m) && m !== null; });
ctx.next(next);
}
},
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isNumber(arg)) {
ctx.metadata.maxAge = arg;
ctx.next(next);
}
},
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isArray(arg)) {
var allowHeaders = arg;
ctx.metadata.allowHeaders = allowHeaders.filter(function (h) { return !!h; });
ctx.next(next);
}
}
]), metadataExtends);
}
exports.createCorsDecorator = createCorsDecorator;
/**
* Cors Decorator, define controller class or controller method support cors.
* @Cors
*/
exports.Cors = createCorsDecorator('Cors');
/**
* Middleware Decorator, definde class as mvc middleware.
*
* @Middleware
*/
exports.Middleware = ioc_1.createClassDecorator('Middleware', null, function (metadata) {
metadata.singleton = true;
if (metadata.name) {
metadata.provide = metadata.name;
}
if (!metadata.scope) {
metadata.scope = 'global';
}
});
/**
* create route decorator.
*
* @export
* @template T
* @param {RequestMethod} [method]
* @param { MetadataExtends<T>} [metaExtends]
*/
function createRouteDecorator(method, actions, metaExtends) {
return ioc_1.createMethodDecorator('Route', tslib_1.__spreadArrays((actions || []), [
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isString(arg)) {
ctx.metadata.route = arg;
ctx.next(next);
}
},
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isArray(arg)) {
ctx.metadata.middlewares = arg;
ctx.next(next);
}
else if (ioc_1.isString(arg)) {
ctx.metadata.contentType = arg;
ctx.next(next);
}
else if (ioc_1.isNumber(arg)) {
ctx.metadata.method = arg;
}
},
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isNumber(arg)) {
ctx.metadata.method = arg;
}
else {
ctx.metadata.contentType = arg;
ctx.next(next);
}
},
function (ctx, next) {
var arg = ctx.currArg;
if (ioc_1.isNumber(arg)) {
ctx.metadata.method = arg;
}
}
]), function (metadata) {
if (metaExtends) {
metaExtends(metadata);
}
if (!exps_1.routeSart.test(metadata.route)) {
metadata.route = '/' + metadata.route;
}
if (method) {
metadata.method = method;
}
else if (!metadata.method) {
metadata.method = RequestMethod_1.RequestMethod.Get;
}
return metadata;
});
}
exports.createRouteDecorator = createRouteDecorator;
/**
* route decorator. define the controller method as an route.
*
* @Route
*/
exports.Route = createRouteDecorator();
/**
* Head decorator. define the route method as head.
*
* @Head
*/
exports.Head = createRouteDecorator(RequestMethod_1.RequestMethod.Head);
/**
* Options decorator. define the route method as an options.
*
* @Options
*/
exports.Options = createRouteDecorator(RequestMethod_1.RequestMethod.Options);
/**
* Get decorator. define the route method as get.
*
* @Get
*/
exports.Get = createRouteDecorator(RequestMethod_1.RequestMethod.Get);
/**
* Delete decorator. define the route method as delete.
*
* @Delete
*/
exports.Delete = createRouteDecorator(RequestMethod_1.RequestMethod.Delete);
/**
* Patch decorator. define the route method as patch.
*
* @Patch
*/
exports.Patch = createRouteDecorator(RequestMethod_1.RequestMethod.Patch);
/**
* Post decorator. define the route method as post.
*
* @Post
*/
exports.Post = createRouteDecorator(RequestMethod_1.RequestMethod.Post);
/**
* Put decorator. define the route method as put.
*
* @Put
*/
exports.Put = createRouteDecorator(RequestMethod_1.RequestMethod.Put);
//# sourceMappingURL=sourcemaps/decorators.js.map