hops-development-proxy
Advanced tools
Comparing version 11.0.0-rc.53 to 11.0.0-rc.54
@@ -6,2 +6,13 @@ # Change Log | ||
# [11.0.0-rc.54](https://github.com/xing/hops/compare/v11.0.0-rc.53...v11.0.0-rc.54) (2018-12-12) | ||
### Features | ||
* **development-proxy:** add proxy mixin hooks ([4aaae00](https://github.com/xing/hops/commit/4aaae00)) | ||
# [11.0.0-rc.53](https://github.com/xing/hops/compare/v11.0.0-rc.52...v11.0.0-rc.53) (2018-12-10) | ||
@@ -8,0 +19,0 @@ |
const debug = require('debug')('hops:development-proxy'); | ||
const { Mixin } = require('hops-mixin'); | ||
const { | ||
Mixin, | ||
strategies: { | ||
sync: { sequence }, | ||
}, | ||
} = require('hops-mixin'); | ||
const proxy = require('http-proxy-middleware'); | ||
@@ -13,2 +18,8 @@ | ||
const hooks = { | ||
onProxyReq: this.onProxyReq, | ||
onProxyRes: this.onProxyRes, | ||
onError: this.onProxyError, | ||
}; | ||
if (typeof proxyConfig === 'string') { | ||
@@ -24,7 +35,10 @@ debug('Using proxy string version: ', proxyConfig); | ||
}, | ||
{ | ||
target: proxyConfig, | ||
changeOrigin: true, | ||
logLevel: 'warn', | ||
} | ||
Object.assign( | ||
{ | ||
target: proxyConfig, | ||
changeOrigin: true, | ||
logLevel: 'warn', | ||
}, | ||
hooks | ||
) | ||
) | ||
@@ -37,3 +51,3 @@ ); | ||
Object.entries(proxyConfig).forEach(([path, config]) => { | ||
Object.entries(proxyConfig).forEach(([path, { target }]) => { | ||
middlewares.initial.push( | ||
@@ -43,8 +57,8 @@ proxy( | ||
Object.assign( | ||
{}, | ||
{ | ||
changeOrigin: true, | ||
logLevel: 'warn', | ||
target, | ||
}, | ||
config | ||
hooks | ||
) | ||
@@ -58,2 +72,7 @@ ) | ||
ProxyMixin.strategies = { | ||
onProxyReq: sequence, | ||
onProxyRes: sequence, | ||
onProxyError: sequence, | ||
}; | ||
module.exports = ProxyMixin; |
{ | ||
"name": "hops-development-proxy", | ||
"version": "11.0.0-rc.53", | ||
"version": "11.0.0-rc.54", | ||
"description": "Proxy", | ||
@@ -18,6 +18,6 @@ "keywords": [ | ||
"debug": "^4.0.0", | ||
"hops-mixin": "^11.0.0-rc.53", | ||
"hops-mixin": "^11.0.0-rc.54", | ||
"http-proxy-middleware": "^0.19.0" | ||
}, | ||
"gitHead": "8ee649ed95dbd1a4b5b43a21062e36824df98fd4" | ||
"gitHead": "ab69660331bb4c5355f9a19c48b262592437b856" | ||
} |
@@ -54,3 +54,3 @@ # `hops-development-proxy` | ||
If more flexibility is needed, `proxy` can also be configured as an object, where the keys represent express route matchers to be proxied and the values configure the respective proxy. In the config, any [http-proxy-middleware option](https://github.com/chimurai/http-proxy-middleware#options) can be used. | ||
If more flexibility is needed, `proxy` can also be configured as an object, where the keys represent express route matchers to be proxied and `target` to configure the respective proxy target. | ||
@@ -70,21 +70,32 @@ `.hopsrc.js` | ||
### Examples | ||
### Mixin Hooks API | ||
#### Adding request headers | ||
#### `onProxyReq(): void` ([sequence](https://github.com/untool/mixinable/blob/master/README.md#definesequence)) **core** | ||
Sometimes additional request headers are needed, for example to add a fake user id for the backend api. Since any [http-proxy-middleware option](https://github.com/chimurai/http-proxy-middleware#options) can be used, you can make use of the `onProxyReq` callback to add additional headers. | ||
Hook to implement [http-proxy-middleware `onProxyReq` event callback](https://github.com/chimurai/http-proxy-middleware#http-proxy-events). | ||
`.hopsrc.js` | ||
##### Example: Add request headers | ||
Sometimes additional request headers are needed, for example to add a fake user id for the backend api. | ||
`mixin.core.js` | ||
```javascript | ||
module.exports = { | ||
proxy: { | ||
'/api': { | ||
target: 'https://httpbin.org/anything/', | ||
onProxyReq(proxyReq) { | ||
proxyReq.setHeader('x-USER-ID', '27'); | ||
}, | ||
}, | ||
}, | ||
}; | ||
const { Mixin } = require('hops'); | ||
class MyMixin extends Mixin { | ||
onProxyReq(proxyReq) { | ||
proxyReq.setHeader('X-USER-ID', '23'); | ||
} | ||
} | ||
module.exports = MyMixin; | ||
``` | ||
#### `onProxyRes(): void` ([sequence](https://github.com/untool/mixinable/blob/master/README.md#definesequence)) **core** | ||
Hook to implement [http-proxy-middleware `onProxyRes` event callback](https://github.com/chimurai/http-proxy-middleware#http-proxy-events). | ||
#### `onProxyError(): void` ([sequence](https://github.com/untool/mixinable/blob/master/README.md#definesequence)) **core** | ||
Hook to implement [http-proxy-middleware `onError` event callback](https://github.com/chimurai/http-proxy-middleware#http-proxy-events). |
12610
68
100
Updatedhops-mixin@^11.0.0-rc.54