1- import fastifyMultipart from ' @fastify/multipart' ;
2- import AdminJS , { Router as AdminRouter } from ' adminjs' ;
3- import { FastifyInstance } from ' fastify' ;
4- import { RouteHandlerMethod } from ' fastify/types/route.js' ;
5- import { readFile } from ' fs/promises' ;
6- import fromPairs from ' lodash/fromPairs.js' ;
7- import * as mime from ' mime-types' ;
8- import path from ' path' ;
1+ import fastifyMultipart from " @fastify/multipart" ;
2+ import AdminJS , { Router as AdminRouter } from " adminjs" ;
3+ import { FastifyInstance } from " fastify" ;
4+ import { RouteHandlerMethod } from " fastify/types/route.js" ;
5+ import { readFile } from " fs/promises" ;
6+ import fromPairs from " lodash/fromPairs.js" ;
7+ import * as mime from " mime-types" ;
8+ import path from " path" ;
99
10- import { WrongArgumentError } from ' ./errors.js' ;
11- import { log } from ' ./logger.js' ;
10+ import { WrongArgumentError } from " ./errors.js" ;
11+ import { log } from " ./logger.js" ;
1212
13- const INVALID_ADMIN_JS_INSTANCE =
14- 'You have to pass an instance of AdminJS to the buildRouter() function' ;
13+ const INVALID_ADMIN_JS_INSTANCE = "You have to pass an instance of AdminJS to the buildRouter() function" ;
1514
16- const getFile = ( fileField ?: {
17- fieldname : string ;
18- filename : string ;
19- file : Record < string , unknown > ;
20- } ) => {
15+ const getFile = ( fileField ?: { fieldname : string ; filename : string ; file : Record < string , unknown > } ) => {
2116 if ( ! fileField ?. file ) {
2217 return null ;
2318 }
@@ -26,44 +21,32 @@ const getFile = (fileField?: {
2621 return file ;
2722} ;
2823
29- export const buildRouter = async (
30- admin : AdminJS ,
31- fastifyApp : FastifyInstance
32- ) : Promise < void > => {
24+ export const buildRouter = async ( admin : AdminJS , fastifyApp : FastifyInstance ) : Promise < void > => {
3325 const { assets } = AdminRouter ;
34- if ( admin ?. constructor ?. name !== ' AdminJS' ) {
26+ if ( admin ?. constructor ?. name !== " AdminJS" ) {
3527 throw new WrongArgumentError ( INVALID_ADMIN_JS_INSTANCE ) ;
3628 }
3729
38- await fastifyApp . register ( fastifyMultipart , { attachFieldsToBody : true } ) ;
30+ // await fastifyApp.register(fastifyMultipart, { attachFieldsToBody: true });
3931
4032 admin . initialize ( ) . then ( ( ) => {
41- log . debug ( ' AdminJS: bundle ready' ) ;
33+ log . debug ( " AdminJS: bundle ready" ) ;
4234 } ) ;
4335
4436 const { routes } = AdminRouter ;
4537
46- routes . forEach ( route => {
38+ routes . forEach ( ( route ) => {
4739 // we have to change routes defined in AdminJS from {recordId} to :recordId
48- const path = route . path . replace ( / { / g, ':' ) . replace ( / } / g, '' ) ;
40+ const path = route . path . replace ( / { / g, ":" ) . replace ( / } / g, "" ) ;
4941
5042 const handler : RouteHandlerMethod = async ( request , reply ) => {
51- const controller = new route . Controller (
52- { admin } ,
53- request . session ?. adminUser
54- ) ;
43+ const controller = new route . Controller ( { admin } , request . session ?. adminUser ) ;
5544 const { params, query } = request ;
5645 const method = request . method . toLowerCase ( ) ;
5746
58- const body = request . body as Record <
59- string ,
60- { value : string ; file ?: File }
61- > ;
47+ const body = request . body as Record < string , { value : string ; file ?: File } > ;
6248 const fields = fromPairs (
63- Object . keys ( ( body ?? { } ) as Record < string , unknown > ) . map ( key => [
64- key ,
65- getFile ( body [ key ] as any ) ?? body [ key ] . value ?? body [ key ] ,
66- ] )
49+ Object . keys ( ( body ?? { } ) as Record < string , unknown > ) . map ( ( key ) => [ key , getFile ( body [ key ] as any ) ?? body [ key ] . value ?? body [ key ] ] )
6750 ) ;
6851 const html = await controller [ route . action ] (
6952 {
@@ -78,35 +61,32 @@ export const buildRouter = async (
7861
7962 if ( route . contentType ) {
8063 reply . type ( route . contentType ) ;
81- } else if ( typeof html === ' string' ) {
82- reply . type ( ' text/html' ) ;
64+ } else if ( typeof html === " string" ) {
65+ reply . type ( " text/html" ) ;
8366 }
8467 if ( html ) {
8568 return reply . send ( html ) ;
8669 }
8770 } ;
8871
89- if ( route . method === ' GET' ) {
72+ if ( route . method === " GET" ) {
9073 fastifyApp . get ( `${ admin . options . rootPath } ${ path } ` , handler ) ;
9174 }
9275
93- if ( route . method === ' POST' ) {
76+ if ( route . method === " POST" ) {
9477 fastifyApp . post ( `${ admin . options . rootPath } ${ path } ` , handler ) ;
9578 }
9679 } ) ;
9780
98- assets . forEach ( asset => {
99- fastifyApp . get (
100- `${ admin . options . rootPath } ${ asset . path } ` ,
101- async ( _req , reply ) => {
102- const mimeType = mime . lookup ( asset . src )
103- const file = await readFile ( path . resolve ( asset . src ) )
81+ assets . forEach ( ( asset ) => {
82+ fastifyApp . get ( `${ admin . options . rootPath } ${ asset . path } ` , async ( _req , reply ) => {
83+ const mimeType = mime . lookup ( asset . src ) ;
84+ const file = await readFile ( path . resolve ( asset . src ) ) ;
10485
105- if ( mimeType ) {
106- return reply . type ( mimeType ) . send ( file ) ;
107- }
108- return reply . send ( file ) ;
86+ if ( mimeType ) {
87+ return reply . type ( mimeType ) . send ( file ) ;
10988 }
110- ) ;
89+ return reply . send ( file ) ;
90+ } ) ;
11191 } ) ;
11292} ;
0 commit comments