@dip-university/common 3.0.6
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 +9 -0
- package/build/errors/database-connection-error.js +16 -0
- package/build/errors/http-error.d.ts +9 -0
- package/build/errors/http-error.js +16 -0
- package/build/errors/not-found-error.d.ts +8 -0
- package/build/errors/not-found-error.js +15 -0
- package/build/errors/request-validation-error.d.ts +16 -0
- package/build/errors/request-validation-error.js +16 -0
- package/build/index.d.ts +7 -0
- package/build/index.js +23 -0
- package/build/middlewares/currentUser.d.ts +12 -0
- package/build/middlewares/currentUser.js +33 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +12 -0
- package/build/middlewares/requireAuth.d.ts +2 -0
- package/build/middlewares/requireAuth.js +11 -0
- package/package.json +31 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseConnectionError = void 0;
|
|
4
|
+
const http_error_1 = require("./http-error");
|
|
5
|
+
class DatabaseConnectionError extends http_error_1.httpError {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Error connecting to db", 500);
|
|
8
|
+
this.statusCode = 500;
|
|
9
|
+
this.reason = "Error connecting to database";
|
|
10
|
+
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: this.reason }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.httpError = void 0;
|
|
4
|
+
class httpError extends Error {
|
|
5
|
+
constructor(message, statusCode = 400, code) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.statusCode = statusCode;
|
|
8
|
+
if (code)
|
|
9
|
+
this.code = code;
|
|
10
|
+
Object.setPrototypeOf(this, httpError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: this.message, code: this.code }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.httpError = httpError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotFoundError = void 0;
|
|
4
|
+
const http_error_1 = require("./http-error");
|
|
5
|
+
class NotFoundError extends http_error_1.httpError {
|
|
6
|
+
constructor(message = "Not Found") {
|
|
7
|
+
super(message, 404);
|
|
8
|
+
this.statusCode = 404;
|
|
9
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serializeErrors() {
|
|
12
|
+
return [{ message: this.message }];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.NotFoundError = NotFoundError;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { httpError } from "./http-error";
|
|
2
|
+
interface ValidationErrorItem {
|
|
3
|
+
msg: string;
|
|
4
|
+
param?: string;
|
|
5
|
+
location?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class RequestValidationError extends httpError {
|
|
8
|
+
errors: ValidationErrorItem[];
|
|
9
|
+
statusCode: number;
|
|
10
|
+
constructor(errors: ValidationErrorItem[]);
|
|
11
|
+
serializeErrors(): {
|
|
12
|
+
message: string;
|
|
13
|
+
field: string | undefined;
|
|
14
|
+
}[];
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestValidationError = void 0;
|
|
4
|
+
const http_error_1 = require("./http-error");
|
|
5
|
+
class RequestValidationError extends http_error_1.httpError {
|
|
6
|
+
constructor(errors) {
|
|
7
|
+
super("Invalid request parameters", 400);
|
|
8
|
+
this.errors = errors;
|
|
9
|
+
this.statusCode = 400;
|
|
10
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return this.errors.map((err) => ({ message: err.msg, field: err.param }));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.RequestValidationError = RequestValidationError;
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './errors/http-error';
|
|
2
|
+
export * from './errors/request-validation-error';
|
|
3
|
+
export * from './errors/not-found-error';
|
|
4
|
+
export * from './errors/database-connection-error';
|
|
5
|
+
export * from './middlewares/currentUser';
|
|
6
|
+
export * from './middlewares/requireAuth';
|
|
7
|
+
export * from './middlewares/error-handler';
|
package/build/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./errors/http-error"), exports);
|
|
18
|
+
__exportStar(require("./errors/request-validation-error"), exports);
|
|
19
|
+
__exportStar(require("./errors/not-found-error"), exports);
|
|
20
|
+
__exportStar(require("./errors/database-connection-error"), exports);
|
|
21
|
+
__exportStar(require("./middlewares/currentUser"), exports);
|
|
22
|
+
__exportStar(require("./middlewares/requireAuth"), exports);
|
|
23
|
+
__exportStar(require("./middlewares/error-handler"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
declare global {
|
|
3
|
+
namespace Express {
|
|
4
|
+
interface Request {
|
|
5
|
+
currentUser?: {
|
|
6
|
+
id: string;
|
|
7
|
+
email: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export declare const currentUser: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.currentUser = void 0;
|
|
16
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
17
|
+
const http_error_1 = require("../errors/http-error");
|
|
18
|
+
const currentUser = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
var _a;
|
|
20
|
+
const token = (_a = req.headers.authorization) === null || _a === void 0 ? void 0 : _a.replace("Bearer ", "");
|
|
21
|
+
if (!token) {
|
|
22
|
+
return next();
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const decoded = jsonwebtoken_1.default.verify(token, process.env.JWT_SECRET);
|
|
26
|
+
req.currentUser = { id: decoded.id, email: decoded.email };
|
|
27
|
+
next();
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
throw new http_error_1.httpError("Invalid token", 401);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
exports.currentUser = currentUser;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorHandler = void 0;
|
|
4
|
+
const http_error_1 = require("../errors/http-error");
|
|
5
|
+
const errorHandler = (err, req, res, next) => {
|
|
6
|
+
if (err instanceof http_error_1.httpError) {
|
|
7
|
+
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
8
|
+
}
|
|
9
|
+
console.error(err);
|
|
10
|
+
res.status(500).send({ errors: [{ message: "Something went wrong" }] });
|
|
11
|
+
};
|
|
12
|
+
exports.errorHandler = errorHandler;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireAuth = void 0;
|
|
4
|
+
const http_error_1 = require("../errors/http-error");
|
|
5
|
+
const requireAuth = (req, res, next) => {
|
|
6
|
+
if (!req.currentUser) {
|
|
7
|
+
throw new http_error_1.httpError("Unauthorized", 401);
|
|
8
|
+
}
|
|
9
|
+
next();
|
|
10
|
+
};
|
|
11
|
+
exports.requireAuth = requireAuth;
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dip-university/common",
|
|
3
|
+
"version": "3.0.6",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./build/index.js",
|
|
6
|
+
"types": "./build/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"build/**/*"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"clean": "del ./build/*",
|
|
12
|
+
"build": "npm run clean && tsc",
|
|
13
|
+
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"del-cli": "^5.1.0",
|
|
20
|
+
"typescript": "^5.6.2"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@types/cookie-session": "^2.0.49",
|
|
24
|
+
"@types/express": "^5.0.0",
|
|
25
|
+
"@types/jsonwebtoken": "^9.0.7",
|
|
26
|
+
"cookie-session": "^2.1.0",
|
|
27
|
+
"express": "^4.21.0",
|
|
28
|
+
"express-validator": "^7.2.0",
|
|
29
|
+
"jsonwebtoken": "^9.0.2"
|
|
30
|
+
}
|
|
31
|
+
}
|