Skip to content

Commit

Permalink
fix(option): optional target when router is used (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai authored Apr 1, 2021
1 parent de31475 commit 1515ba3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## next

- fix(option): optional `target` when `router` is used ([#512](https://github.com/chimurai/http-proxy-middleware/pull/512))

## [v1.1.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.1.0)

- fix(errorHandler): fix confusing error message ([#509](https://github.com/chimurai/http-proxy-middleware/pull/509))
Expand Down
8 changes: 5 additions & 3 deletions src/config-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { Filter, Options } from './types';

const logger = getInstance();

export function createConfig(context, opts?) {
export type Config = { context: Filter; options: Options };

export function createConfig(context, opts?: Options): Config {
// structure of config object to be returned
const config = {
const config: Config = {
context: undefined,
options: {} as Options,
};
Expand Down Expand Up @@ -38,7 +40,7 @@ export function createConfig(context, opts?) {

configureLogger(config.options);

if (!config.options.target) {
if (!config.options.target && !config.options.router) {
throw new Error(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING);
}

Expand Down
4 changes: 2 additions & 2 deletions src/http-proxy-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as https from 'https';
import * as express from 'express';
import * as httpProxy from 'http-proxy';
import { createConfig } from './config-factory';
import { createConfig, Config } from './config-factory';
import * as contextMatcher from './context-matcher';
import * as handlers from './handlers';
import { getArrow, getInstance } from './logger';
Expand All @@ -11,7 +11,7 @@ import { Filter, Request, RequestHandler, Response, Options } from './types';

export class HttpProxyMiddleware {
private logger = getInstance();
private config;
private config: Config;
private wsInternalSubscribed = false;
private serverOnCloseSubscribed = false;
private proxyOptions: Options;
Expand Down
18 changes: 17 additions & 1 deletion test/unit/config-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,27 @@ describe('configFactory', () => {
};
});

it('should throw an error when target option is missing', () => {
it('should throw an error when target and router option are missing', () => {
expect(fn).toThrowError(Error);
});
});

describe('optional option.target when option.router is used', () => {
let fn;

beforeEach(() => {
fn = () => {
createConfig('/api', {
router: (req) => 'http://www.example.com',
});
};
});

it('should not throw an error when target option is missing when router is used', () => {
expect(fn).not.toThrowError(Error);
});
});

describe('faulty config. mixing classic with shorthand', () => {
beforeEach(() => {
result = createConfig('http://localhost:3000/api', {
Expand Down

0 comments on commit 1515ba3

Please sign in to comment.