Skip to content

Commit 010c7de

Browse files
author
hirsch88
committed
Merge branch 'feature/update_structure' into develop
2 parents f20e8a7 + caff490 commit 010c7de

File tree

101 files changed

+1044
-1150
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1044
-1150
lines changed

.env.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
APP_NAME=express-typescript-boilerplate
55
APP_ENV=local
66
APP_HOST=http://localhost
7-
APP_URL_PREFIX=/api/v1
7+
APP_URL_PREFIX=/api
88
APP_PORT=3000
99

1010
#
1111
# LOGGING
1212
#
13-
DEBUG=app*,api*,core*
1413
LOG_LEVEL=debug
1514
LOG_ADAPTER=winston
1615

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ dist/
3030
src/**/*.js
3131
test/**/*.js
3232
coverage/
33+
!test/preprocessor.js

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "node",
99
"request": "launch",
1010
"name": "Debug",
11-
"program": "${workspaceRoot}/dist/index.js",
11+
"program": "${workspaceRoot}/dist/app.js",
1212
"smartStep": true,
1313
"outFiles": [
1414
"../dist/**/*.js"

README.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
# Express Typescript Boilerplate
22
[![Dependency Status](https://david-dm.org/w3tecch/express-typescript-boilerplate/status.svg?style=flat)](https://david-dm.org/w3tecch/express-typescript-boilerplate) [![Build Status](https://travis-ci.org/w3tecch/express-typescript-boilerplate.svg?branch=master)](https://travis-ci.org/w3tecch/express-typescript-boilerplate)
33

4-
A delightful way to building a RESTful API with NodeJs & TypeScript.
4+
> A delightful way to building a RESTful API with NodeJs & TypeScript.
5+
6+
> An Node.js RESTful API boilerplate featuring
7+
[Express](https://expressjs.com/),
8+
[Inversify](http://inversify.io/),
9+
[Winston](https://github.com/winstonjs/winston),
10+
[TypeScript](https://www.typescriptlang.org/),
11+
[TsLint](http://palantir.github.io/tslint/),
12+
[@types](https://www.npmjs.com/~types),
13+
[Jest](https://facebook.github.io/jest/),
14+
[Swagger](http://swagger.io/),
15+
[validatejs](https://validatejs.org/),
16+
[knex](http://knexjs.org/) and
17+
[bookshelf](http://bookshelfjs.org/)
18+
by [w3tech](https://github.com/w3tecch)
19+
20+
## Why
21+
Our main goal with this project is, that we have a feature complete server application.
22+
So you can focus more on main product and not spending hours for a project configuration.
23+
24+
Just try it out and give us some feedback or whishes for new features.
25+
26+
## Features
527
- **Beautiful Syntax** thanks to the awesome annotations from [Inversify Express Utils](https://github.com/inversify/inversify-express-utils).
628
- **Easy API Testing** with included black-box testing.
729
- **Dependency Injection** done with the nice framework from [Inversify](http://inversify.io/).
@@ -26,12 +48,13 @@ A delightful way to building a RESTful API with NodeJs & TypeScript.
2648
### Installing
2749
* `fork` this repo
2850
* `clone` your fork
29-
* `yarn install` to install all dependencies and typings
30-
* `cp .env.example .env` to copy the example .env file.
31-
* Create new database. You will find the name in the .env file.
32-
* `npm run db:migrate` to create the schema
33-
* `npm run db:seed` to insert some test data
34-
* `npm run serve` to start the dev server in another tab
51+
* `cp .env.example .env` to copy the example .env file and enter your database connection
52+
* Run `npm run setup` or enter the following commands manually:
53+
* `yarn install` to install all dependencies and typings.
54+
* Create new database. You will find the name in the .env file.
55+
* `npm run db:migrate` to create the schema.
56+
* `npm run db:seed` to insert some test data.
57+
* `npm run serve` to start the application.
3558

3659
### Running the app
3760
After you have installed all dependencies you can run the app.
@@ -100,13 +123,13 @@ Our IoC automatically looks through the `controllers`, `listeners` , `middleware
100123
`repositories` and `models` folders for files to bind to our IoC - Container, so you have nothing to do.
101124

102125
**However it is very important to keep the naming right, because otherwise our IoC will not find your
103-
created file!!**
126+
created files!!**
104127

105128
## Using the debugger in VS Code
106129
Just set a breakpoint and hit `F5` in your Visual Studio Code.
107130

108131
## API Routes
109-
The route prefix is `/api/v1` by default, but you can change this in the .env file.
132+
The route prefix is `/api` by default, but you can change this in the .env file.
110133

111134
| Route | Description |
112135
| ----------- | ----------- |
@@ -160,7 +183,7 @@ The route prefix is `/api/v1` by default, but you can change this in the .env fi
160183
* [Inversify Express Utils](https://github.com/inversify/inversify-express-utils)
161184
* [class-validator](https://github.com/pleerock/class-validator)
162185
* [Jest](http://facebook.github.io/jest/)
163-
* [Auth0 API Documentation](https://auth0.com/docs/api/management/v2#!/Users/get_users)
186+
* [Auth0 API Documentation](https://auth0.com/docs/api/management/v2)
164187
* [swagger Documentation](http://swagger.io/)
165188

166189
## License

knexfile.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require('dotenv').config();
1+
import * as dotenv from 'dotenv';
2+
dotenv.config();
23

34
/**
45
* This is the database configuration for the migrations and

nodemon.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"ts": "./node_modules/.bin/ts-node"
55
},
66
"events": {
7-
"start": "./node_modules/.bin/tslint -c ./tslint.json 'src/**/*.ts' --format stylish --force",
7+
"start": "./node_modules/.bin/tslint -c ./tslint.json -t stylish 'src/**/*.ts'",
88
"restart": "osascript -e 'display notification \"restarting server\" with title \"node.js application\"'"
99
}
1010
}

package.json

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@
22
"name": "express-typescript-boilerplate",
33
"version": "1.7.0",
44
"description": "A delightful way to building a RESTful API with NodeJs & TypeScript",
5-
"main": "src/index.ts",
5+
"main": "src/app.ts",
66
"scripts": {
7-
"test": "NODE_ENV=test ./node_modules/.bin/jest ./test/unit",
8-
"test:pretty": "NODE_ENV=test ./node_modules/.bin/jest ./test/unit --verbose",
9-
"test:coverage": "NODE_ENV=test ./node_modules/.bin/jest ./test/unit --coverage",
10-
"test:black-box": "NODE_ENV=test ./node_modules/.bin/jest ./test/black-box -i",
11-
"test:black-box:pretty": "NODE_ENV=test ./node_modules/.bin/jest ./test/black-box -i --verbose",
7+
"banner": "./node_modules/.bin/ts-node --fast ./src/console/lib/banner.ts",
8+
"pretest": "./node_modules/.bin/tslint -c ./tslint.json -t stylish './test/unit/**/*.ts'",
9+
"test:pretty": "npm run test -- --verbose",
10+
"test:coverage": "npm run test -- --coverage",
11+
"pretest:black-box": "./node_modules/.bin/tslint -c ./tslint.json -t stylish './test/black-box/**/*.ts'",
12+
"test:black-box": "npm run banner test && NODE_ENV=test ./node_modules/.bin/jest ./test/black-box -i",
13+
"test:black-box:pretty": "npm run test:black-box -- --verbose",
1214
"lint": "./node_modules/.bin/tslint -c ./tslint.json -p tsconfig.json 'src/**/*.ts' --format stylish",
13-
"build": "npm run lint && npm run clean:dist && npm run transpile && npm run copy:assets",
1415
"transpile": "./node_modules/.bin/tsc",
1516
"clean:dist": "./node_modules/.bin/rimraf ./dist",
1617
"copy:assets": "npm run copy:swagger && npm run copy:public",
1718
"copy:swagger": "./node_modules/.bin/copyup ./src/api/swagger.json ./dist",
1819
"copy:public": "./node_modules/.bin/copyup ./src/public/* ./dist",
19-
"db:migrate": "./node_modules/.bin/knex migrate:latest",
20-
"db:migrate:rollback": "./node_modules/.bin/knex migrate:rollback",
21-
"db:seed": "./node_modules/.bin/knex seed:run",
20+
"db:migrate": "npm run banner migrate && ./node_modules/.bin/knex migrate:latest",
21+
"db:migrate:rollback": "npm run banner rollback && ./node_modules/.bin/knex migrate:rollback",
22+
"db:seed": "npm run banner seed && ./node_modules/.bin/knex seed:run",
2223
"db:reset": "npm run console db:reset",
2324
"console": "./node_modules/.bin/ts-node --fast ./src/console/commander.ts",
2425
"console:dev": "./node_modules/.bin/ts-node ./src/console/commander.ts",
2526
"console:help": "./node_modules/.bin/ts-node --fast ./src/console/commander.ts --help",
26-
"serve": "./node_modules/.bin/nodemon --watch 'src/**/*.ts' --watch 'src/**/*.json' --watch '.env'",
27-
"start": "node dist/index.js"
27+
"setup": "yarn install && npm run db:migrate && npm run db:seed",
28+
"serve": "npm run banner serve && ./node_modules/.bin/nodemon --watch 'src/**/*.ts' --watch 'src/**/*.json' --watch '.env'",
29+
"test": "npm run banner test && NODE_ENV=test ./node_modules/.bin/jest ./test/unit",
30+
"build": "npm run banner build && npm run lint && npm run clean:dist && npm run transpile && npm run copy:assets",
31+
"start": "node dist/app.js"
2832
},
2933
"repository": "git+ssh://git@github.com/w3tec/express-typescript-boilerplate.git",
3034
"engines": {
@@ -57,9 +61,9 @@
5761
"@types/bluebird": "^3.5.4",
5862
"@types/body-parser": "^1.16.3",
5963
"@types/bookshelf": "^0.9.1",
64+
"@types/chalk": "^0.4.31",
6065
"@types/commander": "^2.9.0",
6166
"@types/cors": "^2.8.1",
62-
"@types/debug": "0.0.29",
6367
"@types/dotenv": "^4.0.0",
6468
"@types/express": "^4.0.35",
6569
"@types/faker": "^4.1.0",
@@ -82,16 +86,17 @@
8286
"body-parser": "^1.17.2",
8387
"bookshelf": "^0.10.3",
8488
"bookshelf-camelcase": "^1.1.4",
89+
"chalk": "^1.1.3",
8590
"class-validator": "^0.7.0",
8691
"commander": "^2.9.0",
8792
"compression": "^1.6.2",
8893
"copyfiles": "^1.2.0",
8994
"cors": "^2.8.1",
90-
"debug": "^2.6.8",
9195
"dotenv": "^4.0.0",
9296
"express": "^4.15.3",
9397
"express-status-monitor": "^0.1.9",
9498
"faker": "^4.1.0",
99+
"figlet": "^1.2.0",
95100
"glob": "^7.1.2",
96101
"handlebars": "^4.0.10",
97102
"helmet": "^3.6.1",
@@ -125,7 +130,7 @@
125130
},
126131
"jest": {
127132
"transform": {
128-
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
133+
".(ts|tsx)": "<rootDir>/test/preprocessor.js"
129134
},
130135
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
131136
"moduleFileExtensions": [

src/api/controllers/UserController.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,19 @@
88

99
import { inject, named } from 'inversify';
1010
import { Controller, Get, Post, Put, Delete, RequestParam, RequestBody, Response, Request } from 'inversify-express-utils';
11-
import { myExpress } from 'my-express';
11+
import { app } from '../../app';
12+
import { Types, Targets } from '../../constants';
1213
import { UserService } from '../services/UserService';
13-
import { Types } from '../../constants/Types';
14-
import { Service, Middleware } from '../../constants/Targets';
15-
import { AuthenticateMiddleware } from '../middlewares/AuthenticateMiddleware';
16-
import { PopulateUserMiddleware } from '../middlewares/PopulateUserMiddleware';
17-
import { ioc } from '../../core/IoC';
1814

1915
// Get middlewares
20-
const authenticate = ioc.Container.getNamed<AuthenticateMiddleware>(Types.Middleware, Middleware.AuthenticateMiddleware);
21-
const populateUser = ioc.Container.getNamed<PopulateUserMiddleware>(Types.Middleware, Middleware.PopulateUserMiddleware);
16+
const populateUser = app.IoC.getNamed<interfaces.Middleware>(Types.Middleware, Targets.Middleware.PopulateUserMiddleware);
17+
const authenticate = app.IoC.getNamed<interfaces.Middleware>(Types.Middleware, Targets.Middleware.AuthenticateMiddleware);
2218

2319

2420
@Controller('/users', authenticate.use)
2521
export class UserController {
2622

27-
constructor( @inject(Types.Service) @named(Service.UserService) private userService: UserService) { }
23+
constructor( @inject(Types.Service) @named(Targets.Service.UserService) private userService: UserService) { }
2824

2925
@Get('/')
3026
public async findAll( @Response() res: myExpress.Response): Promise<any> {

src/api/listeners/user/UserAuthenticatedListener.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { inject, named } from 'inversify';
2-
import { Types } from '../../../constants/Types';
3-
import { Core } from '../../../core/Targets';
4-
import { Log } from '../../../core/log/';
2+
import { Types, Core } from '../../../constants';
3+
import { Logger as LoggerType } from '../../../core/Logger';
54

65

7-
export class UserAuthenticatedListener {
6+
export class UserAuthenticatedListener implements interfaces.Listener {
87

9-
static Event = Symbol('UserAuthenticatedListener');
8+
public static Event = Symbol('UserAuthenticatedListener');
109

11-
public log: Log;
10+
public log: LoggerType;
1211

1312
constructor(
14-
@inject(Types.Core) @named(Core.Log) Logger: typeof Log
13+
@inject(Types.Core) @named(Core.Logger) Logger: typeof LoggerType
1514
) {
16-
this.log = new Logger('api:listeners:UserAuthenticatedListener');
15+
this.log = new Logger(__filename);
1716
}
1817

19-
public run(user: any): void {
18+
public act(user: any): void {
2019
this.log.info('Receive event UserAuthenticatedListener', user);
2120
}
2221

src/api/listeners/user/UserCreatedListener.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
import { inject, named } from 'inversify';
2-
import { Types } from '../../../constants/Types';
3-
import { Core } from '../../../core/Targets';
4-
import { Log } from '../../../core/log/';
2+
import { Types, Core } from '../../../constants';
3+
import { Logger as LoggerType } from '../../../core/Logger';
54

6-
const log = new Log('api:listeners:UserCreated');
75

6+
export class UserCreatedListener implements interfaces.Listener {
87

9-
export class UserCreatedListener {
8+
public static Event = Symbol('UserCreated');
109

11-
static Event = Symbol('UserCreated');
12-
13-
public log: Log;
10+
public log: LoggerType;
1411

1512
constructor(
16-
@inject(Types.Core) @named(Core.Log) Logger: typeof Log
13+
@inject(Types.Core) @named(Core.Logger) Logger: typeof LoggerType
1714
) {
18-
this.log = new Logger('api:listeners:UserCreatedListener');
15+
this.log = new Logger(__filename);
1916
}
2017

21-
public run(user: any): void {
22-
log.info('Receive event UserCreated', user);
18+
public act(user: any): void {
19+
this.log.info('Receive event UserCreated', user);
2320
}
2421

2522
}

0 commit comments

Comments
 (0)