Skip to content

Commit 0e24c78

Browse files
committed
update docs, koa driver changes
1 parent b248396 commit 0e24c78

File tree

6 files changed

+184
-19
lines changed

6 files changed

+184
-19
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
build/
55
node_modules/
66
npm-debug.log
7+
coverage.lcov
8+
.nyc_output/

Diff for: .travis.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: node_js
22
node_js:
3-
- stable
4-
- 8
5-
- 6
3+
- stable
4+
- 8
5+
- 6
66

77
after_success:
8-
- bash <(curl -s https://codecov.io/bash)
8+
- bash <(curl -s https://codecov.io/bash)

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# rpc-controllers
22

33
[![Build Status](https://travis-ci.org/rustamwin/rpc-controllers.svg?branch=master)](https://travis-ci.org/rustamwin/rpc-controllers)
4-
[![codecov](https://codecov.io/gh/rustamwin/rpc-controllers/branch/master/graph/badge.svg)](https://codecov.io/gh/rustamwin/rpc-controllers)
54
[![npm version](https://badge.fury.io/js/rpc-controllers.svg)](https://badge.fury.io/js/rpc-controllers)
65
[![Dependency Status](https://david-dm.org/rustamwin/rpc-controllers.svg)](https://david-dm.org/rustamwin/rpc-controllers)
76

@@ -136,6 +135,8 @@ useExpressServer(app, { // register created express server in rpc-controllers
136135
app.listen(3000); // run your express server
137136
```
138137

138+
> Note: Koa driver is experimental
139+
139140
#### Using DI container
140141

141142
`rpc-controllers` supports a DI container out of the box.

Diff for: package-lock.json

+112-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"chai": "^4.2.0",
4646
"chai-as-promised": "^7.1.1",
4747
"chakram": "^1.5.0",
48+
"cors": "^2.8.5",
4849
"del": "^3.0.0",
4950
"express": "^4.16.4",
5051
"gulp": "^3.9.1",
@@ -57,7 +58,10 @@
5758
"gulp-typescript": "^5.0.0",
5859
"gulpclass": "^0.1.2",
5960
"jayson": "^2.1.1",
61+
"kcors": "^2.2.2",
6062
"koa": "^2.6.2",
63+
"koa-bodyparser": "^4.2.1",
64+
"koa-router": "^7.4.0",
6165
"mocha": "^5.2.0",
6266
"ts-node": "^7.0.1",
6367
"tslint": "^5.12.1",

Diff for: src/driver/koa/KoaDriver.ts

+60-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import {Action} from "../../Action";
21
import {MethodMetadata} from "../../metadata/MethodMetadata";
3-
import {BaseDriver} from "../BaseDriver";
2+
import {Action} from "../../Action";
43
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";
69

710
/**
811
* Integration with koa framework.
@@ -21,11 +24,15 @@ export class KoaDriver extends BaseDriver {
2124
*/
2225
initialize() {
2326
const bodyParser = require("koa-bodyparser");
24-
this.koa.use(bodyParser());
27+
this.koa.use(bodyParser({
28+
enableTypes: ["json"]
29+
}));
2530
if (this.cors) {
2631
const cors = require("kcors");
2732
if (this.cors === true) {
28-
this.koa.use(cors());
33+
this.koa.use(cors({
34+
origin: "POST"
35+
}));
2936
} else {
3037
this.koa.use(cors(this.cors));
3138
}
@@ -35,19 +42,45 @@ export class KoaDriver extends BaseDriver {
3542
/**
3643
* Registers action in the driver.
3744
*/
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 {
3946

4047
// prepare route and route handler function
4148
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+
};
4256
const routeHandler = (context: any, next: () => Promise<any>) => {
4357
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+
}
4678
};
4779

4880
// finally register action in koa
49-
this.router.use(...[
81+
this.router.all(...[
5082
route,
83+
errorHandler,
5184
routeHandler,
5285
]);
5386
}
@@ -107,24 +140,38 @@ export class KoaDriver extends BaseDriver {
107140
action.response.set(name, method.headers[name]);
108141
});
109142

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+
}
111157
}
112158

113159
/**
114160
* Handles result of failed executed controller action.
115161
*/
116162
handleError(error: any, action: Action) {
117163
return new Promise((resolve, reject) => {
118-
if (this.isDefaultErrorHandlingEnabled) {
164+
if (true) {
119165

120166
// send error content
167+
console.log("aasf");
121168
action.response.body = this.processJsonError(error);
122169

123170
// todo set http status
124171

125172
return resolve();
126173
}
127-
return reject(error);
174+
// return reject(error);
128175
});
129176
}
130177

@@ -162,4 +209,4 @@ export class KoaDriver extends BaseDriver {
162209
}
163210
}
164211

165-
}
212+
}

0 commit comments

Comments
 (0)