@common_ch/common 1.0.0 → 1.0.5
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 +11 -0
- package/build/errors/bad-request-error.js +17 -0
- package/build/errors/custom-error.d.ts +23 -0
- package/build/errors/custom-error.js +11 -0
- package/build/errors/not-authorized-error.d.ts +10 -0
- package/build/errors/not-authorized-error.js +16 -0
- package/build/errors/notfound.d.ts +10 -0
- package/build/errors/notfound.js +19 -0
- package/build/errors/request-validation-error.d.ts +22 -0
- package/build/errors/request-validation-error.js +29 -0
- package/build/errors/user-not-found-error.d.ts +14 -0
- package/build/errors/user-not-found-error.js +24 -0
- package/build/index.d.ts +9 -0
- package/build/index.js +25 -0
- package/build/middlewares/current-user.d.ts +16 -0
- package/build/middlewares/current-user.js +20 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +17 -0
- package/build/middlewares/require-auth.d.ts +2 -0
- package/build/middlewares/require-auth.js +11 -0
- package/package.json +23 -4
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CustomError } from './custom-error';
|
|
2
|
+
export declare class BadRequestError extends CustomError {
|
|
3
|
+
message: string;
|
|
4
|
+
statusCode: number;
|
|
5
|
+
name: string;
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
serializeErrors(): {
|
|
8
|
+
message: string;
|
|
9
|
+
name: string;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
this.name = 'bad_request_error';
|
|
11
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
12
|
+
}
|
|
13
|
+
serializeErrors() {
|
|
14
|
+
return { message: this.message, name: this.name };
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.BadRequestError = BadRequestError;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare const optionsValidation: {
|
|
2
|
+
abortEarly: boolean;
|
|
3
|
+
allowUnknown: boolean;
|
|
4
|
+
stripUnknown: boolean;
|
|
5
|
+
};
|
|
6
|
+
export interface ValidationError {
|
|
7
|
+
message: string;
|
|
8
|
+
type: string;
|
|
9
|
+
}
|
|
10
|
+
export interface JoiError {
|
|
11
|
+
original: unknown;
|
|
12
|
+
details: ValidationError[];
|
|
13
|
+
}
|
|
14
|
+
export declare abstract class CustomError extends Error {
|
|
15
|
+
abstract statusCode: number;
|
|
16
|
+
abstract name: string;
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
abstract serializeErrors(): {
|
|
19
|
+
message: string;
|
|
20
|
+
name: string;
|
|
21
|
+
errors?: JoiError;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomError = exports.optionsValidation = void 0;
|
|
4
|
+
exports.optionsValidation = { abortEarly: false, allowUnknown: false, stripUnknown: false };
|
|
5
|
+
class CustomError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
Object.setPrototypeOf(this, CustomError.prototype);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.CustomError = CustomError;
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
this.name = 'not-authorized-error';
|
|
10
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return { message: 'Not authorized', name: this.name };
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.NotAuthorizedError = NotAuthorizedError;
|
|
@@ -0,0 +1,19 @@
|
|
|
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('Rout Not Found');
|
|
8
|
+
this.statusCode = 404;
|
|
9
|
+
this.name = 'rout_not_found';
|
|
10
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return {
|
|
14
|
+
message: 'Rout Not Found',
|
|
15
|
+
name: 'rout_not_found',
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.NotFoundError = NotFoundError;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ValidationError } from 'joi';
|
|
2
|
+
import { Response } from 'express';
|
|
3
|
+
import { CustomError } from './custom-error';
|
|
4
|
+
export declare class RequestValidationError extends CustomError {
|
|
5
|
+
errors: ValidationError;
|
|
6
|
+
private res;
|
|
7
|
+
statusCode: number;
|
|
8
|
+
name: string;
|
|
9
|
+
constructor(errors: ValidationError, res: Response);
|
|
10
|
+
serializeErrors(): {
|
|
11
|
+
message: string;
|
|
12
|
+
name: string;
|
|
13
|
+
errors: {
|
|
14
|
+
original: any;
|
|
15
|
+
details: {
|
|
16
|
+
message: string;
|
|
17
|
+
type: string;
|
|
18
|
+
field: string;
|
|
19
|
+
}[];
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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, res) {
|
|
7
|
+
super('validation error');
|
|
8
|
+
this.errors = errors;
|
|
9
|
+
this.res = res;
|
|
10
|
+
this.statusCode = 400;
|
|
11
|
+
this.name = 'validation_error';
|
|
12
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
13
|
+
}
|
|
14
|
+
serializeErrors() {
|
|
15
|
+
return {
|
|
16
|
+
message: 'request validation error',
|
|
17
|
+
name: this.name,
|
|
18
|
+
errors: {
|
|
19
|
+
original: this.errors._original,
|
|
20
|
+
details: this.errors.details.map(({ message, type, context, path }) => ({
|
|
21
|
+
message: message.replace(/['"]/g, ''),
|
|
22
|
+
type,
|
|
23
|
+
field: path.toString(),
|
|
24
|
+
})),
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.RequestValidationError = RequestValidationError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CustomError } from './custom-error';
|
|
2
|
+
export declare enum NotFoundCustomErrorEnum {
|
|
3
|
+
userNotFound = "user_not_found"
|
|
4
|
+
}
|
|
5
|
+
export declare class NotFoundCustomError extends CustomError {
|
|
6
|
+
message: string;
|
|
7
|
+
name: string;
|
|
8
|
+
statusCode: number;
|
|
9
|
+
constructor(message: string, name: string);
|
|
10
|
+
serializeErrors(): {
|
|
11
|
+
message: string;
|
|
12
|
+
name: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotFoundCustomError = exports.NotFoundCustomErrorEnum = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
var NotFoundCustomErrorEnum;
|
|
6
|
+
(function (NotFoundCustomErrorEnum) {
|
|
7
|
+
NotFoundCustomErrorEnum["userNotFound"] = "user_not_found";
|
|
8
|
+
})(NotFoundCustomErrorEnum || (exports.NotFoundCustomErrorEnum = NotFoundCustomErrorEnum = {}));
|
|
9
|
+
class NotFoundCustomError extends custom_error_1.CustomError {
|
|
10
|
+
constructor(message, name) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.message = message;
|
|
13
|
+
this.name = name;
|
|
14
|
+
this.statusCode = 404;
|
|
15
|
+
Object.setPrototypeOf(this, NotFoundCustomError.prototype);
|
|
16
|
+
}
|
|
17
|
+
serializeErrors() {
|
|
18
|
+
return {
|
|
19
|
+
message: this.message,
|
|
20
|
+
name: this.name,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.NotFoundCustomError = NotFoundCustomError;
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './errors/bad-request-error';
|
|
2
|
+
export * from './errors/custom-error';
|
|
3
|
+
export * from './errors/not-authorized-error';
|
|
4
|
+
export * from './errors/notfound';
|
|
5
|
+
export * from './errors/request-validation-error';
|
|
6
|
+
export * from './errors/user-not-found-error';
|
|
7
|
+
export * from './middlewares/current-user';
|
|
8
|
+
export * from './middlewares/error-handler';
|
|
9
|
+
export * from './middlewares/require-auth';
|
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("./errors/bad-request-error"), exports);
|
|
18
|
+
__exportStar(require("./errors/custom-error"), exports);
|
|
19
|
+
__exportStar(require("./errors/not-authorized-error"), exports);
|
|
20
|
+
__exportStar(require("./errors/notfound"), exports);
|
|
21
|
+
__exportStar(require("./errors/request-validation-error"), exports);
|
|
22
|
+
__exportStar(require("./errors/user-not-found-error"), exports);
|
|
23
|
+
__exportStar(require("./middlewares/current-user"), exports);
|
|
24
|
+
__exportStar(require("./middlewares/error-handler"), exports);
|
|
25
|
+
__exportStar(require("./middlewares/require-auth"), exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
interface UserPayload {
|
|
3
|
+
_id: string;
|
|
4
|
+
email: string;
|
|
5
|
+
username: string;
|
|
6
|
+
mobile: string;
|
|
7
|
+
}
|
|
8
|
+
declare global {
|
|
9
|
+
namespace Express {
|
|
10
|
+
interface Request {
|
|
11
|
+
currentUser?: UserPayload;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
|
|
16
|
+
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,17 @@
|
|
|
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
|
+
if (err instanceof custom_error_1.CustomError) {
|
|
7
|
+
console.error('custom error', err.serializeErrors());
|
|
8
|
+
res.status(err.statusCode).send(err.serializeErrors());
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
console.error('error! ', err);
|
|
12
|
+
res.status(500).send({
|
|
13
|
+
message: 'Something went wrong!',
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
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;
|
package/package.json
CHANGED
|
@@ -1,12 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common_ch/common",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"main": "index.js",
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"main": "./build/index.js",
|
|
5
|
+
"types": "./build/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"./build/**/*"
|
|
8
|
+
],
|
|
5
9
|
"scripts": {
|
|
6
|
-
"
|
|
10
|
+
"clean": "del ./build/*",
|
|
11
|
+
"build": "npm run clean && tsc",
|
|
12
|
+
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
|
|
7
13
|
},
|
|
8
14
|
"keywords": [],
|
|
9
15
|
"author": "",
|
|
10
16
|
"license": "ISC",
|
|
11
|
-
"description": ""
|
|
17
|
+
"description": "",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"del-cli": "^6.0.0",
|
|
20
|
+
"typescript": "^5.7.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.2",
|
|
28
|
+
"joi": "^17.13.3",
|
|
29
|
+
"jsonwebtoken": "^9.0.2"
|
|
30
|
+
}
|
|
12
31
|
}
|