@@ -7,24 +7,36 @@ export default fp(
7
7
*/
8
8
( fastify , layout , done ) => {
9
9
// Decorate reply with .app.podium we can write to throughout the request
10
- fastify . decorateReply ( 'app' , null ) ;
10
+ if ( ! fastify . hasReplyDecorator ( 'app' ) ) {
11
+ fastify . decorateReply ( 'app' , null ) ;
12
+ }
13
+
11
14
fastify . addHook ( 'onRequest' , async ( request , reply ) => {
12
- // namespace
15
+ // @ts -ignore We decorate this above
16
+ reply . app = reply . app || { } ;
17
+
13
18
// @ts -ignore We type this for our consumers with fixup.sh
14
19
reply . app = reply . app || { } ;
20
+
15
21
// used to pass additional values to HttpIncoming
16
22
// @ts -ignore We type this for our consumers with fixup.sh
17
23
reply . app . params = reply . app . params || { } ;
18
- // used to hold the HttpIncoming object
19
- // @ts -ignore We type this for our consumers with fixup.sh
20
- reply . app . podium = new HttpIncoming (
24
+ } ) ;
25
+
26
+ // Run as late as we can (but before the route handler) so other plugins
27
+ // can add to `reply.app.params` using either of the hooks in
28
+ // the lifecycle before it https://fastify.dev/docs/latest/Reference/Hooks/#hooks
29
+ fastify . addHook ( 'preHandler' , async ( request , reply ) => {
30
+ const incoming = new HttpIncoming (
21
31
request . raw ,
22
32
reply . raw ,
23
33
// @ts -ignore We type this for our consumers with fixup.sh
24
34
reply . app . params ,
25
35
) ;
36
+
37
+ // used to hold the HttpIncoming object
26
38
// @ts -ignore We type this for our consumers with fixup.sh
27
- reply . app . podium = await layout . process ( reply . app . podium , {
39
+ reply . app . podium = await layout . process ( incoming , {
28
40
proxy : false ,
29
41
} ) ;
30
42
} ) ;
0 commit comments