Skip to content

Commit

Permalink
Merge pull request #10 from SoftwareBrothers/feat/custom-loader
Browse files Browse the repository at this point in the history
Custom loader & fix for #7
  • Loading branch information
SimonB407 authored Mar 12, 2021
2 parents e73598c + 8cc5725 commit 3bebfbc
Show file tree
Hide file tree
Showing 14 changed files with 2,082 additions and 1,899 deletions.
15 changes: 8 additions & 7 deletions example-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "NODE_ENV=test jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@admin-bro/express": "^3.0.0-beta.3",
"@admin-bro/mongoose": "^1.0.0-beta.3",
"@admin-bro/nestjs": "^1.0.0-beta.2",
"@nestjs/common": "^6.7.2",
"@nestjs/core": "^6.7.2",
"@admin-bro/express": "^3.0.1",
"@admin-bro/mongoose": "^1.1.0",
"@nestjs/common": "^7.4.2",
"@nestjs/core": "7.4.2",
"@nestjs/mongoose": "^7.0.2",
"@nestjs/platform-express": "^6.7.2",
"admin-bro": "^3.0.0-beta.12",
"admin-bro": "^3.3.1",
"class-transformer": "^0.3.1",
"class-validator": "^0.12.2",
"express": "^4.17.1",
"express-formidable": "^1.2.0",
"express-session": "^1.17.1",
Expand Down
16 changes: 14 additions & 2 deletions example-app/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Controller, Get } from '@nestjs/common';
import mongoose from 'mongoose';
import { Body, Controller, Get, Post } from '@nestjs/common';
import { IsString } from 'class-validator';
import { Expose } from 'class-transformer';

import { AppService } from './app.service';

export class Hello {
@Expose()
@IsString()
public hello!: string
}

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
Expand All @@ -11,4 +18,9 @@ export class AppController {
public getHello(): string {
return this.appService.getHello();
}

@Post()
public postHello(@Body() testBody: Hello): string {
return testBody.hello;
}
}
6 changes: 6 additions & 0 deletions example-app/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { Module } from '@nestjs/common';
import { MongooseModule, getModelToken } from '@nestjs/mongoose';
import AdminBro from 'admin-bro';
import AdminBroMongoose from '@admin-bro/mongoose';
import { Model } from 'mongoose';

import { AdminModule } from '../../src'; // lib

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ExpressCustomLoader } from './express-custom.loader';
import { Admin } from './mongoose/admin-model';
import { MongooseSchemasModule } from './mongoose/mongoose.module';

AdminBro.registerAdapter(AdminBroMongoose);

@Module({
imports: [
MongooseModule.forRoot('mongodb://localhost:27017/nest'),
Expand All @@ -32,6 +37,7 @@ import { MongooseSchemasModule } from './mongoose/mongoose.module';
cookiePassword: 'testPass',
},
}),
customLoader: ExpressCustomLoader,
}),
MongooseSchemasModule,
],
Expand Down
21 changes: 21 additions & 0 deletions example-app/src/express-custom.loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable no-underscore-dangle */
import AdminBro from 'admin-bro';
import { Injectable } from '@nestjs/common';
import { AbstractHttpAdapter } from '@nestjs/core';

import { AbstractLoader } from '../../src/loaders/abstract.loader';
import { AdminModuleOptions } from '../../src/interfaces/admin-module-options.interface';
import { ExpressLoader } from '../../src/loaders/express.loader';

@Injectable()
export class ExpressCustomLoader extends AbstractLoader {
public register(
admin: AdminBro,
httpAdapter: AbstractHttpAdapter,
options: AdminModuleOptions,
) {
// eslint-disable-next-line no-console
console.log('Custom loader')
new ExpressLoader().register(admin, httpAdapter, options);
}
}
10 changes: 6 additions & 4 deletions example-app/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import AdminBro from 'admin-bro';
import AdminBroMongoose from '@admin-bro/mongoose';

import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';

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

AdminBro.registerAdapter(AdminBroMongoose);

const bootstrap = async () => {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe({
transform: true,
whitelist: true,
}))
await app.listen(3000);
}
bootstrap();
8 changes: 7 additions & 1 deletion example-app/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import request from 'supertest';

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

Expand All @@ -19,4 +19,10 @@ describe('AppController (e2e)', () => {
.get('/')
.expect(200)
.expect('Hello World!'));

it('/ (POST)', () => request(app.getHttpServer())
.post('/')
.send({ hello: 'hello' })
.expect(201)
.expect('hello'));
});
Loading

0 comments on commit 3bebfbc

Please sign in to comment.