Skip to content

Commit 1515ba3

Browse files
authored
fix(option): optional target when router is used (chimurai#512)
1 parent de31475 commit 1515ba3

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## next
4+
5+
- fix(option): optional `target` when `router` is used ([#512](https://github.com/chimurai/http-proxy-middleware/pull/512))
6+
37
## [v1.1.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.1.0)
48

59
- fix(errorHandler): fix confusing error message ([#509](https://github.com/chimurai/http-proxy-middleware/pull/509))

src/config-factory.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import { Filter, Options } from './types';
66

77
const logger = getInstance();
88

9-
export function createConfig(context, opts?) {
9+
export type Config = { context: Filter; options: Options };
10+
11+
export function createConfig(context, opts?: Options): Config {
1012
// structure of config object to be returned
11-
const config = {
13+
const config: Config = {
1214
context: undefined,
1315
options: {} as Options,
1416
};
@@ -38,7 +40,7 @@ export function createConfig(context, opts?) {
3840

3941
configureLogger(config.options);
4042

41-
if (!config.options.target) {
43+
if (!config.options.target && !config.options.router) {
4244
throw new Error(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING);
4345
}
4446

src/http-proxy-middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as https from 'https';
22
import * as express from 'express';
33
import * as httpProxy from 'http-proxy';
4-
import { createConfig } from './config-factory';
4+
import { createConfig, Config } from './config-factory';
55
import * as contextMatcher from './context-matcher';
66
import * as handlers from './handlers';
77
import { getArrow, getInstance } from './logger';
@@ -11,7 +11,7 @@ import { Filter, Request, RequestHandler, Response, Options } from './types';
1111

1212
export class HttpProxyMiddleware {
1313
private logger = getInstance();
14-
private config;
14+
private config: Config;
1515
private wsInternalSubscribed = false;
1616
private serverOnCloseSubscribed = false;
1717
private proxyOptions: Options;

test/unit/config-factory.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,27 @@ describe('configFactory', () => {
136136
};
137137
});
138138

139-
it('should throw an error when target option is missing', () => {
139+
it('should throw an error when target and router option are missing', () => {
140140
expect(fn).toThrowError(Error);
141141
});
142142
});
143143

144+
describe('optional option.target when option.router is used', () => {
145+
let fn;
146+
147+
beforeEach(() => {
148+
fn = () => {
149+
createConfig('/api', {
150+
router: (req) => 'http://www.example.com',
151+
});
152+
};
153+
});
154+
155+
it('should not throw an error when target option is missing when router is used', () => {
156+
expect(fn).not.toThrowError(Error);
157+
});
158+
});
159+
144160
describe('faulty config. mixing classic with shorthand', () => {
145161
beforeEach(() => {
146162
result = createConfig('http://localhost:3000/api', {

0 commit comments

Comments
 (0)