Skip to content

Commit ed5c616

Browse files
committed
chore: refact
1 parent 3dff5b6 commit ed5c616

File tree

7 files changed

+89
-280
lines changed

7 files changed

+89
-280
lines changed

packages/core2/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Demo from './demo';
2-
import Fn from './src/fn';
2+
import { Fn } from './src/fn';
33
import RpcServer from './src/server';
44

55
const rpc = new RpcServer({});

packages/core2/src/__tests__/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import request from 'supertest';
22
import { describe, expect, it } from 'vitest';
33

4-
import { lib } from '..';
4+
// import { lib } from '..';
55

66
describe('lib', () => {
77
it('should render lib', () => {
8-
expect(lib()).toBe('lib');
8+
expect('lib').toBe('lib');
99
});
1010
});
1111

packages/core2/src/base.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export default class Plugable implements Strategy {
4949
this.load.push(this.getMiddleware());
5050
}
5151

52+
getConfig(ctx) {
53+
return ctx[this.name];
54+
}
55+
5256
proxy() {
5357
return async (ctx, next) => {
5458
console.dir('proxy prefix=' + this.prefix);
@@ -65,11 +69,11 @@ export default class Plugable implements Strategy {
6569
}
6670

6771
getMiddleware() {
68-
console.dir('getMiddleware');
72+
// console.dir('getMiddleware');
6973
const pre = this.pre();
7074
const process = this.process();
71-
const after = this.after();
72-
return compose([pre, process, after]);
75+
const post = this.post();
76+
return compose([pre, process, post]);
7377
}
7478

7579
pre() {
@@ -79,7 +83,7 @@ export default class Plugable implements Strategy {
7983
console.dir('pre end');
8084
};
8185
}
82-
after() {
86+
post() {
8387
return async (ctx, next) => {
8488
console.dir('after');
8589
await next();

packages/core2/src/core.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

packages/core2/src/fn.ts

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import debug from 'debug';
22

33
import Plugable from './base';
4-
import { mountMiddleware } from './core';
54
import { isArrowFunction, getHttpMethods } from './utils';
65
const log = debug('@tomrpc/core');
76

8-
export default class Fn extends Plugable {
7+
export class Fn extends Plugable {
98
// public name: string;
109

1110
constructor(cfg?: any) {
@@ -15,38 +14,69 @@ export default class Fn extends Plugable {
1514
this.prefix = '/api';
1615
}
1716
fn(key, fn) {
18-
console.dir('=this.config=');
19-
console.dir(this.config);
17+
// console.dir('=this.config=');
18+
// console.dir(this.config);
2019
if (!this.config['functions']) this.config['functions'] = {};
2120
this.config['functions'][key] = fn;
22-
// if (Object.entries(items)) {
23-
// // for (const [name, fn] of Object.entries(items)) {
24-
// // if (isArrowFunction(fn)) {
25-
// // console.log(
26-
// // `this.rpcFunctions[${name}] is arrow function, please use ctx as param, not this`
27-
// // );
28-
// // }
29-
// // if (this.rpcFunctions[name]) {
30-
// // log(`add ${name}: ${fn}`);
31-
// // console.log(`this.rpcFunctions[${name}] exisit`);
32-
// // }
33-
// // this.rpcFunctions[name] = fn;
34-
// // }
35-
// } else {
36-
// this.config['functions'].push(items);
37-
// }
21+
}
22+
add(items) {
23+
for (const [name, fn] of Object.entries(items)) {
24+
if (isArrowFunction(fn)) {
25+
console.log(
26+
`this.rpcFunctions[${name}] is arrow function, please use ctx as param, not this`
27+
);
28+
}
29+
if (this.config['functions'][name]) {
30+
log(`add ${name}: ${fn}`);
31+
console.log(`this.rpcFunctions[${name}] exisit`);
32+
}
33+
// this.rpcFunctions[name] = fn;
34+
this.config['functions'][name] = fn;
35+
}
3836
}
3937

4038
process() {
41-
console.dir("this.config['functions']");
42-
console.dir(this);
43-
console.dir(this.serverConfig);
44-
return mountMiddleware(this);
39+
console.dir('process');
40+
return this.mount();
4541
}
4642

43+
mount() {
44+
return async (ctx, next) => {
45+
const prefix = this.prefix;
46+
const routers = this.config['functions'];
47+
log(routers);
48+
const path = ctx.path.replace(prefix, '');
49+
log('mountMiddleware' + ctx.path);
50+
const key = '/' + path.replace('/', '').split('/').join('.');
51+
52+
if (!['POST', 'PUT', 'PATCH'].includes(ctx.method) && !ctx.query.$p) {
53+
console.log('not match $p param, no process');
54+
ctx.body = 'not match $p param, no process';
55+
} else {
56+
const param = ctx.method === 'POST' ? ctx.request.body : JSON.parse(ctx.query.$p);
57+
58+
log(key);
59+
log(param);
60+
log(routers[key]);
61+
62+
if (routers[key]) {
63+
const args = [...param, ctx];
64+
// console.dir(args);
65+
const result = routers[key].apply(ctx, args);
66+
// console.dir(result);
67+
ctx.body = result;
68+
} else {
69+
const msg = JSON.stringify(ctx, null, 4);
70+
ctx.body = ` not match path ${ctx.path} \n ctx = ${msg}`;
71+
await next();
72+
}
73+
//
74+
}
75+
};
76+
}
4777
pre() {
4878
return async (ctx, next) => {
49-
console.log('beforeOne');
79+
// console.log('pre');
5080
const key = ctx.path.replace('/', '').split('/').join('.');
5181
// this.config.beforeOne(ctx, key);
5282

@@ -56,15 +86,15 @@ export default class Fn extends Plugable {
5686
const supportMethods = [];
5787
httpMethods.forEach(function (m) {
5888
if (lastKey.indexOf(m) != -1) {
59-
console.log(m);
89+
// console.log(m);
6090
supportMethods.push(m);
6191
return m;
6292
}
6393
});
6494
// console.log(supportMethods);
6595

6696
if (supportMethods.length === 0) {
67-
console.log('没有匹配到包含get/post等方法的函数');
97+
console.log(ctx.path + ',没有匹配到包含get/post等开头的函数');
6898
await next();
6999
} else if (ctx.method === supportMethods[0]) {
70100
log('匹配到包含get/post等方法的函数');

0 commit comments

Comments
 (0)