@alirezajvh/common 1.0.2 → 1.0.4
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.d.ts +8 -0
- package/build/errors/bad-request.js +12 -0
- package/build/errors/base.d.ts +6 -0
- package/build/errors/base.js +7 -0
- package/build/errors/database-connection.d.ts +8 -0
- package/build/errors/database-connection.js +11 -0
- package/build/errors/index.d.ts +6 -0
- package/build/errors/index.js +5 -0
- package/build/errors/not-authorized.d.ts +7 -0
- package/build/errors/not-authorized.js +10 -0
- package/build/errors/not-found.d.ts +7 -0
- package/build/errors/not-found.js +10 -0
- package/build/errors/request-validation.d.ts +9 -0
- package/build/errors/request-validation.js +16 -0
- package/build/errors/types.d.ts +7 -0
- package/build/index.d.ts +5 -0
- package/build/index.js +5 -0
- package/build/middlewares/current-user.d.ts +17 -0
- package/build/middlewares/current-user.js +11 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +9 -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 -1
- package/build/src/index.js +0 -2
- /package/build/{src/index.d.ts → errors/types.js} +0 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AbstractError } from './base.ts';
|
|
2
|
+
import type { ErrorItem } from './types.ts';
|
|
3
|
+
export declare class BadRequestError extends AbstractError {
|
|
4
|
+
readonly message: string;
|
|
5
|
+
statusCode: number;
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
serlizeErrors(): ErrorItem[];
|
|
8
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractError } from "./base.js";
|
|
2
|
+
export class BadRequestError extends AbstractError {
|
|
3
|
+
message;
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super();
|
|
7
|
+
this.message = message;
|
|
8
|
+
}
|
|
9
|
+
serlizeErrors() {
|
|
10
|
+
return [{ message: this.message }];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AbstractError } from "./base.js";
|
|
2
|
+
export class DatabaseConnectionError extends AbstractError {
|
|
3
|
+
reason = 'Error connecting to database';
|
|
4
|
+
statusCode = 500;
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
}
|
|
8
|
+
serlizeErrors() {
|
|
9
|
+
return [{ message: this.reason }];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { AbstractError } from './base.ts';
|
|
2
|
+
export { RequestValidationError } from './request-validation.ts';
|
|
3
|
+
export { DatabaseConnectionError } from './database-connection.ts';
|
|
4
|
+
export { NotFoundError } from './not-found.ts';
|
|
5
|
+
export { BadRequestError } from './bad-request.ts';
|
|
6
|
+
export type * from './types.ts';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { AbstractError } from "./base.js";
|
|
2
|
+
export { RequestValidationError } from "./request-validation.js";
|
|
3
|
+
export { DatabaseConnectionError } from "./database-connection.js";
|
|
4
|
+
export { NotFoundError } from "./not-found.js";
|
|
5
|
+
export { BadRequestError } from "./bad-request.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ValidationError } from 'express-validator';
|
|
2
|
+
import { AbstractError } from './base.ts';
|
|
3
|
+
import { type ErrorItem } from './types.ts';
|
|
4
|
+
export declare class RequestValidationError extends AbstractError {
|
|
5
|
+
errors: ValidationError[];
|
|
6
|
+
statusCode: number;
|
|
7
|
+
constructor(errors: ValidationError[]);
|
|
8
|
+
serlizeErrors(): ErrorItem[];
|
|
9
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AbstractError } from "./base.js";
|
|
2
|
+
import {} from "./types.js";
|
|
3
|
+
export class RequestValidationError extends AbstractError {
|
|
4
|
+
errors;
|
|
5
|
+
statusCode = 400;
|
|
6
|
+
constructor(errors) {
|
|
7
|
+
super();
|
|
8
|
+
this.errors = errors;
|
|
9
|
+
}
|
|
10
|
+
serlizeErrors() {
|
|
11
|
+
return this.errors.map((error) => ({
|
|
12
|
+
message: `${error.msg}`,
|
|
13
|
+
filed: error.type == 'field' ? error.path : error.type,
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
}
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -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,11 @@
|
|
|
1
|
+
import jwt from 'jsonwebtoken';
|
|
2
|
+
export const currentUser = (req, res, next) => {
|
|
3
|
+
if (!req.session?.jwt)
|
|
4
|
+
return next();
|
|
5
|
+
try {
|
|
6
|
+
const payload = jwt.verify(req.session.jwt, process.env.JWT_KEY);
|
|
7
|
+
req.currentUser = payload;
|
|
8
|
+
}
|
|
9
|
+
catch (_) { }
|
|
10
|
+
next();
|
|
11
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AbstractError } from "../errors/index.js";
|
|
2
|
+
export const errorHandler = (err, req, res, next) => {
|
|
3
|
+
if (err instanceof AbstractError) {
|
|
4
|
+
return res.status(err.statusCode).send({ errors: err.serlizeErrors() });
|
|
5
|
+
}
|
|
6
|
+
return res.status(400).send({
|
|
7
|
+
errors: [{ message: 'Something went wrong' }],
|
|
8
|
+
});
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { validationResult } from 'express-validator';
|
|
2
|
+
import { RequestValidationError } from "../errors/index.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,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alirezajvh/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"files": [
|
|
8
9
|
"build/**/*"
|
|
9
10
|
],
|
|
@@ -15,6 +16,15 @@
|
|
|
15
16
|
"keywords": [],
|
|
16
17
|
"author": "",
|
|
17
18
|
"license": "ISC",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@types/cookie-session": "^2.0.49",
|
|
21
|
+
"@types/express": "^5.0.6",
|
|
22
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
23
|
+
"cookie-session": "^2.1.1",
|
|
24
|
+
"express": "^5.2.1",
|
|
25
|
+
"express-validator": "^7.3.1",
|
|
26
|
+
"jsonwebtoken": "^9.0.3"
|
|
27
|
+
},
|
|
18
28
|
"devDependencies": {
|
|
19
29
|
"del-cli": "^7.0.0",
|
|
20
30
|
"typescript": "^5.9.3"
|
package/build/src/index.js
DELETED
|
File without changes
|