Skip to content

Commit 72dd7e4

Browse files
committed
fix(react): configure server and route hooks
1 parent 0dd8d91 commit 72dd7e4

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

packages/fastify-react/routing.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ export async function createRoute ({ client, errorHandler, route }, scope, confi
120120
// Replace wildcard routes with Fastify compatible syntax
121121
const routePath = route.path.replace(/:[^+]+\+/, '*')
122122

123+
unshiftHook(route, 'onRequest', onRequest)
124+
unshiftHook(route, 'preHandler', preHandler)
125+
123126
scope.route({
124127
url: routePath,
125128
method: route.method ?? ['GET', 'POST', 'PUT', 'DELETE'],
126129
errorHandler,
127-
onRequest,
128-
preHandler,
129130
handler,
130131
...route,
131132
})
@@ -140,3 +141,16 @@ export async function createRoute ({ client, errorHandler, route }, scope, confi
140141
})
141142
}
142143
}
144+
145+
function unshiftHook (route, hookName, hook) {
146+
if (!route[hookName]) {
147+
route[hookName] = []
148+
}
149+
if (!Array.isArray(hook)) {
150+
hook = [hook]
151+
}
152+
if (!Array.isArray(route[hookName])) {
153+
route[hookName] = [route[hookName]]
154+
}
155+
route[hookName] = [...route[hookName], ...hook]
156+
}

0 commit comments

Comments
 (0)