typescript-express-starter 8.0.2 → 8.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/README.kr.md +21 -21
  2. package/README.md +25 -25
  3. package/lib/default/.env.development.local +13 -0
  4. package/lib/default/.env.production.local +13 -0
  5. package/lib/default/.env.test.local +13 -0
  6. package/lib/default/.swcrc +1 -0
  7. package/lib/default/Makefile +26 -3
  8. package/lib/default/package.json +2 -4
  9. package/lib/default/src/app.ts +6 -7
  10. package/lib/default/src/config/index.ts +5 -0
  11. package/lib/default/src/middlewares/auth.middleware.ts +2 -2
  12. package/lib/default/src/server.ts +0 -2
  13. package/lib/default/src/services/auth.service.ts +2 -2
  14. package/lib/default/src/utils/logger.ts +2 -2
  15. package/lib/default/tsconfig.json +1 -0
  16. package/lib/graphql/.env.development.local +20 -0
  17. package/lib/graphql/.env.production.local +20 -0
  18. package/lib/graphql/.env.test.local +20 -0
  19. package/lib/graphql/.swcrc +1 -0
  20. package/lib/graphql/Makefile +26 -3
  21. package/lib/graphql/package.json +2 -4
  22. package/lib/graphql/src/app.ts +12 -10
  23. package/lib/graphql/src/config/index.ts +6 -0
  24. package/lib/graphql/src/databases/index.ts +6 -8
  25. package/lib/graphql/src/entities/users.entity.ts +1 -1
  26. package/lib/graphql/src/middlewares/auth.middleware.ts +2 -2
  27. package/lib/graphql/src/repositories/auth.repository.ts +3 -3
  28. package/lib/graphql/src/repositories/users.repository.ts +1 -1
  29. package/lib/graphql/src/server.ts +0 -3
  30. package/lib/graphql/src/utils/logger.ts +2 -2
  31. package/lib/graphql/tsconfig.json +1 -0
  32. package/lib/knex/.env.development.local +20 -0
  33. package/lib/knex/.env.production.local +20 -0
  34. package/lib/knex/.env.test.local +20 -0
  35. package/lib/knex/.swcrc +1 -0
  36. package/lib/knex/Makefile +26 -3
  37. package/lib/knex/knexfile.ts +6 -10
  38. package/lib/knex/package.json +2 -4
  39. package/lib/knex/src/app.ts +6 -7
  40. package/lib/knex/src/config/index.ts +5 -0
  41. package/lib/knex/src/databases/index.ts +6 -8
  42. package/lib/knex/src/middlewares/auth.middleware.ts +2 -2
  43. package/lib/knex/src/server.ts +0 -2
  44. package/lib/knex/src/services/auth.service.ts +2 -2
  45. package/lib/knex/src/utils/logger.ts +2 -2
  46. package/lib/knex/tsconfig.json +1 -0
  47. package/lib/mongoose/.env.development.local +18 -0
  48. package/lib/mongoose/.env.production.local +18 -0
  49. package/lib/mongoose/.env.test.local +18 -0
  50. package/lib/mongoose/.swcrc +1 -0
  51. package/lib/mongoose/Makefile +26 -3
  52. package/lib/mongoose/package.json +2 -4
  53. package/lib/mongoose/src/app.ts +6 -7
  54. package/lib/mongoose/src/config/index.ts +5 -0
  55. package/lib/mongoose/src/databases/index.ts +2 -5
  56. package/lib/mongoose/src/middlewares/auth.middleware.ts +2 -2
  57. package/lib/mongoose/src/server.ts +0 -2
  58. package/lib/mongoose/src/services/auth.service.ts +2 -2
  59. package/lib/mongoose/src/utils/logger.ts +2 -2
  60. package/lib/mongoose/tsconfig.json +1 -0
  61. package/lib/prisma/.env.development.local +16 -0
  62. package/lib/prisma/.env.production.local +16 -0
  63. package/lib/prisma/.env.test.local +16 -0
  64. package/lib/prisma/.swcrc +1 -0
  65. package/lib/prisma/Makefile +26 -3
  66. package/lib/prisma/package.json +2 -4
  67. package/lib/prisma/src/app.ts +6 -7
  68. package/lib/prisma/src/config/index.ts +5 -0
  69. package/lib/prisma/src/middlewares/auth.middleware.ts +2 -2
  70. package/lib/prisma/src/server.ts +0 -2
  71. package/lib/prisma/src/services/auth.service.ts +2 -2
  72. package/lib/prisma/src/utils/logger.ts +2 -2
  73. package/lib/prisma/tsconfig.json +1 -0
  74. package/lib/routing-controllers/.env.development.local +13 -0
  75. package/lib/routing-controllers/.env.production.local +13 -0
  76. package/lib/routing-controllers/.env.test.local +13 -0
  77. package/lib/routing-controllers/.swcrc +1 -0
  78. package/lib/routing-controllers/Makefile +26 -3
  79. package/lib/routing-controllers/package.json +2 -4
  80. package/lib/routing-controllers/src/app.ts +7 -8
  81. package/lib/routing-controllers/src/config/index.ts +5 -0
  82. package/lib/routing-controllers/src/middlewares/auth.middleware.ts +2 -2
  83. package/lib/routing-controllers/src/server.ts +0 -2
  84. package/lib/routing-controllers/src/services/auth.service.ts +2 -2
  85. package/lib/routing-controllers/src/utils/logger.ts +2 -2
  86. package/lib/routing-controllers/tsconfig.json +1 -0
  87. package/lib/sequelize/.env.development.local +20 -0
  88. package/lib/sequelize/.env.production.local +20 -0
  89. package/lib/sequelize/.env.test.local +20 -0
  90. package/lib/sequelize/.swcrc +1 -0
  91. package/lib/sequelize/Makefile +26 -3
  92. package/lib/sequelize/package.json +2 -4
  93. package/lib/sequelize/src/app.ts +6 -7
  94. package/lib/sequelize/src/config/index.ts +5 -0
  95. package/lib/sequelize/src/databases/index.ts +7 -8
  96. package/lib/sequelize/src/middlewares/auth.middleware.ts +2 -2
  97. package/lib/sequelize/src/server.ts +0 -2
  98. package/lib/sequelize/src/services/auth.service.ts +2 -2
  99. package/lib/sequelize/src/utils/logger.ts +2 -2
  100. package/lib/sequelize/tsconfig.json +1 -0
  101. package/lib/starter.js +30 -8
  102. package/lib/typegoose/Makefile +26 -3
  103. package/lib/typegoose/package.json +3 -3
  104. package/lib/typeorm/.env.development.local +20 -0
  105. package/lib/typeorm/.env.production.local +20 -0
  106. package/lib/typeorm/.env.test.local +20 -0
  107. package/lib/typeorm/.swcrc +1 -0
  108. package/lib/typeorm/Makefile +26 -3
  109. package/lib/typeorm/package.json +2 -4
  110. package/lib/typeorm/src/app.ts +6 -7
  111. package/lib/typeorm/src/config/index.ts +5 -0
  112. package/lib/typeorm/src/databases/index.ts +6 -8
  113. package/lib/typeorm/src/middlewares/auth.middleware.ts +2 -2
  114. package/lib/typeorm/src/server.ts +0 -2
  115. package/lib/typeorm/src/services/auth.service.ts +2 -2
  116. package/lib/typeorm/src/utils/logger.ts +2 -2
  117. package/lib/typeorm/tsconfig.json +1 -0
  118. package/package.json +1 -1
  119. package/lib/default/.env +0 -1
  120. package/lib/default/src/configs/development.json +0 -12
  121. package/lib/default/src/configs/production.json +0 -12
  122. package/lib/default/src/configs/test.json +0 -12
  123. package/lib/default/src/index.ts +0 -1
  124. package/lib/graphql/.env +0 -1
  125. package/lib/graphql/src/configs/development.json +0 -19
  126. package/lib/graphql/src/configs/production.json +0 -19
  127. package/lib/graphql/src/configs/test.json +0 -19
  128. package/lib/graphql/src/index.ts +0 -1
  129. package/lib/graphql/src/interfaces/db.interface.ts +0 -7
  130. package/lib/knex/.env +0 -1
  131. package/lib/knex/src/configs/development.json +0 -19
  132. package/lib/knex/src/configs/production.json +0 -19
  133. package/lib/knex/src/configs/test.json +0 -19
  134. package/lib/knex/src/index.ts +0 -1
  135. package/lib/knex/src/interfaces/db.interface.ts +0 -7
  136. package/lib/mongoose/.env +0 -1
  137. package/lib/mongoose/src/configs/development.json +0 -17
  138. package/lib/mongoose/src/configs/production.json +0 -17
  139. package/lib/mongoose/src/configs/test.json +0 -17
  140. package/lib/mongoose/src/index.ts +0 -1
  141. package/lib/mongoose/src/interfaces/db.interface.ts +0 -5
  142. package/lib/prisma/.env +0 -10
  143. package/lib/prisma/src/configs/development.json +0 -12
  144. package/lib/prisma/src/configs/production.json +0 -12
  145. package/lib/prisma/src/configs/test.json +0 -12
  146. package/lib/prisma/src/index.ts +0 -1
  147. package/lib/routing-controllers/.env +0 -1
  148. package/lib/routing-controllers/src/configs/development.json +0 -12
  149. package/lib/routing-controllers/src/configs/production.json +0 -12
  150. package/lib/routing-controllers/src/configs/test.json +0 -12
  151. package/lib/routing-controllers/src/index.ts +0 -1
  152. package/lib/sequelize/.env +0 -1
  153. package/lib/sequelize/src/configs/development.json +0 -22
  154. package/lib/sequelize/src/configs/production.json +0 -22
  155. package/lib/sequelize/src/configs/test.json +0 -22
  156. package/lib/sequelize/src/index.ts +0 -1
  157. package/lib/sequelize/src/interfaces/db.interface.ts +0 -10
  158. package/lib/typeorm/.env +0 -1
  159. package/lib/typeorm/src/configs/development.json +0 -18
  160. package/lib/typeorm/src/configs/production.json +0 -18
  161. package/lib/typeorm/src/configs/test.json +0 -18
  162. package/lib/typeorm/src/index.ts +0 -1
  163. package/lib/typeorm/src/interfaces/db.interface.ts +0 -6
@@ -1,7 +1,7 @@
1
- import config from 'config';
2
1
  import { verify } from 'jsonwebtoken';
3
2
  import { AuthChecker } from 'type-graphql';
4
3
  import { getRepository } from 'typeorm';
4
+ import { SECRET_KEY } from '@config';
5
5
  import { UserEntity } from '@entities/users.entity';
6
6
  import { HttpException } from '@exceptions/HttpException';
7
7
  import { RequestWithUser, DataStoredInToken } from '@interfaces/auth.interface';
@@ -10,7 +10,7 @@ export const authMiddleware = async req => {
10
10
  try {
11
11
  const Authorization = req.cookies['Authorization'] || (req.header('Authorization') ? req.header('Authorization').split('Bearer ')[1] : null);
12
12
  if (Authorization) {
13
- const secretKey: string = config.get('secretKey');
13
+ const secretKey: string = SECRET_KEY;
14
14
  const { id } = (await verify(Authorization, secretKey)) as DataStoredInToken;
15
15
  const userRepository = getRepository(UserEntity);
16
16
  const findUser = await userRepository.findOne(id, { select: ['id', 'email', 'password'] });
@@ -1,7 +1,7 @@
1
1
  import { hash, compare } from 'bcrypt';
2
- import config from 'config';
3
2
  import { sign } from 'jsonwebtoken';
4
3
  import { EntityRepository } from 'typeorm';
4
+ import { SECRET_KEY } from '@config';
5
5
  import { CreateUserDto } from '@dtos/users.dto';
6
6
  import { UserEntity } from '@entities/users.entity';
7
7
  import { HttpException } from '@exceptions/HttpException';
@@ -18,7 +18,7 @@ export default class AuthRepository {
18
18
  if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
19
19
 
20
20
  const hashedPassword = await hash(userData.password, 10);
21
- const createUserData: User = await UserEntity.save({ ...userData, password: hashedPassword });
21
+ const createUserData: User = await UserEntity.create({ ...userData, password: hashedPassword }).save();
22
22
 
23
23
  return createUserData;
24
24
  }
@@ -49,7 +49,7 @@ export default class AuthRepository {
49
49
 
50
50
  public createToken(user: User): TokenData {
51
51
  const dataStoredInToken: DataStoredInToken = { id: user.id };
52
- const secretKey: string = config.get('secretKey');
52
+ const secretKey: string = SECRET_KEY;
53
53
  const expiresIn: number = 60 * 60;
54
54
 
55
55
  return { expiresIn, token: sign(dataStoredInToken, secretKey, { expiresIn }) };
@@ -30,7 +30,7 @@ export default class UserRepository {
30
30
  if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
31
31
 
32
32
  const hashedPassword = await hash(userData.password, 10);
33
- const createUserData: User = await UserEntity.save({ ...userData, password: hashedPassword });
33
+ const createUserData: User = await UserEntity.create({ ...userData, password: hashedPassword }).save();
34
34
 
35
35
  return createUserData;
36
36
  }
@@ -1,6 +1,3 @@
1
- import 'reflect-metadata';
2
- import 'dotenv/config';
3
- import '@/index';
4
1
  import App from '@/app';
5
2
  import validateEnv from '@utils/validateEnv';
6
3
 
@@ -1,11 +1,11 @@
1
- import config from 'config';
2
1
  import { existsSync, mkdirSync } from 'fs';
3
2
  import { join } from 'path';
4
3
  import winston from 'winston';
5
4
  import winstonDaily from 'winston-daily-rotate-file';
5
+ import { LOG_DIR } from '@config';
6
6
 
7
7
  // logs dir
8
- const logDir: string = join(__dirname, config.get('log.dir'));
8
+ const logDir: string = join(__dirname, LOG_DIR);
9
9
 
10
10
  if (!existsSync(logDir)) {
11
11
  mkdirSync(logDir);
@@ -22,6 +22,7 @@
22
22
  "baseUrl": "src",
23
23
  "paths": {
24
24
  "@/*": ["*"],
25
+ "@config": ["config"],
25
26
  "@databases": ["databases"],
26
27
  "@dtos/*": ["dtos/*"],
27
28
  "@entities/*": ["entities/*"],
@@ -0,0 +1,20 @@
1
+ # PORT
2
+ PORT = 3000
3
+
4
+ # DATABASE
5
+ DB_HOST = localhost
6
+ DB_PORT = 3306
7
+ DB_USER = root
8
+ DB_PASSWORD = password
9
+ DB_DATABASE = test
10
+
11
+ # TOKEN
12
+ SECRET_KEY = secretKey
13
+
14
+ # LOG
15
+ LOG_FORMAT = dev
16
+ LOG_DIR = ../logs
17
+
18
+ # CORS
19
+ ORIGIN = *
20
+ CREDENTIALS = true
@@ -0,0 +1,20 @@
1
+ # PORT
2
+ PORT = 3000
3
+
4
+ # DATABASE
5
+ DB_HOST = localhost
6
+ DB_PORT = 3306
7
+ DB_USER = root
8
+ DB_PASSWORD = password
9
+ DB_DATABASE = test
10
+
11
+ # TOKEN
12
+ SECRET_KEY = secretKey
13
+
14
+ # LOG
15
+ LOG_FORMAT = combined
16
+ LOG_DIR = ../logs
17
+
18
+ # CORS
19
+ ORIGIN = your.domain.com
20
+ CREDENTIALS = true
@@ -0,0 +1,20 @@
1
+ # PORT
2
+ PORT = 3000
3
+
4
+ # DATABASE
5
+ DB_HOST = localhost
6
+ DB_PORT = 3306
7
+ DB_USER = root
8
+ DB_PASSWORD = password
9
+ DB_DATABASE = test
10
+
11
+ # TOKEN
12
+ SECRET_KEY = secretKey
13
+
14
+ # LOG
15
+ LOG_FORMAT = dev
16
+ LOG_DIR = ../logs
17
+
18
+ # CORS
19
+ ORIGIN = *
20
+ CREDENTIALS = true
package/lib/knex/.swcrc CHANGED
@@ -21,6 +21,7 @@
21
21
  "baseUrl": "src",
22
22
  "paths": {
23
23
  "@/*": ["*"],
24
+ "@config": ["config"],
24
25
  "@controllers/*": ["controllers/*"],
25
26
  "@databases": ["databases"],
26
27
  "@dtos/*": ["dtos/*"],
package/lib/knex/Makefile CHANGED
@@ -1,6 +1,29 @@
1
+ # app name should be overridden.
2
+ # ex) production-stage: make build APP_NAME=<APP_NAME>
3
+ # ex) development-stage: make build-dev APP_NAME=<APP_NAME>
4
+
5
+ APP_NAME = typescript-express
6
+ APP_NAME := $(APP_NAME)
7
+
8
+ .PHONY: build
9
+ # Build the container image - Dvelopment
10
+ build-dev:
11
+ docker build -t ${APP_NAME}\
12
+ --target development-build-stage\
13
+ -f Dockerfile .
14
+
15
+ # Build the container image - Production
1
16
  build:
2
- docker build -t ${tag} .
17
+ docker build -t ${APP_NAME}\
18
+ --target production-build-stage\
19
+ -f Dockerfile .
20
+
21
+ # Clean the container image
3
22
  clean:
4
- docker rmi -f ${tag}
23
+ docker rmi -f ${APP_NAME}
24
+
25
+ # Run the container image
5
26
  run:
6
- docker run -d -p ${port}:${port} --name ${name} ${tag}
27
+ docker run -d -it -p 3000:3000 ${APP_NAME}
28
+
29
+ all: build
@@ -1,19 +1,15 @@
1
- process.env['NODE_CONFIG_DIR'] = __dirname + '/src/configs';
1
+ import { DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_DATABASE } from './src/config';
2
2
 
3
- import config from 'config';
4
- import { dbConfig } from './src/interfaces/db.interface';
5
-
6
- const { host, user, password, database, port }: dbConfig = config.get('dbConfig');
7
3
  export = {
8
4
  client: 'mysql',
9
5
  connection: {
10
6
  charset: 'utf8',
11
7
  timezone: 'UTC',
12
- host: host,
13
- user: user,
14
- password: password,
15
- database: database,
16
- port: port,
8
+ host: DB_HOST,
9
+ port: DB_PORT,
10
+ user: DB_USER,
11
+ password: DB_PASSWORD,
12
+ database: DB_DATABASE
17
13
  },
18
14
  migrations: {
19
15
  directory: 'src/databases/migrations',
@@ -7,8 +7,8 @@
7
7
  "scripts": {
8
8
  "start": "npm run build && cross-env NODE_ENV=production node dist/server.js",
9
9
  "dev": "cross-env NODE_ENV=development nodemon",
10
- "build": "tsc && tsc-alias",
11
- "build:swc": "swc src -d dist --source-maps --copy-files",
10
+ "build": "swc src -d dist --source-maps --copy-files",
11
+ "build:tsc": "tsc && tsc-alias",
12
12
  "test": "jest --forceExit --detectOpenHandles",
13
13
  "lint": "eslint --ignore-path .gitignore --ext .ts src/",
14
14
  "lint:fix": "npm run lint -- --fix",
@@ -25,7 +25,6 @@
25
25
  "class-transformer": "^0.4.0",
26
26
  "class-validator": "^0.13.1",
27
27
  "compression": "^1.7.4",
28
- "config": "^3.3.6",
29
28
  "cookie-parser": "^1.4.5",
30
29
  "cors": "^2.8.5",
31
30
  "dotenv": "^9.0.2",
@@ -48,7 +47,6 @@
48
47
  "@swc/core": "^1.2.108",
49
48
  "@types/bcrypt": "^5.0.0",
50
49
  "@types/compression": "^1.7.0",
51
- "@types/config": "0.0.38",
52
50
  "@types/cookie-parser": "^1.4.2",
53
51
  "@types/cors": "^2.8.10",
54
52
  "@types/dotenv": "^8.2.0",
@@ -1,5 +1,3 @@
1
- import '@/index';
2
- import config from 'config';
3
1
  import compression from 'compression';
4
2
  import cookieParser from 'cookie-parser';
5
3
  import cors from 'cors';
@@ -10,6 +8,7 @@ import morgan from 'morgan';
10
8
  import { Model } from 'objection';
11
9
  import swaggerJSDoc from 'swagger-jsdoc';
12
10
  import swaggerUi from 'swagger-ui-express';
11
+ import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config';
13
12
  import knex from '@databases';
14
13
  import { Routes } from '@interfaces/routes.interface';
15
14
  import errorMiddleware from '@middlewares/error.middleware';
@@ -17,13 +16,13 @@ import { logger, stream } from '@utils/logger';
17
16
 
18
17
  class App {
19
18
  public app: express.Application;
20
- public port: string | number;
21
19
  public env: string;
20
+ public port: string | number;
22
21
 
23
22
  constructor(routes: Routes[]) {
24
23
  this.app = express();
25
- this.port = process.env.PORT || 3000;
26
- this.env = process.env.NODE_ENV || 'development';
24
+ this.env = NODE_ENV || 'development';
25
+ this.port = PORT || 3000;
27
26
 
28
27
  this.connectToDatabase();
29
28
  this.initializeMiddlewares();
@@ -50,8 +49,8 @@ class App {
50
49
  }
51
50
 
52
51
  private initializeMiddlewares() {
53
- this.app.use(morgan(config.get('log.format'), { stream }));
54
- this.app.use(cors({ origin: config.get('cors.origin'), credentials: config.get('cors.credentials') }));
52
+ this.app.use(morgan(LOG_FORMAT, { stream }));
53
+ this.app.use(cors({ origin: ORIGIN, credentials: CREDENTIALS }));
55
54
  this.app.use(hpp());
56
55
  this.app.use(helmet());
57
56
  this.app.use(compression());
@@ -0,0 +1,5 @@
1
+ import { config } from 'dotenv';
2
+ config({ path: `.env.${process.env.NODE_ENV || 'development'}.local` });
3
+
4
+ export const CREDENTIALS = process.env.CREDENTIALS === 'true';
5
+ export const { NODE_ENV, PORT, DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_DATABASE, SECRET_KEY, LOG_FORMAT, LOG_DIR, ORIGIN } = process.env;
@@ -1,18 +1,16 @@
1
- import config from 'config';
2
1
  import Knex from 'knex';
3
- import { dbConfig } from '@interfaces/db.interface';
2
+ import { DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_DATABASE } from '@config';
4
3
 
5
- const { host, port, user, password, database }: dbConfig = config.get('dbConfig');
6
4
  const dbConnection = {
7
5
  client: 'mysql',
8
6
  connection: {
9
7
  charset: 'utf8',
10
8
  timezone: 'UTC',
11
- host: host,
12
- port: port,
13
- user: user,
14
- password: password,
15
- database: database,
9
+ host: DB_HOST,
10
+ port: DB_PORT,
11
+ user: DB_USER,
12
+ password: DB_PASSWORD,
13
+ database: DB_DATABASE,
16
14
  },
17
15
  pool: {
18
16
  min: 2,
@@ -1,6 +1,6 @@
1
- import config from 'config';
2
1
  import { NextFunction, Response } from 'express';
3
2
  import { verify } from 'jsonwebtoken';
3
+ import { SECRET_KEY } from '@config';
4
4
  import { HttpException } from '@exceptions/HttpException';
5
5
  import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';
6
6
  import { User } from '@interfaces/users.interface';
@@ -11,7 +11,7 @@ const authMiddleware = async (req: RequestWithUser, res: Response, next: NextFun
11
11
  const Authorization = req.cookies['Authorization'] || (req.header('Authorization') ? req.header('Authorization').split('Bearer ')[1] : null);
12
12
 
13
13
  if (Authorization) {
14
- const secretKey: string = config.get('secretKey');
14
+ const secretKey: string = SECRET_KEY;
15
15
  const verificationResponse = (await verify(Authorization, secretKey)) as DataStoredInToken;
16
16
  const userId = verificationResponse.id;
17
17
  const findUser: User = await Users.query().findById(userId);
@@ -1,5 +1,3 @@
1
- import 'dotenv/config';
2
- import '@/index';
3
1
  import App from '@/app';
4
2
  import AuthRoute from '@routes/auth.route';
5
3
  import IndexRoute from '@routes/index.route';
@@ -1,6 +1,6 @@
1
1
  import { hash, compare } from 'bcrypt';
2
- import config from 'config';
3
2
  import { sign } from 'jsonwebtoken';
3
+ import { SECRET_KEY } from '@config';
4
4
  import { CreateUserDto } from '@dtos/users.dto';
5
5
  import { HttpException } from '@exceptions/HttpException';
6
6
  import { DataStoredInToken, TokenData } from '@interfaces/auth.interface';
@@ -55,7 +55,7 @@ class AuthService {
55
55
 
56
56
  public createToken(user: User): TokenData {
57
57
  const dataStoredInToken: DataStoredInToken = { id: user.id };
58
- const secretKey: string = config.get('secretKey');
58
+ const secretKey: string = SECRET_KEY;
59
59
  const expiresIn: number = 60 * 60;
60
60
 
61
61
  return { expiresIn, token: sign(dataStoredInToken, secretKey, { expiresIn }) };
@@ -1,11 +1,11 @@
1
- import config from 'config';
2
1
  import { existsSync, mkdirSync } from 'fs';
3
2
  import { join } from 'path';
4
3
  import winston from 'winston';
5
4
  import winstonDaily from 'winston-daily-rotate-file';
5
+ import { LOG_DIR } from '@config';
6
6
 
7
7
  // logs dir
8
- const logDir: string = join(__dirname, config.get('log.dir'));
8
+ const logDir: string = join(__dirname, LOG_DIR);
9
9
 
10
10
  if (!existsSync(logDir)) {
11
11
  mkdirSync(logDir);
@@ -22,6 +22,7 @@
22
22
  "baseUrl": "src",
23
23
  "paths": {
24
24
  "@/*": ["*"],
25
+ "@config": ["config"],
25
26
  "@controllers/*": ["controllers/*"],
26
27
  "@databases": ["databases"],
27
28
  "@dtos/*": ["dtos/*"],
@@ -0,0 +1,18 @@
1
+ # PORT
2
+ PORT = 3000
3
+
4
+ # DATABASE
5
+ DB_HOST = localhost
6
+ DB_PORT = 27017
7
+ DB_DATABASE = test
8
+
9
+ # TOKEN
10
+ SECRET_KEY = secretKey
11
+
12
+ # LOG
13
+ LOG_FORMAT = dev
14
+ LOG_DIR = ../logs
15
+
16
+ # CORS
17
+ ORIGIN = *
18
+ CREDENTIALS = true
@@ -0,0 +1,18 @@
1
+ # PORT
2
+ PORT = 3000
3
+
4
+ # DATABASE
5
+ DB_HOST = localhost
6
+ DB_PORT = 27017
7
+ DB_DATABASE = test
8
+
9
+ # TOKEN
10
+ SECRET_KEY = secretKey
11
+
12
+ # LOG
13
+ LOG_FORMAT = combined
14
+ LOG_DIR = ../logs
15
+
16
+ # CORS
17
+ ORIGIN = your.domain.com
18
+ CREDENTIALS = true
@@ -0,0 +1,18 @@
1
+ # PORT
2
+ PORT = 3000
3
+
4
+ # DATABASE
5
+ DB_HOST = localhost
6
+ DB_PORT = 27017
7
+ DB_DATABASE = test
8
+
9
+ # TOKEN
10
+ SECRET_KEY = secretKey
11
+
12
+ # LOG
13
+ LOG_FORMAT = dev
14
+ LOG_DIR = ../logs
15
+
16
+ # CORS
17
+ ORIGIN = *
18
+ CREDENTIALS = true
@@ -21,6 +21,7 @@
21
21
  "baseUrl": "src",
22
22
  "paths": {
23
23
  "@/*": ["*"],
24
+ "@config": ["config"],
24
25
  "@controllers/*": ["controllers/*"],
25
26
  "@databases": ["databases"],
26
27
  "@dtos/*": ["dtos/*"],
@@ -1,6 +1,29 @@
1
+ # app name should be overridden.
2
+ # ex) production-stage: make build APP_NAME=<APP_NAME>
3
+ # ex) development-stage: make build-dev APP_NAME=<APP_NAME>
4
+
5
+ APP_NAME = typescript-express
6
+ APP_NAME := $(APP_NAME)
7
+
8
+ .PHONY: build
9
+ # Build the container image - Dvelopment
10
+ build-dev:
11
+ docker build -t ${APP_NAME}\
12
+ --target development-build-stage\
13
+ -f Dockerfile .
14
+
15
+ # Build the container image - Production
1
16
  build:
2
- docker build -t ${tag} .
17
+ docker build -t ${APP_NAME}\
18
+ --target production-build-stage\
19
+ -f Dockerfile .
20
+
21
+ # Clean the container image
3
22
  clean:
4
- docker rmi -f ${tag}
23
+ docker rmi -f ${APP_NAME}
24
+
25
+ # Run the container image
5
26
  run:
6
- docker run -d -p ${port}:${port} --name ${name} ${tag}
27
+ docker run -d -it -p 3000:3000 ${APP_NAME}
28
+
29
+ all: build
@@ -7,8 +7,8 @@
7
7
  "scripts": {
8
8
  "start": "npm run build && cross-env NODE_ENV=production node dist/server.js",
9
9
  "dev": "cross-env NODE_ENV=development nodemon",
10
- "build": "tsc && tsc-alias",
11
- "build:swc": "swc src -d dist --source-maps --copy-files",
10
+ "build": "swc src -d dist --source-maps --copy-files",
11
+ "build:tsc": "tsc && tsc-alias",
12
12
  "test": "jest --forceExit --detectOpenHandles",
13
13
  "lint": "eslint --ignore-path .gitignore --ext .ts src/",
14
14
  "lint:fix": "npm run lint -- --fix",
@@ -20,7 +20,6 @@
20
20
  "class-transformer": "^0.4.0",
21
21
  "class-validator": "^0.13.1",
22
22
  "compression": "^1.7.4",
23
- "config": "^3.3.6",
24
23
  "cookie-parser": "^1.4.5",
25
24
  "cors": "^2.8.5",
26
25
  "dotenv": "^10.0.0",
@@ -41,7 +40,6 @@
41
40
  "@swc/core": "^1.2.108",
42
41
  "@types/bcrypt": "^5.0.0",
43
42
  "@types/compression": "^1.7.1",
44
- "@types/config": "^0.0.39",
45
43
  "@types/cookie-parser": "^1.4.2",
46
44
  "@types/cors": "^2.8.11",
47
45
  "@types/dotenv": "^8.2.0",
@@ -1,5 +1,3 @@
1
- import '@/index';
2
- import config from 'config';
3
1
  import compression from 'compression';
4
2
  import cookieParser from 'cookie-parser';
5
3
  import cors from 'cors';
@@ -10,6 +8,7 @@ import morgan from 'morgan';
10
8
  import { connect, set } from 'mongoose';
11
9
  import swaggerJSDoc from 'swagger-jsdoc';
12
10
  import swaggerUi from 'swagger-ui-express';
11
+ import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config';
13
12
  import { dbConnection } from '@databases';
14
13
  import { Routes } from '@interfaces/routes.interface';
15
14
  import errorMiddleware from '@middlewares/error.middleware';
@@ -17,13 +16,13 @@ import { logger, stream } from '@utils/logger';
17
16
 
18
17
  class App {
19
18
  public app: express.Application;
20
- public port: string | number;
21
19
  public env: string;
20
+ public port: string | number;
22
21
 
23
22
  constructor(routes: Routes[]) {
24
23
  this.app = express();
25
- this.port = process.env.PORT || 3000;
26
- this.env = process.env.NODE_ENV || 'development';
24
+ this.env = NODE_ENV || 'development';
25
+ this.port = PORT || 3000;
27
26
 
28
27
  this.connectToDatabase();
29
28
  this.initializeMiddlewares();
@@ -54,8 +53,8 @@ class App {
54
53
  }
55
54
 
56
55
  private initializeMiddlewares() {
57
- this.app.use(morgan(config.get('log.format'), { stream }));
58
- this.app.use(cors({ origin: config.get('cors.origin'), credentials: config.get('cors.credentials') }));
56
+ this.app.use(morgan(LOG_FORMAT, { stream }));
57
+ this.app.use(cors({ origin: ORIGIN, credentials: CREDENTIALS }));
59
58
  this.app.use(hpp());
60
59
  this.app.use(helmet());
61
60
  this.app.use(compression());
@@ -0,0 +1,5 @@
1
+ import { config } from 'dotenv';
2
+ config({ path: `.env.${process.env.NODE_ENV || 'development'}.local` });
3
+
4
+ export const CREDENTIALS = process.env.CREDENTIALS === 'true';
5
+ export const { NODE_ENV, PORT, DB_HOST, DB_PORT, DB_DATABASE, SECRET_KEY, LOG_FORMAT, LOG_DIR, ORIGIN } = process.env;
@@ -1,10 +1,7 @@
1
- import config from 'config';
2
- import { dbConfig } from '@interfaces/db.interface';
3
-
4
- const { host, port, database }: dbConfig = config.get('dbConfig');
1
+ import { DB_HOST, DB_PORT, DB_DATABASE } from '@config';
5
2
 
6
3
  export const dbConnection = {
7
- url: `mongodb://${host}:${port}/${database}`,
4
+ url: `mongodb://${DB_HOST}:${DB_PORT}/${DB_DATABASE}`,
8
5
  options: {
9
6
  useNewUrlParser: true,
10
7
  useUnifiedTopology: true,
@@ -1,6 +1,6 @@
1
- import config from 'config';
2
1
  import { NextFunction, Response } from 'express';
3
2
  import { verify } from 'jsonwebtoken';
3
+ import { SECRET_KEY } from '@config';
4
4
  import { HttpException } from '@exceptions/HttpException';
5
5
  import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';
6
6
  import userModel from '@models/users.model';
@@ -10,7 +10,7 @@ const authMiddleware = async (req: RequestWithUser, res: Response, next: NextFun
10
10
  const Authorization = req.cookies['Authorization'] || (req.header('Authorization') ? req.header('Authorization').split('Bearer ')[1] : null);
11
11
 
12
12
  if (Authorization) {
13
- const secretKey: string = config.get('secretKey');
13
+ const secretKey: string = SECRET_KEY;
14
14
  const verificationResponse = (await verify(Authorization, secretKey)) as DataStoredInToken;
15
15
  const userId = verificationResponse._id;
16
16
  const findUser = await userModel.findById(userId);
@@ -1,5 +1,3 @@
1
- import 'dotenv/config';
2
- import '@/index';
3
1
  import App from '@/app';
4
2
  import AuthRoute from '@routes/auth.route';
5
3
  import IndexRoute from '@routes/index.route';
@@ -1,6 +1,6 @@
1
1
  import { hash, compare } from 'bcrypt';
2
- import config from 'config';
3
2
  import { sign } from 'jsonwebtoken';
3
+ import { SECRET_KEY } from '@config';
4
4
  import { CreateUserDto } from '@dtos/users.dto';
5
5
  import { HttpException } from '@exceptions/HttpException';
6
6
  import { DataStoredInToken, TokenData } from '@interfaces/auth.interface';
@@ -49,7 +49,7 @@ class AuthService {
49
49
 
50
50
  public createToken(user: User): TokenData {
51
51
  const dataStoredInToken: DataStoredInToken = { _id: user._id };
52
- const secretKey: string = config.get('secretKey');
52
+ const secretKey: string = SECRET_KEY;
53
53
  const expiresIn: number = 60 * 60;
54
54
 
55
55
  return { expiresIn, token: sign(dataStoredInToken, secretKey, { expiresIn }) };
@@ -1,11 +1,11 @@
1
- import config from 'config';
2
1
  import { existsSync, mkdirSync } from 'fs';
3
2
  import { join } from 'path';
4
3
  import winston from 'winston';
5
4
  import winstonDaily from 'winston-daily-rotate-file';
5
+ import { LOG_DIR } from '@config';
6
6
 
7
7
  // logs dir
8
- const logDir: string = join(__dirname, config.get('log.dir'));
8
+ const logDir: string = join(__dirname, LOG_DIR);
9
9
 
10
10
  if (!existsSync(logDir)) {
11
11
  mkdirSync(logDir);
@@ -22,6 +22,7 @@
22
22
  "baseUrl": "src",
23
23
  "paths": {
24
24
  "@/*": ["*"],
25
+ "@config": ["config"],
25
26
  "@controllers/*": ["controllers/*"],
26
27
  "@databases": ["databases"],
27
28
  "@dtos/*": ["dtos/*"],