Open EdwardZZZ opened 4 years ago
query 实体化处理
function createQueryDecorator(fn: (data: string, ctx: any) => any): ((paramKey?: string) => (target: any, methodName: string, paramIndex: number) => any) {
return (paramKey: string) => (target: any, methodName: string, paramIndex: number) => {
}
}
export const User = createQueryDecorator((str, ctx) => {
return ctx.query.user;
});
submit(@User('user') user: UserEntity) {
console.log(user);
}
参数转换,参数校验
const Config = createQueryDecorator(() => {
。。。。
})
const Util = createQueryDecorator(() => {
。。。。
})
加载器 所有的资源都可以用单一加载器加载,分拆的原因 热加载实现原因及实现思路
插件系统 插件设计理念 插件使用方式 纯中间件形式 复合形式(ctx, req, res, use, filter, ignore, method) 插件实现思路
依赖注入 控制反转场景 依赖注入优势及实现思路 service、resource 设计及实现区别,设计原因/理念
面向切面 设计理念,实现思路 中间件与 around 的异同点 中间件的复用 中间件快捷装饰器试用 中间件采用切面方式使用的好处
控制层 控制层设计 统一返回的好处 路由设计 默认路由设计 路由匹配规则
装饰器 依赖注入装饰器 面向切面装饰器 路由装饰器 自定义装饰器设计 自定义装饰器扩展 参数校验 参数转换 参数聚合 便捷引用
const Koa = require('koa');
const app = new Koa();
const mws = [...Array(10)].map((_, i) => async (ctx, next) => {
console.log(`${i} before`);
await next();
console.log(`${i} after`);
});;
const compose = (mws) => (ctx, next) => {
return (function dispatch(n) {
return (mws[n] || next)(ctx, () => dispatch(++n));
})(0);
}
/*
function compose(mws) {
return mws.reduceRight((prev, curr) => {
return (ctx, next) => curr(ctx, () => prev(ctx, next));
}, (_, next) => next());
}
*/
app.use(compose(mws));
app.use((ctx) => {
console.log('-------');
ctx.body = '200';
})
app.listen(8058);
/**
* @param {string} [options.env='development'] Environment
* @param {string[]} [options.keys] Signed cookie keys
* @param {boolean} [options.proxy] Trust proxy headers
* @param {number} [options.subdomainOffset] Subdomain offset
* @param {boolean} [options.proxyIpHeader] proxy ip header, default to X-Forwarded-For
* @param {boolean} [options.maxIpsCount] max ips read from proxy ip header, default to 0 (means infinity)
*/
export type TKoaOption = {
env: string,
keys: string[],
proxy: boolean,
subdomainOffset: number,
proxyIpHeader: boolean,
maxIpsCount: boolean,
}
// ['aspect', 'config', 'controller', 'i18n', 'logger', 'middleware', 'plugin', 'service']