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" ;
9
9
10
- import { WrongArgumentError } from ' ./errors.js' ;
11
- import { log } from ' ./logger.js' ;
10
+ import { WrongArgumentError } from " ./errors.js" ;
11
+ import { log } from " ./logger.js" ;
12
12
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" ;
15
14
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 > } ) => {
21
16
if ( ! fileField ?. file ) {
22
17
return null ;
23
18
}
@@ -26,44 +21,32 @@ const getFile = (fileField?: {
26
21
return file ;
27
22
} ;
28
23
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 > => {
33
25
const { assets } = AdminRouter ;
34
- if ( admin ?. constructor ?. name !== ' AdminJS' ) {
26
+ if ( admin ?. constructor ?. name !== " AdminJS" ) {
35
27
throw new WrongArgumentError ( INVALID_ADMIN_JS_INSTANCE ) ;
36
28
}
37
29
38
- await fastifyApp . register ( fastifyMultipart , { attachFieldsToBody : true } ) ;
30
+ // await fastifyApp.register(fastifyMultipart, { attachFieldsToBody: true });
39
31
40
32
admin . initialize ( ) . then ( ( ) => {
41
- log . debug ( ' AdminJS: bundle ready' ) ;
33
+ log . debug ( " AdminJS: bundle ready" ) ;
42
34
} ) ;
43
35
44
36
const { routes } = AdminRouter ;
45
37
46
- routes . forEach ( route => {
38
+ routes . forEach ( ( route ) => {
47
39
// 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, "" ) ;
49
41
50
42
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 ) ;
55
44
const { params, query } = request ;
56
45
const method = request . method . toLowerCase ( ) ;
57
46
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 } > ;
62
48
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 ] ] )
67
50
) ;
68
51
const html = await controller [ route . action ] (
69
52
{
@@ -78,35 +61,32 @@ export const buildRouter = async (
78
61
79
62
if ( route . contentType ) {
80
63
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" ) ;
83
66
}
84
67
if ( html ) {
85
68
return reply . send ( html ) ;
86
69
}
87
70
} ;
88
71
89
- if ( route . method === ' GET' ) {
72
+ if ( route . method === " GET" ) {
90
73
fastifyApp . get ( `${ admin . options . rootPath } ${ path } ` , handler ) ;
91
74
}
92
75
93
- if ( route . method === ' POST' ) {
76
+ if ( route . method === " POST" ) {
94
77
fastifyApp . post ( `${ admin . options . rootPath } ${ path } ` , handler ) ;
95
78
}
96
79
} ) ;
97
80
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 ) ) ;
104
85
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 ) ;
109
88
}
110
- ) ;
89
+ return reply . send ( file ) ;
90
+ } ) ;
111
91
} ) ;
112
92
} ;
0 commit comments