@dip-university/common 3.0.8 → 3.0.9
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/database-connection-error.d.ts +2 -4
- package/build/errors/database-connection-error.js +3 -5
- package/build/errors/http-error.d.ts +1 -1
- package/build/errors/http-error.js +4 -4
- package/build/errors/not-authorized-error.d.ts +7 -0
- package/build/errors/not-authorized-error.js +14 -0
- package/build/errors/not-found-error.d.ts +2 -3
- package/build/errors/not-found-error.js +1 -2
- package/build/errors/request-validation-error.d.ts +2 -3
- package/build/errors/request-validation-error.js +5 -3
- package/build/middlewares/currentUser.js +1 -1
- package/build/middlewares/error-handler.js +1 -1
- package/build/middlewares/requireAuth.js +2 -2
- package/package.json +2 -3
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class DatabaseConnectionError extends
|
|
3
|
-
statusCode: number;
|
|
4
|
-
reason: string;
|
|
1
|
+
import { HttpError } from "./http-error";
|
|
2
|
+
export declare class DatabaseConnectionError extends HttpError {
|
|
5
3
|
constructor();
|
|
6
4
|
serializeErrors(): {
|
|
7
5
|
message: string;
|
|
@@ -2,15 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DatabaseConnectionError = void 0;
|
|
4
4
|
const http_error_1 = require("./http-error");
|
|
5
|
-
class DatabaseConnectionError extends http_error_1.
|
|
5
|
+
class DatabaseConnectionError extends http_error_1.HttpError {
|
|
6
6
|
constructor() {
|
|
7
|
-
super("Error connecting to
|
|
8
|
-
this.statusCode = 500;
|
|
9
|
-
this.reason = "Error connecting to database";
|
|
7
|
+
super("Error connecting to database", 500);
|
|
10
8
|
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
11
9
|
}
|
|
12
10
|
serializeErrors() {
|
|
13
|
-
return [{ message: this.
|
|
11
|
+
return [{ message: this.message }];
|
|
14
12
|
}
|
|
15
13
|
}
|
|
16
14
|
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
class
|
|
3
|
+
exports.HttpError = void 0;
|
|
4
|
+
class HttpError extends Error {
|
|
5
5
|
constructor(message, statusCode = 400, code) {
|
|
6
6
|
super(message);
|
|
7
7
|
this.statusCode = statusCode;
|
|
8
8
|
if (code)
|
|
9
9
|
this.code = code;
|
|
10
|
-
Object.setPrototypeOf(this,
|
|
10
|
+
Object.setPrototypeOf(this, HttpError.prototype);
|
|
11
11
|
}
|
|
12
12
|
serializeErrors() {
|
|
13
13
|
return [{ message: this.message, code: this.code }];
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
-
exports.
|
|
16
|
+
exports.HttpError = HttpError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAuthorizedError = void 0;
|
|
4
|
+
const http_error_1 = require("./http-error");
|
|
5
|
+
class NotAuthorizedError extends http_error_1.HttpError {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Not authorized", 401);
|
|
8
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
|
9
|
+
}
|
|
10
|
+
serializeErrors() {
|
|
11
|
+
return [{ message: this.message }];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.NotAuthorizedError = NotAuthorizedError;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class NotFoundError extends
|
|
3
|
-
statusCode: number;
|
|
1
|
+
import { HttpError } from "./http-error";
|
|
2
|
+
export declare class NotFoundError extends HttpError {
|
|
4
3
|
constructor(message?: string);
|
|
5
4
|
serializeErrors(): {
|
|
6
5
|
message: string;
|
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NotFoundError = void 0;
|
|
4
4
|
const http_error_1 = require("./http-error");
|
|
5
|
-
class NotFoundError extends http_error_1.
|
|
5
|
+
class NotFoundError extends http_error_1.HttpError {
|
|
6
6
|
constructor(message = "Not Found") {
|
|
7
7
|
super(message, 404);
|
|
8
|
-
this.statusCode = 404;
|
|
9
8
|
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
10
9
|
}
|
|
11
10
|
serializeErrors() {
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HttpError } from "./http-error";
|
|
2
2
|
interface ValidationErrorItem {
|
|
3
3
|
msg: string;
|
|
4
4
|
param?: string;
|
|
5
5
|
location?: string;
|
|
6
6
|
}
|
|
7
|
-
export declare class RequestValidationError extends
|
|
7
|
+
export declare class RequestValidationError extends HttpError {
|
|
8
8
|
errors: ValidationErrorItem[];
|
|
9
|
-
statusCode: number;
|
|
10
9
|
constructor(errors: ValidationErrorItem[]);
|
|
11
10
|
serializeErrors(): {
|
|
12
11
|
message: string;
|
|
@@ -2,15 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RequestValidationError = void 0;
|
|
4
4
|
const http_error_1 = require("./http-error");
|
|
5
|
-
class RequestValidationError extends http_error_1.
|
|
5
|
+
class RequestValidationError extends http_error_1.HttpError {
|
|
6
6
|
constructor(errors) {
|
|
7
7
|
super("Invalid request parameters", 400);
|
|
8
8
|
this.errors = errors;
|
|
9
|
-
this.statusCode = 400;
|
|
10
9
|
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
11
10
|
}
|
|
12
11
|
serializeErrors() {
|
|
13
|
-
return this.errors.map((err) => ({
|
|
12
|
+
return this.errors.map((err) => ({
|
|
13
|
+
message: err.msg,
|
|
14
|
+
field: err.param,
|
|
15
|
+
}));
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
exports.RequestValidationError = RequestValidationError;
|
|
@@ -27,7 +27,7 @@ const currentUser = (req, res, next) => __awaiter(void 0, void 0, void 0, functi
|
|
|
27
27
|
next();
|
|
28
28
|
}
|
|
29
29
|
catch (err) {
|
|
30
|
-
throw new http_error_1.
|
|
30
|
+
throw new http_error_1.HttpError("Invalid token", 401);
|
|
31
31
|
}
|
|
32
32
|
});
|
|
33
33
|
exports.currentUser = currentUser;
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.errorHandler = void 0;
|
|
4
4
|
const http_error_1 = require("../errors/http-error");
|
|
5
5
|
const errorHandler = (err, req, res, next) => {
|
|
6
|
-
if (err instanceof http_error_1.
|
|
6
|
+
if (err instanceof http_error_1.HttpError) {
|
|
7
7
|
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
8
8
|
}
|
|
9
9
|
console.error(err);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.requireAuth = void 0;
|
|
4
|
-
const
|
|
4
|
+
const not_authorized_error_1 = require("../errors/not-authorized-error");
|
|
5
5
|
const requireAuth = (req, res, next) => {
|
|
6
6
|
if (!req.currentUser) {
|
|
7
|
-
throw new
|
|
7
|
+
throw new not_authorized_error_1.NotAuthorizedError();
|
|
8
8
|
}
|
|
9
9
|
next();
|
|
10
10
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dip-university/common",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.9",
|
|
4
4
|
"description": "Shared auth, errors, and middleware for microservices",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"clean": "del-cli build",
|
|
12
12
|
"build": "npm run clean && tsc",
|
|
13
|
-
"
|
|
14
|
-
"publish:pkg": "npm run build && npm publish --access=public"
|
|
13
|
+
"publish": "npm run build && npm version patch &&npm publish --access=public"
|
|
15
14
|
},
|
|
16
15
|
"license": "ISC",
|
|
17
16
|
"peerDependencies": {
|