EdwardZZZ / articles

工作点滴记录
2 stars 0 forks source link

router 处理/ core 重新组装 #60

Open EdwardZZZ opened 4 years ago

EdwardZZZ commented 4 years ago
移除文件限制,都依靠类
中间件提取

router 处理,参数不用单独处理,中间件转为Around 无差异

事件顺序,处理逻辑由 router 控制

中间件装饰器调用方便

~~service 层移出 ctx ,不能被其它层使用~~

controller 和 service 不采用装饰器方式声明,因为装饰器继承不如显性继承使用方便及明显

去掉纯中间件支持
中间件装饰器改为切面 Aspect.around 支持,原因有:1、中间件不能处理参数、2、中间件不能处理返回结果、3、中间件和 around 功能类似
切面可以对参数进行修改、判断、注入等操作
还可以对返回值进行修改、校验、统一封装等操作

扩展方式,context,app,frame、methodType、init
interface 考虑改为 abstract class

post 修饰自带 koa-body

动态加载插件

加载优化 resource 加载默认文件夹以外的文件

~~依赖注入错误处理~~

reload 提升dev效率(frame 加入watch )
```js
if (process.env.Env) this.watch

watch() {
    if (path === '${}/serive') loadService();
    if (path === '${}/controller') loadController();
    ……
}

// ['aspect', 'config', 'controller', 'i18n', 'logger', 'middleware', 'plugin', 'service']

EdwardZZZ commented 4 years ago

query 实体化处理

createQueryDecorator

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(() => {
    。。。。
})
EdwardZZZ commented 4 years ago

加载器 所有的资源都可以用单一加载器加载,分拆的原因 热加载实现原因及实现思路

插件系统 插件设计理念 插件使用方式 纯中间件形式 复合形式(ctx, req, res, use, filter, ignore, method) 插件实现思路

依赖注入 控制反转场景 依赖注入优势及实现思路 service、resource 设计及实现区别,设计原因/理念

面向切面 设计理念,实现思路 中间件与 around 的异同点 中间件的复用 中间件快捷装饰器试用 中间件采用切面方式使用的好处

控制层 控制层设计 统一返回的好处 路由设计 默认路由设计 路由匹配规则

装饰器 依赖注入装饰器 面向切面装饰器 路由装饰器 自定义装饰器设计 自定义装饰器扩展 参数校验 参数转换 参数聚合 便捷引用

EdwardZZZ commented 3 years ago
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);
EdwardZZZ commented 3 years ago

/**
  * @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,
}