@raymanselltickets/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/bad-request-error.d.ts +9 -0
- package/build/errors/bad-request-error.js +18 -0
- package/build/errors/custom-error.d.ts +8 -0
- package/build/errors/custom-error.js +7 -0
- package/build/errors/database-connection-error.d.ts +9 -0
- package/build/errors/database-connection-error.js +17 -0
- package/build/errors/not-authorized-error.d.ts +9 -0
- package/build/errors/not-authorized-error.js +16 -0
- package/build/errors/not-found-error.d.ts +8 -0
- package/build/errors/not-found-error.js +16 -0
- package/build/errors/request-validation-error.d.ts +14 -0
- package/build/errors/request-validation-error.js +24 -0
- package/build/index.d.ts +9 -7
- package/build/index.js +9 -10
- package/build/middlewares/current-user.d.ts +14 -0
- package/build/middlewares/current-user.js +12 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +13 -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 +10 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CustomError } from './custom-error.js';
|
|
2
|
+
export class BadRequestError extends CustomError {
|
|
3
|
+
message;
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.message = message;
|
|
8
|
+
// only because we are extending a built in class
|
|
9
|
+
// Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serializeErrors() {
|
|
12
|
+
return [
|
|
13
|
+
{
|
|
14
|
+
message: this.message,
|
|
15
|
+
},
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CustomError } from './custom-error.js';
|
|
2
|
+
export class DatabaseConnectionError extends CustomError {
|
|
3
|
+
statusCode = 500;
|
|
4
|
+
reason = 'Error connecting to database';
|
|
5
|
+
constructor() {
|
|
6
|
+
super('Error connecting to database');
|
|
7
|
+
// only because we are extending a built in class
|
|
8
|
+
// Object.setPrototypeOf(this, DatabaseConnectionError.prototype)
|
|
9
|
+
}
|
|
10
|
+
serializeErrors() {
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
message: this.reason,
|
|
14
|
+
},
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CustomError } from './custom-error.js';
|
|
2
|
+
export class NotAuthorizedError extends CustomError {
|
|
3
|
+
statusCode = 401;
|
|
4
|
+
constructor() {
|
|
5
|
+
super('Not authorized');
|
|
6
|
+
// only because we are extending a built in class
|
|
7
|
+
// Object.setPrototypeOf(this, NotAuthorizedError.prototype)
|
|
8
|
+
}
|
|
9
|
+
serializeErrors() {
|
|
10
|
+
return [
|
|
11
|
+
{
|
|
12
|
+
message: 'Not authorized',
|
|
13
|
+
},
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CustomError } from './custom-error.js';
|
|
2
|
+
export class NotFoundError extends CustomError {
|
|
3
|
+
statusCode = 404;
|
|
4
|
+
constructor() {
|
|
5
|
+
super('Route not found');
|
|
6
|
+
// only because we are extending a built in class
|
|
7
|
+
// Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
8
|
+
}
|
|
9
|
+
serializeErrors() {
|
|
10
|
+
return [
|
|
11
|
+
{
|
|
12
|
+
message: 'Not Found',
|
|
13
|
+
},
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ValidationError } from 'express-validator';
|
|
2
|
+
import { CustomError } from './custom-error.js';
|
|
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
|
+
message: any;
|
|
12
|
+
field?: undefined;
|
|
13
|
+
})[];
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CustomError } from './custom-error.js';
|
|
2
|
+
export class RequestValidationError extends CustomError {
|
|
3
|
+
errors;
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
constructor(errors) {
|
|
6
|
+
super('Invalid request parameters');
|
|
7
|
+
this.errors = errors;
|
|
8
|
+
// only because we are extending a built in class
|
|
9
|
+
// Object.setPrototypeOf(this, RequestValidationError.prototype)
|
|
10
|
+
}
|
|
11
|
+
serializeErrors() {
|
|
12
|
+
return this.errors.map((error) => {
|
|
13
|
+
if (error.type === 'field') {
|
|
14
|
+
return {
|
|
15
|
+
message: error.msg,
|
|
16
|
+
field: error.path,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
message: error.msg,
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
package/build/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export
|
|
1
|
+
export * from './errors/bad-request-error.js';
|
|
2
|
+
export * from './errors/custom-error.js';
|
|
3
|
+
export * from './errors/database-connection-error.js';
|
|
4
|
+
export * from './errors/not-authorized-error.js';
|
|
5
|
+
export * from './errors/request-validation-error.js';
|
|
6
|
+
export * from './middlewares/current-user.js';
|
|
7
|
+
export * from './middlewares/error-handler.js';
|
|
8
|
+
export * from './middlewares/require-auth.js';
|
|
9
|
+
export * from './middlewares/validate-request.js';
|
package/build/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export default printColor;
|
|
1
|
+
export * from './errors/bad-request-error.js';
|
|
2
|
+
export * from './errors/custom-error.js';
|
|
3
|
+
export * from './errors/database-connection-error.js';
|
|
4
|
+
export * from './errors/not-authorized-error.js';
|
|
5
|
+
export * from './errors/request-validation-error.js';
|
|
6
|
+
export * from './middlewares/current-user.js';
|
|
7
|
+
export * from './middlewares/error-handler.js';
|
|
8
|
+
export * from './middlewares/require-auth.js';
|
|
9
|
+
export * from './middlewares/validate-request.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { 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
|
+
currentUser?: UserPayload;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
|
|
14
|
+
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 (error) { }
|
|
11
|
+
next();
|
|
12
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CustomError } from '../../../common/src/errors/custom-error.js';
|
|
2
|
+
export function errorHandler(err, req, res, next) {
|
|
3
|
+
if (err instanceof CustomError) {
|
|
4
|
+
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
5
|
+
}
|
|
6
|
+
res.status(400).send({
|
|
7
|
+
errors: [
|
|
8
|
+
{
|
|
9
|
+
message: 'Something went wrong',
|
|
10
|
+
},
|
|
11
|
+
],
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { validationResult } from 'express-validator';
|
|
2
|
+
import { RequestValidationError } from '../../../common/src/errors/request-validation-error.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": "@raymanselltickets/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",
|
|
@@ -18,8 +18,17 @@
|
|
|
18
18
|
"type": "module",
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@tsconfig/node24": "^24.0.3",
|
|
21
|
+
"@types/cookie-session": "^2.0.49",
|
|
22
|
+
"@types/express": "^5.0.6",
|
|
23
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
21
24
|
"@types/node": "^25.0.3",
|
|
22
25
|
"del-cli": "^7.0.0",
|
|
23
26
|
"typescript": "^5.9.3"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"cookie-session": "^2.1.1",
|
|
30
|
+
"express": "^5.2.1",
|
|
31
|
+
"express-validator": "^7.3.1",
|
|
32
|
+
"jsonwebtoken": "^9.0.3"
|
|
24
33
|
}
|
|
25
34
|
}
|