http-proxy-middleware 0.22.0-alpha → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,9 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## [v1.0.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v1.0.0)
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
+
3
9
  ## [v0.21.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.21.0)
4
10
 
5
11
  - 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))
7
13
  - feat(typescript): types support ([#369](https://github.com/chimurai/http-proxy-middleware/pull/369))
8
14
  - feat: async pathRewrite ([#397](https://github.com/chimurai/http-proxy-middleware/pull/397)) ([rsethc](https://github.com/rsethc))
9
15
 
package/README.md CHANGED
@@ -10,20 +10,40 @@ Node.js proxying made simple. Configure proxy middleware with ease for [connect]
10
10
 
11
11
  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)
12
12
 
13
+ ## ⚠️ Note
14
+
15
+ This page is showing documentation for version v1.x.x ([release notes](https://github.com/chimurai/http-proxy-middleware/releases))
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
+
13
20
  ## TL;DR
14
21
 
15
22
  Proxy `/api` requests to `http://www.example.org`
16
23
 
17
24
  ```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');
20
29
 
21
- var app = express();
30
+ const app = express();
22
31
 
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 }));
27
47
  app.listen(3000);
28
48
 
29
49
  // http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
@@ -68,15 +88,15 @@ $ npm install --save-dev http-proxy-middleware
68
88
 
69
89
  Proxy middleware configuration.
70
90
 
71
- #### proxy([context,] config)
91
+ #### createProxyMiddleware([context,] config)
72
92
 
73
93
  ```javascript
74
- var proxy = require('http-proxy-middleware');
94
+ const { createProxyMiddleware } = require('http-proxy-middleware');
75
95
 
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
80
100
 
81
101
  // 'apiProxy' is now ready to be used as middleware in a server.
82
102
  ```
@@ -87,11 +107,11 @@ var apiProxy = proxy('/api', { target: 'http://www.example.org' });
87
107
 
88
108
  (full list of [`http-proxy-middleware` configuration options](#options))
89
109
 
90
- #### proxy(uri [, config])
110
+ #### createProxyMiddleware(uri [, config])
91
111
 
92
112
  ```javascript
93
113
  // shorthand syntax for the example above:
94
- var apiProxy = proxy('http://www.example.org/api');
114
+ const apiProxy = createProxyMiddleware('http://www.example.org/api');
95
115
  ```
96
116
 
97
117
  More about the [shorthand configuration](#shorthand).
@@ -102,11 +122,11 @@ An example with `express` server.
102
122
 
103
123
  ```javascript
104
124
  // 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');
107
127
 
108
128
  // proxy middleware options
109
- var options = {
129
+ const options = {
110
130
  target: 'http://www.example.org', // target host
111
131
  changeOrigin: true, // needed for virtual hosted sites
112
132
  ws: true, // proxy websockets
@@ -122,10 +142,10 @@ var options = {
122
142
  };
123
143
 
124
144
  // create the proxy (without context)
125
- var exampleProxy = proxy(options);
145
+ const exampleProxy = createProxyMiddleware(options);
126
146
 
127
147
  // mount `exampleProxy` in web server
128
- var app = express();
148
+ const app = express();
129
149
  app.use('/api', exampleProxy);
130
150
  app.listen(3000);
131
151
  ```
@@ -145,24 +165,24 @@ Providing an alternative way to decide which requests should be proxied; In case
145
165
 
146
166
  - **path matching**
147
167
 
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`
151
171
 
152
172
  - **multiple path matching**
153
173
 
154
- - `proxy(['/api', '/ajax', '/someotherpath'], {...})`
174
+ - `createProxyMiddleware(['/api', '/ajax', '/someotherpath'], {...})`
155
175
 
156
176
  - **wildcard path matching**
157
177
 
158
178
  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.
159
179
 
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
166
186
 
167
187
  **Note**: In multiple path matching, you cannot use string paths and wildcard paths together.
168
188
 
@@ -174,11 +194,13 @@ Providing an alternative way to decide which requests should be proxied; In case
174
194
  /**
175
195
  * @return {Boolean}
176
196
  */
177
- var filter = function(pathname, req) {
197
+ const filter = function(pathname, req) {
178
198
  return pathname.match('^/api') && req.method === 'GET';
179
199
  };
180
200
 
181
- var apiProxy = proxy(filter, { target: 'http://www.example.org' });
201
+ const apiProxy = createProxyMiddleware(filter, {
202
+ target: 'http://www.example.org'
203
+ });
182
204
  ```
183
205
 
184
206
  ## Options
@@ -202,7 +224,7 @@ Providing an alternative way to decide which requests should be proxied; In case
202
224
 
203
225
  // custom rewriting, returning Promise
204
226
  pathRewrite: async function (path, req) {
205
- var should_add_something = await httpRequestToDecideSomething(path);
227
+ const should_add_something = await httpRequestToDecideSomething(path);
206
228
  if (should_add_something) path += "something";
207
229
  return path;
208
230
  }
@@ -248,9 +270,9 @@ Providing an alternative way to decide which requests should be proxied; In case
248
270
  ```javascript
249
271
  // verbose replacement
250
272
  function logProvider(provider) {
251
- var logger = new (require('winston').Logger)();
273
+ const logger = new (require('winston').Logger)();
252
274
 
253
- var myCustomProvider = {
275
+ const myCustomProvider = {
254
276
  log: logger.log,
255
277
  debug: logger.debug,
256
278
  info: logger.info,
@@ -272,9 +294,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
272
294
  res.writeHead(500, {
273
295
  'Content-Type': 'text/plain'
274
296
  });
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.');
278
298
  }
279
299
  ```
280
300
 
@@ -401,14 +421,14 @@ The following options are provided by the underlying [http-proxy](https://github
401
421
  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.
402
422
 
403
423
  ```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'});
406
426
 
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'});
409
429
 
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});
412
432
  ```
413
433
 
414
434
  ### app.use(path, proxy)
@@ -417,10 +437,7 @@ If you want to use the server's `app.use` `path` parameter to match requests;
417
437
  Create and mount the proxy without the http-proxy-middleware `context` parameter:
418
438
 
419
439
  ```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 }));
424
441
  ```
425
442
 
426
443
  `app.use` documentation:
@@ -433,13 +450,13 @@ app.use(
433
450
 
434
451
  ```javascript
435
452
  // verbose api
436
- proxy('/', { target: 'http://echo.websocket.org', ws: true });
453
+ createProxyMiddleware('/', { target: 'http://echo.websocket.org', ws: true });
437
454
 
438
455
  // shorthand
439
- proxy('http://echo.websocket.org', { ws: true });
456
+ createProxyMiddleware('http://echo.websocket.org', { ws: true });
440
457
 
441
458
  // shorter shorthand
442
- proxy('ws://echo.websocket.org');
459
+ createProxyMiddleware('ws://echo.websocket.org');
443
460
  ```
444
461
 
445
462
  ### External WebSocket upgrade
@@ -447,12 +464,12 @@ proxy('ws://echo.websocket.org');
447
464
  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.
448
465
 
449
466
  ```javascript
450
- var wsProxy = proxy('ws://echo.websocket.org', { changeOrigin: true });
467
+ const wsProxy = createProxyMiddleware('ws://echo.websocket.org', { changeOrigin: true });
451
468
 
452
- var app = express();
469
+ const app = express();
453
470
  app.use(wsProxy);
454
471
 
455
- var server = app.listen(3000);
472
+ const server = app.listen(3000);
456
473
  server.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade'
457
474
  ```
458
475
 
package/dist/handlers.js CHANGED
@@ -13,14 +13,7 @@ function init(proxy, option) {
13
13
  exports.init = init;
14
14
  function getHandlers(options) {
15
15
  // https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events
16
- const proxyEvents = [
17
- 'error',
18
- 'proxyReq',
19
- 'proxyReqWs',
20
- 'proxyRes',
21
- 'open',
22
- 'close'
23
- ];
16
+ const proxyEvents = ['error', 'proxyReq', 'proxyReqWs', 'proxyRes', 'open', 'close'];
24
17
  const handlers = {};
25
18
  for (const event of proxyEvents) {
26
19
  // all handlers for the http-proxy events are prefixed with 'on'.
@@ -1,4 +1,4 @@
1
- import { Filter, IRequestHandler, Options } from './types';
1
+ import { Filter, RequestHandler, Options } from './types';
2
2
  export declare class HttpProxyMiddleware {
3
3
  private logger;
4
4
  private config;
@@ -7,7 +7,7 @@ export declare class HttpProxyMiddleware {
7
7
  private proxy;
8
8
  private pathRewriter;
9
9
  constructor(context: Filter | Options, opts?: Options);
10
- middleware: IRequestHandler;
10
+ middleware: RequestHandler;
11
11
  private catchUpgradeRequest;
12
12
  private handleUpgrade;
13
13
  /**
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { Filter, Options } from './types';
2
- export declare function createProxyMiddleware(context: Filter | Options, options?: Options): import("./types").IRequestHandler;
3
- export { Options } from './types';
2
+ export declare function createProxyMiddleware(context: Filter | Options, options?: Options): import("./types").RequestHandler;
3
+ export { Filter, Options, RequestHandler } from './types';
package/dist/types.d.ts CHANGED
@@ -3,29 +3,29 @@ import * as express from 'express';
3
3
  import * as http from 'http';
4
4
  import * as httpProxy from 'http-proxy';
5
5
  import * as net from 'net';
6
- export interface IRequest extends express.Request {
6
+ export interface Request extends express.Request {
7
7
  }
8
- export interface IResponse extends express.Response {
8
+ export interface Response extends express.Response {
9
9
  }
10
- export interface IRequestHandler extends express.RequestHandler {
11
- upgrade?: (req: IRequest, socket: net.Socket, head: any) => void;
10
+ export interface RequestHandler extends express.RequestHandler {
11
+ upgrade?: (req: Request, socket: net.Socket, head: any) => void;
12
12
  }
13
- export declare type Filter = string | string[] | ((pathname: string, req: IRequest) => boolean);
13
+ export declare type Filter = string | string[] | ((pathname: string, req: Request) => boolean);
14
14
  export interface Options extends httpProxy.ServerOptions {
15
15
  pathRewrite?: {
16
16
  [regexp: string]: string;
17
- } | ((path: string, req: IRequest) => string) | ((path: string, req: IRequest) => Promise<string>);
17
+ } | ((path: string, req: Request) => string) | ((path: string, req: Request) => Promise<string>);
18
18
  router?: {
19
19
  [hostOrPath: string]: string;
20
- } | ((req: IRequest) => string) | ((req: IRequest) => Promise<string>);
20
+ } | ((req: Request) => string) | ((req: Request) => Promise<string>);
21
21
  logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent';
22
22
  logProvider?(provider: LogProvider): LogProvider;
23
- onError?(err: Error, req: IRequest, res: IResponse): void;
24
- onProxyRes?(proxyRes: http.ServerResponse, req: IRequest, res: IResponse): void;
25
- onProxyReq?(proxyReq: http.ClientRequest, req: IRequest, res: IResponse): void;
26
- onProxyReqWs?(proxyReq: http.ClientRequest, req: IRequest, socket: net.Socket, options: httpProxy.ServerOptions, head: any): void;
23
+ onError?(err: Error, req: Request, res: Response): void;
24
+ onProxyRes?(proxyRes: http.ServerResponse, req: Request, res: Response): void;
25
+ onProxyReq?(proxyReq: http.ClientRequest, req: Request, res: Response): void;
26
+ onProxyReqWs?(proxyReq: http.ClientRequest, req: Request, socket: net.Socket, options: httpProxy.ServerOptions, head: any): void;
27
27
  onOpen?(proxySocket: net.Socket): void;
28
- onClose?(res: IResponse, socket: net.Socket, head: any): void;
28
+ onClose?(res: Response, socket: net.Socket, head: any): void;
29
29
  }
30
30
  interface LogProvider {
31
31
  log: Logger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "http-proxy-middleware",
3
- "version": "0.22.0-alpha",
3
+ "version": "1.0.0",
4
4
  "description": "The one-liner node.js proxy middleware for connect, express and browser-sync",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -48,7 +48,7 @@
48
48
  "bugs": {
49
49
  "url": "https://github.com/chimurai/http-proxy-middleware/issues"
50
50
  },
51
- "homepage": "https://github.com/chimurai/http-proxy-middleware",
51
+ "homepage": "https://github.com/chimurai/http-proxy-middleware#readme",
52
52
  "devDependencies": {
53
53
  "@commitlint/cli": "^8.0.0",
54
54
  "@commitlint/config-conventional": "^8.0.0",