Skip to content

Commit 8cc5725

Browse files
committed
fix: slash route fix, better express reorder, option to not init admin
1 parent fcc448b commit 8cc5725

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

example-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"test:watch": "jest --watch",
1616
"test:cov": "jest --coverage",
1717
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
18-
"test:e2e": "jest --config ./test/jest-e2e.json"
18+
"test:e2e": "NODE_ENV=test jest --config ./test/jest-e2e.json"
1919
},
2020
"dependencies": {
2121
"@admin-bro/express": "^3.0.1",

example-app/src/app.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Module } from '@nestjs/common';
22
import { MongooseModule, getModelToken } from '@nestjs/mongoose';
3+
import AdminBro from 'admin-bro';
4+
import AdminBroMongoose from '@admin-bro/mongoose';
35
import { Model } from 'mongoose';
46

57
import { AdminModule } from '../../src'; // lib
@@ -10,6 +12,8 @@ import { ExpressCustomLoader } from './express-custom.loader';
1012
import { Admin } from './mongoose/admin-model';
1113
import { MongooseSchemasModule } from './mongoose/mongoose.module';
1214

15+
AdminBro.registerAdapter(AdminBroMongoose);
16+
1317
@Module({
1418
imports: [
1519
MongooseModule.forRoot('mongodb://localhost:27017/nest'),

example-app/src/main.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import AdminBro from 'admin-bro';
2-
import AdminBroMongoose from '@admin-bro/mongoose';
1+
32
import { NestFactory } from '@nestjs/core';
43
import { ValidationPipe } from '@nestjs/common';
54

65
import { AppModule } from './app.module';
76

8-
AdminBro.registerAdapter(AdminBroMongoose);
9-
107
const bootstrap = async () => {
118
const app = await NestFactory.create(AppModule);
129
app.useGlobalPipes(new ValidationPipe({

example-app/test/app.e2e-spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2-
import * as request from 'supertest';
2+
import request from 'supertest';
33

44
import { AppModule } from './../src/app.module';
55

@@ -19,4 +19,10 @@ describe('AppController (e2e)', () => {
1919
.get('/')
2020
.expect(200)
2121
.expect('Hello World!'));
22+
23+
it('/ (POST)', () => request(app.getHttpServer())
24+
.post('/')
25+
.send({ hello: 'hello' })
26+
.expect(201)
27+
.expect('hello'));
2228
});

src/admin.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ export class AdminModule implements OnModuleInit {
122122
* Applies given options to AdminBro and initializes it
123123
*/
124124
public onModuleInit() {
125+
if ('shouldBeInitialized' in this.adminModuleOptions && !this.adminModuleOptions.shouldBeInitialized) {
126+
return;
127+
}
128+
125129
const admin = new AdminBro(this.adminModuleOptions.adminBroOptions);
126130

127131
const { httpAdapter } = this.httpAdapterHost;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { Type } from '@nestjs/common';
21
import { AdminBroOptions, CurrentAdmin } from 'admin-bro';
32
import { SessionOptions } from 'express-session';
43

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

96
/**
@@ -38,4 +35,9 @@ export type AdminModuleOptions = {
3835
* something more reliable (i.e. database).
3936
*/
4037
sessionOptions?: SessionOptions,
38+
/**
39+
* Flag indicating if admin-bro should be initialized. Helpful in cases like turning off admin for tests.
40+
* Default is true.
41+
*/
42+
shouldBeInitialized?: boolean,
4143
}

src/loaders/express.loader.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ export class ExpressLoader extends AbstractLoader {
3434
router = adminBroExpressjs.buildRouter(admin, undefined, options.formidableOptions);
3535
}
3636

37-
app.use(options.adminBroOptions.rootPath, router);
38-
this.reorderRoutes(app, options);
37+
// This named function is there on purpose.
38+
// It names layer in main router with the name of the function, which helps localize
39+
// admin layer in reorderRoutes() step.
40+
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
41+
app.use(options.adminBroOptions.rootPath, function admin(req, res, next) {
42+
return router(req, res, next);
43+
});
44+
this.reorderRoutes(app);
3945
}
4046

41-
private reorderRoutes(app, options) {
47+
private reorderRoutes(app) {
4248
let jsonParser;
4349
let urlencodedParser;
4450
let admin;
@@ -61,9 +67,9 @@ export class ExpressLoader extends AbstractLoader {
6167
urlencodedParser = app._router.stack.splice(urlencodedParserIndex, 1);
6268
}
6369

64-
const adminIndex = app._router.stack.findIndex((layer: { regexp: RegExp }) =>
65-
layer.regexp.toString().includes(options.adminBroOptions.rootPath),
66-
)
70+
const adminIndex = app._router.stack.findIndex(
71+
(layer: { name: string }) => layer.name === 'admin'
72+
);
6773
if (adminIndex >= 0) {
6874
admin = app._router.stack.splice(adminIndex, 1)
6975
}

0 commit comments

Comments
 (0)