UNPKG

naily

Version:

写了一个没有模块系统的 Minimal Nest.js 写着玩的~

211 lines 7.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Res = exports.Req = exports.RequestIp = exports.RequestBody = exports.RequestParam = exports.RequestQuery = exports.RequestMapping = exports.OptionsMapping = exports.DeleteMapping = exports.PatchMapping = exports.PutMapping = exports.PostMapping = exports.GetMapping = exports.RestController = exports.HTTP_KEY = exports.componentContiner = void 0; require("reflect-metadata"); const di_decorator_1 = require("./di.decorator"); const http_filter_1 = require("../errors/http.filter"); exports.componentContiner = []; var HTTP_KEY; (function (HTTP_KEY) { HTTP_KEY["Controller"] = "controller"; HTTP_KEY["Get"] = "get"; HTTP_KEY["Post"] = "post"; HTTP_KEY["Put"] = "put"; HTTP_KEY["Patch"] = "patch"; HTTP_KEY["Delete"] = "delete"; HTTP_KEY["Options"] = "options"; HTTP_KEY["All"] = "all"; HTTP_KEY["ExceptionFilter"] = "exception_filter"; HTTP_KEY["UnknownErrorFilter"] = "unknown_filter"; HTTP_KEY["Query"] = "query"; HTTP_KEY["Param"] = "param"; HTTP_KEY["Body"] = "body"; HTTP_KEY["Ip"] = "ip"; HTTP_KEY["Req"] = "req"; HTTP_KEY["Res"] = "res"; })(HTTP_KEY = exports.HTTP_KEY || (exports.HTTP_KEY = {})); const RestController = function (path = "/") { return (target) => { exports.componentContiner.push(target); const classParams = Reflect.getMetadata("design:paramtypes", target); function parseParam(param, parent) { return param.map((item, index) => { if (typeof item === "function") { const isInjectable2 = Reflect.getMetadata(di_decorator_1.DI_KEY.Injectable, parent); if (isInjectable2) { const childFunctionParam = Reflect.getMetadata("design:paramtypes", item); if (childFunctionParam) { const value = parseParam(childFunctionParam, item)[index]; return value; } else { return new item(); } } else { return item; } } else { return item; } }); } function childCall(params, args = []) { let whatCircle = []; if (params) { whatCircle = params; } else { whatCircle = classParams; } if (typeof whatCircle !== "object") { return []; } whatCircle.forEach((jtem, index) => { if (typeof jtem === "function") { const isInjectable = Reflect.getMetadata(di_decorator_1.DI_KEY.Injectable, jtem); if (isInjectable) { const param = Reflect.getMetadata("design:paramtypes", jtem); if (param) { if (!params) { args.push(new jtem(...args)); } else { const key = Object.keys(args)[index]; args[key] = new jtem(...parseParam(param, jtem)); } childCall(param, args[index]); } else { if (!params) { args.push(new jtem()); } else { const key = Object.keys(args)[index]; args[key] = new jtem(); } } } else { const ERR = `有一个被没有被注入的名为${jtem.name}的服务被强制注入到${target.name}控制器里面。请检查错误。`; throw new http_filter_1.ICoreError(ERR); } } }); return args; } const arg = childCall(); Reflect.defineMetadata(HTTP_KEY.Controller, { path, clazz: new target(...arg), }, target); }; }; exports.RestController = RestController; const GetMapping = (info = "/") => { return (target, methodName, desc) => { Reflect.defineMetadata(HTTP_KEY.Get, { info, fn: desc.value, }, desc.value); }; }; exports.GetMapping = GetMapping; const PostMapping = (info = "/") => { return (target, methodName, desc) => { Reflect.defineMetadata(HTTP_KEY.Post, { info, fn: desc.value, }, desc.value); }; }; exports.PostMapping = PostMapping; const PutMapping = (info = "/") => { return (target, methodName, desc) => { Reflect.defineMetadata(HTTP_KEY.Put, { info, fn: desc.value, }, desc.value); }; }; exports.PutMapping = PutMapping; const PatchMapping = (info = "/") => { return (target, methodName, desc) => { Reflect.defineMetadata(HTTP_KEY.Patch, { info, fn: desc.value, }, desc.value); }; }; exports.PatchMapping = PatchMapping; const DeleteMapping = (info = "/") => { return (target, methodName, desc) => { Reflect.defineMetadata(HTTP_KEY.Delete, { info, fn: desc.value, }, desc.value); }; }; exports.DeleteMapping = DeleteMapping; const OptionsMapping = (info = "/") => { return (target, methodName, desc) => { Reflect.defineMetadata(HTTP_KEY.Options, { info, fn: desc.value, }, desc.value); }; }; exports.OptionsMapping = OptionsMapping; const RequestMapping = (info = "/") => { return (target, methodName, desc) => { Reflect.defineMetadata(HTTP_KEY.All, { info, fn: desc.value, }, desc.value); }; }; exports.RequestMapping = RequestMapping; const RequestQuery = (target, name, index) => { Reflect.defineMetadata(HTTP_KEY.Query, { name: name, index: index, }, target); }; exports.RequestQuery = RequestQuery; const RequestParam = (target, name, index) => { Reflect.defineMetadata(HTTP_KEY.Param, { name: name, index: index, }, target); }; exports.RequestParam = RequestParam; const RequestBody = (target, name, index) => { Reflect.defineMetadata(HTTP_KEY.Body, { name: name, index: index, }, target); }; exports.RequestBody = RequestBody; const RequestIp = (target, name, index) => { Reflect.defineMetadata(HTTP_KEY.Ip, { name: name, index: index, }, target); }; exports.RequestIp = RequestIp; const Req = (target, name, index) => { Reflect.defineMetadata(HTTP_KEY.Req, { name: name, index: index, }, target); }; exports.Req = Req; const Res = (target, name, index) => { Reflect.defineMetadata(HTTP_KEY.Res, { name: name, index: index, }, target); }; exports.Res = Res; //# sourceMappingURL=http.decorator.js.map