@onkarnik-tickets/common 1.0.0 → 1.0.2
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 +14 -0
- package/build/errors/custom-error.d.ts +8 -0
- package/build/errors/custom-error.js +6 -0
- package/build/errors/database-connection-error.d.ts +9 -0
- package/build/errors/database-connection-error.js +14 -0
- package/build/errors/not-authorised-error.d.ts +9 -0
- package/build/errors/not-authorised-error.js +13 -0
- package/build/errors/not-found-error.d.ts +9 -0
- package/build/errors/not-found-error.js +13 -0
- package/build/errors/request-validation-error.d.ts +14 -0
- package/build/errors/request-validation-error.js +19 -0
- package/build/index.d.ts +10 -0
- package/build/index.js +10 -0
- package/build/middlewares/current-user.d.ts +14 -0
- package/build/middlewares/current-user.js +14 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +11 -0
- package/build/middlewares/require-auth.d.ts +2 -0
- package/build/middlewares/require-auth.js +7 -0
- package/build/middlewares/validateRequest.d.ts +2 -0
- package/build/middlewares/validateRequest.js +9 -0
- package/package.json +23 -4
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
export class BadRequestError extends CustomError {
|
|
3
|
+
statusCode = 400;
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
// line for modifying in-built class
|
|
7
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
8
|
+
}
|
|
9
|
+
serializeErrors() {
|
|
10
|
+
return [
|
|
11
|
+
{ message: this.message }
|
|
12
|
+
];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
export class DatabaseConnectionError extends CustomError {
|
|
3
|
+
reason = 'Error connecting to database';
|
|
4
|
+
statusCode = 500;
|
|
5
|
+
constructor() {
|
|
6
|
+
super('Error conencting to DB');
|
|
7
|
+
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
8
|
+
}
|
|
9
|
+
serializeErrors() {
|
|
10
|
+
return [
|
|
11
|
+
{ message: this.reason }
|
|
12
|
+
];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
export class NotAuthorisedError extends CustomError {
|
|
3
|
+
statusCode = 401;
|
|
4
|
+
constructor() {
|
|
5
|
+
super('Not authorised');
|
|
6
|
+
Object.setPrototypeOf(this, NotAuthorisedError.prototype);
|
|
7
|
+
}
|
|
8
|
+
serializeErrors() {
|
|
9
|
+
return [
|
|
10
|
+
{ message: 'Not authorised' }
|
|
11
|
+
];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
export class NotFoundError extends CustomError {
|
|
3
|
+
statusCode = 404;
|
|
4
|
+
constructor() {
|
|
5
|
+
super('Route not found');
|
|
6
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
7
|
+
}
|
|
8
|
+
serializeErrors() {
|
|
9
|
+
return [{
|
|
10
|
+
message: 'Not Found'
|
|
11
|
+
}];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ValidationError } from "express-validator";
|
|
2
|
+
import { CustomError } from "./custom-error";
|
|
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,19 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
export class RequestValidationError extends CustomError {
|
|
3
|
+
errors;
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
constructor(errors) {
|
|
6
|
+
super('Invalid request params');
|
|
7
|
+
this.errors = errors;
|
|
8
|
+
// Only because we are extending a build in class
|
|
9
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serializeErrors() {
|
|
12
|
+
return this.errors.map((err) => {
|
|
13
|
+
if (err.type === 'field') {
|
|
14
|
+
return { message: err.msg, field: err.path };
|
|
15
|
+
}
|
|
16
|
+
return { message: err.msg };
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './errors/bad-request-error';
|
|
2
|
+
export * from './errors/custom-error';
|
|
3
|
+
export * from './errors/database-connection-error';
|
|
4
|
+
export * from './errors/not-authorised-error';
|
|
5
|
+
export * from './errors/not-found-error';
|
|
6
|
+
export * from './errors/request-validation-error';
|
|
7
|
+
export * from './middlewares/current-user';
|
|
8
|
+
export * from './middlewares/error-handler';
|
|
9
|
+
export * from './middlewares/require-auth';
|
|
10
|
+
export * from './middlewares/validateRequest';
|
package/build/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './errors/bad-request-error';
|
|
2
|
+
export * from './errors/custom-error';
|
|
3
|
+
export * from './errors/database-connection-error';
|
|
4
|
+
export * from './errors/not-authorised-error';
|
|
5
|
+
export * from './errors/not-found-error';
|
|
6
|
+
export * from './errors/request-validation-error';
|
|
7
|
+
export * from './middlewares/current-user';
|
|
8
|
+
export * from './middlewares/error-handler';
|
|
9
|
+
export * from './middlewares/require-auth';
|
|
10
|
+
export * from './middlewares/validateRequest';
|
|
@@ -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,14 @@
|
|
|
1
|
+
import * as 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 (e) {
|
|
11
|
+
console.log(e);
|
|
12
|
+
}
|
|
13
|
+
next();
|
|
14
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CustomError } from "../errors/custom-error";
|
|
2
|
+
export const errorHandler = (err, req, res, next) => {
|
|
3
|
+
if (err instanceof CustomError) {
|
|
4
|
+
return res.status(err.statusCode).send({
|
|
5
|
+
errors: err.serializeErrors()
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
res.status(500).send({
|
|
9
|
+
errors: [{ message: 'Internal Server Error' }]
|
|
10
|
+
});
|
|
11
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { validationResult } from "express-validator";
|
|
2
|
+
import { RequestValidationError } from "../errors/request-validation-error";
|
|
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,13 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onkarnik-tickets/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "./build/index.js",
|
|
6
|
+
"types": "./build/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"build/**/*"
|
|
9
|
+
],
|
|
6
10
|
"scripts": {
|
|
7
|
-
"
|
|
11
|
+
"clean": "rimraf ./build",
|
|
12
|
+
"build": "npm run clean && tsc",
|
|
13
|
+
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
|
|
8
14
|
},
|
|
9
15
|
"keywords": [],
|
|
10
16
|
"author": "",
|
|
11
17
|
"license": "ISC",
|
|
12
|
-
"type": "commonjs"
|
|
18
|
+
"type": "commonjs",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"rimraf": "^6.1.3",
|
|
21
|
+
"typescript": "^6.0.3"
|
|
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.2.1",
|
|
29
|
+
"express-validator": "^7.3.2",
|
|
30
|
+
"jsonwebtoken": "^9.0.3"
|
|
31
|
+
}
|
|
13
32
|
}
|