@a.m.ticketing/common 1.0.2 → 1.0.3
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/build/errors/BadRequestError.d.ts +9 -0
- package/build/errors/BadRequestError.js +12 -0
- package/build/errors/DatabaseConnectionError.d.ts +9 -0
- package/build/errors/DatabaseConnectionError.js +11 -0
- package/build/errors/RequestValidationError.d.ts +14 -0
- package/build/errors/RequestValidationError.js +17 -0
- package/build/errors/customError.d.ts +8 -0
- package/build/errors/customError.js +5 -0
- package/build/errors/not-authorized-error.d.ts +8 -0
- package/build/errors/not-authorized-error.js +10 -0
- package/build/errors/not-found-error.d.ts +8 -0
- package/build/errors/not-found-error.js +10 -0
- package/build/index.d.ts +10 -1
- package/build/index.js +10 -6
- package/build/middlewares/current-user.d.ts +17 -0
- package/build/middlewares/current-user.js +12 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +8 -0
- package/build/middlewares/require-auth.d.ts +2 -0
- package/build/middlewares/require-auth.js +7 -0
- package/build/middlewares/validate-request.d.ts +2 -0
- package/build/middlewares/validate-request.js +9 -0
- package/package.json +11 -2
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CustomError } from "./customError.js";
|
|
2
|
+
export class BadRequestError extends CustomError {
|
|
3
|
+
message;
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.message = message;
|
|
8
|
+
}
|
|
9
|
+
serializeErrors() {
|
|
10
|
+
return [{ message: this.message }];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CustomError } from "./customError.js";
|
|
2
|
+
export class DatabaseConnectionError extends CustomError {
|
|
3
|
+
statusCode = 500;
|
|
4
|
+
reason = "database connection error";
|
|
5
|
+
constructor() {
|
|
6
|
+
super('error connection with DB');
|
|
7
|
+
}
|
|
8
|
+
serializeErrors() {
|
|
9
|
+
return [{ message: this.reason }];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CustomError } from "./customError.js";
|
|
2
|
+
import type { ValidationError } from "express-validator";
|
|
3
|
+
export declare class RequestValidationError extends CustomError {
|
|
4
|
+
errors: ValidationError[];
|
|
5
|
+
statusCode: number;
|
|
6
|
+
constructor(errors: ValidationError[]);
|
|
7
|
+
serializeErrors(): ({
|
|
8
|
+
message: any;
|
|
9
|
+
field: string;
|
|
10
|
+
} | {
|
|
11
|
+
field?: never;
|
|
12
|
+
message: any;
|
|
13
|
+
})[];
|
|
14
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CustomError } from "./customError.js";
|
|
2
|
+
export class RequestValidationError extends CustomError {
|
|
3
|
+
errors;
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
constructor(errors) {
|
|
6
|
+
super("invalid error");
|
|
7
|
+
this.errors = errors;
|
|
8
|
+
}
|
|
9
|
+
serializeErrors() {
|
|
10
|
+
return this.errors.map(error => {
|
|
11
|
+
if (error.type === "field") {
|
|
12
|
+
return { message: error.msg, field: error.path };
|
|
13
|
+
}
|
|
14
|
+
return { message: error.msg };
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
package/build/index.d.ts
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from "./errors/BadRequestError.js";
|
|
2
|
+
export * from "./errors/DatabaseConnectionError.js";
|
|
3
|
+
export * from "./errors/RequestValidationError.js";
|
|
4
|
+
export * from "./errors/customError.js";
|
|
5
|
+
export * from "./errors/not-authorized-error.js";
|
|
6
|
+
export * from "./errors/not-found-error.js";
|
|
7
|
+
export * from "./middlewares/current-user.js";
|
|
8
|
+
export * from "./middlewares/error-handler.js";
|
|
9
|
+
export * from "./middlewares/validate-request.js";
|
|
10
|
+
export * from "./middlewares/require-auth.js";
|
package/build/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export * from "./errors/BadRequestError.js";
|
|
2
|
+
export * from "./errors/DatabaseConnectionError.js";
|
|
3
|
+
export * from "./errors/RequestValidationError.js";
|
|
4
|
+
export * from "./errors/customError.js";
|
|
5
|
+
export * from "./errors/not-authorized-error.js";
|
|
6
|
+
export * from "./errors/not-found-error.js";
|
|
7
|
+
export * from "./middlewares/current-user.js";
|
|
8
|
+
export * from "./middlewares/error-handler.js";
|
|
9
|
+
export * from "./middlewares/validate-request.js";
|
|
10
|
+
export * from "./middlewares/require-auth.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
+
interface UserPayload {
|
|
3
|
+
id: string;
|
|
4
|
+
email: string;
|
|
5
|
+
}
|
|
6
|
+
declare global {
|
|
7
|
+
namespace Express {
|
|
8
|
+
interface Request {
|
|
9
|
+
session?: {
|
|
10
|
+
jwt?: string;
|
|
11
|
+
};
|
|
12
|
+
currentUser?: UserPayload;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import jwt from 'jsonwebtoken';
|
|
2
|
+
export const currentUser = (req, res, next) => {
|
|
3
|
+
if (!req.session?.jwt) {
|
|
4
|
+
return next();
|
|
5
|
+
}
|
|
6
|
+
try {
|
|
7
|
+
const payload = jwt.verify(req.session.jwt, process.env.JWT_KEY);
|
|
8
|
+
req.currentUser = payload;
|
|
9
|
+
}
|
|
10
|
+
catch (err) { }
|
|
11
|
+
next();
|
|
12
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { validationResult } from 'express-validator';
|
|
2
|
+
import { RequestValidationError } from '../errors/RequestValidationError.js';
|
|
3
|
+
export const validateRequest = (req, res, next) => {
|
|
4
|
+
const errors = validationResult(req);
|
|
5
|
+
if (!errors.isEmpty()) {
|
|
6
|
+
throw new RequestValidationError(errors.array());
|
|
7
|
+
}
|
|
8
|
+
next();
|
|
9
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a.m.ticketing/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -15,9 +15,18 @@
|
|
|
15
15
|
"keywords": [],
|
|
16
16
|
"author": "",
|
|
17
17
|
"license": "ISC",
|
|
18
|
-
"type": "
|
|
18
|
+
"type": "module",
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"del-cli": "^8.0.1",
|
|
21
21
|
"typescript": "^7.0.2"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@types/cookie-session": "^2.0.49",
|
|
25
|
+
"@types/express": "^5.0.6",
|
|
26
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
27
|
+
"cookie-session": "^2.1.1",
|
|
28
|
+
"express": "^5.3.0",
|
|
29
|
+
"express-validator": "^7.3.2",
|
|
30
|
+
"jsonwebtoken": "^9.0.3"
|
|
22
31
|
}
|
|
23
32
|
}
|