@wrcb/cb-common 1.0.0 → 1.0.2
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/badRequestError.d.ts +9 -0
- package/build/errors/badRequestError.js +16 -0
- package/build/errors/customError.d.ts +8 -0
- package/build/errors/customError.js +10 -0
- package/build/errors/databaseConnectionError.d.ts +9 -0
- package/build/errors/databaseConnectionError.js +18 -0
- package/build/errors/notAuthorizedError.d.ts +8 -0
- package/build/errors/notAuthorizedError.js +15 -0
- package/build/errors/notFoundError.d.ts +8 -0
- package/build/errors/notFoundError.js +15 -0
- package/build/errors/requestValidationError.d.ts +11 -0
- package/build/errors/requestValidationError.js +21 -0
- package/build/index.d.ts +10 -0
- package/build/index.js +26 -0
- package/build/middlewares/currentUser.d.ts +14 -0
- package/build/middlewares/currentUser.js +24 -0
- package/build/middlewares/errorHandler.d.ts +2 -0
- package/build/middlewares/errorHandler.js +15 -0
- package/build/middlewares/requireAuth.d.ts +2 -0
- package/build/middlewares/requireAuth.js +11 -0
- package/build/middlewares/validateRequest.d.ts +2 -0
- package/build/middlewares/validateRequest.js +13 -0
- package/package.json +24 -4
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BadRequestError = void 0;
|
|
4
|
+
const customError_1 = require("./customError");
|
|
5
|
+
class BadRequestError extends customError_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,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseConnectionError = void 0;
|
|
4
|
+
const customError_1 = require("./customError");
|
|
5
|
+
class DatabaseConnectionError extends customError_1.CustomError {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Error connecting to database"); // apenas para logging pourpouses
|
|
8
|
+
this.statusCode = 500;
|
|
9
|
+
this.reason = 'Error connecting to database';
|
|
10
|
+
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [
|
|
14
|
+
{ message: this.reason }
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAuthorizedError = void 0;
|
|
4
|
+
const customError_1 = require("./customError");
|
|
5
|
+
class NotAuthorizedError extends customError_1.CustomError {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.statusCode = 401;
|
|
9
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serializeErrors() {
|
|
12
|
+
return [{ message: this.message }];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.NotAuthorizedError = NotAuthorizedError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotFoundError = void 0;
|
|
4
|
+
const customError_1 = require("./customError");
|
|
5
|
+
class NotFoundError extends customError_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,11 @@
|
|
|
1
|
+
import { ValidationError } from 'express-validator';
|
|
2
|
+
import { CustomError } from './customError';
|
|
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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestValidationError = void 0;
|
|
4
|
+
const customError_1 = require("./customError");
|
|
5
|
+
class RequestValidationError extends customError_1.CustomError {
|
|
6
|
+
constructor(errors) {
|
|
7
|
+
super("Invalid request parameters"); // apenas para logging pourpouses
|
|
8
|
+
this.errors = errors;
|
|
9
|
+
this.statusCode = 400;
|
|
10
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return this.errors.map((error) => {
|
|
14
|
+
return {
|
|
15
|
+
message: error.msg,
|
|
16
|
+
field: error.type === 'field' ? error.path : '',
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.RequestValidationError = RequestValidationError;
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './errors/badRequestError';
|
|
2
|
+
export * from './errors/customError';
|
|
3
|
+
export * from './errors/databaseConnectionError';
|
|
4
|
+
export * from './errors/notAuthorizedError';
|
|
5
|
+
export * from './errors/notFoundError';
|
|
6
|
+
export * from './errors/requestValidationError';
|
|
7
|
+
export * from './middlewares/currentUser';
|
|
8
|
+
export * from './middlewares/errorHandler';
|
|
9
|
+
export * from './middlewares/requireAuth';
|
|
10
|
+
export * from './middlewares/validateRequest';
|
package/build/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
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/badRequestError"), exports);
|
|
18
|
+
__exportStar(require("./errors/customError"), exports);
|
|
19
|
+
__exportStar(require("./errors/databaseConnectionError"), exports);
|
|
20
|
+
__exportStar(require("./errors/notAuthorizedError"), exports);
|
|
21
|
+
__exportStar(require("./errors/notFoundError"), exports);
|
|
22
|
+
__exportStar(require("./errors/requestValidationError"), exports);
|
|
23
|
+
__exportStar(require("./middlewares/currentUser"), exports);
|
|
24
|
+
__exportStar(require("./middlewares/errorHandler"), exports);
|
|
25
|
+
__exportStar(require("./middlewares/requireAuth"), exports);
|
|
26
|
+
__exportStar(require("./middlewares/validateRequest"), 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,24 @@
|
|
|
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
|
+
// vamos apenas descobrir se o usuario esta logado. se estiver, vamos extrair o payload
|
|
11
|
+
// e setar no currentUser property
|
|
12
|
+
// se não tiver logado, outro middleware irá retornar um erro
|
|
13
|
+
console.log(('-----------------'));
|
|
14
|
+
if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
|
|
15
|
+
return next();
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
|
|
19
|
+
req.currentUser = payload;
|
|
20
|
+
}
|
|
21
|
+
catch (error) { }
|
|
22
|
+
next();
|
|
23
|
+
};
|
|
24
|
+
exports.currentUser = currentUser;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorHandler = void 0;
|
|
4
|
+
const customError_1 = require("../errors/customError");
|
|
5
|
+
const errorHandler = (err, req, res, next) => {
|
|
6
|
+
if (err instanceof customError_1.CustomError) {
|
|
7
|
+
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
8
|
+
}
|
|
9
|
+
console.error(err);
|
|
10
|
+
res.status(400).send({ errors: [
|
|
11
|
+
{ message: 'Something went wrong' }
|
|
12
|
+
]
|
|
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 notAuthorizedError_1 = require("../errors/notAuthorizedError");
|
|
5
|
+
const requireAuth = (req, res, next) => {
|
|
6
|
+
if (!req.currentUser) {
|
|
7
|
+
throw new notAuthorizedError_1.NotAuthorizedError('Not Authorized');
|
|
8
|
+
}
|
|
9
|
+
next();
|
|
10
|
+
};
|
|
11
|
+
exports.requireAuth = requireAuth;
|
|
@@ -0,0 +1,13 @@
|
|
|
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 requestValidationError_1 = require("../errors/requestValidationError");
|
|
6
|
+
const validateRequest = (req, res, next) => {
|
|
7
|
+
const errors = (0, express_validator_1.validationResult)(req);
|
|
8
|
+
if (!errors.isEmpty()) {
|
|
9
|
+
throw new requestValidationError_1.RequestValidationError(errors.array());
|
|
10
|
+
}
|
|
11
|
+
next();
|
|
12
|
+
};
|
|
13
|
+
exports.validateRequest = validateRequest;
|
package/package.json
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wrcb/cb-common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Common resources between services",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "./build/index.js",
|
|
6
|
+
"types": "./build/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"build/**/*"
|
|
9
|
+
],
|
|
6
10
|
"scripts": {
|
|
7
|
-
"
|
|
11
|
+
"clean": "rimraf ./build",
|
|
12
|
+
"build": "npm run clean && tsc",
|
|
13
|
+
"pub": "git add . && git commit -m \"updates\" && npm version patch && npm run build && npm publish"
|
|
8
14
|
},
|
|
9
15
|
"author": "Willian Rios",
|
|
10
|
-
"license": "ISC"
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"rimraf": "^5.0.7",
|
|
19
|
+
"typescript": "^5.4.5"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@types/cookie-session": "^2.0.49",
|
|
23
|
+
"@types/express": "^4.17.21",
|
|
24
|
+
"@types/jsonwebtoken": "^9.0.6",
|
|
25
|
+
"cookie-session": "^2.1.0",
|
|
26
|
+
"express": "^4.19.2",
|
|
27
|
+
"express-validator": "^7.1.0",
|
|
28
|
+
"jsonwebtoken": "^9.0.2",
|
|
29
|
+
"node-nats-streaming": "^0.3.2"
|
|
30
|
+
}
|
|
11
31
|
}
|