@shoppingapp.org/common 1.0.22
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 +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 +8 -0
- package/build/errors/database-connection-error.js +15 -0
- package/build/errors/not-authorized-error.d.ts +8 -0
- package/build/errors/not-authorized-error.js +15 -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 +11 -0
- package/build/errors/request-validation-error.js +18 -0
- package/build/index.d.ts +8 -0
- package/build/index.js +25 -0
- package/build/middlewares/current-user.d.ts +8 -0
- package/build/middlewares/current-user.js +27 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +12 -0
- package/build/middlewares/require-auth.d.ts +0 -0
- package/build/middlewares/require-auth.js +10 -0
- package/build/middlewares/validate-request.d.ts +2 -0
- package/build/middlewares/validate-request.js +22 -0
- package/package.json +32 -0
|
@@ -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
|
+
serialiseErrors() {
|
|
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,15 @@
|
|
|
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 the database');
|
|
8
|
+
this.statusCode = 500;
|
|
9
|
+
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serialiseErrors() {
|
|
12
|
+
return [{ message: 'Error connecting to the database' }];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAuthorizedError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
class NotAuthorizedError extends custom_error_1.CustomError {
|
|
6
|
+
constructor() {
|
|
7
|
+
super('not authorized');
|
|
8
|
+
this.statusCode = 401;
|
|
9
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serialiseErrors() {
|
|
12
|
+
return [{ message: 'not authorized' }];
|
|
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 custom_error_1 = require("./custom-error");
|
|
5
|
+
class NotFoundError extends custom_error_1.CustomError {
|
|
6
|
+
constructor() {
|
|
7
|
+
super('not found!');
|
|
8
|
+
this.statusCode = 404;
|
|
9
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
10
|
+
}
|
|
11
|
+
serialiseErrors() {
|
|
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 '../';
|
|
3
|
+
export declare class RequestValidationError extends CustomError {
|
|
4
|
+
errors: ValidationError[];
|
|
5
|
+
statusCode: number;
|
|
6
|
+
constructor(errors: ValidationError[]);
|
|
7
|
+
serialiseErrors(): {
|
|
8
|
+
message: any;
|
|
9
|
+
field: string;
|
|
10
|
+
}[];
|
|
11
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestValidationError = void 0;
|
|
4
|
+
const __1 = require("../");
|
|
5
|
+
class RequestValidationError extends __1.CustomError {
|
|
6
|
+
constructor(errors) {
|
|
7
|
+
super('Invalid request');
|
|
8
|
+
this.errors = errors;
|
|
9
|
+
this.statusCode = 400;
|
|
10
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serialiseErrors() {
|
|
13
|
+
return this.errors.map(error => {
|
|
14
|
+
return { message: error.msg, field: error.param };
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.RequestValidationError = RequestValidationError;
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './/middlewares/current-user.js';
|
|
2
|
+
export * from './/middlewares/error-handler.js';
|
|
3
|
+
export * from './/errors/bad-request-error.js';
|
|
4
|
+
export * from './/errors/custom-error.js';
|
|
5
|
+
export * from './/errors/database-connection-error.js';
|
|
6
|
+
export * from './/errors/not-authorized-error.js';
|
|
7
|
+
export * from './/errors/not-found-error.js';
|
|
8
|
+
export * from './/errors/request-validation-error.js';
|
package/build/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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(".//middlewares/current-user.js"), exports);
|
|
18
|
+
// export * from './/middlewares/require-auth.js';
|
|
19
|
+
__exportStar(require(".//middlewares/error-handler.js"), exports);
|
|
20
|
+
__exportStar(require(".//errors/bad-request-error.js"), exports);
|
|
21
|
+
__exportStar(require(".//errors/custom-error.js"), exports);
|
|
22
|
+
__exportStar(require(".//errors/database-connection-error.js"), exports);
|
|
23
|
+
__exportStar(require(".//errors/not-authorized-error.js"), exports);
|
|
24
|
+
__exportStar(require(".//errors/not-found-error.js"), exports);
|
|
25
|
+
__exportStar(require(".//errors/request-validation-error.js"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Response, Request, NextFunction } from 'express';
|
|
2
|
+
declare global {
|
|
3
|
+
interface Req extends Request {
|
|
4
|
+
session?: any;
|
|
5
|
+
currentUser: any;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export declare const currentUser: (jwt_key: string) => (req: Req, res: Response, next: NextFunction) => void;
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
;
|
|
9
|
+
const currentUser = (jwt_key) => {
|
|
10
|
+
return (req, res, next) => {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
|
|
13
|
+
return next();
|
|
14
|
+
}
|
|
15
|
+
;
|
|
16
|
+
try {
|
|
17
|
+
const payload = (jsonwebtoken_1.default.verify((_b = req.session) === null || _b === void 0 ? void 0 : _b.jwt, process.env.JWT_KEY));
|
|
18
|
+
req.currentUser = payload;
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
return next(err);
|
|
22
|
+
}
|
|
23
|
+
;
|
|
24
|
+
next();
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
exports.currentUser = currentUser;
|
|
@@ -0,0 +1,12 @@
|
|
|
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 errorHandler = (err, req, res, next) => {
|
|
6
|
+
console.log(err);
|
|
7
|
+
if (err instanceof custom_error_1.CustomError) {
|
|
8
|
+
return res.status(err.statusCode).json({ errors: err.serialiseErrors() });
|
|
9
|
+
}
|
|
10
|
+
res.status(500).send({ errors: [{ message: 'something went wrong' }] });
|
|
11
|
+
};
|
|
12
|
+
exports.errorHandler = errorHandler;
|
|
File without changes
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import { Request, Response, NextFunction } from 'express';
|
|
3
|
+
// import User from '../../src/models/user.js';
|
|
4
|
+
// import { NotAuthorizedError } from '../errors/not-authorized-error'
|
|
5
|
+
// export const requireAuth = async (req: Request, res: Response, next: NextFunction) => {
|
|
6
|
+
// if(!req.currentUser) throw new NotAuthorizedError()
|
|
7
|
+
// const user = await User.findById(req.currentUser?.userId);
|
|
8
|
+
// if(!user) throw new NotAuthorizedError()
|
|
9
|
+
// next()
|
|
10
|
+
// }
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.validateRequest = void 0;
|
|
13
|
+
const express_validator_1 = require("express-validator");
|
|
14
|
+
const request_validation_error_1 = require("../errors/request-validation-error");
|
|
15
|
+
const validateRequest = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
const errors = (0, express_validator_1.validationResult)(req);
|
|
17
|
+
if (!errors.isEmpty()) {
|
|
18
|
+
next(new request_validation_error_1.RequestValidationError(errors.array()));
|
|
19
|
+
}
|
|
20
|
+
next();
|
|
21
|
+
});
|
|
22
|
+
exports.validateRequest = validateRequest;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shoppingapp.org/common",
|
|
3
|
+
"version": "1.0.22",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "/build/index.js",
|
|
6
|
+
"types": "./build/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"build/**/*"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"clean": "del-cli build",
|
|
12
|
+
"build": "npm run clean && tsc",
|
|
13
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"express": "^4.18.1",
|
|
20
|
+
"express-validator": "^6.14.2",
|
|
21
|
+
"jsonwebtoken": "^8.5.1",
|
|
22
|
+
"mongoose": "^6.6.1",
|
|
23
|
+
"multer": "^1.4.5-lts.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/express": "^4.17.13",
|
|
27
|
+
"@types/jsonwebtoken": "^8.5.9",
|
|
28
|
+
"@types/multer": "^1.4.7",
|
|
29
|
+
"del-cli": "^5.0.0",
|
|
30
|
+
"typescript": "^6.0.3"
|
|
31
|
+
}
|
|
32
|
+
}
|