Skip to content

Commit 4aaaae6

Browse files
committed
feat(typescript): export http-proxy-middleware types [BREAKING CHANGE]
1 parent 777b911 commit 4aaaae6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+412
-547
lines changed

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"printWidth": 100,
23
"tabWidth": 2,
34
"semi": true,
45
"singleQuote": true

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Changelog
22

3+
## next
4+
5+
- feat(createProxyMiddleware): explicit import http-proxy-middleware (BREAKING CHANGE)([#400](https://github.com/chimurai/http-proxy-middleware/issues/400#issuecomment-587162378))
6+
- feat(typescript): export http-proxy-middleware types ([#400](https://github.com/chimurai/http-proxy-middleware/issues/400))
7+
- fix(typescript): ES6 target - TS1192 ([#400](https://github.com/chimurai/http-proxy-middleware/issues/400#issuecomment-587064349))
8+
39
## [v0.21.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.21.0)
410

511
- feat(http-proxy): bump to v1.18.0
6-
- feat: async router ([#379](https://github.com/chimurai/http-proxy-middleware/issues/335)) ([LiranBri](https://github.com/LiranBri))
12+
- feat: async router ([#379](https://github.com/chimurai/http-proxy-middleware/issues/379)) ([LiranBri](https://github.com/LiranBri))
713
- feat(typescript): types support ([#369](https://github.com/chimurai/http-proxy-middleware/pull/369))
814
- feat: async pathRewrite ([#397](https://github.com/chimurai/http-proxy-middleware/pull/397)) ([rsethc](https://github.com/rsethc))
915

README.md

+71-54
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,40 @@ Node.js proxying made simple. Configure proxy middleware with ease for [connect]
1010

1111
Powered by the popular Nodejitsu [`http-proxy`](https://github.com/nodejitsu/node-http-proxy). [![GitHub stars](https://img.shields.io/github/stars/nodejitsu/node-http-proxy.svg?style=social&label=Star)](https://github.com/nodejitsu/node-http-proxy)
1212

13+
## ⚠️ NOTE
14+
15+
This page is showing documentation for version v1.x.x
16+
17+
If you're looking for v0.x documentation. Go to:
18+
https://github.com/chimurai/http-proxy-middleware/tree/v0.21.0#readme
19+
1320
## TL;DR
1421

1522
Proxy `/api` requests to `http://www.example.org`
1623

1724
```javascript
18-
var express = require('express');
19-
var proxy = require('http-proxy-middleware');
25+
// javascript
26+
27+
const express = require('express');
28+
const { createProxyMiddleware } = require('http-proxy-middleware');
2029

21-
var app = express();
30+
const app = express();
2231

23-
app.use(
24-
'/api',
25-
proxy({ target: 'http://www.example.org', changeOrigin: true })
26-
);
32+
app.use('/api', createProxyMiddleware({ target: 'http://www.example.org', changeOrigin: true }));
33+
app.listen(3000);
34+
35+
// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
36+
```
37+
38+
```typescript
39+
// typescript
40+
41+
import * as express from 'express';
42+
import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-proxy-middleware';
43+
44+
const app = express();
45+
46+
app.use('/api', createProxyMiddleware({ target: 'http://www.example.org', changeOrigin: true }));
2747
app.listen(3000);
2848

2949
// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
@@ -68,15 +88,15 @@ $ npm install --save-dev http-proxy-middleware
6888

6989
Proxy middleware configuration.
7090

71-
#### proxy([context,] config)
91+
#### createProxyMiddleware([context,] config)
7292

7393
```javascript
74-
var proxy = require('http-proxy-middleware');
94+
const { createProxyMiddleware } = require('http-proxy-middleware');
7595

76-
var apiProxy = proxy('/api', { target: 'http://www.example.org' });
77-
// \____/ \_____________________________/
78-
// | |
79-
// context options
96+
const apiProxy = createProxyMiddleware('/api', { target: 'http://www.example.org' });
97+
// \____/ \_____________________________/
98+
// | |
99+
// context options
80100

81101
// 'apiProxy' is now ready to be used as middleware in a server.
82102
```
@@ -87,11 +107,11 @@ var apiProxy = proxy('/api', { target: 'http://www.example.org' });
87107

88108
(full list of [`http-proxy-middleware` configuration options](#options))
89109

90-
#### proxy(uri [, config])
110+
#### createProxyMiddleware(uri [, config])
91111

92112
```javascript
93113
// shorthand syntax for the example above:
94-
var apiProxy = proxy('http://www.example.org/api');
114+
const apiProxy = createProxyMiddleware('http://www.example.org/api');
95115
```
96116

97117
More about the [shorthand configuration](#shorthand).
@@ -102,11 +122,11 @@ An example with `express` server.
102122

103123
```javascript
104124
// include dependencies
105-
var express = require('express');
106-
var proxy = require('http-proxy-middleware');
125+
const express = require('express');
126+
const { createProxyMiddleware } = require('http-proxy-middleware');
107127

108128
// proxy middleware options
109-
var options = {
129+
const options = {
110130
target: 'http://www.example.org', // target host
111131
changeOrigin: true, // needed for virtual hosted sites
112132
ws: true, // proxy websockets
@@ -122,10 +142,10 @@ var options = {
122142
};
123143

124144
// create the proxy (without context)
125-
var exampleProxy = proxy(options);
145+
const exampleProxy = createProxyMiddleware(options);
126146

127147
// mount `exampleProxy` in web server
128-
var app = express();
148+
const app = express();
129149
app.use('/api', exampleProxy);
130150
app.listen(3000);
131151
```
@@ -145,24 +165,24 @@ Providing an alternative way to decide which requests should be proxied; In case
145165

146166
- **path matching**
147167

148-
- `proxy({...})` - matches any path, all requests will be proxied.
149-
- `proxy('/', {...})` - matches any path, all requests will be proxied.
150-
- `proxy('/api', {...})` - matches paths starting with `/api`
168+
- `createProxyMiddleware({...})` - matches any path, all requests will be proxied.
169+
- `createProxyMiddleware('/', {...})` - matches any path, all requests will be proxied.
170+
- `createProxyMiddleware('/api', {...})` - matches paths starting with `/api`
151171

152172
- **multiple path matching**
153173

154-
- `proxy(['/api', '/ajax', '/someotherpath'], {...})`
174+
- `createProxyMiddleware(['/api', '/ajax', '/someotherpath'], {...})`
155175

156176
- **wildcard path matching**
157177

158178
For fine-grained control you can use wildcard matching. Glob pattern matching is done by _micromatch_. Visit [micromatch](https://www.npmjs.com/package/micromatch) or [glob](https://www.npmjs.com/package/glob) for more globbing examples.
159179

160-
- `proxy('**', {...})` matches any path, all requests will be proxied.
161-
- `proxy('**/*.html', {...})` matches any path which ends with `.html`
162-
- `proxy('/*.html', {...})` matches paths directly under path-absolute
163-
- `proxy('/api/**/*.html', {...})` matches requests ending with `.html` in the path of `/api`
164-
- `proxy(['/api/**', '/ajax/**'], {...})` combine multiple patterns
165-
- `proxy(['/api/**', '!**/bad.json'], {...})` exclusion
180+
- `createProxyMiddleware('**', {...})` matches any path, all requests will be proxied.
181+
- `createProxyMiddleware('**/*.html', {...})` matches any path which ends with `.html`
182+
- `createProxyMiddleware('/*.html', {...})` matches paths directly under path-absolute
183+
- `createProxyMiddleware('/api/**/*.html', {...})` matches requests ending with `.html` in the path of `/api`
184+
- `createProxyMiddleware(['/api/**', '/ajax/**'], {...})` combine multiple patterns
185+
- `createProxyMiddleware(['/api/**', '!**/bad.json'], {...})` exclusion
166186

167187
**Note**: In multiple path matching, you cannot use string paths and wildcard paths together.
168188

@@ -174,11 +194,13 @@ Providing an alternative way to decide which requests should be proxied; In case
174194
/**
175195
* @return {Boolean}
176196
*/
177-
var filter = function(pathname, req) {
197+
const filter = function(pathname, req) {
178198
return pathname.match('^/api') && req.method === 'GET';
179199
};
180200

181-
var apiProxy = proxy(filter, { target: 'http://www.example.org' });
201+
const apiProxy = createProxyMiddleware(filter, {
202+
target: 'http://www.example.org'
203+
});
182204
```
183205

184206
## Options
@@ -202,7 +224,7 @@ Providing an alternative way to decide which requests should be proxied; In case
202224

203225
// custom rewriting, returning Promise
204226
pathRewrite: async function (path, req) {
205-
var should_add_something = await httpRequestToDecideSomething(path);
227+
const should_add_something = await httpRequestToDecideSomething(path);
206228
if (should_add_something) path += "something";
207229
return path;
208230
}
@@ -248,9 +270,9 @@ Providing an alternative way to decide which requests should be proxied; In case
248270
```javascript
249271
// verbose replacement
250272
function logProvider(provider) {
251-
var logger = new (require('winston').Logger)();
273+
const logger = new (require('winston').Logger)();
252274

253-
var myCustomProvider = {
275+
const myCustomProvider = {
254276
log: logger.log,
255277
debug: logger.debug,
256278
info: logger.info,
@@ -272,9 +294,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
272294
res.writeHead(500, {
273295
'Content-Type': 'text/plain'
274296
});
275-
res.end(
276-
'Something went wrong. And we are reporting a custom error message.'
277-
);
297+
res.end('Something went wrong. And we are reporting a custom error message.');
278298
}
279299
```
280300

@@ -401,14 +421,14 @@ The following options are provided by the underlying [http-proxy](https://github
401421
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.
402422

403423
```javascript
404-
proxy('http://www.example.org:8000/api');
405-
// proxy('/api', {target: 'http://www.example.org:8000'});
424+
createProxyMiddleware('http://www.example.org:8000/api');
425+
// createProxyMiddleware('/api', {target: 'http://www.example.org:8000'});
406426

407-
proxy('http://www.example.org:8000/api/books/*/**.json');
408-
// proxy('/api/books/*/**.json', {target: 'http://www.example.org:8000'});
427+
createProxyMiddleware('http://www.example.org:8000/api/books/*/**.json');
428+
// createProxyMiddleware('/api/books/*/**.json', {target: 'http://www.example.org:8000'});
409429

410-
proxy('http://www.example.org:8000/api', { changeOrigin: true });
411-
// proxy('/api', {target: 'http://www.example.org:8000', changeOrigin: true});
430+
createProxyMiddleware('http://www.example.org:8000/api', { changeOrigin: true });
431+
// createProxyMiddleware('/api', {target: 'http://www.example.org:8000', changeOrigin: true});
412432
```
413433

414434
### app.use(path, proxy)
@@ -417,10 +437,7 @@ If you want to use the server's `app.use` `path` parameter to match requests;
417437
Create and mount the proxy without the http-proxy-middleware `context` parameter:
418438

419439
```javascript
420-
app.use(
421-
'/api',
422-
proxy({ target: 'http://www.example.org', changeOrigin: true })
423-
);
440+
app.use('/api', createProxyMiddleware({ target: 'http://www.example.org', changeOrigin: true }));
424441
```
425442

426443
`app.use` documentation:
@@ -433,26 +450,26 @@ app.use(
433450

434451
```javascript
435452
// verbose api
436-
proxy('/', { target: 'http://echo.websocket.org', ws: true });
453+
createProxyMiddleware('/', { target: 'http://echo.websocket.org', ws: true });
437454

438455
// shorthand
439-
proxy('http://echo.websocket.org', { ws: true });
456+
createProxyMiddleware('http://echo.websocket.org', { ws: true });
440457

441458
// shorter shorthand
442-
proxy('ws://echo.websocket.org');
459+
createProxyMiddleware('ws://echo.websocket.org');
443460
```
444461

445462
### External WebSocket upgrade
446463

447464
In the previous WebSocket examples, http-proxy-middleware relies on a initial http request in order to listen to the http `upgrade` event. If you need to proxy WebSockets without the initial http request, you can subscribe to the server's http `upgrade` event manually.
448465

449466
```javascript
450-
var wsProxy = proxy('ws://echo.websocket.org', { changeOrigin: true });
467+
const wsProxy = createProxyMiddleware('ws://echo.websocket.org', { changeOrigin: true });
451468

452-
var app = express();
469+
const app = express();
453470
app.use(wsProxy);
454471

455-
var server = app.listen(3000);
472+
const server = app.listen(3000);
456473
server.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade'
457474
```
458475

examples/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ To run and view the [examples](https://github.com/chimurai/http-proxy-middleware
77
```bash
88
$ git clone https://github.com/chimurai/http-proxy-middleware.git
99
$ cd http-proxy-middleware
10-
$ npm install
10+
$ yarn
11+
$ yarn build
1112
```
1213

1314
Run the example from root folder:

examples/_proxy.js

-1
This file was deleted.

examples/browser-sync/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* Module dependencies.
33
*/
4-
var browserSync = require('browser-sync').create();
5-
var proxy = require('../_proxy'); // require('http-proxy-middleware');
4+
const browserSync = require('browser-sync').create();
5+
const { createProxyMiddleware } = require('../../dist'); // require('http-proxy-middleware');
66

77
/**
88
* Configure proxy middleware
99
*/
10-
var jsonPlaceholderProxy = proxy('/users', {
10+
const jsonPlaceholderProxy = createProxyMiddleware('/users', {
1111
target: 'http://jsonplaceholder.typicode.com',
1212
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
1313
logLevel: 'debug'

examples/connect/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/**
22
* Module dependencies.
33
*/
4-
var http = require('http');
5-
var connect = require('connect');
6-
var proxy = require('../_proxy'); // require('http-proxy-middleware');
4+
const http = require('http');
5+
const connect = require('connect');
6+
const { createProxyMiddleware } = require('../../dist'); // require('http-proxy-middleware');
77

88
/**
99
* Configure proxy middleware
1010
*/
11-
var jsonPlaceholderProxy = proxy({
11+
const jsonPlaceholderProxy = createProxyMiddleware({
1212
target: 'http://jsonplaceholder.typicode.com',
1313
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
1414
logLevel: 'debug'
1515
});
1616

17-
var app = connect();
17+
const app = connect();
1818

1919
/**
2020
* Add the proxy to connect

examples/express/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/**
22
* Module dependencies.
33
*/
4-
var express = require('express');
5-
var proxy = require('../_proxy'); // require('http-proxy-middleware');
4+
const express = require('express');
5+
const { createProxyMiddleware } = require('../../dist'); // require('http-proxy-middleware');
66

77
/**
88
* Configure proxy middleware
99
*/
10-
var jsonPlaceholderProxy = proxy({
10+
const jsonPlaceholderProxy = createProxyMiddleware({
1111
target: 'http://jsonplaceholder.typicode.com',
1212
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
1313
logLevel: 'debug'
1414
});
1515

16-
var app = express();
16+
const app = express();
1717

1818
/**
1919
* Add the proxy to express

0 commit comments

Comments
 (0)