Skip to content

Commit 378fd2c

Browse files
committed
feat(middleware): Improve support for middleware
1 parent a2e1c21 commit 378fd2c

File tree

4 files changed

+76
-37
lines changed

4 files changed

+76
-37
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"license": "MIT",
77
"repository": "git+https://github.com/jakehamilton/leverage-plugin-http",
88
"main": "dist/lib.js",
9-
"browser": "dist/lib.browser.js",
109
"types": "dist/lib.d.ts",
1110
"scripts": {
1211
"commit": "git-cz",

src/__mocks__/http.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const instance = {
2+
listen: jest.fn(),
3+
};
4+
5+
function Server () {
6+
return instance;
7+
}
8+
9+
module.exports = {
10+
instance,
11+
Server,
12+
};

src/__tests__/index.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import HTTP, { HTTPComponent, HTTPMiddleware } from '..';
2-
import * as express from '../__mocks__/express';
1+
import HTTP, { HTTPComponent, HTTPMiddleware, HTTPComponentInstance } from '..';
2+
import * as httpMock from '../__mocks__/http';
3+
import * as expressMock from '../__mocks__/express';
34

5+
jest.mock('http');
46
jest.mock('express');
57

6-
const { instance } = express;
8+
const { instance: httpInstance } = httpMock;
9+
const { instance: expressInstance } = expressMock;
710

811
describe('HTTP', () => {
912
beforeEach(() => {
10-
instance.get.mockReset();
11-
instance.put.mockReset();
12-
instance.post.mockReset();
13-
instance.delete.mockReset();
13+
expressInstance.get.mockReset();
14+
expressInstance.put.mockReset();
15+
expressInstance.post.mockReset();
16+
expressInstance.delete.mockReset();
1417
});
1518

1619
describe('#constructor', () => {
@@ -27,7 +30,7 @@ describe('HTTP', () => {
2730

2831
expect(http.app).not.toBeUndefined();
2932

30-
expect(http.app).toBe(instance);
33+
expect(http.app).toBe(expressInstance);
3134
}).not.toThrow();
3235
});
3336
});
@@ -36,7 +39,7 @@ describe('HTTP', () => {
3639
test('installs http components where path is a string', () => {
3740
const http = new HTTP();
3841

39-
const component: HTTPComponent = {
42+
const component: HTTPComponentInstance = {
4043
is: 'component',
4144
type: 'http',
4245
config: {
@@ -62,17 +65,19 @@ describe('HTTP', () => {
6265
http.http(component);
6366
}).not.toThrow();
6467

65-
expect(instance.get.mock.calls.length).toBe(1);
66-
expect(instance.get.mock.calls[0][0]).toBe(
68+
expect(expressInstance.get.mock.calls.length).toBe(1);
69+
expect(expressInstance.get.mock.calls[0][0]).toBe(
6770
component.config.http.path,
6871
);
69-
expect(typeof instance.get.mock.calls[0][1]).toBe('function');
72+
expect(typeof expressInstance.get.mock.calls[0][1]).toBe(
73+
'function',
74+
);
7075
});
7176

7277
test('installs http components where path is a regex', () => {
7378
const http = new HTTP();
7479

75-
const component: HTTPComponent = {
80+
const component: HTTPComponentInstance = {
7681
is: 'component',
7782
type: 'http',
7883
config: {
@@ -98,17 +103,19 @@ describe('HTTP', () => {
98103
http.http(component);
99104
}).not.toThrow();
100105

101-
expect(instance.get.mock.calls.length).toBe(1);
102-
expect(instance.get.mock.calls[0][0]).toBe(
106+
expect(expressInstance.get.mock.calls.length).toBe(1);
107+
expect(expressInstance.get.mock.calls[0][0]).toBe(
103108
component.config.http.path,
104109
);
105-
expect(typeof instance.get.mock.calls[0][1]).toBe('function');
110+
expect(typeof expressInstance.get.mock.calls[0][1]).toBe(
111+
'function',
112+
);
106113
});
107114

108115
test('installs http components where path is an array', () => {
109116
const http = new HTTP();
110117

111-
const component: HTTPComponent = {
118+
const component: HTTPComponentInstance = {
112119
is: 'component',
113120
type: 'http',
114121
config: {
@@ -137,11 +144,13 @@ describe('HTTP', () => {
137144
http.http(component);
138145
}).not.toThrow();
139146

140-
expect(instance.get.mock.calls.length).toBe(1);
141-
expect(instance.get.mock.calls[0][0]).toBe(
147+
expect(expressInstance.get.mock.calls.length).toBe(1);
148+
expect(expressInstance.get.mock.calls[0][0]).toBe(
142149
component.config.http.path,
143150
);
144-
expect(typeof instance.get.mock.calls[0][1]).toBe('function');
151+
expect(typeof expressInstance.get.mock.calls[0][1]).toBe(
152+
'function',
153+
);
145154
});
146155

147156
test('does not accept invalid components', () => {
@@ -353,9 +362,12 @@ describe('HTTP', () => {
353362
expect((middleware.http as jest.Mock<{}>).mock.calls.length).toBe(
354363
1,
355364
);
356-
expect((middleware.http as jest.Mock<{}>).mock.calls[0][0]).toBe(
357-
instance,
358-
);
365+
expect(
366+
(middleware.http as jest.Mock<{}>).mock.calls[0][0].app,
367+
).toEqual(expressInstance);
368+
expect(
369+
(middleware.http as jest.Mock<{}>).mock.calls[0][0].server,
370+
).toBeTruthy();
359371
});
360372

361373
test('does not accept invalid http middleware', () => {

src/index.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,47 @@ import {
33
PluginUnit,
44
ComponentInstance,
55
MiddlewareInstance,
6+
ComponentUnit,
7+
ComponentConfig,
68
ComponentConfigInstance,
79
} from '@leverage/core';
8-
10+
import * as http from 'http';
911
import * as express from 'express';
1012

1113
export type Route = string | RegExp;
1214

13-
export interface HTTPComponent extends ComponentInstance {
14-
config: ComponentConfigInstance & {
15-
http: {
16-
path: Route | Route[];
17-
method: string;
18-
};
15+
type HTTPCallback = (
16+
request: Express.Request,
17+
response: Express.Response,
18+
) => void;
19+
20+
interface HTTPComponentConfig {
21+
http: {
22+
path: Route | Route[];
23+
method: string;
1924
};
25+
}
26+
27+
export interface HTTPComponent extends ComponentUnit {
28+
config: ComponentConfig & HTTPComponentConfig;
29+
http: HTTPCallback;
30+
}
2031

21-
http: (request: Express.Request, response: Express.Response) => void;
32+
export interface HTTPComponentInstance extends ComponentInstance {
33+
config: ComponentConfigInstance & HTTPComponentConfig;
34+
http: HTTPCallback;
2235
}
2336

2437
export interface HTTPMiddleware extends MiddlewareInstance {
25-
http: (app: Express.Application) => void;
38+
http: (options: { app: Express.Application; server: http.Server }) => void;
2639
}
2740

2841
export class HTTP extends Plugin implements PluginUnit {
2942
type = 'http';
3043
app = express();
44+
server = new http.Server(this.app);
3145

32-
http (component: HTTPComponent) {
46+
http (component: HTTPComponentInstance) {
3347
/*
3448
* Validate component config
3549
*/
@@ -113,7 +127,10 @@ export class HTTP extends Plugin implements PluginUnit {
113127
);
114128
}
115129

116-
(middleware as HTTPMiddleware).http(this.app);
130+
(middleware as HTTPMiddleware).http({
131+
app: this.app,
132+
server: this.server,
133+
});
117134
}
118135

119136
listen (port: number) {
@@ -127,8 +144,7 @@ export class HTTP extends Plugin implements PluginUnit {
127144
);
128145
}
129146

130-
// Typings seem to think that `app.listen` doesn't exist...
131-
(this.app as any).listen(port);
147+
this.server.listen(port);
132148
}
133149
}
134150

0 commit comments

Comments
 (0)