Skip to content

Commit c7e1732

Browse files
Merge pull request #1267 from opencomponents/add-express-promise-router
[CB-INTERNAL] add express-promise-router to allow promises on routes
2 parents a134c55 + 53dfd69 commit c7e1732

File tree

6 files changed

+68
-19
lines changed

6 files changed

+68
-19
lines changed

package-lock.json

Lines changed: 48 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"dependency-graph": "0.11.0",
102102
"errorhandler": "1.5.1",
103103
"express": "4.17.1",
104+
"express-promise-router": "^4.1.0",
104105
"form-data": "4.0.0",
105106
"fs-extra": "10.0.0",
106107
"getport": "0.1.0",

src/registry/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as eventsHandler from './domain/events-handler';
99
import * as middleware from './middleware';
1010
import * as pluginsInitialiser from './domain/plugins-initialiser';
1111
import Repository from './domain/repository';
12-
import * as router from './router';
12+
import { create as createRouter } from './router';
1313
import sanitiseOptions from './domain/options-sanitiser';
1414
import * as validator from './domain/validators';
1515
import { ComponentsList, Config, Plugin } from '../types';
@@ -20,15 +20,16 @@ interface Input extends Partial<Omit<Config, 'beforePublish'>> {
2020
}
2121

2222
export default function registry(inputOptions: Input) {
23-
const validationResult =
24-
validator.validateRegistryConfiguration(inputOptions);
23+
const validationResult = validator.validateRegistryConfiguration(
24+
inputOptions
25+
);
2526
if (!validationResult.isValid) {
2627
throw validationResult.message;
2728
}
2829
const options = sanitiseOptions(inputOptions);
2930

3031
const plugins: Plugin[] = [];
31-
const app = middleware.bind(express(), options);
32+
const { app, router } = middleware.bind(express(), options);
3233
let server: http.Server;
3334
const repository = Repository(options);
3435

@@ -54,7 +55,7 @@ export default function registry(inputOptions: Input) {
5455
if (typeof callback !== 'function') {
5556
callback = _.noop;
5657
}
57-
router.create(app, options, repository);
58+
createRouter(router, options, repository);
5859
async.waterfall(
5960
[
6061
(cb: Callback<Dictionary<(...args: unknown[]) => unknown>, unknown>) =>

src/registry/middleware/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express, { Express } from 'express';
22
import errorhandler from 'errorhandler';
33
import morgan from 'morgan';
4+
import Router from 'express-promise-router';
45

56
import baseUrlHandler from './base-url-handler';
67
import cors from './cors';
@@ -16,9 +17,13 @@ const bodyParserUrlEncodedArgument: { extended: boolean; limit?: number } = {
1617
extended: true
1718
};
1819

19-
export const bind = (app: Express, options: Config): Express => {
20+
export const bind = (
21+
app: Express,
22+
options: Config
23+
): { app: Express; router: express.Router } => {
2024
app.set('port', options.port);
2125
app.set('json spaces', 0);
26+
app.set('etag', 'strong');
2227

2328
app.use((_req, res, next) => {
2429
res.conf = options;
@@ -48,5 +53,8 @@ export const bind = (app: Express, options: Config): Express => {
4853
app.use(errorhandler());
4954
}
5055

51-
return app;
56+
const router = Router();
57+
app.use(router);
58+
59+
return { app, router };
5260
};

src/registry/router.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ import StaticRedirectorRoute from './routes/static-redirector';
88
import PluginsRoute from './routes/plugins';
99
import DependenciesRoute from './routes/dependencies';
1010
import settings from '../resources/settings';
11-
import { Express } from 'express';
11+
import { Router } from 'express';
1212
import { Config, Repository } from '../types';
1313

14-
export function create(
15-
app: Express,
16-
conf: Config,
17-
repository: Repository
18-
): Express {
14+
export function create(app: Router, conf: Config, repository: Repository) {
1915
const routes = {
2016
component: ComponentRoute(conf, repository),
2117
components: ComponentsRoute(conf, repository),
@@ -85,8 +81,4 @@ export function create(
8581
](route.route, route.handler)
8682
);
8783
}
88-
89-
app.set('etag', 'strong');
90-
91-
return app;
9284
}

test/unit/registry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('registry', () => {
1414
http: {
1515
createServer: sinon.stub()
1616
},
17-
'./middleware': { bind: sinon.stub() },
17+
'./middleware': { bind: sinon.stub().returns({}) },
1818
'./domain/plugins-initialiser': { init: sinon.stub() },
1919
'./domain/repository': sinon.stub().returns({
2020
init: repositoryInitStub

0 commit comments

Comments
 (0)