Files
freeCodeCamp/api/src/plugins/growth-book.ts
Mrugesh Mohapatra 3c822da243 fix(api): catch GB init errors on prod only (#59912)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2025-04-24 10:26:33 +05:30

30 lines
756 B
TypeScript

import { GrowthBook, Options } from '@growthbook/growthbook';
import { FastifyPluginAsync } from 'fastify';
import fp from 'fastify-plugin';
import { FREECODECAMP_NODE_ENV } from '../utils/env';
declare module 'fastify' {
interface FastifyInstance {
gb: GrowthBook;
}
}
const growthBook: FastifyPluginAsync<Options> = async (fastify, options) => {
const gb = new GrowthBook(options);
const res = await gb.init({ timeout: 3000 });
if (res.error && FREECODECAMP_NODE_ENV === 'production') {
fastify.log.error(res.error, 'Failed to initialize GrowthBook');
fastify.Sentry.captureException(res.error);
}
fastify.decorate('gb', gb);
fastify.addHook('onClose', () => {
gb.destroy();
});
};
export default fp(growthBook);