diff --git a/packages/sample-server/src/app.ts b/packages/sample-server/src/app.ts index 697b75d..ed2b958 100644 --- a/packages/sample-server/src/app.ts +++ b/packages/sample-server/src/app.ts @@ -15,12 +15,12 @@ const build = (opts: buildOpts = {}): FastifyInstance => { app.addSchema(presentationRequestSchema); app.addSchema(errorSchema); + app.register(vclSdkPlugin); + app.register(autoload, { dir: path.join(__dirname, "routes"), }); - app.register(vclSdkPlugin); - return app; }; diff --git a/packages/sample-server/src/plugins/vclSdkPlugin.ts b/packages/sample-server/src/plugins/vclSdkPlugin.ts index ab8eb95..d50f40e 100644 --- a/packages/sample-server/src/plugins/vclSdkPlugin.ts +++ b/packages/sample-server/src/plugins/vclSdkPlugin.ts @@ -22,7 +22,7 @@ import { import { CurrentEnvironment, XVnfProtocolVersion } from "../global-config"; const vclSdkPlugin: FastifyPluginAsync | FastifyPluginCallback = async ( - fastify, + fastify ) => { const vclSdk = VCLProvider.getInstance(); @@ -41,12 +41,19 @@ const vclSdkPlugin: FastifyPluginAsync | FastifyPluginCallback = async ( console.error('Failed to initialize VCL SDK', e); throw e; } - fastify.decorate('vclSdk', vclSdk) - fastify.addHook('preHandler', function (req, reply, done) { - req.vclSdk = vclSdk - reply.vclSdk = vclSdk - done() - }) + fastify + .decorate('vclSdk', vclSdk) + .decorateRequest('vclSdk', null) + .addHook('preHandler', async (req, reply, done) => { + req.vclSdk = fastify.vclSdk + reply.vclSdk = fastify.vclSdk + done() + }) + .addHook('preValidation', async (req, reply, done) => { + req.vclSdk = fastify.vclSdk + reply.vclSdk = fastify.vclSdk + done() + }); }; export default vclSdkPlugin;