Skip to content

Commit

Permalink
Merge branch 'release/2.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
hirsch88 committed Nov 22, 2017
2 parents 9f2963b + fb8bc48 commit eba3bbf
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
APP_NAME="express-typescript-boilerplate"
APP_ENV="local"
APP_HOST="http://localhost:3000"
APP_HOST="http://localhost"
APP_URL_PREFIX="/api"
APP_PORT=3000

Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: node_js
node_js:
- "7.7.3"
- "8.2.1"
install:
- yarn install
scripts:
- nps test
- nps build
- npm test
- npm start build
notifications:
email: false
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ Create a new database with the name you have in your `.env`-file.

Then setup your application environment.
```
nps setup
npm start setup
```

> This installs all dependencies with yarn. After that it migrates the database and seeds some test data into it. So after that your development environment is ready to use.
### Step 3: Serve your App
Go to the project dir and start your app with this npm script.
```
nps serve
npm start serve
```

> This starts a local server using `nodemon`, which will watch for any file changes and will restart the sever according to these changes.
Expand Down Expand Up @@ -149,26 +149,26 @@ All script are defined in the package.json file, but the most important ones are
* Install all dependencies with `yarn install`

### Linting
* Run code quality analysis using `nps lint`. This runs tslint.
* Run code quality analysis using `npm start lint`. This runs tslint.
* There is also a vscode task for this called `lint`.

### Tests
* Run the unit tests using `nps test` (There is also a vscode task for this called `test`).
* Run the e2e tests using `nps test:e2e` and don't forget to start your application and your [Auth0 Mock Server](https://github.com/hirsch88/auth0-mock-server).
* Run the unit tests using `npm start test` (There is also a vscode task for this called `test`).
* Run the e2e tests using `npm start test:e2e` and don't forget to start your application and your [Auth0 Mock Server](https://github.com/hirsch88/auth0-mock-server).

### Running in dev mode
* Run `nps serve` to start nodemon with ts-node, to serve the app.
* Run `npm start serve` to start nodemon with ts-node, to serve the app.
* The server address will be displayed to you as `http://0.0.0.0:3000`

### Building the project and run it
* Run `nps build` to generated all JavaScript files from the TypeScript sources (There is also a vscode task for this called `build`).
* Run `npm start build` to generated all JavaScript files from the TypeScript sources (There is also a vscode task for this called `build`).
* To start the builded app located in `dist` use `npm start`.

### Database
* Run `nps db:migrate` to migrate schema changes to the database
* Run `nps db:migrate:rollback` to rollback one migration
* Run `nps db:seed` to seed sample data into the database
* Run `nps db:reset` to rollback all migrations and migrate any migration again
* Run `npm start db:migrate` to migrate schema changes to the database
* Run `npm start db:migrate:rollback` to rollback one migration
* Run `npm start db:seed` to seed sample data into the database
* Run `npm start db:reset` to rollback all migrations and migrate any migration again

### Console
* To run your own created command enter `npm run console <command-name>`.
Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
environment:
nodejs_version: "7"
nodejs_version: "8"

install:
- ps: Install-Product node $env:nodejs_version
- yarn install

build_script:
- nps build
- npm start build

test_script:
- nps test
- npm test
21 changes: 12 additions & 9 deletions package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const { series, crossEnv, concurrent, rimraf, runInNewWindow } = require('nps-ut

module.exports = {
scripts: {
default: {
script: 'nps start'
},
/**
* Starts the builded app from the dist directory
*/
Expand Down Expand Up @@ -53,10 +56,10 @@ module.exports = {
)
},
pretest: {
script: './node_modules/.bin/tslint -c ./tslint.json -t stylish "./test/unit/**/*.ts"'
script: '\"./node_modules/.bin/tslint\" -c ./tslint.json -t stylish "./test/unit/**/*.ts"'
},
run: {
script: './node_modules/.bin/cross-env NODE_ENV=test \"./node_modules/.bin/jest\" --testPathPattern=unit'
script: '\"./node_modules/.bin/cross-env\" NODE_ENV=test \"./node_modules/.bin/jest\" --testPathPattern=unit'
},
verbose: {
script: 'nps "test --verbose"'
Expand All @@ -75,28 +78,28 @@ module.exports = {
)
},
pretest: {
script: './node_modules/.bin/tslint -c ./tslint.json -t stylish "./test/e2e/**/*.ts"'
script: '\"./node_modules/.bin/tslint\" -c ./tslint.json -t stylish "./test/e2e/**/*.ts"'
},
verbose: {
script: 'nps "test.e2e --verbose"'
},
run: series(
`wait-on --timeout 120000 http-get://localhost:3000/api/info`,
'./node_modules/.bin/cross-env NODE_ENV=test \"./node_modules/.bin/jest\" --testPathPattern=e2e -i'
'\"./node_modules/.bin/cross-env\" NODE_ENV=test \"./node_modules/.bin/jest\" --testPathPattern=e2e -i'
),
}
},
/**
* Runs TSLint over your project
*/
lint: {
script: `./node_modules/.bin/tslint -c ./tslint.json -p tsconfig.json 'src/**/*.ts' --format stylish`
script: `"./node_modules/.bin/tslint" -c ./tslint.json -p tsconfig.json 'src/**/*.ts' --format stylish`
},
/**
* Transpile your app into javascript
*/
transpile: {
script: `./node_modules/.bin/tsc`
script: `"./node_modules/.bin/tsc"`
},
/**
* Clean files and folders
Expand All @@ -106,7 +109,7 @@ module.exports = {
script: series(`nps banner.clean`, `nps clean.dist`)
},
dist: {
script: `./node_modules/.bin/trash './dist'`
script: `"./node_modules/.bin/trash" './dist'`
}
},
/**
Expand Down Expand Up @@ -178,11 +181,11 @@ function banner(name) {
}

function copy(source, target) {
return `./node_modules/.bin/copyup ${source} ${target}`
return `"./node_modules/.bin/copyup" ${source} ${target}`
}

function run(path) {
return `./node_modules/.bin/ts-node ${path}`
return `"./node_modules/.bin/ts-node" ${path}`
}

function runFast(path) {
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "express-typescript-boilerplate",
"version": "2.0.0",
"version": "2.0.1",
"description": "A delightful way to building a RESTful API with NodeJs & TypeScript",
"main": "src/app.ts",
"engines": {
"node": "8.2.1"
"node": ">= 6.9.1"
},
"scripts": {
"start": "node dist/app.js",
"test": "nps test",
"start": "nps",
"test": "npm start test",
"build": "npm start build",
"ts-node": "./node_modules/.bin/ts-node",
"ts-node:fast": "./node_modules/.bin/ts-node -F",
"console": "npm run ts-node:fast -- ./src/console/lib/console.ts",
Expand Down Expand Up @@ -85,7 +86,7 @@
"helmet": "^3.9.0",
"inquirer": "^3.3.0",
"inversify": "^4.5.0",
"inversify-express-utils": "^4.1.0",
"inversify-express-utils": "^4.2.2",
"jsonwebtoken": "^8.1.0",
"knex": "^0.13.0",
"lodash": "^4.17.4",
Expand Down
18 changes: 8 additions & 10 deletions src/api/middlewares/AuthenticateMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { inject, named } from 'inversify';
import {inject, named} from 'inversify';
import * as Request from 'request';
import { Logger as LoggerType } from '../../core/Logger';
import { Types, Core } from '../../constants';
import { events } from '../../core/api/events';
import { UserAuthenticatedListener } from '../listeners/user/UserAuthenticatedListener';
import {Logger as LoggerType} from '../../core/Logger';
import {Types, Core} from '../../constants';
import {events} from '../../core/api/events';
import {UserAuthenticatedListener} from '../listeners/user/UserAuthenticatedListener';


export class AuthenticateMiddleware implements interfaces.Middleware {

public log: LoggerType;

constructor(
@inject(Types.Core) @named(Core.Logger) Logger: typeof LoggerType,
@inject(Types.Lib) @named('request') private request: typeof Request
) {
constructor(@inject(Types.Core) @named(Core.Logger) Logger: typeof LoggerType,
@inject(Types.Lib) @named('request') private request: typeof Request) {
this.log = new Logger(__filename);
}

Expand Down Expand Up @@ -57,7 +55,7 @@ export class AuthenticateMiddleware implements interfaces.Middleware {
}

private getToken(req: myExpress.Request): string | null {
const authorization = req.headers.authorization;
const authorization: string = req.headers.authorization as string;

// Retrieve the token form the Authorization header
if (authorization && authorization.split(' ')[0] === 'Bearer') {
Expand Down
4 changes: 2 additions & 2 deletions src/config/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const DatabaseConfig = {
client: process.env.DB_CLIENT,
connection: process.env.DB_CONNECTION,
pool: {
min: parseInt(process.env.DB_POOL_MIN, 10),
max: parseInt(process.env.DB_POOL_MAX, 10)
min: parseInt(process.env.DB_POOL_MIN as string, 10),
max: parseInt(process.env.DB_POOL_MAX as string, 10)
},
migrations: {
directory: process.env.DB_MIGRATION_DIR,
Expand Down
2 changes: 1 addition & 1 deletion src/config/LoggerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ import { Configurable } from '../core/App';
export class LoggerConfig implements Configurable {
public configure(): void {
Logger.addAdapter('winston', WinstonAdapter);
Logger.setAdapter(process.env.LOG_ADAPTER);
Logger.setAdapter(process.env.LOG_ADAPTER as string);
}
}
1 change: 1 addition & 0 deletions src/console/templates/seed.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'reflect-metadata';
import * as Knex from 'knex';

import { Factory } from '../factories';
Expand Down
8 changes: 4 additions & 4 deletions src/core/ApiInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { ApiMonitor } from './ApiMonitor';
export class ApiInfo {

public static getRoute(): string {
return process.env.APP_URL_PREFIX + process.env.API_INFO_ROUTE;
return process.env.APP_URL_PREFIX as string + process.env.API_INFO_ROUTE;
}

public setup(application: express.Application): void {
if (Environment.isTruthy(process.env.API_INFO_ENABLED)) {
if (Environment.isTruthy(process.env.API_INFO_ENABLED as string)) {
application.get(
ApiInfo.getRoute(),
// @ts-ignore: False type definitions from express
Expand All @@ -20,11 +20,11 @@ export class ApiInfo {
const links = {
links: {}
};
if (Environment.isTruthy(process.env.SWAGGER_ENABLED)) {
if (Environment.isTruthy(process.env.SWAGGER_ENABLED as string)) {
links.links['swagger'] =
`${application.get('host')}${SwaggerUI.getRoute()}`;
}
if (Environment.isTruthy(process.env.MONITOR_ENABLED)) {
if (Environment.isTruthy(process.env.MONITOR_ENABLED as string)) {
links.links['monitor'] =
`${application.get('host')}${ApiMonitor.getRoute()}`;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/ApiMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { BasicAuthentication } from './BasicAuthentication';
export class ApiMonitor {

public static getRoute(): string {
return process.env.MONITOR_ROUTE;
return process.env.MONITOR_ROUTE as string;
}

public setup(app: express.Application): void {
if (Environment.isTruthy(process.env.MONITOR_ENABLED)) {
if (Environment.isTruthy(process.env.MONITOR_ENABLED as string)) {
app.use(monitor());
app.get(ApiMonitor.getRoute(), BasicAuthentication(), monitor().pageRoute);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/BasicAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as basicAuth from 'express-basic-auth';
export const BasicAuthentication = (): any => {
return basicAuth({
users: {
[process.env.APP_BASIC_USER]: process.env.APP_BASIC_PASSWORD
[process.env.APP_BASIC_USER as string]: process.env.APP_BASIC_PASSWORD as string
},
challenge: true
});
Expand Down
2 changes: 1 addition & 1 deletion src/core/Bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Bootstrap {

public setupInversifyExpressServer(app: express.Application, ioc: IoC): InversifyExpressServer {
const inversifyExpressServer = new InversifyExpressServer(ioc.container, undefined, {
rootPath: process.env.APP_URL_PREFIX
rootPath: process.env.APP_URL_PREFIX as string
}, app);
// @ts-ignore: False type definitions from express
inversifyExpressServer.setConfig((a) => a.use(extendExpressResponse));
Expand Down
6 changes: 3 additions & 3 deletions src/core/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ export class Server {
this.log.debug(`Environment : ${Environment.getNodeEnv()}`);
this.log.debug(`Version : ${Environment.getPkg().version}`);
this.log.debug(``);
if (Environment.isTruthy(process.env.API_INFO_ENABLED)) {
if (Environment.isTruthy(process.env.API_INFO_ENABLED as string)) {
this.log.debug(`API Info : ${app.get('host')}${ApiInfo.getRoute()}`);
}
if (Environment.isTruthy(process.env.SWAGGER_ENABLED)) {
if (Environment.isTruthy(process.env.SWAGGER_ENABLED as string)) {
this.log.debug(`Swagger : ${app.get('host')}${SwaggerUI.getRoute()}`);
}
if (Environment.isTruthy(process.env.MONITOR_ENABLED)) {
if (Environment.isTruthy(process.env.MONITOR_ENABLED as string)) {
this.log.debug(`Monitor : ${app.get('host')}${ApiMonitor.getRoute()}`);
}
this.log.debug('-------------------------------------------------------');
Expand Down
6 changes: 3 additions & 3 deletions src/core/SwaggerUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { BasicAuthentication } from './BasicAuthentication';
export class SwaggerUI {

public static getRoute(): string {
return process.env.SWAGGER_ROUTE;
return process.env.SWAGGER_ROUTE as string;
}

public setup(app: express.Application): void {
if (Environment.isTruthy(process.env.SWAGGER_ENABLED)) {
if (Environment.isTruthy(process.env.SWAGGER_ENABLED as string)) {
const baseFolder = __dirname.indexOf(`${path.sep}src${path.sep}`) >= 0 ? `${path.sep}src${path.sep}` : `${path.sep}dist${path.sep}`;
const basePath = __dirname.substring(0, __dirname.indexOf(baseFolder));
const swaggerFile = require(path.join(basePath, process.env.SWAGGER_FILE));
const swaggerFile = require(path.join(basePath, process.env.SWAGGER_FILE as string));
const packageJson = require(path.join(basePath, 'package.json'));

// Add npm infos to the swagger doc
Expand Down
2 changes: 1 addition & 1 deletion src/database/seeds/create_users.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'reflect-metadata';
import * as Knex from 'knex';

import { Factory } from '../factories';
import { User } from '../../api/models/User';


exports.seed = async (db: Knex) => {
const factory = Factory.getInstance();
await factory.get(User)
Expand Down

0 comments on commit eba3bbf

Please sign in to comment.