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
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { plainToClass } from 'class-transformer';
|
|
2
|
+
import { validate, ValidationError } from 'class-validator';
|
|
3
|
+
import { RequestHandler } from 'express';
|
|
4
|
+
import { HttpException } from '@exceptions/HttpException';
|
|
5
|
+
|
|
6
|
+
const validationMiddleware = (
|
|
7
|
+
type: any,
|
|
8
|
+
value: string | 'body' | 'query' | 'params' = 'body',
|
|
9
|
+
skipMissingProperties = false,
|
|
10
|
+
whitelist = true,
|
|
11
|
+
forbidNonWhitelisted = true,
|
|
12
|
+
): RequestHandler => {
|
|
13
|
+
return (req, res, next) => {
|
|
14
|
+
validate(plainToClass(type, req[value]), { skipMissingProperties, whitelist, forbidNonWhitelisted }).then((errors: ValidationError[]) => {
|
|
15
|
+
if (errors.length > 0) {
|
|
16
|
+
const message = errors.map((error: ValidationError) => Object.values(error.constraints)).join(', ');
|
|
17
|
+
next(new HttpException(400, message));
|
|
18
|
+
} else {
|
|
19
|
+
next();
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default validationMiddleware;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { prop, getModelForClass, modelOptions } from '@typegoose/typegoose';
|
|
2
|
+
|
|
3
|
+
@modelOptions({ schemaOptions: { collection: 'users', timestamps: true } })
|
|
4
|
+
class User {
|
|
5
|
+
@prop({ type: String, required: true, unique: true })
|
|
6
|
+
public email: string;
|
|
7
|
+
|
|
8
|
+
@prop({ type: String, required: true })
|
|
9
|
+
public password: string;
|
|
10
|
+
|
|
11
|
+
public createdAt?: Date;
|
|
12
|
+
|
|
13
|
+
public updatedAt?: Date;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const UserModel = getModelForClass(User);
|
|
17
|
+
|
|
18
|
+
export default UserModel;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import AuthController from '@controllers/auth.controller';
|
|
3
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
4
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
5
|
+
import authMiddleware from '@middlewares/auth.middleware';
|
|
6
|
+
import validationMiddleware from '@middlewares/validation.middleware';
|
|
7
|
+
|
|
8
|
+
class AuthRoute implements Routes {
|
|
9
|
+
public path = '/';
|
|
10
|
+
public router = Router();
|
|
11
|
+
public authController = new AuthController();
|
|
12
|
+
|
|
13
|
+
constructor() {
|
|
14
|
+
this.initializeRoutes();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
private initializeRoutes() {
|
|
18
|
+
this.router.post(`${this.path}signup`, validationMiddleware(CreateUserDto, 'body'), this.authController.signUp);
|
|
19
|
+
this.router.post(`${this.path}login`, validationMiddleware(CreateUserDto, 'body'), this.authController.logIn);
|
|
20
|
+
this.router.post(`${this.path}logout`, authMiddleware, this.authController.logOut);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default AuthRoute;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import IndexController from '@controllers/index.controller';
|
|
3
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
4
|
+
|
|
5
|
+
class IndexRoute implements Routes {
|
|
6
|
+
public path = '/';
|
|
7
|
+
public router = Router();
|
|
8
|
+
public indexController = new IndexController();
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
this.initializeRoutes();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
private initializeRoutes() {
|
|
15
|
+
this.router.get(`${this.path}`, this.indexController.index);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default IndexRoute;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import UsersController from '@controllers/users.controller';
|
|
3
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
4
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
5
|
+
import validationMiddleware from '@middlewares/validation.middleware';
|
|
6
|
+
|
|
7
|
+
class UsersRoute implements Routes {
|
|
8
|
+
public path = '/users';
|
|
9
|
+
public router = Router();
|
|
10
|
+
public usersController = new UsersController();
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.initializeRoutes();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private initializeRoutes() {
|
|
17
|
+
this.router.get(`${this.path}`, this.usersController.getUsers);
|
|
18
|
+
this.router.get(`${this.path}/:id`, this.usersController.getUserById);
|
|
19
|
+
this.router.post(`${this.path}`, validationMiddleware(CreateUserDto, 'body'), this.usersController.createUser);
|
|
20
|
+
this.router.put(`${this.path}/:id`, validationMiddleware(CreateUserDto, 'body', true), this.usersController.updateUser);
|
|
21
|
+
this.router.delete(`${this.path}/:id`, this.usersController.deleteUser);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default UsersRoute;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import App from '@/app';
|
|
2
|
+
import AuthRoute from '@routes/auth.route';
|
|
3
|
+
import IndexRoute from '@routes/index.route';
|
|
4
|
+
import UsersRoute from '@routes/users.route';
|
|
5
|
+
import validateEnv from '@utils/validateEnv';
|
|
6
|
+
|
|
7
|
+
validateEnv();
|
|
8
|
+
|
|
9
|
+
const app = new App([new IndexRoute(), new UsersRoute(), new AuthRoute()]);
|
|
10
|
+
|
|
11
|
+
app.listen();
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { hash, compare } from 'bcrypt';
|
|
2
|
+
import { sign } from 'jsonwebtoken';
|
|
3
|
+
import { SECRET_KEY } from '@config';
|
|
4
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
5
|
+
import { HttpException } from '@exceptions/HttpException';
|
|
6
|
+
import { DataStoredInToken, TokenData } from '@interfaces/auth.interface';
|
|
7
|
+
import { User } from '@interfaces/users.interface';
|
|
8
|
+
import userModel from '@models/users.model';
|
|
9
|
+
import { isEmpty } from '@utils/util';
|
|
10
|
+
|
|
11
|
+
class AuthService {
|
|
12
|
+
public async signup(userData: CreateUserDto): Promise<User> {
|
|
13
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
14
|
+
|
|
15
|
+
const findUser: User = await userModel.findOne({ email: userData.email });
|
|
16
|
+
if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
17
|
+
|
|
18
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
19
|
+
const createUserData: User = await userModel.create({ ...userData, password: hashedPassword });
|
|
20
|
+
|
|
21
|
+
return createUserData;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async login(userData: CreateUserDto): Promise<{ cookie: string; findUser: User }> {
|
|
25
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
26
|
+
|
|
27
|
+
const findUser: User = await userModel.findOne({ email: userData.email });
|
|
28
|
+
if (!findUser) throw new HttpException(409, `You're email ${userData.email} not found`);
|
|
29
|
+
|
|
30
|
+
const isPasswordMatching: boolean = await compare(userData.password, findUser.password);
|
|
31
|
+
if (!isPasswordMatching) throw new HttpException(409, "You're password not matching");
|
|
32
|
+
|
|
33
|
+
const tokenData = this.createToken(findUser);
|
|
34
|
+
const cookie = this.createCookie(tokenData);
|
|
35
|
+
|
|
36
|
+
return { cookie, findUser };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public async logout(userData: User): Promise<User> {
|
|
40
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
41
|
+
|
|
42
|
+
const findUser: User = await userModel.findOne({ email: userData.email, password: userData.password });
|
|
43
|
+
if (!findUser) throw new HttpException(409, `You're email ${userData.email} not found`);
|
|
44
|
+
|
|
45
|
+
return findUser;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public createToken(user: User): TokenData {
|
|
49
|
+
const dataStoredInToken: DataStoredInToken = { _id: user._id };
|
|
50
|
+
const secretKey: string = SECRET_KEY;
|
|
51
|
+
const expiresIn: number = 60 * 60;
|
|
52
|
+
|
|
53
|
+
return { expiresIn, token: sign(dataStoredInToken, secretKey, { expiresIn }) };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public createCookie(tokenData: TokenData): string {
|
|
57
|
+
return `Authorization=${tokenData.token}; HttpOnly; Max-Age=${tokenData.expiresIn};`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default AuthService;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { hash } from 'bcrypt';
|
|
2
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
3
|
+
import { HttpException } from '@exceptions/HttpException';
|
|
4
|
+
import { User } from '@interfaces/users.interface';
|
|
5
|
+
import userModel from '@models/users.model';
|
|
6
|
+
import { isEmpty } from '@utils/util';
|
|
7
|
+
|
|
8
|
+
class UserService {
|
|
9
|
+
// public users = userModel;
|
|
10
|
+
|
|
11
|
+
public async findAllUser(): Promise<User[]> {
|
|
12
|
+
const users: User[] = await userModel.find();
|
|
13
|
+
return users;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public async findUserById(userId: string): Promise<User> {
|
|
17
|
+
if (isEmpty(userId)) throw new HttpException(400, "You're not userId");
|
|
18
|
+
|
|
19
|
+
const findUser: User = await userModel.findOne({ _id: userId });
|
|
20
|
+
if (!findUser) throw new HttpException(409, "You're not user");
|
|
21
|
+
|
|
22
|
+
return findUser;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public async createUser(userData: CreateUserDto): Promise<User> {
|
|
26
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
27
|
+
|
|
28
|
+
const findUser: User = await userModel.findOne({ email: userData.email });
|
|
29
|
+
if (findUser) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
30
|
+
|
|
31
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
32
|
+
const createUserData: User = await userModel.create({ ...userData, password: hashedPassword });
|
|
33
|
+
|
|
34
|
+
return createUserData;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public async updateUser(userId: string, userData: CreateUserDto): Promise<User> {
|
|
38
|
+
if (isEmpty(userData)) throw new HttpException(400, "You're not userData");
|
|
39
|
+
|
|
40
|
+
if (userData.email) {
|
|
41
|
+
const findUser: User = await userModel.findOne({ email: userData.email });
|
|
42
|
+
if (findUser && findUser._id != userId) throw new HttpException(409, `You're email ${userData.email} already exists`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (userData.password) {
|
|
46
|
+
const hashedPassword = await hash(userData.password, 10);
|
|
47
|
+
userData = { ...userData, password: hashedPassword };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const updateUserById: User = await userModel.findByIdAndUpdate(userId, { userData });
|
|
51
|
+
if (!updateUserById) throw new HttpException(409, "You're not user");
|
|
52
|
+
|
|
53
|
+
return updateUserById;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public async deleteUser(userId: string): Promise<User> {
|
|
57
|
+
const deleteUserById: User = await userModel.findByIdAndDelete(userId);
|
|
58
|
+
if (!deleteUserById) throw new HttpException(409, "You're not user");
|
|
59
|
+
|
|
60
|
+
return deleteUserById;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export default UserService;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import bcrypt from 'bcrypt';
|
|
2
|
+
import mongoose from 'mongoose';
|
|
3
|
+
import request from 'supertest';
|
|
4
|
+
import App from '@/app';
|
|
5
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
6
|
+
import AuthRoute from '@routes/auth.route';
|
|
7
|
+
|
|
8
|
+
afterAll(async () => {
|
|
9
|
+
await new Promise<void>(resolve => setTimeout(() => resolve(), 500));
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('Testing Auth', () => {
|
|
13
|
+
describe('[POST] /signup', () => {
|
|
14
|
+
it('response should have the Create userData', async () => {
|
|
15
|
+
const userData: CreateUserDto = {
|
|
16
|
+
email: 'test@email.com',
|
|
17
|
+
password: 'q1w2e3r4!',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const authRoute = new AuthRoute();
|
|
21
|
+
const users = authRoute.authController.authService.users;
|
|
22
|
+
|
|
23
|
+
users.findOne = jest.fn().mockReturnValue(null);
|
|
24
|
+
users.create = jest.fn().mockReturnValue({
|
|
25
|
+
_id: '60706478aad6c9ad19a31c84',
|
|
26
|
+
email: userData.email,
|
|
27
|
+
password: await bcrypt.hash(userData.password, 10),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
(mongoose as any).connect = jest.fn();
|
|
31
|
+
const app = new App([authRoute]);
|
|
32
|
+
return request(app.getServer()).post(`${authRoute.path}signup`).send(userData);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('[POST] /login', () => {
|
|
37
|
+
it('response should have the Set-Cookie header with the Authorization token', async () => {
|
|
38
|
+
const userData: CreateUserDto = {
|
|
39
|
+
email: 'test@email.com',
|
|
40
|
+
password: 'q1w2e3r4!',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const authRoute = new AuthRoute();
|
|
44
|
+
const users = authRoute.authController.authService.users;
|
|
45
|
+
|
|
46
|
+
users.findOne = jest.fn().mockReturnValue({
|
|
47
|
+
_id: '60706478aad6c9ad19a31c84',
|
|
48
|
+
email: userData.email,
|
|
49
|
+
password: await bcrypt.hash(userData.password, 10),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
(mongoose as any).connect = jest.fn();
|
|
53
|
+
const app = new App([authRoute]);
|
|
54
|
+
return request(app.getServer())
|
|
55
|
+
.post(`${authRoute.path}login`)
|
|
56
|
+
.send(userData)
|
|
57
|
+
.expect('Set-Cookie', /^Authorization=.+/);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// describe('[POST] /logout', () => {
|
|
62
|
+
// it('logout Set-Cookie Authorization=; Max-age=0', async () => {
|
|
63
|
+
// const userData: User = {
|
|
64
|
+
// _id: '60706478aad6c9ad19a31c84',
|
|
65
|
+
// email: 'test@email.com',
|
|
66
|
+
// password: await bcrypt.hash('q1w2e3r4!', 10),
|
|
67
|
+
// };
|
|
68
|
+
|
|
69
|
+
// const authRoute = new AuthRoute();
|
|
70
|
+
// const users = authRoute.authController.authService.users;
|
|
71
|
+
|
|
72
|
+
// users.findOne = jest.fn().mockReturnValue(userData);
|
|
73
|
+
|
|
74
|
+
// (mongoose as any).connect = jest.fn();
|
|
75
|
+
// const app = new App([authRoute]);
|
|
76
|
+
// return request(app.getServer())
|
|
77
|
+
// .post(`${authRoute.path}logout`)
|
|
78
|
+
// .send(userData)
|
|
79
|
+
// .set('Set-Cookie', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ')
|
|
80
|
+
// .expect('Set-Cookie', /^Authorization=\; Max-age=0/);
|
|
81
|
+
// });
|
|
82
|
+
// });
|
|
83
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import request from 'supertest';
|
|
2
|
+
import App from '@/app';
|
|
3
|
+
import IndexRoute from '@routes/index.route';
|
|
4
|
+
|
|
5
|
+
afterAll(async () => {
|
|
6
|
+
await new Promise<void>(resolve => setTimeout(() => resolve(), 500));
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe('Testing Index', () => {
|
|
10
|
+
describe('[GET] /', () => {
|
|
11
|
+
it('response statusCode 200', () => {
|
|
12
|
+
const indexRoute = new IndexRoute();
|
|
13
|
+
const app = new App([indexRoute]);
|
|
14
|
+
|
|
15
|
+
return request(app.getServer()).get(`${indexRoute.path}`).expect(200);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import bcrypt from 'bcrypt';
|
|
2
|
+
import mongoose from 'mongoose';
|
|
3
|
+
import request from 'supertest';
|
|
4
|
+
import App from '@/app';
|
|
5
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
6
|
+
import UsersRoute from '@routes/users.route';
|
|
7
|
+
|
|
8
|
+
afterAll(async () => {
|
|
9
|
+
await new Promise<void>(resolve => setTimeout(() => resolve(), 500));
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('Testing Users', () => {
|
|
13
|
+
describe('[GET] /users', () => {
|
|
14
|
+
it('response fineAll Users', async () => {
|
|
15
|
+
const usersRoute = new UsersRoute();
|
|
16
|
+
const users = usersRoute.usersController.userService.users;
|
|
17
|
+
|
|
18
|
+
users.find = jest.fn().mockReturnValue([
|
|
19
|
+
{
|
|
20
|
+
_id: 'qpwoeiruty',
|
|
21
|
+
email: 'a@email.com',
|
|
22
|
+
password: await bcrypt.hash('q1w2e3r4!', 10),
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
_id: 'alskdjfhg',
|
|
26
|
+
email: 'b@email.com',
|
|
27
|
+
password: await bcrypt.hash('a1s2d3f4!', 10),
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
_id: 'zmxncbv',
|
|
31
|
+
email: 'c@email.com',
|
|
32
|
+
password: await bcrypt.hash('z1x2c3v4!', 10),
|
|
33
|
+
},
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
(mongoose as any).connect = jest.fn();
|
|
37
|
+
const app = new App([usersRoute]);
|
|
38
|
+
return request(app.getServer()).get(`${usersRoute.path}`).expect(200);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('[GET] /users/:id', () => {
|
|
43
|
+
it('response findOne User', async () => {
|
|
44
|
+
const userId = 'qpwoeiruty';
|
|
45
|
+
|
|
46
|
+
const usersRoute = new UsersRoute();
|
|
47
|
+
const users = usersRoute.usersController.userService.users;
|
|
48
|
+
|
|
49
|
+
users.findOne = jest.fn().mockReturnValue({
|
|
50
|
+
_id: 'qpwoeiruty',
|
|
51
|
+
email: 'a@email.com',
|
|
52
|
+
password: await bcrypt.hash('q1w2e3r4!', 10),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
(mongoose as any).connect = jest.fn();
|
|
56
|
+
const app = new App([usersRoute]);
|
|
57
|
+
return request(app.getServer()).get(`${usersRoute.path}/${userId}`).expect(200);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('[POST] /users', () => {
|
|
62
|
+
it('response Create User', async () => {
|
|
63
|
+
const userData: CreateUserDto = {
|
|
64
|
+
email: 'test@email.com',
|
|
65
|
+
password: 'q1w2e3r4',
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const usersRoute = new UsersRoute();
|
|
69
|
+
const users = usersRoute.usersController.userService.users;
|
|
70
|
+
|
|
71
|
+
users.findOne = jest.fn().mockReturnValue(null);
|
|
72
|
+
users.create = jest.fn().mockReturnValue({
|
|
73
|
+
_id: '60706478aad6c9ad19a31c84',
|
|
74
|
+
email: userData.email,
|
|
75
|
+
password: await bcrypt.hash(userData.password, 10),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
(mongoose as any).connect = jest.fn();
|
|
79
|
+
const app = new App([usersRoute]);
|
|
80
|
+
return request(app.getServer()).post(`${usersRoute.path}`).send(userData).expect(201);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('[PUT] /users/:id', () => {
|
|
85
|
+
it('response Update User', async () => {
|
|
86
|
+
const userId = '60706478aad6c9ad19a31c84';
|
|
87
|
+
const userData: CreateUserDto = {
|
|
88
|
+
email: 'test@email.com',
|
|
89
|
+
password: 'q1w2e3r4',
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const usersRoute = new UsersRoute();
|
|
93
|
+
const users = usersRoute.usersController.userService.users;
|
|
94
|
+
|
|
95
|
+
if (userData.email) {
|
|
96
|
+
users.findOne = jest.fn().mockReturnValue({
|
|
97
|
+
_id: userId,
|
|
98
|
+
email: userData.email,
|
|
99
|
+
password: await bcrypt.hash(userData.password, 10),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
users.findByIdAndUpdate = jest.fn().mockReturnValue({
|
|
104
|
+
_id: userId,
|
|
105
|
+
email: userData.email,
|
|
106
|
+
password: await bcrypt.hash(userData.password, 10),
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
(mongoose as any).connect = jest.fn();
|
|
110
|
+
const app = new App([usersRoute]);
|
|
111
|
+
return request(app.getServer()).put(`${usersRoute.path}/${userId}`).send(userData);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('[DELETE] /users/:id', () => {
|
|
116
|
+
it('response Delete User', async () => {
|
|
117
|
+
const userId = '60706478aad6c9ad19a31c84';
|
|
118
|
+
|
|
119
|
+
const usersRoute = new UsersRoute();
|
|
120
|
+
const users = usersRoute.usersController.userService.users;
|
|
121
|
+
|
|
122
|
+
users.findByIdAndDelete = jest.fn().mockReturnValue({
|
|
123
|
+
_id: '60706478aad6c9ad19a31c84',
|
|
124
|
+
email: 'test@email.com',
|
|
125
|
+
password: await bcrypt.hash('q1w2e3r4!', 10),
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
(mongoose as any).connect = jest.fn();
|
|
129
|
+
const app = new App([usersRoute]);
|
|
130
|
+
return request(app.getServer()).delete(`${usersRoute.path}/${userId}`).expect(200);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { existsSync, mkdirSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import winston from 'winston';
|
|
4
|
+
import winstonDaily from 'winston-daily-rotate-file';
|
|
5
|
+
import { LOG_DIR } from '@config';
|
|
6
|
+
|
|
7
|
+
// logs dir
|
|
8
|
+
const logDir: string = join(__dirname, LOG_DIR);
|
|
9
|
+
|
|
10
|
+
if (!existsSync(logDir)) {
|
|
11
|
+
mkdirSync(logDir);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Define log format
|
|
15
|
+
const logFormat = winston.format.printf(({ timestamp, level, message }) => `${timestamp} ${level}: ${message}`);
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
* Log Level
|
|
19
|
+
* error: 0, warn: 1, info: 2, http: 3, verbose: 4, debug: 5, silly: 6
|
|
20
|
+
*/
|
|
21
|
+
const logger = winston.createLogger({
|
|
22
|
+
format: winston.format.combine(
|
|
23
|
+
winston.format.timestamp({
|
|
24
|
+
format: 'YYYY-MM-DD HH:mm:ss',
|
|
25
|
+
}),
|
|
26
|
+
logFormat,
|
|
27
|
+
),
|
|
28
|
+
transports: [
|
|
29
|
+
// debug log setting
|
|
30
|
+
new winstonDaily({
|
|
31
|
+
level: 'debug',
|
|
32
|
+
datePattern: 'YYYY-MM-DD',
|
|
33
|
+
dirname: logDir + '/debug', // log file /logs/debug/*.log in save
|
|
34
|
+
filename: `%DATE%.log`,
|
|
35
|
+
maxFiles: 30, // 30 Days saved
|
|
36
|
+
json: false,
|
|
37
|
+
zippedArchive: true,
|
|
38
|
+
}),
|
|
39
|
+
// error log setting
|
|
40
|
+
new winstonDaily({
|
|
41
|
+
level: 'error',
|
|
42
|
+
datePattern: 'YYYY-MM-DD',
|
|
43
|
+
dirname: logDir + '/error', // log file /logs/error/*.log in save
|
|
44
|
+
filename: `%DATE%.log`,
|
|
45
|
+
maxFiles: 30, // 30 Days saved
|
|
46
|
+
handleExceptions: true,
|
|
47
|
+
json: false,
|
|
48
|
+
zippedArchive: true,
|
|
49
|
+
}),
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
logger.add(
|
|
54
|
+
new winston.transports.Console({
|
|
55
|
+
format: winston.format.combine(winston.format.splat(), winston.format.colorize()),
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const stream = {
|
|
60
|
+
write: (message: string) => {
|
|
61
|
+
logger.info(message.substring(0, message.lastIndexOf('\n')));
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export { logger, stream };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @method isEmpty
|
|
3
|
+
* @param {String | Number | Object} value
|
|
4
|
+
* @returns {Boolean} true & false
|
|
5
|
+
* @description this value is Empty Check
|
|
6
|
+
*/
|
|
7
|
+
export const isEmpty = (value: string | number | object): boolean => {
|
|
8
|
+
if (value === null) {
|
|
9
|
+
return true;
|
|
10
|
+
} else if (typeof value !== 'number' && value === '') {
|
|
11
|
+
return true;
|
|
12
|
+
} else if (typeof value === 'undefined' || value === undefined) {
|
|
13
|
+
return true;
|
|
14
|
+
} else if (value !== null && typeof value === 'object' && !Object.keys(value).length) {
|
|
15
|
+
return true;
|
|
16
|
+
} else {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
};
|