From 84a9529679a421b3d01ccf48a5fe7baa37a5f02f Mon Sep 17 00:00:00 2001 From: npmstudy Date: Wed, 1 Nov 2023 12:03:44 +0800 Subject: [PATCH] chore: refact --- packages/core2/app.ts | 4 ++-- packages/core2/demo.ts | 4 ++-- packages/core2/src/base.ts | 5 ++++- packages/core2/src/fn.ts | 8 ++++++-- packages/core2/src/server.ts | 5 ++++- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/core2/app.ts b/packages/core2/app.ts index e74fc49..81f999b 100644 --- a/packages/core2/app.ts +++ b/packages/core2/app.ts @@ -4,8 +4,8 @@ import RpcServer from './src/server'; const rpc = new RpcServer({}); -const fn = new Fn(); -const demo = new Demo(); +const fn = new Fn({}); +const demo = new Demo({}); rpc.plugin(fn); rpc.plugin(demo); diff --git a/packages/core2/demo.ts b/packages/core2/demo.ts index 3404413..79cec74 100644 --- a/packages/core2/demo.ts +++ b/packages/core2/demo.ts @@ -2,8 +2,8 @@ import Plugable from './src/base'; export default class Fn extends Plugable { // public name: string; - constructor() { - super(); + constructor(cfg?) { + super(cfg); this.prefix = '/demo'; // this.name = 'fn'; diff --git a/packages/core2/src/base.ts b/packages/core2/src/base.ts index 4e8dfe9..236bed4 100644 --- a/packages/core2/src/base.ts +++ b/packages/core2/src/base.ts @@ -16,6 +16,7 @@ export interface Strategy { app; compose; proxy; + config; server; serverConfig; } @@ -29,8 +30,10 @@ export default class Plugable implements Strategy { public serverConfig; public prefix; public compose; + public config; - constructor() { + constructor(cfg?) { + this.config = Object.assign({}, cfg); this.name = 'base'; this.app = new Koa(); this.init = []; diff --git a/packages/core2/src/fn.ts b/packages/core2/src/fn.ts index 81216ff..cf50ab5 100644 --- a/packages/core2/src/fn.ts +++ b/packages/core2/src/fn.ts @@ -2,8 +2,8 @@ import Plugable from './base'; export default class Fn extends Plugable { // public name: string; - constructor() { - super(); + constructor(cfg?: any) { + super(cfg); this.prefix = '/api'; // this.name = 'fn'; @@ -16,6 +16,10 @@ export default class Fn extends Plugable { const c = this.c(); // this.addLoad(c); + this.server['fn'].add = function (i) { + if (!this.config['functions']) this.config['functions'] = []; + this.config['functions'].push(i); + }; } process() { return async (ctx, next) => { diff --git a/packages/core2/src/server.ts b/packages/core2/src/server.ts index 5d16dd5..3f0103f 100644 --- a/packages/core2/src/server.ts +++ b/packages/core2/src/server.ts @@ -117,7 +117,10 @@ export default class RpcServer { plugin.serverConfig = this.config; // set config namespace - if (plugin.name !== 'base') this.config.plugin[plugin.name] = {}; + if (plugin.name !== 'base') { + console.error('plugin name 没有修改,可能会出现serverConfig获取问题,请关注'); + } + this.config.plugin[plugin.name] = plugin.config; if (plugin.init.length > 0) this.config.hooks.init.push(...plugin.init);