@@ -17,7 +17,6 @@ import fastify, {
1717import { String } from '@secjs/utils'
1818import { Request } from './Context/Request'
1919import { Response } from './Context/Response'
20- import { RouteContract } from './Contracts/RouteContract'
2120import { HttpMethodTypes } from './Contracts/HttpMethodTypes'
2221import { HandlerContract } from './Contracts/Context/HandlerContract'
2322import { FastifyHandlerContract } from './Contracts/FastifyHandlerContract'
@@ -29,14 +28,9 @@ declare module 'fastify' {
2928}
3029
3130export class Http {
32- private readonly routes : RouteContract [ ]
3331 private readonly server : FastifyInstance
34- private readonly middlewares : HandlerContract [ ]
3532
3633 constructor ( ) {
37- this . routes = [ ]
38- this . middlewares = [ ]
39-
4034 this . server = fastify ( )
4135 this . server . setErrorHandler ( Http . defaultErrorHandler )
4236 }
@@ -68,23 +62,22 @@ export class Http {
6862 private createFastifyHandler (
6963 handler : ( ctx ) => Promise < void > | void ,
7064 ) : FastifyHandlerContract {
71- return async ( req : FastifyRequest , res : FastifyReply , next ?: any ) => {
65+ return async ( req : FastifyRequest , res : FastifyReply ) => {
7266 const request = new Request ( req )
7367 const response = new Response ( res )
7468
7569 if ( ! req . data ) req . data = { }
7670 if ( ! req . query ) req . query = { }
7771 if ( ! req . params ) req . params = { }
78- // eslint-disable-next-line @typescript-eslint/no-empty-function
79- if ( ! next ) next = ( ) => { }
8072
8173 return handler ( {
8274 request,
8375 response,
8476 params : req . params ,
8577 queries : req . query ,
8678 data : req . data ,
87- next,
79+ // eslint-disable-next-line @typescript-eslint/no-empty-function
80+ next : ( ) => { } ,
8881 } )
8982 }
9083 }
@@ -93,24 +86,38 @@ export class Http {
9386 return this . server
9487 }
9588
96- use ( handler : HandlerContract ) {
97- this . middlewares . push ( handler )
98- }
99-
10089 getRoutes ( options ?: PrintRoutesOptions ) {
10190 return this . server . printRoutes ( options )
10291 }
10392
104- async listen (
93+ use ( handler : HandlerContract ) {
94+ this . server . addHook ( 'preHandler' , ( req , res , done ) => {
95+ const request = new Request ( req )
96+ const response = new Response ( res )
97+
98+ if ( ! req . data ) req . data = { }
99+ if ( ! req . query ) req . query = { }
100+ if ( ! req . params ) req . params = { }
101+
102+ return handler ( {
103+ request,
104+ response,
105+ params : req . params as Record < string , string > ,
106+ queries : req . query as Record < string , string > ,
107+ data : req . data ,
108+ next : done ,
109+ } )
110+ } )
111+ }
112+
113+ listen (
105114 port ?: number ,
106115 cb ?: ( err : Error | null , address : string ) => void ,
107- ) : Promise < void > {
108- this . routes . forEach ( route => this . server . route ( route ) )
109-
116+ ) : void | string {
110117 return this . server . listen ( port || 1335 , cb )
111118 }
112119
113- async close ( cb ?: ( ) => void ) : Promise < void > {
120+ close ( cb ?: ( ) => void ) : void {
114121 return this . server . close ( cb )
115122 }
116123
@@ -120,23 +127,12 @@ export class Http {
120127 handler : HandlerContract ,
121128 middlewares ?: HandlerContract [ ] ,
122129 ) {
123- let allMiddlewares = this . middlewares
124-
125- if ( middlewares && middlewares . length ) {
126- allMiddlewares = allMiddlewares . concat ( middlewares )
127- }
128-
129- const preHandlers : FastifyHandlerContract [ ] = allMiddlewares . map ( mid =>
130- this . createFastifyHandler ( mid ) ,
131- )
132-
133- methods . forEach ( method => {
134- this . routes . push ( {
135- url,
136- method,
137- preHandler : preHandlers ,
138- handler : this . createFastifyHandler ( handler ) ,
139- } )
130+ this . server . route ( {
131+ url,
132+ method : methods ,
133+ handler : this . createFastifyHandler ( handler ) ,
134+ preHandler :
135+ middlewares && middlewares . map ( mid => this . createFastifyHandler ( mid ) ) ,
140136 } )
141137 }
142138
0 commit comments