typescript-express-starter 6.2.0 → 8.0.0
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/README.kr.md +55 -26
- package/README.md +54 -25
- package/bin/cli.js +4 -3
- package/lib/default/.swcrc +41 -0
- package/lib/default/nodemon.json +2 -2
- package/lib/default/package.json +12 -8
- package/lib/default/src/app.ts +1 -2
- package/lib/default/src/http/auth.http +12 -15
- package/lib/default/src/http/users.http +12 -16
- package/lib/default/src/index.ts +1 -0
- package/lib/default/src/middlewares/auth.middleware.ts +3 -3
- package/lib/default/src/models/users.model.ts +5 -5
- package/lib/default/src/server.ts +1 -2
- package/lib/default/src/services/auth.service.ts +5 -5
- package/lib/default/src/services/users.service.ts +3 -3
- package/lib/default/src/tests/auth.test.ts +4 -4
- package/lib/default/src/tests/users.test.ts +4 -4
- package/lib/default/src/utils/logger.ts +5 -5
- package/lib/default/tsconfig.json +2 -2
- package/lib/graphql/.dockerignore +18 -0
- package/lib/graphql/.editorconfig +9 -0
- package/lib/graphql/.env +1 -0
- package/lib/graphql/.eslintignore +1 -0
- package/lib/graphql/.eslintrc +18 -0
- package/lib/graphql/.huskyrc +5 -0
- package/lib/graphql/.lintstagedrc.json +5 -0
- package/lib/graphql/.prettierrc +8 -0
- package/lib/graphql/.swcrc +39 -0
- package/lib/graphql/.vscode/launch.json +35 -0
- package/lib/graphql/.vscode/settings.json +6 -0
- package/lib/graphql/Dockerfile +24 -0
- package/lib/graphql/Makefile +6 -0
- package/lib/graphql/docker-compose.yml +50 -0
- package/lib/graphql/ecosystem.config.js +59 -0
- package/lib/graphql/jest.config.js +12 -0
- package/lib/graphql/nginx.conf +40 -0
- package/lib/graphql/nodemon.json +12 -0
- package/lib/graphql/package.json +79 -0
- package/lib/graphql/src/app.ts +105 -0
- package/lib/graphql/src/configs/development.json +19 -0
- package/lib/graphql/src/configs/production.json +19 -0
- package/lib/graphql/src/configs/test.json +19 -0
- package/lib/graphql/src/databases/index.ts +24 -0
- package/lib/graphql/src/dtos/users.dto.ts +14 -0
- package/lib/{typeorm/src/entity → graphql/src/entities}/users.entity.ts +3 -3
- package/lib/graphql/src/exceptions/HttpException.ts +10 -0
- package/lib/graphql/src/http/auth.http +49 -0
- package/lib/graphql/src/http/users.http +78 -0
- package/lib/graphql/src/index.ts +1 -0
- package/lib/graphql/src/interfaces/auth.interface.ts +14 -0
- package/lib/graphql/src/interfaces/db.interface.ts +7 -0
- package/lib/graphql/src/interfaces/users.interface.ts +5 -0
- package/lib/graphql/src/middlewares/auth.middleware.ts +32 -0
- package/lib/graphql/src/middlewares/error.middleware.ts +17 -0
- package/lib/graphql/src/repositories/auth.repository.ts +61 -0
- package/lib/graphql/src/repositories/users.repository.ts +60 -0
- package/lib/graphql/src/resolvers/auth.resolver.ts +32 -0
- package/lib/graphql/src/resolvers/users.resolver.ts +47 -0
- package/lib/graphql/src/server.ts +14 -0
- package/lib/graphql/src/tests/auth.test.ts +52 -0
- package/lib/graphql/src/tests/index.test.ts +18 -0
- package/lib/graphql/src/tests/users.test.ts +71 -0
- package/lib/graphql/src/typedefs/users.type.ts +13 -0
- package/lib/graphql/src/utils/logger.ts +75 -0
- package/lib/graphql/src/utils/util.ts +19 -0
- package/lib/graphql/src/utils/validateEnv.ts +10 -0
- package/lib/graphql/tsconfig.json +39 -0
- package/lib/knex/.swcrc +39 -0
- package/lib/knex/knexfile.ts +6 -6
- package/lib/knex/nodemon.json +2 -2
- package/lib/knex/package.json +11 -8
- package/lib/knex/src/app.ts +2 -3
- package/lib/knex/src/databases/index.ts +6 -6
- package/lib/knex/src/http/auth.http +12 -15
- package/lib/knex/src/http/users.http +12 -16
- package/lib/knex/src/index.ts +1 -0
- package/lib/knex/src/middlewares/auth.middleware.ts +4 -4
- package/lib/knex/src/server.ts +1 -2
- package/lib/knex/src/services/auth.service.ts +5 -5
- package/lib/knex/src/services/users.service.ts +3 -3
- package/lib/knex/src/tests/auth.test.ts +2 -2
- package/lib/knex/src/tests/index.test.ts +1 -1
- package/lib/knex/src/tests/users.test.ts +2 -2
- package/lib/knex/src/utils/logger.ts +5 -5
- package/lib/knex/tsconfig.json +2 -2
- package/lib/mongoose/.swcrc +39 -0
- package/lib/mongoose/docker-compose.yml +0 -4
- package/lib/mongoose/nodemon.json +2 -2
- package/lib/mongoose/package.json +12 -8
- package/lib/mongoose/src/app.ts +2 -3
- package/lib/mongoose/src/configs/development.json +1 -1
- package/lib/mongoose/src/configs/production.json +1 -1
- package/lib/mongoose/src/configs/test.json +1 -1
- package/lib/mongoose/src/http/auth.http +32 -0
- package/lib/mongoose/src/http/users.http +34 -0
- package/lib/mongoose/src/index.ts +1 -0
- package/lib/mongoose/src/middlewares/auth.middleware.ts +3 -3
- package/lib/mongoose/src/server.ts +1 -2
- package/lib/mongoose/src/services/auth.service.ts +5 -5
- package/lib/mongoose/src/services/users.service.ts +3 -3
- package/lib/mongoose/src/utils/logger.ts +5 -5
- package/lib/mongoose/tsconfig.json +2 -2
- package/lib/prisma/.env +1 -1
- package/lib/prisma/.swcrc +37 -0
- package/lib/prisma/docker-compose.yml +1 -6
- package/lib/prisma/nodemon.json +2 -2
- package/lib/prisma/package.json +14 -10
- package/lib/prisma/src/app.ts +2 -3
- package/lib/prisma/src/http/auth.http +12 -15
- package/lib/prisma/src/http/users.http +12 -16
- package/lib/prisma/src/index.ts +1 -0
- package/lib/prisma/src/middlewares/auth.middleware.ts +4 -4
- package/lib/prisma/src/server.ts +1 -2
- package/lib/prisma/src/services/auth.service.ts +5 -5
- package/lib/prisma/src/services/users.service.ts +3 -3
- package/lib/prisma/src/utils/logger.ts +5 -5
- package/lib/prisma/tsconfig.json +2 -2
- package/lib/routing-controllers/.swcrc +37 -0
- package/lib/routing-controllers/nodemon.json +2 -2
- package/lib/routing-controllers/package.json +12 -9
- package/lib/routing-controllers/src/app.ts +3 -4
- package/lib/routing-controllers/src/http/auth.http +12 -15
- package/lib/routing-controllers/src/http/users.http +12 -16
- package/lib/routing-controllers/src/index.ts +1 -0
- package/lib/routing-controllers/src/middlewares/auth.middleware.ts +3 -3
- package/lib/routing-controllers/src/middlewares/validation.middleware.ts +10 -2
- package/lib/routing-controllers/src/models/users.model.ts +5 -5
- package/lib/routing-controllers/src/server.ts +1 -3
- package/lib/routing-controllers/src/services/auth.service.ts +5 -5
- package/lib/routing-controllers/src/services/users.service.ts +3 -3
- package/lib/routing-controllers/src/utils/logger.ts +5 -5
- package/lib/routing-controllers/tsconfig.json +2 -2
- package/lib/sequelize/.swcrc +39 -0
- package/lib/sequelize/docker-compose.yml +1 -6
- package/lib/sequelize/nodemon.json +2 -2
- package/lib/sequelize/package.json +12 -8
- package/lib/sequelize/src/app.ts +1 -2
- package/lib/sequelize/src/configs/development.json +2 -2
- package/lib/sequelize/src/configs/production.json +1 -1
- package/lib/sequelize/src/configs/test.json +1 -1
- package/lib/sequelize/src/http/auth.http +32 -0
- package/lib/sequelize/src/http/users.http +34 -0
- package/lib/sequelize/src/index.ts +1 -0
- package/lib/sequelize/src/middlewares/auth.middleware.ts +4 -4
- package/lib/sequelize/src/server.ts +1 -2
- package/lib/sequelize/src/services/auth.service.ts +5 -5
- package/lib/sequelize/src/services/users.service.ts +3 -3
- package/lib/sequelize/src/utils/logger.ts +5 -5
- package/lib/sequelize/tsconfig.json +2 -2
- package/lib/starter.js +5 -9
- package/lib/typegoose/.dockerignore +18 -0
- package/lib/typegoose/.editorconfig +9 -0
- package/lib/typegoose/.env.development.local +18 -0
- package/lib/typegoose/.env.production.local +18 -0
- package/lib/typegoose/.env.test.local +18 -0
- package/lib/typegoose/.eslintignore +1 -0
- package/lib/typegoose/.eslintrc +18 -0
- package/lib/typegoose/.huskyrc +5 -0
- package/lib/typegoose/.lintstagedrc.json +5 -0
- package/lib/typegoose/.prettierrc +8 -0
- package/lib/typegoose/.swcrc +40 -0
- package/lib/typegoose/.vscode/launch.json +35 -0
- package/lib/typegoose/.vscode/settings.json +6 -0
- package/lib/typegoose/Dockerfile +24 -0
- package/lib/typegoose/Makefile +6 -0
- package/lib/typegoose/docker-compose.yml +46 -0
- package/lib/typegoose/ecosystem.config.js +57 -0
- package/lib/typegoose/jest.config.js +12 -0
- package/lib/typegoose/nginx.conf +40 -0
- package/lib/typegoose/nodemon.json +12 -0
- package/lib/typegoose/package.json +80 -0
- package/lib/typegoose/src/app.ts +93 -0
- package/lib/typegoose/src/config/index.ts +5 -0
- package/lib/typegoose/src/controllers/auth.controller.ts +46 -0
- package/lib/typegoose/src/controllers/index.controller.ts +13 -0
- package/lib/typegoose/src/controllers/users.controller.ts +65 -0
- package/lib/typegoose/src/databases/index.ts +3 -0
- package/lib/typegoose/src/dtos/users.dto.ts +9 -0
- package/lib/typegoose/src/exceptions/HttpException.ts +10 -0
- package/lib/typegoose/src/http/auth.http +32 -0
- package/lib/typegoose/src/http/users.http +34 -0
- package/lib/typegoose/src/interfaces/auth.interface.ts +15 -0
- package/lib/typegoose/src/interfaces/routes.interface.ts +6 -0
- package/lib/typegoose/src/interfaces/users.interface.ts +5 -0
- package/lib/typegoose/src/middlewares/auth.middleware.ts +32 -0
- package/lib/typegoose/src/middlewares/error.middleware.ts +17 -0
- package/lib/typegoose/src/middlewares/validation.middleware.ts +25 -0
- package/lib/typegoose/src/models/users.model.ts +18 -0
- package/lib/typegoose/src/routes/auth.route.ts +24 -0
- package/lib/typegoose/src/routes/index.route.ts +19 -0
- package/lib/typegoose/src/routes/users.route.ts +25 -0
- package/lib/typegoose/src/server.ts +11 -0
- package/lib/typegoose/src/services/auth.service.ts +61 -0
- package/lib/typegoose/src/services/users.service.ts +64 -0
- package/lib/typegoose/src/tests/auth.test.ts +83 -0
- package/lib/typegoose/src/tests/index.test.ts +18 -0
- package/lib/typegoose/src/tests/users.test.ts +133 -0
- package/lib/typegoose/src/utils/logger.ts +65 -0
- package/lib/typegoose/src/utils/util.ts +19 -0
- package/lib/typegoose/src/utils/validateEnv.ts +10 -0
- package/lib/typegoose/swagger.yaml +122 -0
- package/lib/typegoose/tsconfig.json +40 -0
- package/lib/typeorm/.swcrc +39 -0
- package/lib/typeorm/docker-compose.yml +1 -1
- package/lib/typeorm/nodemon.json +2 -2
- package/lib/typeorm/package.json +12 -8
- package/lib/typeorm/src/app.ts +1 -2
- package/lib/typeorm/src/configs/development.json +1 -1
- package/lib/typeorm/src/configs/production.json +1 -1
- package/lib/typeorm/src/configs/test.json +1 -1
- package/lib/typeorm/src/databases/index.ts +5 -5
- package/lib/typeorm/src/entities/users.entity.ts +26 -0
- package/lib/typeorm/src/http/auth.http +12 -15
- package/lib/typeorm/src/http/users.http +12 -16
- package/lib/typeorm/src/index.ts +1 -0
- package/lib/typeorm/src/middlewares/auth.middleware.ts +5 -9
- package/lib/typeorm/src/server.ts +1 -2
- package/lib/typeorm/src/services/auth.service.ts +13 -17
- package/lib/typeorm/src/services/users.service.ts +16 -22
- package/lib/typeorm/src/utils/logger.ts +5 -5
- package/lib/typeorm/tsconfig.json +3 -3
- package/package.json +14 -4
|
@@ -1,35 +1,32 @@
|
|
|
1
|
-
# auth API
|
|
2
|
-
|
|
3
|
-
###
|
|
4
1
|
# baseURL
|
|
5
2
|
@baseURL = http://localhost:3000
|
|
6
3
|
|
|
7
4
|
###
|
|
8
|
-
#
|
|
9
|
-
POST {{baseURL}}/signup
|
|
5
|
+
# User Signup
|
|
6
|
+
POST {{ baseURL }}/signup
|
|
10
7
|
Content-Type: application/json
|
|
11
8
|
|
|
12
9
|
{
|
|
13
|
-
"email": "example@
|
|
14
|
-
"password": "
|
|
10
|
+
"email": "example@email.com",
|
|
11
|
+
"password": "password"
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
###
|
|
18
|
-
# Login
|
|
19
|
-
POST {{baseURL}}/login
|
|
15
|
+
# User Login
|
|
16
|
+
POST {{ baseURL }}/login
|
|
20
17
|
Content-Type: application/json
|
|
21
18
|
|
|
22
19
|
{
|
|
23
|
-
"email": "
|
|
24
|
-
"password": "
|
|
20
|
+
"email": "example@email.com",
|
|
21
|
+
"password": "password"
|
|
25
22
|
}
|
|
26
23
|
|
|
27
24
|
###
|
|
28
|
-
# Logout
|
|
29
|
-
POST {{baseURL}}/logout
|
|
25
|
+
# User Logout
|
|
26
|
+
POST {{ baseURL }}/logout
|
|
30
27
|
Content-Type: application/json
|
|
31
28
|
|
|
32
29
|
{
|
|
33
|
-
"email": "
|
|
34
|
-
"password": "
|
|
30
|
+
"email": "example@email.com",
|
|
31
|
+
"password": "password"
|
|
35
32
|
}
|
|
@@ -1,38 +1,34 @@
|
|
|
1
|
-
# users API
|
|
2
|
-
|
|
3
|
-
###
|
|
4
1
|
# baseURL
|
|
5
2
|
@baseURL = http://localhost:3000
|
|
6
3
|
|
|
7
4
|
###
|
|
8
5
|
# Find All Users
|
|
9
|
-
GET {{baseURL}}/users
|
|
6
|
+
GET {{ baseURL }}/users
|
|
10
7
|
|
|
11
8
|
###
|
|
12
9
|
# Find User By Id
|
|
13
|
-
GET {{baseURL}}/users/1
|
|
14
|
-
|
|
10
|
+
GET {{ baseURL }}/users/1
|
|
15
11
|
|
|
16
12
|
###
|
|
17
|
-
#
|
|
18
|
-
POST {{baseURL}}/users
|
|
13
|
+
# Create User
|
|
14
|
+
POST {{ baseURL }}/users
|
|
19
15
|
Content-Type: application/json
|
|
20
16
|
|
|
21
17
|
{
|
|
22
|
-
"email": "example@
|
|
23
|
-
"password": "
|
|
18
|
+
"email": "example@email.com",
|
|
19
|
+
"password": "password"
|
|
24
20
|
}
|
|
25
21
|
|
|
26
22
|
###
|
|
27
|
-
#
|
|
28
|
-
PUT {{baseURL}}/users/1
|
|
23
|
+
# Modify User By Id
|
|
24
|
+
PUT {{ baseURL }}/users/1
|
|
29
25
|
Content-Type: application/json
|
|
30
26
|
|
|
31
27
|
{
|
|
32
|
-
"email": "example@
|
|
33
|
-
"password": "
|
|
28
|
+
"email": "example@email.com",
|
|
29
|
+
"password": "password"
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
###
|
|
37
|
-
#
|
|
38
|
-
DELETE {{baseURL}}/users/1
|
|
33
|
+
# Delete User By Id
|
|
34
|
+
DELETE {{ baseURL }}/users/1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
process.env['NODE_CONFIG_DIR'] = __dirname + '/configs';
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { NextFunction, Response } from 'express';
|
|
2
1
|
import config from 'config';
|
|
3
|
-
import
|
|
2
|
+
import { NextFunction, Response } from 'express';
|
|
3
|
+
import { verify } from 'jsonwebtoken';
|
|
4
4
|
import { PrismaClient } from '@prisma/client';
|
|
5
5
|
import { HttpException } from '@exceptions/HttpException';
|
|
6
6
|
import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';
|
|
7
7
|
|
|
8
8
|
const authMiddleware = async (req: RequestWithUser, res: Response, next: NextFunction) => {
|
|
9
9
|
try {
|
|
10
|
-
const Authorization = req.cookies['Authorization'] || req.header('Authorization').split('Bearer ')[1]
|
|
10
|
+
const Authorization = req.cookies['Authorization'] || (req.header('Authorization') ? req.header('Authorization').split('Bearer ')[1] : null);
|
|
11
11
|
|
|
12
12
|
if (Authorization) {
|
|
13
13
|
const secretKey: string = config.get('secretKey');
|
|
14
|
-
const verificationResponse = (await
|
|
14
|
+
const verificationResponse = (await verify(Authorization, secretKey)) as DataStoredInToken;
|
|
15
15
|
const userId = verificationResponse.id;
|
|
16
16
|
|
|
17
17
|
const users = new PrismaClient().user;
|
package/lib/prisma/src/server.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { compare, hash } from 'bcrypt';
|
|
2
2
|
import config from 'config';
|
|
3
|
-
import
|
|
3
|
+
import { sign } from 'jsonwebtoken';
|
|
4
4
|
import { PrismaClient, User } from '@prisma/client';
|
|
5
5
|
import { CreateUserDto } from '@dtos/users.dto';
|
|
6
6
|
import { HttpException } from '@exceptions/HttpException';
|
|
@@ -16,7 +16,7 @@ class AuthService {
|
|
|
16
16
|
const findUser: User = await this.users.findUnique({ where: { email: userData.email } });
|
|
17
17
|
if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
18
18
|
|
|
19
|
-
const hashedPassword = await
|
|
19
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
20
20
|
const createUserData: Promise<User> = this.users.create({ data: { ...userData, password: hashedPassword } });
|
|
21
21
|
|
|
22
22
|
return createUserData;
|
|
@@ -28,7 +28,7 @@ class AuthService {
|
|
|
28
28
|
const findUser: User = await this.users.findUnique({ where: { email: userData.email } });
|
|
29
29
|
if (!findUser) throw new HttpException(409, `You're email ${userData.email} not found`);
|
|
30
30
|
|
|
31
|
-
const isPasswordMatching: boolean = await
|
|
31
|
+
const isPasswordMatching: boolean = await compare(userData.password, findUser.password);
|
|
32
32
|
if (!isPasswordMatching) throw new HttpException(409, "You're password not matching");
|
|
33
33
|
|
|
34
34
|
const tokenData = this.createToken(findUser);
|
|
@@ -51,7 +51,7 @@ class AuthService {
|
|
|
51
51
|
const secretKey: string = config.get('secretKey');
|
|
52
52
|
const expiresIn: number = 60 * 60;
|
|
53
53
|
|
|
54
|
-
return { expiresIn, token:
|
|
54
|
+
return { expiresIn, token: sign(dataStoredInToken, secretKey, { expiresIn }) };
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
public createCookie(tokenData: TokenData): string {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { hash } from 'bcrypt';
|
|
2
2
|
import { PrismaClient, User } from '@prisma/client';
|
|
3
3
|
import { CreateUserDto } from '@dtos/users.dto';
|
|
4
4
|
import { HttpException } from '@exceptions/HttpException';
|
|
@@ -27,7 +27,7 @@ class UserService {
|
|
|
27
27
|
const findUser: User = await this.users.findUnique({ where: { email: userData.email } });
|
|
28
28
|
if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
29
29
|
|
|
30
|
-
const hashedPassword = await
|
|
30
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
31
31
|
const createUserData: User = await this.users.create({ data: { ...userData, password: hashedPassword } });
|
|
32
32
|
return createUserData;
|
|
33
33
|
}
|
|
@@ -38,7 +38,7 @@ class UserService {
|
|
|
38
38
|
const findUser: User = await this.users.findUnique({ where: { id: userId } });
|
|
39
39
|
if (!findUser) throw new HttpException(409, "You're not user");
|
|
40
40
|
|
|
41
|
-
const hashedPassword = await
|
|
41
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
42
42
|
const updateUserData = await this.users.update({ where: { id: userId }, data: { ...userData, password: hashedPassword } });
|
|
43
43
|
return updateUserData;
|
|
44
44
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import config from 'config';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import { existsSync, mkdirSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
4
|
import winston from 'winston';
|
|
5
5
|
import winstonDaily from 'winston-daily-rotate-file';
|
|
6
6
|
|
|
7
7
|
// logs dir
|
|
8
|
-
const logDir: string =
|
|
8
|
+
const logDir: string = join(__dirname, config.get('log.dir'));
|
|
9
9
|
|
|
10
|
-
if (!
|
|
11
|
-
|
|
10
|
+
if (!existsSync(logDir)) {
|
|
11
|
+
mkdirSync(logDir);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
// Define log format
|
package/lib/prisma/tsconfig.json
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"pretty": true,
|
|
14
14
|
"sourceMap": true,
|
|
15
15
|
"declaration": true,
|
|
16
|
-
"outDir": "
|
|
16
|
+
"outDir": "dist",
|
|
17
17
|
"allowJs": true,
|
|
18
18
|
"noEmit": false,
|
|
19
19
|
"esModuleInterop": true,
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"include": ["src/**/*.ts", "src/**/*.json", ".env"],
|
|
36
|
-
"exclude": ["node_modules"]
|
|
36
|
+
"exclude": ["node_modules", "src/http", "src/logs", "src/tests"]
|
|
37
37
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"jsc": {
|
|
3
|
+
"parser": {
|
|
4
|
+
"syntax": "typescript",
|
|
5
|
+
"tsx": false,
|
|
6
|
+
"dynamicImport": true,
|
|
7
|
+
"decorators": true
|
|
8
|
+
},
|
|
9
|
+
"transform": {
|
|
10
|
+
"legacyDecorator": true,
|
|
11
|
+
"decoratorMetadata": true
|
|
12
|
+
},
|
|
13
|
+
"target": "es2017",
|
|
14
|
+
"externalHelpers": false,
|
|
15
|
+
"keepClassNames": true,
|
|
16
|
+
"loose": false,
|
|
17
|
+
"minify": {
|
|
18
|
+
"compress": false,
|
|
19
|
+
"mangle": false
|
|
20
|
+
},
|
|
21
|
+
"baseUrl": "src",
|
|
22
|
+
"paths": {
|
|
23
|
+
"@/*": ["*"],
|
|
24
|
+
"@controllers/*": ["controllers/*"],
|
|
25
|
+
"@dtos/*": ["dtos/*"],
|
|
26
|
+
"@exceptions/*": ["exceptions/*"],
|
|
27
|
+
"@interfaces/*": ["interfaces/*"],
|
|
28
|
+
"@middlewares/*": ["middlewares/*"],
|
|
29
|
+
"@models/*": ["models/*"],
|
|
30
|
+
"@services/*": ["services/*"],
|
|
31
|
+
"@utils/*": ["utils/*"]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"module": {
|
|
35
|
+
"type": "commonjs"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -7,7 +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": "
|
|
10
|
+
"build": "swc src -d dist --source-maps --copy-files",
|
|
11
|
+
"build:tsc": "tsc && tsc-alias",
|
|
11
12
|
"test": "jest --forceExit --detectOpenHandles",
|
|
12
13
|
"lint": "eslint --ignore-path .gitignore --ext .ts src/",
|
|
13
14
|
"lint:fix": "npm run lint -- --fix",
|
|
@@ -23,27 +24,23 @@
|
|
|
23
24
|
"config": "^3.3.6",
|
|
24
25
|
"cookie-parser": "^1.4.5",
|
|
25
26
|
"cors": "^2.8.5",
|
|
26
|
-
"cross-env": "^7.0.3",
|
|
27
27
|
"dotenv": "^10.0.0",
|
|
28
28
|
"envalid": "^7.1.0",
|
|
29
29
|
"express": "^4.17.1",
|
|
30
30
|
"helmet": "^4.6.0",
|
|
31
31
|
"hpp": "^0.2.3",
|
|
32
|
-
"jest": "^27.0.5",
|
|
33
32
|
"jsonwebtoken": "^8.5.1",
|
|
34
33
|
"morgan": "^1.10.0",
|
|
35
|
-
"pm2": "^5.1.0",
|
|
36
34
|
"reflect-metadata": "^0.1.13",
|
|
37
35
|
"routing-controllers": "^0.9.0",
|
|
38
36
|
"routing-controllers-openapi": "^3.0.0",
|
|
39
37
|
"swagger-ui-express": "^4.1.6",
|
|
40
|
-
"ts-jest": "^27.0.3",
|
|
41
|
-
"ts-node": "^10.0.0",
|
|
42
|
-
"typescript": "^4.3.5",
|
|
43
38
|
"winston": "^3.3.3",
|
|
44
39
|
"winston-daily-rotate-file": "^4.5.5"
|
|
45
40
|
},
|
|
46
41
|
"devDependencies": {
|
|
42
|
+
"@swc/cli": "^0.1.51",
|
|
43
|
+
"@swc/core": "^1.2.108",
|
|
47
44
|
"@types/bcrypt": "^5.0.0",
|
|
48
45
|
"@types/compression": "^1.7.1",
|
|
49
46
|
"@types/config": "^0.0.39",
|
|
@@ -62,16 +59,22 @@
|
|
|
62
59
|
"@types/winston": "^2.4.4",
|
|
63
60
|
"@typescript-eslint/eslint-plugin": "^4.28.2",
|
|
64
61
|
"@typescript-eslint/parser": "^4.28.2",
|
|
62
|
+
"cross-env": "^7.0.3",
|
|
65
63
|
"eslint": "^7.30.0",
|
|
66
64
|
"eslint-config-prettier": "^8.3.0",
|
|
67
65
|
"eslint-plugin-prettier": "^3.4.0",
|
|
68
66
|
"husky": "^7.0.1",
|
|
67
|
+
"jest": "^27.0.5",
|
|
69
68
|
"lint-staged": "^11.0.0",
|
|
70
69
|
"node-config": "^0.0.2",
|
|
71
70
|
"node-gyp": "^8.1.0",
|
|
72
71
|
"nodemon": "^2.0.9",
|
|
72
|
+
"pm2": "^5.1.0",
|
|
73
73
|
"prettier": "^2.3.2",
|
|
74
|
-
"
|
|
75
|
-
"
|
|
74
|
+
"ts-jest": "^27.0.7",
|
|
75
|
+
"ts-node": "^10.4.0",
|
|
76
|
+
"tsc-alias": "^1.4.1",
|
|
77
|
+
"tsconfig-paths": "^3.10.1",
|
|
78
|
+
"typescript": "^4.5.2"
|
|
76
79
|
}
|
|
77
80
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import '@/index';
|
|
3
|
+
import { defaultMetadataStorage } from 'class-transformer';
|
|
3
4
|
import { validationMetadatasToSchemas } from 'class-validator-jsonschema';
|
|
4
5
|
import compression from 'compression';
|
|
5
6
|
import cookieParser from 'cookie-parser';
|
|
@@ -65,8 +66,6 @@ class App {
|
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
private initializeSwagger(controllers: Function[]) {
|
|
68
|
-
const { defaultMetadataStorage } = require('class-transformer/cjs/storage');
|
|
69
|
-
|
|
70
69
|
const schemas = validationMetadatasToSchemas({
|
|
71
70
|
classTransformerMetadataStorage: defaultMetadataStorage,
|
|
72
71
|
refPointerPrefix: '#/components/schemas/',
|
|
@@ -1,35 +1,32 @@
|
|
|
1
|
-
# auth API
|
|
2
|
-
|
|
3
|
-
###
|
|
4
1
|
# baseURL
|
|
5
2
|
@baseURL = http://localhost:3000
|
|
6
3
|
|
|
7
4
|
###
|
|
8
|
-
#
|
|
9
|
-
POST {{baseURL}}/signup
|
|
5
|
+
# User Signup
|
|
6
|
+
POST {{ baseURL }}/signup
|
|
10
7
|
Content-Type: application/json
|
|
11
8
|
|
|
12
9
|
{
|
|
13
|
-
"email": "example@
|
|
14
|
-
"password": "
|
|
10
|
+
"email": "example@email.com",
|
|
11
|
+
"password": "password"
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
###
|
|
18
|
-
# Login
|
|
19
|
-
POST {{baseURL}}/login
|
|
15
|
+
# User Login
|
|
16
|
+
POST {{ baseURL }}/login
|
|
20
17
|
Content-Type: application/json
|
|
21
18
|
|
|
22
19
|
{
|
|
23
|
-
"email": "
|
|
24
|
-
"password": "
|
|
20
|
+
"email": "example@email.com",
|
|
21
|
+
"password": "password"
|
|
25
22
|
}
|
|
26
23
|
|
|
27
24
|
###
|
|
28
|
-
# Logout
|
|
29
|
-
POST {{baseURL}}/logout
|
|
25
|
+
# User Logout
|
|
26
|
+
POST {{ baseURL }}/logout
|
|
30
27
|
Content-Type: application/json
|
|
31
28
|
|
|
32
29
|
{
|
|
33
|
-
"email": "
|
|
34
|
-
"password": "
|
|
30
|
+
"email": "example@email.com",
|
|
31
|
+
"password": "password"
|
|
35
32
|
}
|
|
@@ -1,38 +1,34 @@
|
|
|
1
|
-
# users API
|
|
2
|
-
|
|
3
|
-
###
|
|
4
1
|
# baseURL
|
|
5
2
|
@baseURL = http://localhost:3000
|
|
6
3
|
|
|
7
4
|
###
|
|
8
5
|
# Find All Users
|
|
9
|
-
GET {{baseURL}}/users
|
|
6
|
+
GET {{ baseURL }}/users
|
|
10
7
|
|
|
11
8
|
###
|
|
12
9
|
# Find User By Id
|
|
13
|
-
GET {{baseURL}}/users/1
|
|
14
|
-
|
|
10
|
+
GET {{ baseURL }}/users/1
|
|
15
11
|
|
|
16
12
|
###
|
|
17
|
-
#
|
|
18
|
-
POST {{baseURL}}/users
|
|
13
|
+
# Create User
|
|
14
|
+
POST {{ baseURL }}/users
|
|
19
15
|
Content-Type: application/json
|
|
20
16
|
|
|
21
17
|
{
|
|
22
|
-
"email": "example@
|
|
23
|
-
"password": "
|
|
18
|
+
"email": "example@email.com",
|
|
19
|
+
"password": "password"
|
|
24
20
|
}
|
|
25
21
|
|
|
26
22
|
###
|
|
27
|
-
#
|
|
28
|
-
PUT {{baseURL}}/users/1
|
|
23
|
+
# Modify User By Id
|
|
24
|
+
PUT {{ baseURL }}/users/1
|
|
29
25
|
Content-Type: application/json
|
|
30
26
|
|
|
31
27
|
{
|
|
32
|
-
"email": "example@
|
|
33
|
-
"password": "
|
|
28
|
+
"email": "example@email.com",
|
|
29
|
+
"password": "password"
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
###
|
|
37
|
-
#
|
|
38
|
-
DELETE {{baseURL}}/users/1
|
|
33
|
+
# Delete User By Id
|
|
34
|
+
DELETE {{ baseURL }}/users/1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
process.env['NODE_CONFIG_DIR'] = __dirname + '/configs';
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import config from 'config';
|
|
2
2
|
import { NextFunction, Response } from 'express';
|
|
3
|
-
import
|
|
3
|
+
import { verify } from 'jsonwebtoken';
|
|
4
4
|
import { HttpException } from '@exceptions/HttpException';
|
|
5
5
|
import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';
|
|
6
6
|
import userModel from '@models/users.model';
|
|
7
7
|
|
|
8
8
|
const authMiddleware = async (req: RequestWithUser, res: Response, next: NextFunction) => {
|
|
9
9
|
try {
|
|
10
|
-
const Authorization = req.cookies['Authorization'] || req.header('Authorization').split('Bearer ')[1]
|
|
10
|
+
const Authorization = req.cookies['Authorization'] || (req.header('Authorization') ? req.header('Authorization').split('Bearer ')[1] : null);
|
|
11
11
|
|
|
12
12
|
if (Authorization) {
|
|
13
13
|
const secretKey: string = config.get('secretKey');
|
|
14
|
-
const verificationResponse = (await
|
|
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);
|
|
17
17
|
|
|
@@ -3,6 +3,13 @@ import { validate, ValidationError } from 'class-validator';
|
|
|
3
3
|
import { RequestHandler } from 'express';
|
|
4
4
|
import { HttpException } from '@exceptions/HttpException';
|
|
5
5
|
|
|
6
|
+
const getAllNestedErrors = (error: ValidationError) => {
|
|
7
|
+
if(error.constraints){
|
|
8
|
+
return Object.values(error.constraints)
|
|
9
|
+
}
|
|
10
|
+
return error.children.map(getAllNestedErrors).join(',')
|
|
11
|
+
}
|
|
12
|
+
|
|
6
13
|
export const validationMiddleware = (
|
|
7
14
|
type: any,
|
|
8
15
|
value: string | 'body' | 'query' | 'params' = 'body',
|
|
@@ -11,9 +18,10 @@ export const validationMiddleware = (
|
|
|
11
18
|
forbidNonWhitelisted = true,
|
|
12
19
|
): RequestHandler => {
|
|
13
20
|
return (req, res, next) => {
|
|
14
|
-
|
|
21
|
+
const obj = plainToClass(type, req[value]);
|
|
22
|
+
validate(obj, { skipMissingProperties, whitelist, forbidNonWhitelisted }).then((errors: ValidationError[]) => {
|
|
15
23
|
if (errors.length > 0) {
|
|
16
|
-
const message = errors.map(
|
|
24
|
+
const message = errors.map(getAllNestedErrors).join(', ');
|
|
17
25
|
next(new HttpException(400, message));
|
|
18
26
|
} else {
|
|
19
27
|
next();
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { User } from '@interfaces/users.interface';
|
|
2
2
|
|
|
3
|
-
// password:
|
|
3
|
+
// password: password
|
|
4
4
|
const userModel: User[] = [
|
|
5
|
-
{ id: 1, email: '
|
|
6
|
-
{ id: 2, email: '
|
|
7
|
-
{ id: 3, email: '
|
|
8
|
-
{ id: 4, email: '
|
|
5
|
+
{ id: 1, email: 'example1@email.com', password: '$2b$10$TBEfaCe1oo.2jfkBDWcj/usBj4oECsW2wOoDXpCa2IH9xqCpEK/hC' },
|
|
6
|
+
{ id: 2, email: 'example2@email.com', password: '$2b$10$TBEfaCe1oo.2jfkBDWcj/usBj4oECsW2wOoDXpCa2IH9xqCpEK/hC' },
|
|
7
|
+
{ id: 3, email: 'example3@email.com', password: '$2b$10$TBEfaCe1oo.2jfkBDWcj/usBj4oECsW2wOoDXpCa2IH9xqCpEK/hC' },
|
|
8
|
+
{ id: 4, email: 'example4@email.com', password: '$2b$10$TBEfaCe1oo.2jfkBDWcj/usBj4oECsW2wOoDXpCa2IH9xqCpEK/hC' },
|
|
9
9
|
];
|
|
10
10
|
|
|
11
11
|
export default userModel;
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
process.env['NODE_CONFIG_DIR'] = __dirname + '/configs';
|
|
2
|
-
|
|
3
1
|
import 'dotenv/config';
|
|
4
|
-
import '
|
|
2
|
+
import '@/index';
|
|
5
3
|
import App from '@/app';
|
|
6
4
|
import { AuthController } from '@controllers/auth.controller';
|
|
7
5
|
import { IndexController } from '@controllers/index.controller';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { compare, hash } from 'bcrypt';
|
|
2
2
|
import config from 'config';
|
|
3
|
-
import
|
|
3
|
+
import { sign } from 'jsonwebtoken';
|
|
4
4
|
import { CreateUserDto } from '@dtos/users.dto';
|
|
5
5
|
import { HttpException } from '@exceptions/HttpException';
|
|
6
6
|
import { DataStoredInToken, TokenData } from '@interfaces/auth.interface';
|
|
@@ -17,7 +17,7 @@ class AuthService {
|
|
|
17
17
|
const findUser: User = this.users.find(user => user.email === userData.email);
|
|
18
18
|
if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
19
19
|
|
|
20
|
-
const hashedPassword = await
|
|
20
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
21
21
|
const createUserData: User = { id: this.users.length + 1, ...userData, password: hashedPassword };
|
|
22
22
|
|
|
23
23
|
return createUserData;
|
|
@@ -29,7 +29,7 @@ class AuthService {
|
|
|
29
29
|
const findUser: User = this.users.find(user => user.email === userData.email);
|
|
30
30
|
if (!findUser) throw new HttpException(409, `You're email ${userData.email} not found`);
|
|
31
31
|
|
|
32
|
-
const isPasswordMatching: boolean = await
|
|
32
|
+
const isPasswordMatching: boolean = await compare(userData.password, findUser.password);
|
|
33
33
|
if (!isPasswordMatching) throw new HttpException(409, "You're password not matching");
|
|
34
34
|
|
|
35
35
|
const tokenData = this.createToken(findUser);
|
|
@@ -52,7 +52,7 @@ class AuthService {
|
|
|
52
52
|
const secretKey: string = config.get('secretKey');
|
|
53
53
|
const expiresIn: number = 60 * 60;
|
|
54
54
|
|
|
55
|
-
return { expiresIn, token:
|
|
55
|
+
return { expiresIn, token: sign(dataStoredInToken, secretKey, { expiresIn }) };
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
public createCookie(tokenData: TokenData): string {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { hash } from 'bcrypt';
|
|
2
2
|
import { CreateUserDto } from '@dtos/users.dto';
|
|
3
3
|
import { HttpException } from '@exceptions/HttpException';
|
|
4
4
|
import { User } from '@interfaces/users.interface';
|
|
@@ -26,7 +26,7 @@ class UserService {
|
|
|
26
26
|
const findUser: User = this.users.find(user => user.email === userData.email);
|
|
27
27
|
if (findUser) throw new HttpException(409, `Your email ${userData.email} already exists`);
|
|
28
28
|
|
|
29
|
-
const hashedPassword = await
|
|
29
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
30
30
|
const createUserData: User = { id: this.users.length + 1, ...userData, password: hashedPassword };
|
|
31
31
|
this.users = [...this.users, createUserData];
|
|
32
32
|
|
|
@@ -39,7 +39,7 @@ class UserService {
|
|
|
39
39
|
const findUser: User = this.users.find(user => user.id === userId);
|
|
40
40
|
if (!findUser) throw new HttpException(409, "You're not user");
|
|
41
41
|
|
|
42
|
-
const hashedPassword = await
|
|
42
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
43
43
|
const updateUserData: User[] = this.users.map((user: User) => {
|
|
44
44
|
if (user.id === findUser.id) user = { id: userId, ...userData, password: hashedPassword };
|
|
45
45
|
return user;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import config from 'config';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import { existsSync, mkdirSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
4
|
import winston from 'winston';
|
|
5
5
|
import winstonDaily from 'winston-daily-rotate-file';
|
|
6
6
|
|
|
7
7
|
// logs dir
|
|
8
|
-
const logDir: string =
|
|
8
|
+
const logDir: string = join(__dirname, config.get('log.dir'));
|
|
9
9
|
|
|
10
|
-
if (!
|
|
11
|
-
|
|
10
|
+
if (!existsSync(logDir)) {
|
|
11
|
+
mkdirSync(logDir);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
// Define log format
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"pretty": true,
|
|
14
14
|
"sourceMap": true,
|
|
15
15
|
"declaration": true,
|
|
16
|
-
"outDir": "
|
|
16
|
+
"outDir": "dist",
|
|
17
17
|
"allowJs": true,
|
|
18
18
|
"noEmit": false,
|
|
19
19
|
"esModuleInterop": true,
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"include": ["src/**/*.ts", "src/**/*.json", ".env"],
|
|
36
|
-
"exclude": ["node_modules"]
|
|
36
|
+
"exclude": ["node_modules", "src/http", "src/logs", "src/tests"]
|
|
37
37
|
}
|