@@ -18,20 +18,13 @@ import { Annotation, type ServiceMeta } from '@athenna/ioc'
1818import { File , Path , Module , String } from '@athenna/common'
1919import { HttpExceptionHandler } from '#src/handlers/HttpExceptionHandler'
2020
21- const corsPlugin = await Module . safeImport ( '@fastify/cors' )
22- const helmetPlugin = await Module . safeImport ( '@fastify/helmet' )
23- const swaggerPlugin = await Module . safeImport ( '@fastify/swagger' )
24- const swaggerUiPlugin = await Module . safeImport ( '@fastify/swagger-ui' )
25- const rateLimitPlugin = await Module . safeImport ( '@fastify/rate-limit' )
26- const staticPlugin = await Module . safeImport ( '@fastify/static' )
27- const rTracerPlugin = await Module . safeImport ( 'cls-rtracer' )
28- const vitePlugin = await Module . safeImport ( '@athenna/vite/plugins/fastify' )
29-
3021export class HttpKernel {
3122 /**
3223 * Register the @fastify/cors plugin in the Http server.
3324 */
3425 public async registerCors ( ) : Promise < void > {
26+ const corsPlugin = await Module . safeImport ( '@fastify/cors' )
27+
3528 if ( Config . is ( 'http.cors.enabled' , false ) ) {
3629 debug (
3730 'Not able to register cors plugin. Set the http.cors.enabled configuration as true.'
@@ -53,6 +46,8 @@ export class HttpKernel {
5346 * Register the @fastify/helmet plugin in the Http server.
5447 */
5548 public async registerHelmet ( ) : Promise < void > {
49+ const helmetPlugin = await Module . safeImport ( '@fastify/helmet' )
50+
5651 if ( Config . is ( 'http.helmet.enabled' , false ) ) {
5752 debug (
5853 'Not able to register helmet plugin. Set the http.helmet.enabled configuration as true.'
@@ -76,6 +71,9 @@ export class HttpKernel {
7671 * Register the @fastify/swagger plugin in the Http server.
7772 */
7873 public async registerSwagger ( ) : Promise < void > {
74+ const swaggerPlugin = await Module . safeImport ( '@fastify/swagger' )
75+ const swaggerUiPlugin = await Module . safeImport ( '@fastify/swagger-ui' )
76+
7977 if ( Config . is ( 'http.swagger.enabled' , false ) ) {
8078 debug (
8179 'Not able to register swagger plugin. Set the http.swagger.enabled configuration as true.'
@@ -129,6 +127,8 @@ export class HttpKernel {
129127 * Register the @fastify/rate-limit plugin in the Http server.
130128 */
131129 public async registerRateLimit ( ) : Promise < void > {
130+ const rateLimitPlugin = await Module . safeImport ( '@fastify/rate-limit' )
131+
132132 if ( Config . is ( 'http.rateLimit.enabled' , false ) ) {
133133 debug (
134134 'Not able to register rate limit plugin. Set the http.rateLimit.enabled configuration as true.'
@@ -152,6 +152,8 @@ export class HttpKernel {
152152 * Register the @fastify/static plugin in the Http server.
153153 */
154154 public async registerStatic ( ) : Promise < void > {
155+ const staticPlugin = await Module . safeImport ( '@fastify/static' )
156+
155157 if ( Config . is ( 'http.static.enabled' , false ) ) {
156158 debug (
157159 'Not able to register static plugin. Set the http.static.enabled configuration as true.'
@@ -175,6 +177,8 @@ export class HttpKernel {
175177 * Register the cls-rtracer plugin in the Http server.
176178 */
177179 public async registerRTracer ( trace ?: boolean ) : Promise < void > {
180+ const rTracerPlugin = await Module . safeImport ( 'cls-rtracer' )
181+
178182 if ( trace === false ) {
179183 debug (
180184 'Not able to register rTracer plugin. Set the trace option as true in your http server options.'
@@ -209,6 +213,8 @@ export class HttpKernel {
209213 * Register the @athenna/vite plugin in the Http server.
210214 */
211215 public async registerVite ( trace ?: boolean ) : Promise < void > {
216+ const vitePlugin = await Module . safeImport ( '@athenna/vite/plugins/fastify' )
217+
212218 if ( trace === false ) {
213219 debug (
214220 'Not able to register vite plugin. Set the trace option as true in your http server options.'
@@ -234,6 +240,31 @@ export class HttpKernel {
234240 await Server . plugin ( vitePlugin , this . getConfig ( 'http.vite' ) )
235241 }
236242
243+ /**
244+ * Register the @fastify/multipart plugin in the Http server.
245+ */
246+ public async registerMultipart ( ) : Promise < void > {
247+ const multipartPlugin = await Module . safeImport ( '@fastify/multipart' )
248+
249+ if ( Config . is ( 'http.multipart.enabled' , false ) ) {
250+ debug (
251+ 'Not able to register multipart plugin. Set the http.multipart.enabled configuration as true.'
252+ )
253+
254+ return
255+ }
256+
257+ if ( ! multipartPlugin ) {
258+ debug (
259+ 'Not able to register multipart plugin. Install @fastify/multipart package.'
260+ )
261+
262+ return
263+ }
264+
265+ await Server . plugin ( multipartPlugin , this . getConfig ( 'http.multipart' ) )
266+ }
267+
237268 /**
238269 * Register the global log terminator in the Http server.
239270 */
0 commit comments