Skip to content

Commit fe140d2

Browse files
committed
[Feature] Support multiple types for path value
1 parent c3d775e commit fe140d2

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

src/__tests__/index.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('HTTP', () => {
3434
});
3535

3636
describe('#http', () => {
37-
test('installs http components', () => {
37+
test('installs http components where path is a string', () => {
3838
const http = new HTTP();
3939

4040
const component: HTTPComponent = {
@@ -70,6 +70,81 @@ describe('HTTP', () => {
7070
expect(typeof instance.get.mock.calls[0][1]).toBe('function');
7171
});
7272

73+
test('installs http components where path is a regex', () => {
74+
const http = new HTTP();
75+
76+
const component: HTTPComponent = {
77+
is: 'component',
78+
config: {
79+
type: 'http',
80+
http: {
81+
path: /\//,
82+
method: 'get',
83+
},
84+
dependencies: {
85+
plugins: [
86+
'http',
87+
],
88+
services: [],
89+
},
90+
},
91+
http: jest.fn(),
92+
plugins: [
93+
http,
94+
] as any,
95+
services: [] as any,
96+
};
97+
98+
expect(() => {
99+
http.http(component);
100+
}).not.toThrow();
101+
102+
expect(instance.get.mock.calls.length).toBe(1);
103+
expect(instance.get.mock.calls[0][0]).toBe(
104+
component.config.http.path,
105+
);
106+
expect(typeof instance.get.mock.calls[0][1]).toBe('function');
107+
});
108+
109+
test('installs http components where path is an array', () => {
110+
const http = new HTTP();
111+
112+
const component: HTTPComponent = {
113+
is: 'component',
114+
config: {
115+
type: 'http',
116+
http: {
117+
path: [
118+
'/',
119+
/x/,
120+
],
121+
method: 'get',
122+
},
123+
dependencies: {
124+
plugins: [
125+
'http',
126+
],
127+
services: [],
128+
},
129+
},
130+
http: jest.fn(),
131+
plugins: [
132+
http,
133+
] as any,
134+
services: [] as any,
135+
};
136+
137+
expect(() => {
138+
http.http(component);
139+
}).not.toThrow();
140+
141+
expect(instance.get.mock.calls.length).toBe(1);
142+
expect(instance.get.mock.calls[0][0]).toBe(
143+
component.config.http.path,
144+
);
145+
expect(typeof instance.get.mock.calls[0][1]).toBe('function');
146+
});
147+
73148
test('does not accept invalid components', () => {
74149
const http = new HTTP();
75150

src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import {
99
// tslint:disable-next-line:no-require-imports no-var-requires
1010
const express = require('express');
1111

12+
export type Path = string | RegExp;
13+
1214
export interface HTTPComponent extends ComponentInstanceWithDependencies {
1315
config: ComponentConfigWithDependencies & {
1416
http: {
15-
path: string;
17+
path: Path | Path[];
1618
method: string;
1719
};
1820
};
@@ -60,11 +62,15 @@ export class HTTP implements PluginUnit {
6062
);
6163
}
6264

63-
if (typeof component.config.http.path !== 'string') {
65+
if (
66+
typeof component.config.http.path !== 'string' &&
67+
!Array.isArray(component.config.http.path) &&
68+
typeof component.config.http.path !== 'object'
69+
) {
6470
throw new Error(
6571
// prettier-ignore
6672
// tslint:disable-next-line:max-line-length
67-
`[HTTP] Expected \`component.config.http.path\` to be a string but got "${typeof component.config.http.path}"`,
73+
`[HTTP] Expected \`component.config.http.path\` to be a string, array, or regex but got "${typeof component.config.http.path}"`,
6874
);
6975
}
7076

0 commit comments

Comments
 (0)