Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(option): optional target when router is used #512

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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