Skip to content

Commit 279ce11

Browse files
committed
feat: added custom loader option, fixed #7 (thanks @niksoc)
1 parent e73598c commit 279ce11

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

src/admin.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export class AdminModule implements OnModuleInit {
6161
provide: CONFIG_TOKEN,
6262
useValue: options,
6363
},
64+
options.customLoader ? {
65+
provide: AbstractLoader,
66+
useClass: options.customLoader,
67+
} : serveStaticProvider,
6468
],
6569
}
6670
}

src/interfaces/admin-module-options.interface.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { Type } from '@nestjs/common';
12
import { AdminBroOptions, CurrentAdmin } from 'admin-bro';
23
import { SessionOptions } from 'express-session';
34

5+
import { AbstractLoader } from '../loaders/abstract.loader';
6+
47
import { ExpressFormidableOptions } from './express-formidable-options.interface';
58

69
/**
@@ -35,4 +38,9 @@ export type AdminModuleOptions = {
3538
* something more reliable (i.e. database).
3639
*/
3740
sessionOptions?: SessionOptions,
41+
/**
42+
* In order not to use default Express (@admin-bro/express) or Fastify (not yet implemented) loader
43+
* you can provide your own implementation of Loader that plugs in AdminBro.
44+
*/
45+
customLoader?: Type<AbstractLoader>,
3846
}

src/loaders/express.loader.ts

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,64 @@ export class ExpressLoader extends AbstractLoader {
2323
);
2424
loadPackage('express-formidable', '@admin-bro/nestjs');
2525

26+
let router;
27+
28+
if ('auth' in options) {
29+
loadPackage('express-session', '@admin-bro/nestjs');
30+
router = adminBroExpressjs.buildAuthenticatedRouter(
31+
admin, options.auth, undefined, options.sessionOptions, options.formidableOptions
32+
);
33+
} else {
34+
router = adminBroExpressjs.buildRouter(admin, undefined, options.formidableOptions);
35+
}
36+
37+
app.use(options.adminBroOptions.rootPath, router);
38+
this.reorderRoutes(app, options);980
39+
}
40+
41+
private reorderRoutes(app, options) {
2642
let jsonParser;
2743
let urlencodedParser;
44+
let admin;
2845

2946
// Nestjs uses bodyParser under the hood which is in conflict with admin-bro setup.
3047
// Due to admin-bro-expressjs usage of formidable we have to move body parser in layer tree after admin-bro init.
3148
// Notice! This is not documented feature of express, so this may change in the future. We have to keep an eye on it.
3249
if (app && app._router && app._router.stack) {
33-
const jsonParserIndex = app._router.stack.findIndex(layer => layer.name === 'jsonParser');
50+
const jsonParserIndex = app._router.stack.findIndex(
51+
(layer: { name: string }) => layer.name === 'jsonParser'
52+
);
3453
if (jsonParserIndex >= 0) {
3554
jsonParser = app._router.stack.splice(jsonParserIndex, 1);
3655
}
3756

38-
const urlencodedParserIndex = app._router.stack.findIndex(layer => layer.name === 'urlencodedParser');
57+
const urlencodedParserIndex = app._router.stack.findIndex(
58+
(layer: { name: string }) => layer.name === 'urlencodedParser'
59+
);
3960
if (urlencodedParserIndex >= 0) {
4061
urlencodedParser = app._router.stack.splice(urlencodedParserIndex, 1);
4162
}
42-
}
43-
44-
let router;
4563

46-
if ('auth' in options) {
47-
loadPackage('express-session', '@admin-bro/nestjs');
48-
router = adminBroExpressjs.buildAuthenticatedRouter(
49-
admin, options.auth, undefined, options.sessionOptions, options.formidableOptions
50-
);
51-
} else {
52-
router = adminBroExpressjs.buildRouter(admin, undefined, options.formidableOptions);
53-
}
54-
55-
app.use(options.adminBroOptions.rootPath, router);
64+
const adminIndex = app._router.stack.findIndex((layer: { regexp: RegExp }) =>
65+
layer.regexp.toString().includes(options.adminBroOptions.rootPath),
66+
)
67+
if (adminIndex >= 0) {
68+
admin = app._router.stack.splice(adminIndex, 1)
69+
}
5670

57-
if (jsonParser) {
58-
app._router.stack.push(...jsonParser);
59-
}
71+
// if admin-bro-nestjs didn't reorder the middleware
72+
// the body parser would have come after corsMiddleware
73+
const corsIndex = app._router.stack.findIndex(
74+
(layer: { name: string }) => layer.name === 'corsMiddleware',
75+
)
6076

61-
if (urlencodedParser) {
62-
app._router.stack.push(...urlencodedParser);
77+
app._router.stack.splice(
78+
corsIndex >= 0 ? corsIndex + 1 : app._router.stack.length,
79+
0,
80+
...admin,
81+
...jsonParser,
82+
...urlencodedParser,
83+
)
6384
}
6485
}
6586
}

0 commit comments

Comments
 (0)