Skip to content

Commit

Permalink
fix: Export
Browse files Browse the repository at this point in the history
  • Loading branch information
hxg2050 committed Mar 21, 2024
1 parent 4a0143c commit 560c0f2
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 7 deletions.
1 change: 1 addition & 0 deletions class-transformer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'class-transformer';
1 change: 1 addition & 0 deletions class-validator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'class-validator';
8 changes: 1 addition & 7 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import typescript from '@rollup/plugin-typescript'
import { defineConfig } from 'rollup'
import { dts } from "rollup-plugin-dts";

export default defineConfig({
input: 'src/index.ts',
Expand All @@ -12,9 +11,4 @@ export default defineConfig({
format: 'esm',
}],
plugins: [typescript()],
},
{
input: 'src/index.ts',
output: [{ file: "dist/index.d.ts", format: "es" }],
plugins: [dts()],
},)
})
177 changes: 177 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import "reflect-metadata";
import { glob } from "glob";
import path from "node:path";
import { plainToInstance } from "class-transformer";
import { validate } from "class-validator";
const PATH_METADATA = 'path';
const METHOD_METADATA = 'method';
const PARAM_METADATA = 'param';
const PARAM_VALIDATE_METADATA = 'param_validate';
/**
* 类装饰
* @param path 路径
* @returns
*/
export const Handler = (path) => {
return target => {
path = typeof path === 'undefined' ? target.name : path;
Reflect.defineMetadata(PATH_METADATA, path, target);
};
};
/**
* 方法装饰
* @param path 路径
* @returns
*/
export const Method = (path) => {
return (target, key, descriptor) => {
if (typeof key === 'string') {
path = typeof path === 'undefined' ? key : path;
Reflect.defineMetadata(METHOD_METADATA, path, descriptor.value);
}
};
};
/**
* 参数装饰
* @param name 参数名
* @param checker 检查器/处理器
* @returns
*/
export const Param = (name, checker) => {
return (target, key, index) => {
if (!key) {
return;
}
const param = Reflect.get(target, key);
let params = Reflect.getMetadata(PARAM_METADATA, param);
if (!params) {
params = [];
}
params.push([index, name, checker]);
Reflect.defineMetadata(PARAM_METADATA, params, param);
};
};
/**
* 声明参数需要进行校验,并用目标类型重新实例
* @param target
* @param key
* @param index
*/
export const ParamType = (target, key, index) => {
if (!key) {
return;
}
const types = Reflect.getMetadata("design:paramtypes", target, key);
// console.log(Object.getOwnPropertyDescriptors(Reflect.ownKeys(types[index].prototype)));
Reflect.defineMetadata(PARAM_VALIDATE_METADATA, types[index], Reflect.get(target, key));
};
const METHOD_DOC_METADATA = 'method_doc';
/**
* 文档
*/
export const ApiDoc = (options) => {
return (target, key, descriptor) => {
Reflect.defineMetadata(METHOD_DOC_METADATA, options, descriptor.value);
};
};
export const useMapRoute = (handlers) => {
const all = new Map();
for (let i = 0; i < handlers.length; i++) {
console.log(handlers[i]);
const handler = new handlers[i]();
const prototype = Object.getPrototypeOf(handler);
// 取出路径
const handlerPathMeta = Reflect.getMetadata(PATH_METADATA, prototype.constructor);
const methods = Object.getOwnPropertyNames(prototype).filter(item => {
// 构造函数,暂时不处理
if (item === 'constructor') {
return false;
}
return typeof Reflect.get(prototype, item) === 'function';
});
methods.forEach(methodName => {
const method = Reflect.get(prototype, methodName);
const methodPath = Reflect.getMetadata(METHOD_METADATA, method);
if (!methodPath) {
return;
}
const path = handlerPathMeta + '/' + methodPath;
all.set(path, {
method: method.bind(handler),
validate: Reflect.getMetadata(PARAM_VALIDATE_METADATA, method),
doc: Reflect.getMetadata(METHOD_DOC_METADATA, method),
});
});
}
return all;
};
/**
* 异常抛出基类
*/
export class ResponseError extends Error {
code;
constructor(msg, code = 500) {
super(msg);
this.code = code;
}
}
const runHandler = async (handler, ctx) => {
if (handler.validate) {
const errors = await validate(plainToInstance(handler.validate, ctx.request.data));
if (errors.length > 0) {
console.error(errors);
ctx.response.data = {
code: 500,
msg: Object.values(errors[0].constraints)[0]
};
}
return;
}
try {
const res = await handler.method(ctx.request.data, ctx.socket);
if (res) {
ctx.response.data = {
data: res
};
}
}
catch (e) {
if (e instanceof ResponseError) {
ctx.response.data = {
code: e.code,
msg: e.message
};
}
else {
console.error(e);
}
}
};
/**
* 自动导入应用文件
* @param baseDir 应用文件夹
* @param rule 导入路径规则
* @returns
*/
export const include = async (baseDir, rule = '**/*.handler.ts') => {
// const dirs = await readdir(path);
// const files = await findUp(path + '/' + rule);
// console.log(files);
console.log(path.resolve(baseDir, rule));
const handlerPaths = await glob(path.resolve(baseDir, rule));
const handlers = [];
for (let i = 0; i < handlerPaths.length; i++) {
const handler = await import(handlerPaths[i]);
if (handler.default) {
handlers.push(handler.default);
}
}
const mapRoute = useMapRoute(handlers);
return async (ctx, next) => {
const handler = mapRoute.get(ctx.request.url);
if (handler) {
await runHandler(handler, ctx);
}
next();
};
};

0 comments on commit 560c0f2

Please sign in to comment.