@maysun/common 2.0.2 → 2.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-error.d.ts +10 -0
- package/build/errors/bad-request-error.js +16 -0
- package/build/errors/custom-error.d.ts +8 -0
- package/build/errors/custom-error.js +10 -0
- package/build/errors/database-connection-error.d.ts +9 -0
- package/build/errors/database-connection-error.js +16 -0
- package/build/errors/jsendStatus.d.ts +1 -0
- package/build/errors/jsendStatus.js +11 -0
- package/build/errors/not-authenticated-error.d.ts +9 -0
- package/build/errors/not-authenticated-error.js +15 -0
- package/build/errors/not-authorized-error.d.ts +9 -0
- package/build/errors/not-authorized-error.js +18 -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 +14 -0
- package/build/errors/request-validation-error.js +21 -0
- package/build/errors/server-error.d.ts +10 -0
- package/build/errors/server-error.js +16 -0
- package/build/index.d.ts +13 -7
- package/build/index.js +27 -8
- package/build/middlewares/current-user.d.ts +14 -0
- package/build/middlewares/current-user.js +20 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +22 -0
- package/build/middlewares/require-authentication.d.ts +2 -0
- package/build/middlewares/require-authentication.js +10 -0
- package/build/middlewares/require-authorization.d.ts +0 -0
- package/build/middlewares/require-authorization.js +1 -0
- package/build/middlewares/validate-request.d.ts +2 -0
- package/build/middlewares/validate-request.js +12 -0
- package/package.json +10 -1
@@ -0,0 +1,16 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.BadRequestError = void 0;
|
4
|
+
const custom_error_1 = require("./custom-error");
|
5
|
+
class BadRequestError extends custom_error_1.CustomError {
|
6
|
+
constructor(message) {
|
7
|
+
super(message);
|
8
|
+
this.message = message;
|
9
|
+
this.statusCode = 400;
|
10
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
11
|
+
}
|
12
|
+
serializeErrors() {
|
13
|
+
return [{ message: this.message }];
|
14
|
+
}
|
15
|
+
}
|
16
|
+
exports.BadRequestError = BadRequestError;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.CustomError = void 0;
|
4
|
+
class CustomError extends Error {
|
5
|
+
constructor(message) {
|
6
|
+
super(message);
|
7
|
+
Object.setPrototypeOf(this, CustomError.prototype);
|
8
|
+
}
|
9
|
+
}
|
10
|
+
exports.CustomError = CustomError;
|
@@ -0,0 +1,16 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.DatabaseConnectionError = void 0;
|
4
|
+
const custom_error_1 = require("./custom-error");
|
5
|
+
class DatabaseConnectionError extends custom_error_1.CustomError {
|
6
|
+
constructor() {
|
7
|
+
super('Error connecting to DB');
|
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 @@
|
|
1
|
+
export declare const setJSENDStatusName: (statuscode: number) => "error" | "fail";
|
@@ -0,0 +1,11 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.setJSENDStatusName = void 0;
|
4
|
+
const setJSENDStatusName = (statuscode) => {
|
5
|
+
const hundredsDigit = statuscode.toString()[0];
|
6
|
+
if (hundredsDigit === '5') {
|
7
|
+
return 'error';
|
8
|
+
}
|
9
|
+
return 'fail';
|
10
|
+
};
|
11
|
+
exports.setJSENDStatusName = setJSENDStatusName;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.NotAuthenticatedError = void 0;
|
4
|
+
const custom_error_1 = require("./custom-error");
|
5
|
+
class NotAuthenticatedError extends custom_error_1.CustomError {
|
6
|
+
constructor() {
|
7
|
+
super('Not authenticated');
|
8
|
+
this.statusCode = 401;
|
9
|
+
Object.setPrototypeOf(this, NotAuthenticatedError.prototype);
|
10
|
+
}
|
11
|
+
serializeErrors() {
|
12
|
+
return [{ message: 'Not authenticated' }];
|
13
|
+
}
|
14
|
+
}
|
15
|
+
exports.NotAuthenticatedError = NotAuthenticatedError;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.NotAuthorizedError = void 0;
|
4
|
+
const custom_error_1 = require("./custom-error");
|
5
|
+
// i change this class name to from NotAuthorizedError
|
6
|
+
// to NotAuthErro because i want cover NotAuthenticationError
|
7
|
+
// and NotAuthorizedError and send clean error message
|
8
|
+
class NotAuthorizedError extends custom_error_1.CustomError {
|
9
|
+
constructor() {
|
10
|
+
super('Not authorized');
|
11
|
+
this.statusCode = 401;
|
12
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
13
|
+
}
|
14
|
+
serializeErrors() {
|
15
|
+
return [{ message: 'Not authorized' }];
|
16
|
+
}
|
17
|
+
}
|
18
|
+
exports.NotAuthorizedError = NotAuthorizedError;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.NotFoundError = void 0;
|
4
|
+
const custom_error_1 = require("./custom-error");
|
5
|
+
class NotFoundError extends custom_error_1.CustomError {
|
6
|
+
constructor() {
|
7
|
+
super('Route not found');
|
8
|
+
this.statusCode = 404;
|
9
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
10
|
+
}
|
11
|
+
serializeErrors() {
|
12
|
+
return [{ message: 'Not found' }];
|
13
|
+
}
|
14
|
+
}
|
15
|
+
exports.NotFoundError = NotFoundError;
|
@@ -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,21 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.RequestValidationError = void 0;
|
4
|
+
const custom_error_1 = require("./custom-error");
|
5
|
+
class RequestValidationError extends custom_error_1.CustomError {
|
6
|
+
constructor(errors) {
|
7
|
+
super('Invalid request parameters');
|
8
|
+
this.errors = errors;
|
9
|
+
this.statusCode = 400;
|
10
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
11
|
+
}
|
12
|
+
serializeErrors() {
|
13
|
+
return this.errors.map((err) => {
|
14
|
+
if (err.type === 'field') {
|
15
|
+
return { message: err.msg, field: err.path };
|
16
|
+
}
|
17
|
+
return { message: err.msg };
|
18
|
+
});
|
19
|
+
}
|
20
|
+
}
|
21
|
+
exports.RequestValidationError = RequestValidationError;
|
@@ -0,0 +1,16 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ServerError = void 0;
|
4
|
+
const custom_error_1 = require("./custom-error");
|
5
|
+
class ServerError extends custom_error_1.CustomError {
|
6
|
+
constructor(message) {
|
7
|
+
super(message);
|
8
|
+
this.message = message;
|
9
|
+
this.statusCode = 500;
|
10
|
+
Object.setPrototypeOf(this, ServerError.prototype);
|
11
|
+
}
|
12
|
+
serializeErrors() {
|
13
|
+
return [{ message: this.message }];
|
14
|
+
}
|
15
|
+
}
|
16
|
+
exports.ServerError = ServerError;
|
package/build/index.d.ts
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
export
|
1
|
+
export * from "./errors/bad-request-error";
|
2
|
+
export * from "./errors/custom-error";
|
3
|
+
export * from "./errors/database-connection-error";
|
4
|
+
export * from "./errors/jsendStatus";
|
5
|
+
export * from "./errors/not-authenticated-error";
|
6
|
+
export * from "./errors/not-authorized-error";
|
7
|
+
export * from "./errors/not-found-error";
|
8
|
+
export * from "./errors/request-validation-error";
|
9
|
+
export * from "./errors/server-error";
|
10
|
+
export * from "./middlewares/current-user";
|
11
|
+
export * from "./middlewares/error-handler";
|
12
|
+
export * from "./middlewares/require-authentication";
|
13
|
+
export * from "./middlewares/validate-request";
|
package/build/index.js
CHANGED
@@ -1,10 +1,29 @@
|
|
1
1
|
"use strict";
|
2
|
-
Object.
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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);
|
7
15
|
};
|
8
|
-
|
9
|
-
|
10
|
-
exports
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./errors/bad-request-error"), exports);
|
18
|
+
__exportStar(require("./errors/custom-error"), exports);
|
19
|
+
__exportStar(require("./errors/database-connection-error"), exports);
|
20
|
+
__exportStar(require("./errors/jsendStatus"), exports);
|
21
|
+
__exportStar(require("./errors/not-authenticated-error"), exports);
|
22
|
+
__exportStar(require("./errors/not-authorized-error"), exports);
|
23
|
+
__exportStar(require("./errors/not-found-error"), exports);
|
24
|
+
__exportStar(require("./errors/request-validation-error"), exports);
|
25
|
+
__exportStar(require("./errors/server-error"), exports);
|
26
|
+
__exportStar(require("./middlewares/current-user"), exports);
|
27
|
+
__exportStar(require("./middlewares/error-handler"), exports);
|
28
|
+
__exportStar(require("./middlewares/require-authentication"), exports);
|
29
|
+
__exportStar(require("./middlewares/validate-request"), exports);
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
2
|
+
interface UserPayLoad {
|
3
|
+
id: string;
|
4
|
+
username: 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,20 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.currentUser = void 0;
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
8
|
+
const currentUser = (req, res, next) => {
|
9
|
+
var _a;
|
10
|
+
if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
|
11
|
+
return next();
|
12
|
+
}
|
13
|
+
try {
|
14
|
+
const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
|
15
|
+
req.currentUser = payload;
|
16
|
+
}
|
17
|
+
catch (err) { }
|
18
|
+
next();
|
19
|
+
};
|
20
|
+
exports.currentUser = currentUser;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.errorHandler = void 0;
|
4
|
+
const custom_error_1 = require("../errors/custom-error");
|
5
|
+
const jsendStatus_1 = require("../errors/jsendStatus");
|
6
|
+
const errorHandler = (err, req, res, next) => {
|
7
|
+
if (err instanceof custom_error_1.CustomError) {
|
8
|
+
return res.status(err.statusCode).send({
|
9
|
+
status: (0, jsendStatus_1.setJSENDStatusName)(err.statusCode),
|
10
|
+
data: {
|
11
|
+
errors: err.serializeErrors(),
|
12
|
+
},
|
13
|
+
});
|
14
|
+
}
|
15
|
+
res.status(400).send({
|
16
|
+
status: (0, jsendStatus_1.setJSENDStatusName)(400),
|
17
|
+
data: {
|
18
|
+
errors: [{ message: 'Something went wrong' }],
|
19
|
+
},
|
20
|
+
});
|
21
|
+
};
|
22
|
+
exports.errorHandler = errorHandler;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.requireAuthentication = void 0;
|
4
|
+
const not_authenticated_error_1 = require("../errors/not-authenticated-error");
|
5
|
+
const requireAuthentication = (req, res, next) => {
|
6
|
+
if (!req.currentUser)
|
7
|
+
throw new not_authenticated_error_1.NotAuthenticatedError();
|
8
|
+
next();
|
9
|
+
};
|
10
|
+
exports.requireAuthentication = requireAuthentication;
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|
@@ -0,0 +1,12 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.validateRequest = void 0;
|
4
|
+
const express_validator_1 = require("express-validator");
|
5
|
+
const request_validation_error_1 = require("../errors/request-validation-error");
|
6
|
+
const validateRequest = (req, res, next) => {
|
7
|
+
const errors = (0, express_validator_1.validationResult)(req);
|
8
|
+
if (!errors.isEmpty())
|
9
|
+
throw new request_validation_error_1.RequestValidationError(errors.array());
|
10
|
+
next();
|
11
|
+
};
|
12
|
+
exports.validateRequest = validateRequest;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@maysun/common",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.4",
|
4
4
|
"description": "",
|
5
5
|
"main": "./build/index.js",
|
6
6
|
"types": "./build/index.d.ts",
|
@@ -18,5 +18,14 @@
|
|
18
18
|
"devDependencies": {
|
19
19
|
"del-cli": "^6.0.0",
|
20
20
|
"typescript": "^5.7.3"
|
21
|
+
},
|
22
|
+
"dependencies": {
|
23
|
+
"@types/cookie-session": "^2.0.49",
|
24
|
+
"@types/express": "^4.17.21",
|
25
|
+
"@types/jsonwebtoken": "^9.0.7",
|
26
|
+
"cookie-session": "^2.1.0",
|
27
|
+
"express": "^4.21.2",
|
28
|
+
"express-validator": "^7.2.1",
|
29
|
+
"jsonwebtoken": "^9.0.2"
|
21
30
|
}
|
22
31
|
}
|