Skip to content

Commit d231b9f

Browse files
committed
feat: remove shorthand usage [BREAKING CHANGE] (#716)
1 parent 4baae76 commit d231b9f

File tree

6 files changed

+17
-280
lines changed

6 files changed

+17
-280
lines changed

README.md

+16-47
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
5757
- [Install](#install)
5858
- [Core concept](#core-concept)
5959
- [Example](#example)
60+
- [app.use(path, proxy)](#appusepath-proxy)
6061
- [Context matching](#context-matching)
6162
- [Options](#options)
6263
- [http-proxy-middleware options](#http-proxy-middleware-options)
6364
- [http-proxy events](#http-proxy-events)
6465
- [http-proxy options](#http-proxy-options)
65-
- [Shorthand](#shorthand)
66-
- [app.use(path, proxy)](#appusepath-proxy)
6766
- [WebSocket](#websocket)
6867
- [External WebSocket upgrade](#external-websocket-upgrade)
6968
- [Intercept and manipulate requests](#intercept-and-manipulate-requests)
@@ -104,15 +103,6 @@ const apiProxy = createProxyMiddleware('/api', { target: 'http://www.example.org
104103

105104
(full list of [`http-proxy-middleware` configuration options](#options))
106105

107-
#### createProxyMiddleware(uri [, config])
108-
109-
```javascript
110-
// shorthand syntax for the example above:
111-
const apiProxy = createProxyMiddleware('http://www.example.org/api');
112-
```
113-
114-
More about the [shorthand configuration](#shorthand).
115-
116106
## Example
117107

118108
An example with `express` server.
@@ -148,6 +138,21 @@ app.use('/api', exampleProxy);
148138
app.listen(3000);
149139
```
150140

141+
### app.use(path, proxy)
142+
143+
If you want to use the server's `app.use` `path` parameter to match requests;
144+
Create and mount the proxy without the http-proxy-middleware `context` parameter:
145+
146+
```javascript
147+
app.use('/api', createProxyMiddleware({ target: 'http://www.example.org', changeOrigin: true }));
148+
```
149+
150+
`app.use` documentation:
151+
152+
- express: http://expressjs.com/en/4x/api.html#app.use
153+
- connect: https://github.com/senchalabs/connect#mount-middleware
154+
- polka: https://github.com/lukeed/polka#usebase-fn
155+
151156
## Context matching
152157

153158
Providing an alternative way to decide which requests should be proxied; In case you are not able to use the server's [`path` parameter](http://expressjs.com/en/4x/api.html#app.use) to mount the proxy or when you need more flexibility.
@@ -422,47 +427,11 @@ The following options are provided by the underlying [http-proxy](https://github
422427
};
423428
```
424429

425-
## Shorthand
426-
427-
Use the shorthand syntax when verbose configuration is not needed. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed.
428-
429-
```javascript
430-
createProxyMiddleware('http://www.example.org:8000/api');
431-
// createProxyMiddleware('/api', {target: 'http://www.example.org:8000'});
432-
433-
createProxyMiddleware('http://www.example.org:8000/api/books/*/**.json');
434-
// createProxyMiddleware('/api/books/*/**.json', {target: 'http://www.example.org:8000'});
435-
436-
createProxyMiddleware('http://www.example.org:8000/api', { changeOrigin: true });
437-
// createProxyMiddleware('/api', {target: 'http://www.example.org:8000', changeOrigin: true});
438-
```
439-
440-
### app.use(path, proxy)
441-
442-
If you want to use the server's `app.use` `path` parameter to match requests;
443-
Create and mount the proxy without the http-proxy-middleware `context` parameter:
444-
445-
```javascript
446-
app.use('/api', createProxyMiddleware({ target: 'http://www.example.org', changeOrigin: true }));
447-
```
448-
449-
`app.use` documentation:
450-
451-
- express: http://expressjs.com/en/4x/api.html#app.use
452-
- connect: https://github.com/senchalabs/connect#mount-middleware
453-
- polka: https://github.com/lukeed/polka#usebase-fn
454-
455430
## WebSocket
456431

457432
```javascript
458433
// verbose api
459434
createProxyMiddleware('/', { target: 'http://echo.websocket.org', ws: true });
460-
461-
// shorthand
462-
createProxyMiddleware('http://echo.websocket.org', { ws: true });
463-
464-
// shorter shorthand
465-
createProxyMiddleware('ws://echo.websocket.org');
466435
```
467436

468437
### External WebSocket upgrade

recipes/shorthand.md

-62
This file was deleted.

src/config-factory.ts

-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import isPlainObj = require('is-plain-obj');
2-
import * as url from 'url';
32
import { ERRORS } from './errors';
43
import { getInstance } from './logger';
54
import { Filter, Options } from './types';
@@ -22,17 +21,6 @@ export function createConfig(context, opts?: Options): Config {
2221

2322
// app.use('/api', proxy('http://localhost:9000'));
2423
// app.use(proxy('http://localhost:9000/api'));
25-
} else if (isStringShortHand(context)) {
26-
const oUrl = url.parse(context);
27-
const target = [oUrl.protocol, '//', oUrl.host].join('');
28-
29-
config.context = oUrl.pathname || '/';
30-
config.options = Object.assign(config.options, { target }, opts);
31-
32-
if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') {
33-
config.options.ws = true;
34-
}
35-
// app.use('/api', proxy({target:'http://localhost:9000'}));
3624
} else {
3725
config.context = context;
3826
config.options = Object.assign(config.options, opts);
@@ -47,23 +35,6 @@ export function createConfig(context, opts?: Options): Config {
4735
return config;
4836
}
4937

50-
/**
51-
* Checks if a String only target/config is provided.
52-
* This can be just the host or with the optional path.
53-
*
54-
* @example
55-
* app.use('/api', proxy('http://localhost:9000'));
56-
* app.use(proxy('http://localhost:9000/api'));
57-
*
58-
* @param {String} context [description]
59-
* @return {Boolean} [description]
60-
*/
61-
function isStringShortHand(context: Filter) {
62-
if (typeof context === 'string') {
63-
return !!url.parse(context).host;
64-
}
65-
}
66-
6738
/**
6839
* Checks if a Object only config is provided, without a context.
6940
* In this case the all paths will be proxied.

test/e2e/http-proxy-middleware.spec.ts

-14
Original file line numberDiff line numberDiff line change
@@ -389,20 +389,6 @@ describe('E2E http-proxy-middleware', () => {
389389
});
390390
});
391391

392-
describe('shorthand usage', () => {
393-
beforeEach(() => {
394-
agent = request(
395-
createApp(createProxyMiddleware(`http://localhost:${mockTargetServer.port}/api`))
396-
);
397-
});
398-
399-
it('should have proxy with shorthand configuration', async () => {
400-
await mockTargetServer.get('/api/foo/bar').thenReply(200, 'HELLO /api/foo/bar');
401-
const response = await agent.get(`/api/foo/bar`).expect(200);
402-
expect(response.text).toBe('HELLO /api/foo/bar');
403-
});
404-
});
405-
406392
describe('express with path + proxy', () => {
407393
beforeEach(() => {
408394
agent = request(

test/e2e/websocket.spec.ts

-26
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,6 @@ describe('E2E WebSocket proxy', () => {
9595
});
9696
});
9797

98-
describe('option.ws with external server "upgrade" and shorthand usage', () => {
99-
beforeEach(() => {
100-
proxyServer = createApp(
101-
createProxyMiddleware(`ws://localhost:${WS_SERVER_PORT}`, {
102-
pathRewrite: { '^/socket': '' },
103-
})
104-
).listen(SERVER_PORT);
105-
106-
proxyServer.on('upgrade', proxyMiddleware.upgrade);
107-
});
108-
109-
beforeEach((done) => {
110-
ws = new WebSocket(`ws://localhost:${SERVER_PORT}/socket`);
111-
ws.on('open', done);
112-
});
113-
114-
it('should proxy to path', (done) => {
115-
ws.on('message', (data, isBinary) => {
116-
const message = isBinary ? data : data.toString();
117-
expect(message).toBe('foobar');
118-
done();
119-
});
120-
ws.send('foobar');
121-
});
122-
});
123-
12498
describe('with router and pathRewrite', () => {
12599
beforeEach(() => {
126100
// override

test/unit/config-factory.spec.ts

+1-102
Original file line numberDiff line numberDiff line change
@@ -26,92 +26,7 @@ describe('configFactory', () => {
2626
});
2727
});
2828

29-
describe('shorthand String', () => {
30-
describe('shorthand String config', () => {
31-
beforeEach(() => {
32-
result = createConfig('http://www.example.org:8000/api');
33-
});
34-
35-
it('should return config object', () => {
36-
expect(Object.keys(result)).toEqual(['context', 'options']);
37-
});
38-
39-
it('should return config object with context', () => {
40-
expect(result.context).toBe('/api');
41-
});
42-
43-
it('should return config object with options', () => {
44-
expect(result.options).toEqual({
45-
target: 'http://www.example.org:8000',
46-
});
47-
});
48-
});
49-
50-
describe('shorthand String config for whole domain', () => {
51-
beforeEach(() => {
52-
result = createConfig('http://www.example.org:8000');
53-
});
54-
55-
it('should return config object with context', () => {
56-
expect(result.context).toBe('/');
57-
});
58-
});
59-
60-
describe('shorthand String config for websocket url', () => {
61-
beforeEach(() => {
62-
result = createConfig('ws://www.example.org:8000');
63-
});
64-
65-
it('should return config object with context', () => {
66-
expect(result.context).toBe('/');
67-
});
68-
69-
it('should return options with ws = true', () => {
70-
expect(result.options.ws).toBe(true);
71-
});
72-
});
73-
74-
describe('shorthand String config for secure websocket url', () => {
75-
beforeEach(() => {
76-
result = createConfig('wss://www.example.org:8000');
77-
});
78-
79-
it('should return config object with context', () => {
80-
expect(result.context).toBe('/');
81-
});
82-
83-
it('should return options with ws = true', () => {
84-
expect(result.options.ws).toBe(true);
85-
});
86-
});
87-
88-
describe('shorthand String config with globbing', () => {
89-
beforeEach(() => {
90-
result = createConfig('http://www.example.org:8000/api/*.json');
91-
});
92-
93-
it('should return config object with context', () => {
94-
expect(result.context).toBe('/api/*.json');
95-
});
96-
});
97-
98-
describe('shorthand String config with options', () => {
99-
beforeEach(() => {
100-
result = createConfig('http://www.example.org:8000/api', {
101-
changeOrigin: true,
102-
});
103-
});
104-
105-
it('should return config object with additional options', () => {
106-
expect(result.options).toEqual({
107-
changeOrigin: true,
108-
target: 'http://www.example.org:8000',
109-
});
110-
});
111-
});
112-
});
113-
114-
describe('shorthand Object config', () => {
29+
describe('Object config', () => {
11530
beforeEach(() => {
11631
result = createConfig({ target: 'http://www.example.org:8000' });
11732
});
@@ -156,21 +71,5 @@ describe('configFactory', () => {
15671
expect(fn).not.toThrowError(Error);
15772
});
15873
});
159-
160-
describe('faulty config. mixing classic with shorthand', () => {
161-
beforeEach(() => {
162-
result = createConfig('http://localhost:3000/api', {
163-
target: 'http://localhost:8000',
164-
});
165-
});
166-
167-
it('should use the target in the configuration as target', () => {
168-
expect(result.options.target).toBe('http://localhost:8000');
169-
});
170-
171-
it('should not use the host from the shorthand as target', () => {
172-
expect(result.options.target).not.toBe('http://localhost:3000');
173-
});
174-
});
17574
});
17675
});

0 commit comments

Comments
 (0)