@tabletennisshop/common 1.0.0
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/errors/custom-error.ts +14 -0
- package/errors/not-authorized-error.ts +13 -0
- package/errors/not-found-error.ts +12 -0
- package/errors/request-validate-error.ts +17 -0
- package/middlewares/check-authorized-middleware.ts +10 -0
- package/middlewares/error-handler.ts +17 -0
- package/middlewares/validate-request-middleware.ts +12 -0
- package/package.json +13 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
export abstract class CustomError extends Error{
|
2
|
+
abstract statusCode: number;
|
3
|
+
constructor(message: string) {
|
4
|
+
super(message);
|
5
|
+
/*
|
6
|
+
Always use this line in subclasses
|
7
|
+
This will ensure that return true for "instanceof"
|
8
|
+
const error = new CustomError("message");
|
9
|
+
console.log(error instanceof CustomError);
|
10
|
+
*/
|
11
|
+
Object.setPrototypeOf(this, CustomError.prototype);
|
12
|
+
}
|
13
|
+
abstract serializeErrors(): {message: string, details?: any}[];
|
14
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { CustomError } from "./custom-error";
|
2
|
+
|
3
|
+
export class NotAuthorizedError extends CustomError{
|
4
|
+
statusCode: number = 401;
|
5
|
+
constructor(message: string){
|
6
|
+
super(message);
|
7
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
8
|
+
}
|
9
|
+
|
10
|
+
serializeErrors(): { message: string; details?: any; }[] {
|
11
|
+
return [{message: this.message}];
|
12
|
+
}
|
13
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { CustomError } from "./custom-error";
|
2
|
+
|
3
|
+
export class NotFoundError extends CustomError{
|
4
|
+
statusCode: number = 401;
|
5
|
+
constructor(message: string){
|
6
|
+
super(message);
|
7
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
8
|
+
}
|
9
|
+
serializeErrors(): { message: string; details?: any; }[] {
|
10
|
+
return [{message: this.message}];
|
11
|
+
}
|
12
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { CustomError } from './custom-error';
|
2
|
+
import { ValidationError } from 'express-validator';
|
3
|
+
export class RequestValidateError extends CustomError{
|
4
|
+
statusCode = 400;
|
5
|
+
private errors: ValidationError[];
|
6
|
+
constructor(errors: ValidationError[]){
|
7
|
+
//Assign message to CustomError abstract class
|
8
|
+
super("Invalid request parameters");
|
9
|
+
this.errors = errors;
|
10
|
+
Object.setPrototypeOf(this, RequestValidateError.prototype);
|
11
|
+
}
|
12
|
+
serializeErrors() {
|
13
|
+
return this.errors.map((err)=>{
|
14
|
+
return {message: err.msg};
|
15
|
+
});
|
16
|
+
}
|
17
|
+
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { NextFunction } from "express"
|
2
|
+
import axios from "axios";
|
3
|
+
import { NotAuthorizedError } from "../errors/not-authorized-error";
|
4
|
+
export const CheckAuthorized= async (req: Request, res: Response, next: NextFunction)=>{
|
5
|
+
const axiosRes = await axios.get("localhost:3000/api/users/current-user");
|
6
|
+
if (axiosRes.status != 200){
|
7
|
+
throw new NotAuthorizedError("Not Authorized");
|
8
|
+
}
|
9
|
+
next();
|
10
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import express,{NextFunction, Request, Response } from "express";
|
2
|
+
import { CustomError } from "../errors/custom-error";
|
3
|
+
import { RequestValidateError } from "../errors/request-validate-error";
|
4
|
+
|
5
|
+
export const errorHandler = (
|
6
|
+
err:Error,
|
7
|
+
req: Request,
|
8
|
+
res: Response,
|
9
|
+
next: NextFunction
|
10
|
+
)=>{
|
11
|
+
if (err instanceof CustomError){
|
12
|
+
res.status(err.statusCode).send({errors: err.serializeErrors()});
|
13
|
+
return;
|
14
|
+
}
|
15
|
+
res.status(400).send({errors: [{message: "Something is wrong"}]});
|
16
|
+
return;
|
17
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import express, { NextFunction, Request, Response } from 'express';
|
2
|
+
import { validationResult } from 'express-validator';
|
3
|
+
import { RequestValidateError } from '../errors/request-validate-error';
|
4
|
+
|
5
|
+
const validateRequestMiddleware = (req: Request, res: Response, next: NextFunction) => {
|
6
|
+
const errors = validationResult(req);
|
7
|
+
if (!errors.isEmpty()) {
|
8
|
+
throw new RequestValidateError(errors.array());
|
9
|
+
}
|
10
|
+
next();
|
11
|
+
}
|
12
|
+
export default validateRequestMiddleware;
|
package/package.json
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
{
|
2
|
+
"name": "@tabletennisshop/common",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
+
},
|
9
|
+
"keywords": [],
|
10
|
+
"author": "",
|
11
|
+
"license": "ISC",
|
12
|
+
"type": "commonjs"
|
13
|
+
}
|