Spaces:
Running
Running
Commit
·
f39e411
1
Parent(s):
96594d4
feat: add async handler
Browse files
src/helpers/async-handler.ts
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export function asyncHandler(fn) {
|
2 |
+
return async function (req, res, next) {
|
3 |
+
try {
|
4 |
+
await fn(req, res, next);
|
5 |
+
} catch (err) {
|
6 |
+
next(err);
|
7 |
+
}
|
8 |
+
};
|
9 |
+
}
|
src/lib/env/env.ts
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import dotenv from "dotenv";
|
2 |
+
dotenv.config();
|
3 |
+
|
4 |
+
export class EnvValue {
|
5 |
+
constructor(public value: string | number | boolean) {}
|
6 |
+
|
7 |
+
toString(): string {
|
8 |
+
return String(this.value);
|
9 |
+
}
|
10 |
+
toNumber(): number {
|
11 |
+
return Number(this.value);
|
12 |
+
}
|
13 |
+
toBoolean(): boolean {
|
14 |
+
return this.value === "true";
|
15 |
+
}
|
16 |
+
}
|
17 |
+
|
18 |
+
export class Env {
|
19 |
+
static get(key: string, defaultValue?: string | number | boolean): EnvValue {
|
20 |
+
const value = process.env[key] || defaultValue;
|
21 |
+
|
22 |
+
if (!value) {
|
23 |
+
throw new Error(`Environment variable ${key} not found`);
|
24 |
+
}
|
25 |
+
|
26 |
+
return new EnvValue(value);
|
27 |
+
}
|
28 |
+
}
|
src/lib/responses/json-response.ts
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export class JsonResponse {
|
2 |
+
public status: number;
|
3 |
+
public message: string;
|
4 |
+
public data: Record<string, any> | Record<string, any>[];
|
5 |
+
public meta?: {
|
6 |
+
total: number;
|
7 |
+
page: number;
|
8 |
+
perPage: number;
|
9 |
+
};
|
10 |
+
|
11 |
+
constructor(props: {
|
12 |
+
status?: number;
|
13 |
+
message?: string;
|
14 |
+
data?: Record<string, any> | Record<string, any>[];
|
15 |
+
meta?: {
|
16 |
+
total: number;
|
17 |
+
page: number;
|
18 |
+
perPage: number;
|
19 |
+
};
|
20 |
+
}) {
|
21 |
+
this.status = props.status || 200;
|
22 |
+
this.message = props.message || "Success";
|
23 |
+
this.data = props.data || {};
|
24 |
+
this.meta = props.meta;
|
25 |
+
}
|
26 |
+
}
|
src/modules/console/admins/controllers/admins.controller.ts
CHANGED
@@ -7,22 +7,31 @@ import {
|
|
7 |
bodyValidator,
|
8 |
paramsValidator,
|
9 |
} from "../../../../helpers/validation.helper";
|
|
|
10 |
|
11 |
@Prefix("/console/admins")
|
12 |
export class AdminsController extends BaseController {
|
13 |
private adminsService = new AdminsService();
|
14 |
|
15 |
setRoutes() {
|
16 |
-
this.router.get("/", this.list);
|
17 |
-
this.router.get("/:id", paramsValidator("id"), this.get);
|
18 |
-
this.router.post(
|
|
|
|
|
|
|
|
|
19 |
this.router.patch(
|
20 |
"/:id",
|
21 |
paramsValidator("id"),
|
22 |
bodyValidator(createAdminSchema),
|
23 |
-
this.update
|
|
|
|
|
|
|
|
|
|
|
24 |
);
|
25 |
-
this.router.delete("/:id", paramsValidator("id"), this.delete);
|
26 |
}
|
27 |
|
28 |
list = (_, res: Response) => {
|
|
|
7 |
bodyValidator,
|
8 |
paramsValidator,
|
9 |
} from "../../../../helpers/validation.helper";
|
10 |
+
import { asyncHandler } from "../../../../helpers/async-handler";
|
11 |
|
12 |
@Prefix("/console/admins")
|
13 |
export class AdminsController extends BaseController {
|
14 |
private adminsService = new AdminsService();
|
15 |
|
16 |
setRoutes() {
|
17 |
+
this.router.get("/", asyncHandler(this.list));
|
18 |
+
this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
|
19 |
+
this.router.post(
|
20 |
+
"/",
|
21 |
+
bodyValidator(createAdminSchema),
|
22 |
+
asyncHandler(this.create)
|
23 |
+
);
|
24 |
this.router.patch(
|
25 |
"/:id",
|
26 |
paramsValidator("id"),
|
27 |
bodyValidator(createAdminSchema),
|
28 |
+
asyncHandler(this.update)
|
29 |
+
);
|
30 |
+
this.router.delete(
|
31 |
+
"/:id",
|
32 |
+
paramsValidator("id"),
|
33 |
+
asyncHandler(this.delete)
|
34 |
);
|
|
|
35 |
}
|
36 |
|
37 |
list = (_, res: Response) => {
|
src/modules/console/users/controllers/users.controller.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import { jwtHelper } from "../../../../helpers/jwt.helper";
|
2 |
import { bodyValidator } from "../../../../helpers/validation.helper";
|
3 |
import { BaseController } from "../../../../lib/controllers/controller.base";
|
@@ -16,7 +17,7 @@ export class AdminUsersController extends BaseController {
|
|
16 |
"/create",
|
17 |
jwtHelper.verifyToken(allowedRoles),
|
18 |
bodyValidator(userRegisterValidation),
|
19 |
-
this.create
|
20 |
);
|
21 |
}
|
22 |
|
|
|
1 |
+
import { asyncHandler } from "../../../../helpers/async-handler";
|
2 |
import { jwtHelper } from "../../../../helpers/jwt.helper";
|
3 |
import { bodyValidator } from "../../../../helpers/validation.helper";
|
4 |
import { BaseController } from "../../../../lib/controllers/controller.base";
|
|
|
17 |
"/create",
|
18 |
jwtHelper.verifyToken(allowedRoles),
|
19 |
bodyValidator(userRegisterValidation),
|
20 |
+
asyncHandler(this.create)
|
21 |
);
|
22 |
}
|
23 |
|
src/modules/user/auth/controllers/auth.controller.ts
CHANGED
@@ -5,6 +5,7 @@ import { Prefix } from "../../../common/decorators/prefix.decorator";
|
|
5 |
import { bodyValidator } from "../../../../helpers/validation.helper";
|
6 |
import { userRegisterValidation } from "../../../common/users/validation/user-register.validation";
|
7 |
import { loginValidation } from "../validation/user.Validation";
|
|
|
8 |
|
9 |
@Prefix("/user/auth")
|
10 |
export class AuthController extends BaseController {
|
@@ -14,9 +15,13 @@ export class AuthController extends BaseController {
|
|
14 |
this.router.post(
|
15 |
"/register",
|
16 |
bodyValidator(userRegisterValidation),
|
17 |
-
this.register
|
|
|
|
|
|
|
|
|
|
|
18 |
);
|
19 |
-
this.router.post("/login", bodyValidator(loginValidation), this.login);
|
20 |
}
|
21 |
|
22 |
register = async (req, res) => {
|
|
|
5 |
import { bodyValidator } from "../../../../helpers/validation.helper";
|
6 |
import { userRegisterValidation } from "../../../common/users/validation/user-register.validation";
|
7 |
import { loginValidation } from "../validation/user.Validation";
|
8 |
+
import { asyncHandler } from "../../../../helpers/async-handler";
|
9 |
|
10 |
@Prefix("/user/auth")
|
11 |
export class AuthController extends BaseController {
|
|
|
15 |
this.router.post(
|
16 |
"/register",
|
17 |
bodyValidator(userRegisterValidation),
|
18 |
+
asyncHandler(this.register)
|
19 |
+
);
|
20 |
+
this.router.post(
|
21 |
+
"/login",
|
22 |
+
bodyValidator(loginValidation),
|
23 |
+
asyncHandler(this.login)
|
24 |
);
|
|
|
25 |
}
|
26 |
|
27 |
register = async (req, res) => {
|