File tree Expand file tree Collapse file tree 3 files changed +33
-2
lines changed Expand file tree Collapse file tree 3 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,13 @@ Rewrite the prefix to the specified string. Default: `''`.
94
94
95
95
A ` preHandler ` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication).
96
96
97
+ ### config
98
+
99
+ An object accessible within the ` preHandler ` via ` reply.context.config ` .
100
+ See [ Config] ( https://www.fastify.io/docs/v2.1.x/Routes/#config ) in the Fastify
101
+ documentation for information on this option. Note: this is merged with other
102
+ configuration passed to the route.
103
+
97
104
### replyOptions
98
105
99
106
Object with [ reply options] ( https://github.com/fastify/fastify-reply-from#replyfromsource-opts ) for ` fastify-reply-from ` .
Original file line number Diff line number Diff line change @@ -40,8 +40,8 @@ module.exports = async function (fastify, opts) {
40
40
done ( null , req )
41
41
}
42
42
43
- fastify . all ( '/' , { preHandler } , reply )
44
- fastify . all ( '/*' , { preHandler } , reply )
43
+ fastify . all ( '/' , { preHandler, config : opts . config || { } } , reply )
44
+ fastify . all ( '/*' , { preHandler, config : opts . config || { } } , reply )
45
45
46
46
function reply ( request , reply ) {
47
47
var dest = request . req . url
Original file line number Diff line number Diff line change @@ -141,6 +141,30 @@ async function run () {
141
141
t . ok ( errored )
142
142
} )
143
143
144
+ test ( 'preHandler gets config' , async t => {
145
+ const server = Fastify ( )
146
+ server . register ( proxy , {
147
+ upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
148
+ config : { foo : 'bar' } ,
149
+ async preHandler ( request , reply ) {
150
+ t . deepEqual ( reply . context . config , { foo : 'bar' , url : '/*' } )
151
+ throw new Unauthorized ( )
152
+ }
153
+ } )
154
+
155
+ await server . listen ( 0 )
156
+ t . tearDown ( server . close . bind ( server ) )
157
+
158
+ var errored = false
159
+ try {
160
+ await got ( `http://localhost:${ server . server . address ( ) . port } ` )
161
+ } catch ( err ) {
162
+ t . equal ( err . statusCode , 401 )
163
+ errored = true
164
+ }
165
+ t . ok ( errored )
166
+ } )
167
+
144
168
test ( 'beforeHandler(deprecated)' , async t => {
145
169
const server = Fastify ( )
146
170
server . register ( proxy , {
You can’t perform that action at this time.
0 commit comments