typescript-express-starter 8.0.2 → 8.1.2
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.
- package/lib/default/.env.development.local +13 -0
- package/lib/default/.env.production.local +13 -0
- package/lib/default/.env.test.local +13 -0
- package/lib/default/.swcrc +1 -0
- package/lib/default/package.json +0 -2
- package/lib/default/src/app.ts +6 -7
- package/lib/default/src/config/index.ts +5 -0
- package/lib/default/src/middlewares/auth.middleware.ts +2 -2
- package/lib/default/src/server.ts +0 -2
- package/lib/default/src/services/auth.service.ts +2 -2
- package/lib/default/src/utils/logger.ts +2 -2
- package/lib/default/tsconfig.json +1 -0
- package/lib/graphql/.env.development.local +20 -0
- package/lib/graphql/.env.production.local +20 -0
- package/lib/graphql/.env.test.local +20 -0
- package/lib/graphql/.swcrc +1 -0
- package/lib/graphql/package.json +0 -2
- package/lib/graphql/src/app.ts +7 -8
- package/lib/graphql/src/config/index.ts +5 -0
- package/lib/graphql/src/databases/index.ts +6 -8
- package/lib/graphql/src/middlewares/auth.middleware.ts +2 -2
- package/lib/graphql/src/repositories/auth.repository.ts +2 -2
- package/lib/graphql/src/server.ts +0 -3
- package/lib/graphql/src/utils/logger.ts +2 -2
- package/lib/graphql/tsconfig.json +1 -0
- package/lib/knex/.env.development.local +20 -0
- package/lib/knex/.env.production.local +20 -0
- package/lib/knex/.env.test.local +20 -0
- package/lib/knex/.swcrc +1 -0
- package/lib/knex/knexfile.ts +6 -10
- package/lib/knex/package.json +0 -2
- package/lib/knex/src/app.ts +6 -7
- package/lib/knex/src/config/index.ts +5 -0
- package/lib/knex/src/databases/index.ts +6 -8
- package/lib/knex/src/middlewares/auth.middleware.ts +2 -2
- package/lib/knex/src/server.ts +0 -2
- package/lib/knex/src/services/auth.service.ts +2 -2
- package/lib/knex/src/utils/logger.ts +2 -2
- package/lib/knex/tsconfig.json +1 -0
- package/lib/mongoose/.env.development.local +18 -0
- package/lib/mongoose/.env.production.local +18 -0
- package/lib/mongoose/.env.test.local +18 -0
- package/lib/mongoose/.swcrc +1 -0
- package/lib/mongoose/package.json +0 -2
- package/lib/mongoose/src/app.ts +6 -7
- package/lib/mongoose/src/config/index.ts +5 -0
- package/lib/mongoose/src/databases/index.ts +2 -5
- package/lib/mongoose/src/middlewares/auth.middleware.ts +2 -2
- package/lib/mongoose/src/server.ts +0 -2
- package/lib/mongoose/src/services/auth.service.ts +2 -2
- package/lib/mongoose/src/utils/logger.ts +2 -2
- package/lib/mongoose/tsconfig.json +1 -0
- package/lib/prisma/.env.development.local +16 -0
- package/lib/prisma/.env.production.local +16 -0
- package/lib/prisma/.env.test.local +16 -0
- package/lib/prisma/.swcrc +1 -0
- package/lib/prisma/package.json +0 -2
- package/lib/prisma/src/app.ts +6 -7
- package/lib/prisma/src/config/index.ts +5 -0
- package/lib/prisma/src/middlewares/auth.middleware.ts +2 -2
- package/lib/prisma/src/server.ts +0 -2
- package/lib/prisma/src/services/auth.service.ts +2 -2
- package/lib/prisma/src/utils/logger.ts +2 -2
- package/lib/prisma/tsconfig.json +1 -0
- package/lib/routing-controllers/.env.development.local +13 -0
- package/lib/routing-controllers/.env.production.local +13 -0
- package/lib/routing-controllers/.env.test.local +13 -0
- package/lib/routing-controllers/.swcrc +1 -0
- package/lib/routing-controllers/package.json +0 -2
- package/lib/routing-controllers/src/app.ts +7 -8
- package/lib/routing-controllers/src/config/index.ts +5 -0
- package/lib/routing-controllers/src/middlewares/auth.middleware.ts +2 -2
- package/lib/routing-controllers/src/server.ts +0 -2
- package/lib/routing-controllers/src/services/auth.service.ts +2 -2
- package/lib/routing-controllers/src/utils/logger.ts +2 -2
- package/lib/routing-controllers/tsconfig.json +1 -0
- package/lib/sequelize/.env.development.local +20 -0
- package/lib/sequelize/.env.production.local +20 -0
- package/lib/sequelize/.env.test.local +20 -0
- package/lib/sequelize/.swcrc +1 -0
- package/lib/sequelize/package.json +0 -2
- package/lib/sequelize/src/app.ts +6 -7
- package/lib/sequelize/src/config/index.ts +5 -0
- package/lib/sequelize/src/databases/index.ts +7 -8
- package/lib/sequelize/src/middlewares/auth.middleware.ts +2 -2
- package/lib/sequelize/src/server.ts +0 -2
- package/lib/sequelize/src/services/auth.service.ts +2 -2
- package/lib/sequelize/src/utils/logger.ts +2 -2
- package/lib/sequelize/tsconfig.json +1 -0
- package/lib/typeorm/.env.development.local +20 -0
- package/lib/typeorm/.env.production.local +20 -0
- package/lib/typeorm/.env.test.local +20 -0
- package/lib/typeorm/.swcrc +1 -0
- package/lib/typeorm/package.json +0 -2
- package/lib/typeorm/src/app.ts +6 -7
- package/lib/typeorm/src/config/index.ts +5 -0
- package/lib/typeorm/src/databases/index.ts +6 -8
- package/lib/typeorm/src/middlewares/auth.middleware.ts +2 -2
- package/lib/typeorm/src/server.ts +0 -2
- package/lib/typeorm/src/services/auth.service.ts +2 -2
- package/lib/typeorm/src/utils/logger.ts +2 -2
- package/lib/typeorm/tsconfig.json +1 -0
- package/package.json +1 -1
- package/lib/default/.env +0 -1
- package/lib/default/src/configs/development.json +0 -12
- package/lib/default/src/configs/production.json +0 -12
- package/lib/default/src/configs/test.json +0 -12
- package/lib/default/src/index.ts +0 -1
- package/lib/graphql/.env +0 -1
- package/lib/graphql/src/configs/development.json +0 -19
- package/lib/graphql/src/configs/production.json +0 -19
- package/lib/graphql/src/configs/test.json +0 -19
- package/lib/graphql/src/index.ts +0 -1
- package/lib/graphql/src/interfaces/db.interface.ts +0 -7
- package/lib/knex/.env +0 -1
- package/lib/knex/src/configs/development.json +0 -19
- package/lib/knex/src/configs/production.json +0 -19
- package/lib/knex/src/configs/test.json +0 -19
- package/lib/knex/src/index.ts +0 -1
- package/lib/knex/src/interfaces/db.interface.ts +0 -7
- package/lib/mongoose/.env +0 -1
- package/lib/mongoose/src/configs/development.json +0 -17
- package/lib/mongoose/src/configs/production.json +0 -17
- package/lib/mongoose/src/configs/test.json +0 -17
- package/lib/mongoose/src/index.ts +0 -1
- package/lib/mongoose/src/interfaces/db.interface.ts +0 -5
- package/lib/prisma/.env +0 -10
- package/lib/prisma/src/configs/development.json +0 -12
- package/lib/prisma/src/configs/production.json +0 -12
- package/lib/prisma/src/configs/test.json +0 -12
- package/lib/prisma/src/index.ts +0 -1
- package/lib/routing-controllers/.env +0 -1
- package/lib/routing-controllers/src/configs/development.json +0 -12
- package/lib/routing-controllers/src/configs/production.json +0 -12
- package/lib/routing-controllers/src/configs/test.json +0 -12
- package/lib/routing-controllers/src/index.ts +0 -1
- package/lib/sequelize/.env +0 -1
- package/lib/sequelize/src/configs/development.json +0 -22
- package/lib/sequelize/src/configs/production.json +0 -22
- package/lib/sequelize/src/configs/test.json +0 -22
- package/lib/sequelize/src/index.ts +0 -1
- package/lib/sequelize/src/interfaces/db.interface.ts +0 -10
- package/lib/typeorm/.env +0 -1
- package/lib/typeorm/src/configs/development.json +0 -18
- package/lib/typeorm/src/configs/production.json +0 -18
- package/lib/typeorm/src/configs/test.json +0 -18
- package/lib/typeorm/src/index.ts +0 -1
- package/lib/typeorm/src/interfaces/db.interface.ts +0 -6
|
@@ -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 =
|
|
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,
|
|
8
|
+
const logDir: string = join(__dirname, LOG_DIR);
|
|
9
9
|
|
|
10
10
|
if (!existsSync(logDir)) {
|
|
11
11
|
mkdirSync(logDir);
|
package/lib/knex/tsconfig.json
CHANGED
|
@@ -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
|
package/lib/mongoose/.swcrc
CHANGED
|
@@ -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",
|
package/lib/mongoose/src/app.ts
CHANGED
|
@@ -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.
|
|
26
|
-
this.
|
|
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(
|
|
58
|
-
this.app.use(cors({ origin:
|
|
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
|
|
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://${
|
|
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 =
|
|
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,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 =
|
|
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,
|
|
8
|
+
const logDir: string = join(__dirname, LOG_DIR);
|
|
9
9
|
|
|
10
10
|
if (!existsSync(logDir)) {
|
|
11
11
|
mkdirSync(logDir);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# PORT
|
|
2
|
+
PORT = 3000
|
|
3
|
+
|
|
4
|
+
# DATABASE
|
|
5
|
+
DATABASE_URL= mysql://root:password@localhost:3306/test
|
|
6
|
+
|
|
7
|
+
# TOKEN
|
|
8
|
+
SECRET_KEY = secretKey
|
|
9
|
+
|
|
10
|
+
# LOG
|
|
11
|
+
LOG_FORMAT = combined
|
|
12
|
+
LOG_DIR = ../logs
|
|
13
|
+
|
|
14
|
+
# CORS
|
|
15
|
+
ORIGIN = your.domain.com
|
|
16
|
+
CREDENTIALS = true
|
package/lib/prisma/.swcrc
CHANGED
package/lib/prisma/package.json
CHANGED
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
"class-transformer": "^0.4.0",
|
|
28
28
|
"class-validator": "^0.13.1",
|
|
29
29
|
"compression": "^1.7.4",
|
|
30
|
-
"config": "^3.3.6",
|
|
31
30
|
"cookie-parser": "^1.4.5",
|
|
32
31
|
"cors": "^2.8.5",
|
|
33
32
|
"dotenv": "^10.0.0",
|
|
@@ -47,7 +46,6 @@
|
|
|
47
46
|
"@swc/core": "^1.2.108",
|
|
48
47
|
"@types/bcrypt": "^5.0.0",
|
|
49
48
|
"@types/compression": "^1.7.1",
|
|
50
|
-
"@types/config": "^0.0.39",
|
|
51
49
|
"@types/cookie-parser": "^1.4.2",
|
|
52
50
|
"@types/cors": "^2.8.11",
|
|
53
51
|
"@types/dotenv": "^8.2.0",
|
package/lib/prisma/src/app.ts
CHANGED
|
@@ -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';
|
|
@@ -9,19 +7,20 @@ import hpp from 'hpp';
|
|
|
9
7
|
import morgan from 'morgan';
|
|
10
8
|
import swaggerJSDoc from 'swagger-jsdoc';
|
|
11
9
|
import swaggerUi from 'swagger-ui-express';
|
|
10
|
+
import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config';
|
|
12
11
|
import { Routes } from '@interfaces/routes.interface';
|
|
13
12
|
import errorMiddleware from '@middlewares/error.middleware';
|
|
14
13
|
import { logger, stream } from '@utils/logger';
|
|
15
14
|
|
|
16
15
|
class App {
|
|
17
16
|
public app: express.Application;
|
|
18
|
-
public port: string | number;
|
|
19
17
|
public env: string;
|
|
18
|
+
public port: string | number;
|
|
20
19
|
|
|
21
20
|
constructor(routes: Routes[]) {
|
|
22
21
|
this.app = express();
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
22
|
+
this.env = NODE_ENV || 'development';
|
|
23
|
+
this.port = PORT || 3000;
|
|
25
24
|
|
|
26
25
|
this.initializeMiddlewares();
|
|
27
26
|
this.initializeRoutes(routes);
|
|
@@ -43,8 +42,8 @@ class App {
|
|
|
43
42
|
}
|
|
44
43
|
|
|
45
44
|
private initializeMiddlewares() {
|
|
46
|
-
this.app.use(morgan(
|
|
47
|
-
this.app.use(cors({ origin:
|
|
45
|
+
this.app.use(morgan(LOG_FORMAT, { stream }));
|
|
46
|
+
this.app.use(cors({ origin: ORIGIN, credentials: CREDENTIALS }));
|
|
48
47
|
this.app.use(hpp());
|
|
49
48
|
this.app.use(helmet());
|
|
50
49
|
this.app.use(compression());
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import config from 'config';
|
|
2
1
|
import { NextFunction, Response } from 'express';
|
|
3
2
|
import { verify } from 'jsonwebtoken';
|
|
4
3
|
import { PrismaClient } from '@prisma/client';
|
|
4
|
+
import { SECRET_KEY } from '@config';
|
|
5
5
|
import { HttpException } from '@exceptions/HttpException';
|
|
6
6
|
import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';
|
|
7
7
|
|
|
@@ -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 =
|
|
13
|
+
const secretKey: string = SECRET_KEY;
|
|
14
14
|
const verificationResponse = (await verify(Authorization, secretKey)) as DataStoredInToken;
|
|
15
15
|
const userId = verificationResponse.id;
|
|
16
16
|
|
package/lib/prisma/src/server.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { compare, hash } from 'bcrypt';
|
|
2
|
-
import config from 'config';
|
|
3
2
|
import { sign } from 'jsonwebtoken';
|
|
4
3
|
import { PrismaClient, User } from '@prisma/client';
|
|
4
|
+
import { SECRET_KEY } from '@config';
|
|
5
5
|
import { CreateUserDto } from '@dtos/users.dto';
|
|
6
6
|
import { HttpException } from '@exceptions/HttpException';
|
|
7
7
|
import { DataStoredInToken, TokenData } from '@interfaces/auth.interface';
|
|
@@ -48,7 +48,7 @@ class AuthService {
|
|
|
48
48
|
|
|
49
49
|
public createToken(user: User): TokenData {
|
|
50
50
|
const dataStoredInToken: DataStoredInToken = { id: user.id };
|
|
51
|
-
const secretKey: string =
|
|
51
|
+
const secretKey: string = SECRET_KEY;
|
|
52
52
|
const expiresIn: number = 60 * 60;
|
|
53
53
|
|
|
54
54
|
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,
|
|
8
|
+
const logDir: string = join(__dirname, LOG_DIR);
|
|
9
9
|
|
|
10
10
|
if (!existsSync(logDir)) {
|
|
11
11
|
mkdirSync(logDir);
|
package/lib/prisma/tsconfig.json
CHANGED
|
@@ -21,7 +21,6 @@
|
|
|
21
21
|
"class-validator": "^0.13.1",
|
|
22
22
|
"class-validator-jsonschema": "^3.0.2",
|
|
23
23
|
"compression": "^1.7.4",
|
|
24
|
-
"config": "^3.3.6",
|
|
25
24
|
"cookie-parser": "^1.4.5",
|
|
26
25
|
"cors": "^2.8.5",
|
|
27
26
|
"dotenv": "^10.0.0",
|
|
@@ -43,7 +42,6 @@
|
|
|
43
42
|
"@swc/core": "^1.2.108",
|
|
44
43
|
"@types/bcrypt": "^5.0.0",
|
|
45
44
|
"@types/compression": "^1.7.1",
|
|
46
|
-
"@types/config": "^0.0.39",
|
|
47
45
|
"@types/cookie-parser": "^1.4.2",
|
|
48
46
|
"@types/cors": "^2.8.11",
|
|
49
47
|
"@types/dotenv": "^8.2.0",
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
import '@/index';
|
|
3
2
|
import { defaultMetadataStorage } from 'class-transformer';
|
|
4
3
|
import { validationMetadatasToSchemas } from 'class-validator-jsonschema';
|
|
5
4
|
import compression from 'compression';
|
|
6
5
|
import cookieParser from 'cookie-parser';
|
|
7
|
-
import config from 'config';
|
|
8
6
|
import express from 'express';
|
|
9
7
|
import helmet from 'helmet';
|
|
10
8
|
import hpp from 'hpp';
|
|
@@ -12,18 +10,19 @@ import morgan from 'morgan';
|
|
|
12
10
|
import { useExpressServer, getMetadataArgsStorage } from 'routing-controllers';
|
|
13
11
|
import { routingControllersToSpec } from 'routing-controllers-openapi';
|
|
14
12
|
import swaggerUi from 'swagger-ui-express';
|
|
13
|
+
import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config';
|
|
15
14
|
import errorMiddleware from '@middlewares/error.middleware';
|
|
16
15
|
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(Controllers: Function[]) {
|
|
24
23
|
this.app = express();
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
24
|
+
this.env = NODE_ENV || 'development';
|
|
25
|
+
this.port = PORT || 3000;
|
|
27
26
|
|
|
28
27
|
this.initializeMiddlewares();
|
|
29
28
|
this.initializeRoutes(Controllers);
|
|
@@ -45,7 +44,7 @@ class App {
|
|
|
45
44
|
}
|
|
46
45
|
|
|
47
46
|
private initializeMiddlewares() {
|
|
48
|
-
this.app.use(morgan(
|
|
47
|
+
this.app.use(morgan(LOG_FORMAT, { stream }));
|
|
49
48
|
this.app.use(hpp());
|
|
50
49
|
this.app.use(helmet());
|
|
51
50
|
this.app.use(compression());
|
|
@@ -57,8 +56,8 @@ class App {
|
|
|
57
56
|
private initializeRoutes(controllers: Function[]) {
|
|
58
57
|
useExpressServer(this.app, {
|
|
59
58
|
cors: {
|
|
60
|
-
origin:
|
|
61
|
-
credentials:
|
|
59
|
+
origin: ORIGIN,
|
|
60
|
+
credentials: CREDENTIALS,
|
|
62
61
|
},
|
|
63
62
|
controllers: controllers,
|
|
64
63
|
defaultErrorHandler: false,
|
|
@@ -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 =
|
|
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 = userModel.find(user => user.id === userId);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { compare, hash } 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 =
|
|
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,
|
|
8
|
+
const logDir: string = join(__dirname, LOG_DIR);
|
|
9
9
|
|
|
10
10
|
if (!existsSync(logDir)) {
|
|
11
11
|
mkdirSync(logDir);
|