UNPKG

forget-api

Version:

无需封装 Axios,无需写接口请求函数,无需维护返回值类型,把这些琐碎重复的事情交给工具来处理,让精力聚焦在核心功能的实现上。

93 lines (84 loc) 3.56 kB
/** * forget-api@0.0.0-alpha.2 * * @author Ambit Tsai <ambit_tsai@qq.com> * @license MulanPSL-2.0 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var path = require('node:path'); var common = require('@nestjs/common'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var path__default = /*#__PURE__*/_interopDefaultLegacy(path); function generateRoute(filePath, type = 'dist') { const pathProps = path__default["default"].parse(filePath); const config = getRootConfig(); const ctorName = pathProps.name.replace(config.suffix, ''); const targetPath = pathProps.dir.endsWith(path__default["default"].sep + ctorName) ? pathProps.dir : pathProps.dir + path__default["default"].sep + ctorName; const routeRoot = path__default["default"].resolve(getProjectRoot(), config[type === 'dist' ? 'routeDistRoot' : 'routeSrcRoot']); return '/' + path__default["default"].relative(routeRoot, targetPath).replace(/\\/g, '/'); } const CONFIG_FILE_NAME = 'full-stack.json'; function getRootConfig() { const configPath = path__default["default"].resolve(getProjectRoot(), CONFIG_FILE_NAME); return require(configPath); } function getProjectRoot() { const config = getCurrentConfig(); return config.root ? path__default["default"].resolve(process.cwd(), config.root) : process.cwd(); } function getCurrentConfig() { const configPath = path__default["default"].resolve(process.cwd(), CONFIG_FILE_NAME); return require(configPath); } function defineExpose(Ctor) { return null; } const PROCESSED = Symbol('Processed'); let methodDecoratorMap = { delete: common.Delete, get: common.Get, head: common.Head, options: common.Options, patch: common.Patch, post: common.Post, put: common.Put, }; methodDecoratorMap = Object.assign(Object.create(null), methodDecoratorMap); function Controller(filePath) { if (typeof filePath !== 'string' || !path__default["default"].isAbsolute(filePath)) { return Reflect.apply(common.Controller, this, arguments); } const prefix = generateRoute(filePath); const classDecorator = common.Controller(prefix); return function (Ctor) { if (!Ctor[PROCESSED]) { for (const [key, desc] of Object.entries(Object.getOwnPropertyDescriptors(Ctor.prototype))) { const arr = key.replace(/([A-Z])/g, '-$1').split('-'); const decorator = methodDecoratorMap[arr[0]]; if (!decorator) { continue; } arr.shift(); // 首个元素为 HTTP 方法 // TODO: 需要取出 metaData,判断 :id 等 // console.log('[MK]', Reflect.getMetadataKeys(desc.value)); // console.log('[MD]', Reflect.getMetadata('path', desc.value)); Reflect.decorate([ decorator(arr.join('-').toLowerCase()), Reflect.metadata('design:type', Function), Reflect.metadata('design:paramtypes', []), Reflect.metadata('design:returntype', void 0), ], Ctor.prototype, key, desc); } Ctor[PROCESSED] = true; } return Reflect.apply(classDecorator, this, arguments); }; } exports.Controller = Controller; exports.defineExpose = defineExpose; //# sourceMappingURL=index.js.map