frontend-recharge-system-vue/mock/requestDecorator.ts

55 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'reflect-metadata'
import { ROUTER_MAP ,BASE_PATH_MAP} from './constant'
/**
* @desc 生成 http method 装饰器
* @param {string} method - http method如 get、post、head
* @return Decorator - 装饰器
*/
function createMethodDecorator(method: string) {
// 装饰器接收路由 path 作为参数
return function httpMethodDecorator(path: string) {
return (proto: any, name: string) => {
const target = proto.constructor;
const routeMap = Reflect.getMetadata(ROUTER_MAP, target, 'method') || [];
routeMap.push({ name, method, path });
Reflect.defineMetadata(ROUTER_MAP, routeMap, target, 'method');
};
};
}
/**
* 创建类路径装饰器
* @param className
*/
function createClassDecorator() {
// 装饰器接收路由 path 作为参数
return function httpMethodDecorator(basePath: string):ClassDecorator {
return (proto: any) => {
const target = proto;
const pathMap = Reflect.getMetadata(BASE_PATH_MAP, target) || [];
pathMap.push({path:basePath});
Reflect.defineMetadata(BASE_PATH_MAP, pathMap, target);
};
};
}
// 路径前缀
export const prefix = createClassDecorator()
// 导出 http method 装饰器
export const post = createMethodDecorator('post');
export const get = createMethodDecorator('get');
export const del = createMethodDecorator('del');
export const put = createMethodDecorator('put');
export const patch = createMethodDecorator('patch');
export const options = createMethodDecorator('options');
export const head = createMethodDecorator('head');
export const all = createMethodDecorator('all');