Skip to content

Commit 7ae5c58

Browse files
author
hirsch88
committed
Merge branch 'release/1.5.0'
2 parents e1ba8c9 + 256afaa commit 7ae5c58

File tree

11 files changed

+422
-21
lines changed

11 files changed

+422
-21
lines changed

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ DEBUG=app*,api*,core*
1414
LOG_LEVEL=debug
1515
LOG_ADAPTER=debug
1616

17+
#
18+
# Swagger Documentation
19+
#
20+
SWAGGER_ENABLED=true
21+
SWAGGER_ROUTE=/docs
22+
SWAGGER_FILE='./swagger.json'
23+
1724
#
1825
# DATABASE
1926
#

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-typescript-boilerplate",
3-
"version": "1.4.2",
3+
"version": "1.5.0",
44
"description": "A delightful way to building a RESTful API with NodeJs & TypeScript",
55
"main": "src/index.ts",
66
"scripts": {
@@ -104,6 +104,7 @@
104104
"require-dir": "^0.3.1",
105105
"run-sequence": "^1.2.2",
106106
"serve-favicon": "^2.4.3",
107+
"swagger-ui-express": "^2.0.0",
107108
"ts-jest": "^20.0.4",
108109
"ts-node": "^3.0.4",
109110
"tslint": "^5.2.0",

src/api/controllers/UserController.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ export class UserController {
2828
return res.found(users.toJSON());
2929
}
3030

31+
@Post('/')
32+
public async create( @Response() res: my.Response, @RequestBody() body: any): Promise<any> {
33+
log.debug('create ', body);
34+
const user = await this.userService.create(body);
35+
return res.created(user.toJSON());
36+
}
37+
3138
@Get('/me', populateUser)
3239
public async findMe( @Request() req: my.Request, @Response() res: my.Response): Promise<any> {
3340
log.debug('findMe');
@@ -41,13 +48,6 @@ export class UserController {
4148
return res.found(user.toJSON());
4249
}
4350

44-
@Post('/')
45-
public async create( @Response() res: my.Response, @RequestBody() body: any): Promise<any> {
46-
log.debug('create ', body);
47-
const user = await this.userService.create(body);
48-
return res.created(user.toJSON());
49-
}
50-
5151
@Put('/:id')
5252
public async update( @Response() res: my.Response, @RequestParam('id') id: string, @RequestBody() body: any): Promise<any> {
5353
log.debug('update ', body);

src/api/requests/UserCreateRequest.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export class UserCreateRequest extends RequestBody {
2424

2525
picture: string;
2626

27-
@IsNotEmpty()
2827
auth0UserId: string;
2928

3029
}

src/api/requests/UserUpdateRequest.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export class UserUpdateRequest extends RequestBody {
2727

2828
picture: string;
2929

30-
@IsNotEmpty()
3130
auth0UserId: string;
3231

3332
setFirstName(value: string): void {

src/core/Bootstrap.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ export class Bootstrap {
2828
*/
2929
static getApp(): express.Application {
3030
const app = express();
31-
32-
// Set serve configs for running it
3331
app.set('host', Environment.get('APP_HOST'));
3432
app.set('port', Bootstrap.normalizePort(Environment.get<string>('PORT') || Environment.get<string>('APP_PORT')));
3533
log.debug('app is defined');
@@ -48,11 +46,47 @@ export class Bootstrap {
4846
* @memberof Bootstrap
4947
*/
5048
static build(app: express.Application, container: Container): express.Application {
49+
app = Bootstrap.setupSwagger(app);
5150
let server = new InversifyExpressServer(container, undefined, { rootPath: Environment.get<string>('APP_URL_PREFIX') }, app);
5251
log.debug('ioc is bonded');
52+
server = Bootstrap.setupConfigurations(server);
53+
return server.build();
54+
}
55+
56+
/**
57+
* Sets up the express middlewares witch are last in the chain
58+
*
59+
* @static
60+
* @param {InversifyExpressServer} server
61+
* @returns {InversifyExpressServer}
62+
*
63+
* @memberof Bootstrap
64+
*/
65+
static setupConfigurations(server: InversifyExpressServer): InversifyExpressServer {
5366
server.setConfig((a) => a.use(extendExpressResponse));
5467
server.setErrorConfig((a) => a.use(exceptionHandler));
55-
return server.build();
68+
return server;
69+
}
70+
71+
/**
72+
* Sets up the swagger documentation
73+
*
74+
* @static
75+
* @param {express.Application} app
76+
* @returns {express.Application}
77+
*
78+
* @memberof Bootstrap
79+
*/
80+
static setupSwagger(app: express.Application): express.Application {
81+
if (Environment.get<string>('SWAGGER_ENABLED') === 'true') {
82+
const basePath = __dirname.substring(0, __dirname.indexOf('/src/') + 4);
83+
const swaggerUi = require('swagger-ui-express');
84+
const swaggerDocument = require(basePath + Environment.get<string>('SWAGGER_FILE'));
85+
const route = Environment.get<string>('APP_URL_PREFIX') + Environment.get<string>('SWAGGER_ROUTE');
86+
app.use(route, swaggerUi.serve, swaggerUi.setup(swaggerDocument));
87+
log.info(`Now you can access the swagger docs under -> ${app.get('host')}:${(app.get('port'))}${route}`);
88+
}
89+
return app;
5690
}
5791

5892
/**

src/core/Server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class Server {
3939
* @memberof Server
4040
*/
4141
static onStartUp(app: express.Application): void {
42-
log.info(`started on ${app.get('host')}:${app.get('port')}`);
42+
log.info(`Aloha, your app is ready on ${app.get('host')}:${app.get('port')}`);
4343
}
4444

4545
/**

src/core/api/extendExpressResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ export const extendExpressResponse = (req: my.Request, res: my.Response, next: e
4444
};
4545

4646
/**
47-
* 204 - Destroyed
47+
* 200 - Destroyed
4848
* This is the response after a resource has been removed
4949
*/
5050
res.destroyed = (options: my.ResponseOptions = {}) => {
51-
res.status(204);
51+
res.status(200);
5252
return res.json(bodySuccessful(null));
5353
};
5454

0 commit comments

Comments
 (0)