@sn1006/common 1.0.2 → 1.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 +9 -0
- package/build/errors/bad-request-error.js +19 -0
- package/build/errors/custom-error.d.ts +9 -0
- package/build/errors/custom-error.js +9 -0
- package/build/errors/db-error.d.ts +9 -0
- package/build/errors/db-error.js +17 -0
- package/build/errors/not-authorized-error.d.ts +8 -0
- package/build/errors/not-authorized-error.js +18 -0
- package/build/errors/not-found-error.d.ts +9 -0
- package/build/errors/not-found-error.js +19 -0
- package/build/errors/validation-error.d.ts +12 -0
- package/build/errors/validation-error.js +21 -0
- package/build/index.d.ts +9 -7
- package/build/index.js +23 -8
- package/build/middlewares/current-user.d.ts +14 -0
- package/build/middlewares/current-user.js +22 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +15 -0
- package/build/middlewares/require-auth.d.ts +2 -0
- package/build/middlewares/require-auth.js +11 -0
- package/build/middlewares/validate-request.d.ts +2 -0
- package/build/middlewares/validate-request.js +16 -0
- package/package.json +10 -1
|
@@ -0,0 +1,19 @@
|
|
|
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.BadRequestError = void 0;
|
|
7
|
+
const custom_error_1 = __importDefault(require("./custom-error"));
|
|
8
|
+
class BadRequestError extends custom_error_1.default {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.message = message;
|
|
12
|
+
this.status = 400;
|
|
13
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
14
|
+
}
|
|
15
|
+
serializeError() {
|
|
16
|
+
return [{ message: this.message }];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.BadRequestError = BadRequestError;
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
const custom_error_1 = __importDefault(require("./custom-error"));
|
|
7
|
+
class DBError extends custom_error_1.default {
|
|
8
|
+
constructor() {
|
|
9
|
+
super('Error connecting to DB');
|
|
10
|
+
this.status = 500;
|
|
11
|
+
Object.setPrototypeOf(this, DBError.prototype);
|
|
12
|
+
}
|
|
13
|
+
serializeError() {
|
|
14
|
+
return [{ message: this.message }];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.default = DBError;
|
|
@@ -0,0 +1,18 @@
|
|
|
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.NotAuthorizedError = void 0;
|
|
7
|
+
const custom_error_1 = __importDefault(require("./custom-error"));
|
|
8
|
+
class NotAuthorizedError extends custom_error_1.default {
|
|
9
|
+
constructor() {
|
|
10
|
+
super('Not authorized');
|
|
11
|
+
this.status = 401;
|
|
12
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
|
13
|
+
}
|
|
14
|
+
serializeError() {
|
|
15
|
+
return [{ message: this.message }];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.NotAuthorizedError = NotAuthorizedError;
|
|
@@ -0,0 +1,19 @@
|
|
|
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.NotFoundError = void 0;
|
|
7
|
+
const custom_error_1 = __importDefault(require("./custom-error"));
|
|
8
|
+
class NotFoundError extends custom_error_1.default {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.message = message;
|
|
12
|
+
this.status = 404;
|
|
13
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
14
|
+
}
|
|
15
|
+
serializeError() {
|
|
16
|
+
return [{ message: this.message }];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.NotFoundError = NotFoundError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import CustomError from './custom-error';
|
|
2
|
+
import { ValidationError } from 'express-validator';
|
|
3
|
+
declare class CustomValidationError extends CustomError {
|
|
4
|
+
errors: ValidationError[];
|
|
5
|
+
status: number;
|
|
6
|
+
constructor(errors: ValidationError[]);
|
|
7
|
+
serializeError(): {
|
|
8
|
+
message: any;
|
|
9
|
+
field: string;
|
|
10
|
+
}[];
|
|
11
|
+
}
|
|
12
|
+
export default CustomValidationError;
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
const custom_error_1 = __importDefault(require("./custom-error"));
|
|
7
|
+
class CustomValidationError extends custom_error_1.default {
|
|
8
|
+
constructor(errors) {
|
|
9
|
+
super("Invalid username or password");
|
|
10
|
+
this.errors = errors;
|
|
11
|
+
this.status = 400;
|
|
12
|
+
Object.setPrototypeOf(this, CustomValidationError.prototype);
|
|
13
|
+
}
|
|
14
|
+
serializeError() {
|
|
15
|
+
return this.errors.map(err => {
|
|
16
|
+
const ferr = err;
|
|
17
|
+
return { message: ferr.msg, field: ferr.path };
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.default = CustomValidationError;
|
package/build/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
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/db-error';
|
|
4
|
+
export * from './errors/validation-error';
|
|
5
|
+
export * from './errors/not-authorized-error';
|
|
6
|
+
export * from './middlewares/current-user';
|
|
7
|
+
export * from './middlewares/error-handler';
|
|
8
|
+
export * from './middlewares/require-auth';
|
|
9
|
+
export * from './middlewares/validate-request';
|
package/build/index.js
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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);
|
|
8
15
|
};
|
|
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/db-error"), exports);
|
|
20
|
+
__exportStar(require("./errors/validation-error"), exports);
|
|
21
|
+
__exportStar(require("./errors/not-authorized-error"), exports);
|
|
22
|
+
__exportStar(require("./middlewares/current-user"), exports);
|
|
23
|
+
__exportStar(require("./middlewares/error-handler"), exports);
|
|
24
|
+
__exportStar(require("./middlewares/require-auth"), exports);
|
|
25
|
+
__exportStar(require("./middlewares/validate-request"), exports);
|
|
@@ -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,22 @@
|
|
|
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
|
+
console.log(err);
|
|
19
|
+
}
|
|
20
|
+
next();
|
|
21
|
+
};
|
|
22
|
+
exports.currentUser = currentUser;
|
|
@@ -0,0 +1,15 @@
|
|
|
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.errorHandler = void 0;
|
|
7
|
+
const custom_error_1 = __importDefault(require("../errors/custom-error"));
|
|
8
|
+
const errorHandler = (err, req, res, next) => {
|
|
9
|
+
// console.log('Something went wrong', err);
|
|
10
|
+
//
|
|
11
|
+
if (err instanceof custom_error_1.default) {
|
|
12
|
+
res.status(err.status).send(err.serializeError());
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
exports.errorHandler = errorHandler;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireAuth = void 0;
|
|
4
|
+
const not_authorized_error_1 = require("../errors/not-authorized-error");
|
|
5
|
+
const requireAuth = (req, res, next) => {
|
|
6
|
+
if (!req.currentUser) {
|
|
7
|
+
throw new not_authorized_error_1.NotAuthorizedError();
|
|
8
|
+
}
|
|
9
|
+
next();
|
|
10
|
+
};
|
|
11
|
+
exports.requireAuth = requireAuth;
|
|
@@ -0,0 +1,16 @@
|
|
|
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.validateRequest = void 0;
|
|
7
|
+
const express_validator_1 = require("express-validator");
|
|
8
|
+
const validation_error_1 = __importDefault(require("../errors/validation-error"));
|
|
9
|
+
const validateRequest = (req, res, next) => {
|
|
10
|
+
const errors = (0, express_validator_1.validationResult)(req);
|
|
11
|
+
if (!errors.isEmpty()) {
|
|
12
|
+
return next(new validation_error_1.default(errors.array()));
|
|
13
|
+
}
|
|
14
|
+
next();
|
|
15
|
+
};
|
|
16
|
+
exports.validateRequest = validateRequest;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sn1006/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.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": "^5.0.1",
|
|
20
20
|
"typescript": "^5.2.2"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@types/cookie-session": "^2.0.44",
|
|
24
|
+
"@types/express": "^4.17.17",
|
|
25
|
+
"@types/jsonwebtoken": "^9.0.2",
|
|
26
|
+
"cookie-session": "^2.0.0",
|
|
27
|
+
"express": "^4.18.2",
|
|
28
|
+
"express-validator": "^7.0.1",
|
|
29
|
+
"jsonwebtoken": "^9.0.1"
|
|
21
30
|
}
|
|
22
31
|
}
|