Skip to content

Commit a9291ee

Browse files
Add in express to polyfill listen and body parser
1 parent 9b93465 commit a9291ee

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Route.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ export default class Route implements IMountableItem {
229229

230230
asExpressRoute() {
231231
return async (req: express.Request, res: express.Response) => {
232-
const { query, params } = req;
232+
const { query, params, body } = req;
233233

234-
const providedVariables = { ...query, ...params };
234+
const providedVariables = { ...query, ...params, ...body };
235235

236236
// Assemble variables from query, path and default values
237237
const assembledVariables = this.assembleVariables(providedVariables);
@@ -247,11 +247,12 @@ export default class Route implements IMountableItem {
247247
return;
248248
}
249249

250-
const { statusCode, body } = await this.makeRequest(assembledVariables, headers);
250+
const { statusCode, body: responseBody } =
251+
await this.makeRequest(assembledVariables, headers);
251252

252253
res
253254
.status(statusCode)
254-
.json(body);
255+
.json(responseBody);
255256
};
256257
}
257258

Router.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '.';
77
import Route from './Route';
88
import express from 'express';
9+
import bodyParser from 'body-parser';
910
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
1011
import { parse, DocumentNode, getOperationAST } from 'graphql';
1112
import { version } from './package.json';
@@ -124,13 +125,20 @@ export default class Router {
124125
return mountedItem;
125126
}
126127

128+
// TODO: Temporarily using express as metal
127129
listen(port: number, callback?: () => void) {
128-
throw new Error('Not Implemented');
130+
const router = express();
131+
132+
router.use(this.asExpressRouter());
133+
134+
router.listen(port, callback);
129135
}
130136

131137
asExpressRouter() {
132138
const router: any = express.Router();
133139

140+
router.use(bodyParser.json());
141+
134142
[...this.modules, ...this.routes].forEach((route) => {
135143
const { path, httpMethod } = route;
136144
const routeFn = route.asExpressRoute();

0 commit comments

Comments
 (0)