1
1
import debug from 'debug' ;
2
2
3
3
import Plugable from './base' ;
4
- import { mountMiddleware } from './core' ;
5
4
import { isArrowFunction , getHttpMethods } from './utils' ;
6
5
const log = debug ( '@tomrpc/core' ) ;
7
6
8
- export default class Fn extends Plugable {
7
+ export class Fn extends Plugable {
9
8
// public name: string;
10
9
11
10
constructor ( cfg ?: any ) {
@@ -15,38 +14,69 @@ export default class Fn extends Plugable {
15
14
this . prefix = '/api' ;
16
15
}
17
16
fn ( key , fn ) {
18
- console . dir ( '=this.config=' ) ;
19
- console . dir ( this . config ) ;
17
+ // console.dir('=this.config=');
18
+ // console.dir(this.config);
20
19
if ( ! this . config [ 'functions' ] ) this . config [ 'functions' ] = { } ;
21
20
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
+ }
38
36
}
39
37
40
38
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 ( ) ;
45
41
}
46
42
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
+ }
47
77
pre ( ) {
48
78
return async ( ctx , next ) => {
49
- console . log ( 'beforeOne ' ) ;
79
+ // console.log('pre ');
50
80
const key = ctx . path . replace ( '/' , '' ) . split ( '/' ) . join ( '.' ) ;
51
81
// this.config.beforeOne(ctx, key);
52
82
@@ -56,15 +86,15 @@ export default class Fn extends Plugable {
56
86
const supportMethods = [ ] ;
57
87
httpMethods . forEach ( function ( m ) {
58
88
if ( lastKey . indexOf ( m ) != - 1 ) {
59
- console . log ( m ) ;
89
+ // console.log(m);
60
90
supportMethods . push ( m ) ;
61
91
return m ;
62
92
}
63
93
} ) ;
64
94
// console.log(supportMethods);
65
95
66
96
if ( supportMethods . length === 0 ) {
67
- console . log ( ' 没有匹配到包含get/post等方法的函数 ') ;
97
+ console . log ( ctx . path + ', 没有匹配到包含get/post等开头的函数 ') ;
68
98
await next ( ) ;
69
99
} else if ( ctx . method === supportMethods [ 0 ] ) {
70
100
log ( '匹配到包含get/post等方法的函数' ) ;
0 commit comments