1
- import { Action } from "../../Action" ;
2
1
import { MethodMetadata } from "../../metadata/MethodMetadata" ;
3
- import { BaseDriver } from "../BaseDriver " ;
2
+ import { Action } from "../../Action " ;
4
3
import { ParamMetadata } from "../../metadata/ParamMetadata" ;
5
- import { error } from "util" ;
4
+ import { BaseDriver } from "../BaseDriver" ;
5
+ import { MethodNotAllowedError } from "../../http-error/MethodNotAllowedError" ;
6
+ import { MethodNotFoundError } from "../../rpc-error/MethodNotFoundError" ;
7
+ import { ParseError } from "../../rpc-error/ParseError" ;
8
+ import { InvalidRequestError } from "../../rpc-error/InvalidRequestError" ;
6
9
7
10
/**
8
11
* Integration with koa framework.
@@ -21,11 +24,15 @@ export class KoaDriver extends BaseDriver {
21
24
*/
22
25
initialize ( ) {
23
26
const bodyParser = require ( "koa-bodyparser" ) ;
24
- this . koa . use ( bodyParser ( ) ) ;
27
+ this . koa . use ( bodyParser ( {
28
+ enableTypes : [ "json" ]
29
+ } ) ) ;
25
30
if ( this . cors ) {
26
31
const cors = require ( "kcors" ) ;
27
32
if ( this . cors === true ) {
28
- this . koa . use ( cors ( ) ) ;
33
+ this . koa . use ( cors ( {
34
+ origin : "POST"
35
+ } ) ) ;
29
36
} else {
30
37
this . koa . use ( cors ( this . cors ) ) ;
31
38
}
@@ -35,19 +42,45 @@ export class KoaDriver extends BaseDriver {
35
42
/**
36
43
* Registers action in the driver.
37
44
*/
38
- registerMethod ( methods : MethodMetadata [ ] , executeCallback : ( methodMetadata : MethodMetadata , action : Action , error : any ) => any ) : void {
45
+ registerMethod ( methods : MethodMetadata [ ] , executeCallback : ( error : any , action : Action , method ?: MethodMetadata ) => any ) : void {
39
46
40
47
// prepare route and route handler function
41
48
const route = this . routePrefix + "*" ;
49
+ const errorHandler = async ( request : any , next : Function ) => {
50
+ try {
51
+ await next ( ) ;
52
+ } catch ( e ) {
53
+ console . log ( e ) ;
54
+ }
55
+ } ;
42
56
const routeHandler = ( context : any , next : ( ) => Promise < any > ) => {
43
57
const action : Action = { request : context . request , response : context . response , context, next} ;
44
- const method = methods . find ( method => method . fullName === action . request . method ) ;
45
- return executeCallback ( method , action , error ) ;
58
+ const method : MethodMetadata = methods . find ( ( methodMetadata ) => methodMetadata . fullName === action . request . body . method ) ;
59
+ try {
60
+ if ( action . request . method . toLowerCase ( ) !== "post" ) {
61
+
62
+ return next ( ) ;
63
+ } else if ( ! action . request . body || typeof action . request . body !== "object" ) {
64
+
65
+ return executeCallback ( new ParseError ( ) , action , method ) ;
66
+ } else if ( ! action . request . body . params ) {
67
+
68
+ return executeCallback ( new InvalidRequestError ( ) , action , method ) ;
69
+ } else if ( ! action . request . body . method || ! method ) {
70
+
71
+ return executeCallback ( new MethodNotFoundError ( ) , action , method ) ;
72
+ }
73
+
74
+ return executeCallback ( null , action , method ) ;
75
+ } catch ( e ) {
76
+ return executeCallback ( new ParseError ( ) , action , method ) ;
77
+ }
46
78
} ;
47
79
48
80
// finally register action in koa
49
- this . router . use ( ...[
81
+ this . router . all ( ...[
50
82
route ,
83
+ errorHandler ,
51
84
routeHandler ,
52
85
] ) ;
53
86
}
@@ -107,24 +140,38 @@ export class KoaDriver extends BaseDriver {
107
140
action . response . set ( name , method . headers [ name ] ) ;
108
141
} ) ;
109
142
110
- return action . next ( ) ;
143
+ if ( result === undefined ) { // throw NotFoundError on undefined response
144
+ // todo send error
145
+
146
+ } else if ( result === null ) { // send null response
147
+ // todo send null response
148
+ action . next ( ) ;
149
+ } else { // send regular result
150
+ action . response . body = {
151
+ jsonrpc : "2.0" ,
152
+ id : action . request . body . id ,
153
+ result : result
154
+ } ;
155
+ action . next ( ) ;
156
+ }
111
157
}
112
158
113
159
/**
114
160
* Handles result of failed executed controller action.
115
161
*/
116
162
handleError ( error : any , action : Action ) {
117
163
return new Promise ( ( resolve , reject ) => {
118
- if ( this . isDefaultErrorHandlingEnabled ) {
164
+ if ( true ) {
119
165
120
166
// send error content
167
+ console . log ( "aasf" ) ;
121
168
action . response . body = this . processJsonError ( error ) ;
122
169
123
170
// todo set http status
124
171
125
172
return resolve ( ) ;
126
173
}
127
- return reject ( error ) ;
174
+ // return reject(error);
128
175
} ) ;
129
176
}
130
177
@@ -162,4 +209,4 @@ export class KoaDriver extends BaseDriver {
162
209
}
163
210
}
164
211
165
- }
212
+ }
0 commit comments