@dnlycticket/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/base-error.d.ts +8 -0
- package/build/errors/base-error.js +28 -0
- package/build/errors/base-request-error.d.ts +9 -0
- package/build/errors/base-request-error.js +34 -0
- package/build/errors/database-connect-error.d.ts +9 -0
- package/build/errors/database-connect-error.js +34 -0
- package/build/errors/not-authorized-error.d.ts +8 -0
- package/build/errors/not-authorized-error.js +33 -0
- package/build/errors/not-found-error.d.ts +9 -0
- package/build/errors/not-found-error.js +34 -0
- package/build/errors/request-validation-error.d.ts +11 -0
- package/build/errors/request-validation-error.js +41 -0
- package/build/index.d.ts +9 -7
- package/build/index.js +23 -9
- package/build/middlewares/current-user.d.ts +14 -0
- package/build/middlewares/current-user.js +21 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +16 -0
- package/build/middlewares/require-auth.d.ts +2 -0
- package/build/middlewares/require-auth.js +10 -0
- package/build/middlewares/validate-request.d.ts +2 -0
- package/build/middlewares/validate-request.js +14 -0
- package/package.json +13 -2
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.BaseError = void 0;
|
|
19
|
+
var BaseError = /** @class */ (function (_super) {
|
|
20
|
+
__extends(BaseError, _super);
|
|
21
|
+
function BaseError() {
|
|
22
|
+
var _this = _super.call(this) || this;
|
|
23
|
+
Object.setPrototypeOf(_this, BaseError.prototype);
|
|
24
|
+
return _this;
|
|
25
|
+
}
|
|
26
|
+
return BaseError;
|
|
27
|
+
}(Error));
|
|
28
|
+
exports.BaseError = BaseError;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.BadRequestError = void 0;
|
|
19
|
+
var base_error_1 = require("./base-error");
|
|
20
|
+
var BadRequestError = /** @class */ (function (_super) {
|
|
21
|
+
__extends(BadRequestError, _super);
|
|
22
|
+
function BadRequestError(reason) {
|
|
23
|
+
var _this = _super.call(this) || this;
|
|
24
|
+
_this.reason = reason;
|
|
25
|
+
_this.statusCode = 400;
|
|
26
|
+
Object.setPrototypeOf(_this, BadRequestError.prototype);
|
|
27
|
+
return _this;
|
|
28
|
+
}
|
|
29
|
+
BadRequestError.prototype.serializeErrors = function () {
|
|
30
|
+
return [{ message: this.reason }];
|
|
31
|
+
};
|
|
32
|
+
return BadRequestError;
|
|
33
|
+
}(base_error_1.BaseError));
|
|
34
|
+
exports.BadRequestError = BadRequestError;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.DatabaseConnectError = void 0;
|
|
19
|
+
var base_error_1 = require("./base-error");
|
|
20
|
+
var DatabaseConnectError = /** @class */ (function (_super) {
|
|
21
|
+
__extends(DatabaseConnectError, _super);
|
|
22
|
+
function DatabaseConnectError() {
|
|
23
|
+
var _this = _super.call(this) || this;
|
|
24
|
+
_this.statusCode = 400;
|
|
25
|
+
_this.reason = 'Error connect to database';
|
|
26
|
+
Object.setPrototypeOf(_this, DatabaseConnectError.prototype);
|
|
27
|
+
return _this;
|
|
28
|
+
}
|
|
29
|
+
DatabaseConnectError.prototype.serializeErrors = function () {
|
|
30
|
+
return [{ message: this.reason }];
|
|
31
|
+
};
|
|
32
|
+
return DatabaseConnectError;
|
|
33
|
+
}(base_error_1.BaseError));
|
|
34
|
+
exports.DatabaseConnectError = DatabaseConnectError;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.NotAuthorizedError = void 0;
|
|
19
|
+
var base_error_1 = require("./base-error");
|
|
20
|
+
var NotAuthorizedError = /** @class */ (function (_super) {
|
|
21
|
+
__extends(NotAuthorizedError, _super);
|
|
22
|
+
function NotAuthorizedError() {
|
|
23
|
+
var _this = _super.call(this) || this;
|
|
24
|
+
_this.statusCode = 401;
|
|
25
|
+
Object.setPrototypeOf(_this, NotAuthorizedError.prototype);
|
|
26
|
+
return _this;
|
|
27
|
+
}
|
|
28
|
+
NotAuthorizedError.prototype.serializeErrors = function () {
|
|
29
|
+
return [{ message: 'Not authorized' }];
|
|
30
|
+
};
|
|
31
|
+
return NotAuthorizedError;
|
|
32
|
+
}(base_error_1.BaseError));
|
|
33
|
+
exports.NotAuthorizedError = NotAuthorizedError;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.NotFindError = void 0;
|
|
19
|
+
var base_error_1 = require("./base-error");
|
|
20
|
+
var NotFindError = /** @class */ (function (_super) {
|
|
21
|
+
__extends(NotFindError, _super);
|
|
22
|
+
function NotFindError() {
|
|
23
|
+
var _this = _super.call(this) || this;
|
|
24
|
+
_this.statusCode = 400;
|
|
25
|
+
_this.reason = 'page not exit';
|
|
26
|
+
Object.setPrototypeOf(_this, NotFindError.prototype);
|
|
27
|
+
return _this;
|
|
28
|
+
}
|
|
29
|
+
NotFindError.prototype.serializeErrors = function () {
|
|
30
|
+
return [{ message: this.reason }];
|
|
31
|
+
};
|
|
32
|
+
return NotFindError;
|
|
33
|
+
}(base_error_1.BaseError));
|
|
34
|
+
exports.NotFindError = NotFindError;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ValidationError } from 'express-validator';
|
|
2
|
+
import { BaseError } from './base-error';
|
|
3
|
+
export declare class RequestValidationError extends BaseError {
|
|
4
|
+
private errors;
|
|
5
|
+
constructor(errors: ValidationError[]);
|
|
6
|
+
statusCode: number;
|
|
7
|
+
serializeErrors(): {
|
|
8
|
+
message: any;
|
|
9
|
+
field: string;
|
|
10
|
+
}[];
|
|
11
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.RequestValidationError = void 0;
|
|
19
|
+
var base_error_1 = require("./base-error");
|
|
20
|
+
var RequestValidationError = /** @class */ (function (_super) {
|
|
21
|
+
__extends(RequestValidationError, _super);
|
|
22
|
+
function RequestValidationError(errors) {
|
|
23
|
+
var _this = _super.call(this) || this;
|
|
24
|
+
_this.errors = errors;
|
|
25
|
+
_this.statusCode = 400;
|
|
26
|
+
// 这行代码的作用就是: 强制将当前实例(this)的原型重新指向 RequestValidationError 的原型。
|
|
27
|
+
// ES5环境下
|
|
28
|
+
// const error = new RequestValidationError(errors);
|
|
29
|
+
// console.log(error instanceof RequestValidationError); // false
|
|
30
|
+
// console.log(error instanceof Error); // true
|
|
31
|
+
Object.setPrototypeOf(_this, RequestValidationError.prototype);
|
|
32
|
+
return _this;
|
|
33
|
+
}
|
|
34
|
+
RequestValidationError.prototype.serializeErrors = function () {
|
|
35
|
+
return this.errors.map(function (err) {
|
|
36
|
+
return { message: err.msg, field: err.type === 'field' ? err.path : '' };
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
return RequestValidationError;
|
|
40
|
+
}(base_error_1.BaseError));
|
|
41
|
+
exports.RequestValidationError = RequestValidationError;
|
package/build/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export
|
|
1
|
+
export * from './errors/base-request-error';
|
|
2
|
+
export * from './errors/database-connect-error';
|
|
3
|
+
export * from './errors/not-authorized-error';
|
|
4
|
+
export * from './errors/not-found-error';
|
|
5
|
+
export * from './errors/request-validation-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,11 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Object.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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);
|
|
9
15
|
};
|
|
10
|
-
|
|
11
|
-
exports
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./errors/base-request-error"), exports);
|
|
18
|
+
__exportStar(require("./errors/database-connect-error"), exports);
|
|
19
|
+
__exportStar(require("./errors/not-authorized-error"), exports);
|
|
20
|
+
__exportStar(require("./errors/not-found-error"), exports);
|
|
21
|
+
__exportStar(require("./errors/request-validation-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,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
|
+
exports.currentUser = void 0;
|
|
7
|
+
var jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
var currentUser = function (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
|
+
var payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
|
|
15
|
+
req.currentUser = payload;
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
}
|
|
19
|
+
next();
|
|
20
|
+
};
|
|
21
|
+
exports.currentUser = currentUser;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorHandler = void 0;
|
|
4
|
+
var base_error_1 = require("../errors/base-error");
|
|
5
|
+
var errorHandler = function (err, req, res, next) {
|
|
6
|
+
// 检查错误是否继承自我们的抽象类 CustomError
|
|
7
|
+
if (err instanceof base_error_1.BaseError) {
|
|
8
|
+
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
9
|
+
}
|
|
10
|
+
// 处理意料之外的错误(如语法错误、断网等)
|
|
11
|
+
console.error('Unexpected error:', err);
|
|
12
|
+
res.status(500).send({
|
|
13
|
+
errors: [{ message: 'Something went wrong' }]
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
exports.errorHandler = errorHandler;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireAuth = void 0;
|
|
4
|
+
var requireAuth = function (req, res, next) {
|
|
5
|
+
if (!req.currentUser) {
|
|
6
|
+
// throw new NotAuthorizedError();
|
|
7
|
+
}
|
|
8
|
+
next();
|
|
9
|
+
};
|
|
10
|
+
exports.requireAuth = requireAuth;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateRequest = void 0;
|
|
4
|
+
var express_validator_1 = require("express-validator");
|
|
5
|
+
var request_validation_error_1 = require("../errors/request-validation-error");
|
|
6
|
+
var validateRequest = function (req, res, next) {
|
|
7
|
+
var errors = (0, express_validator_1.validationResult)(req);
|
|
8
|
+
// 中间件抛出异常
|
|
9
|
+
if (!errors.isEmpty()) {
|
|
10
|
+
throw new request_validation_error_1.RequestValidationError(errors.array());
|
|
11
|
+
}
|
|
12
|
+
next();
|
|
13
|
+
};
|
|
14
|
+
exports.validateRequest = validateRequest;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dnlycticket/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/"
|
|
6
6
|
},
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
13
|
"clean": "rimraf build",
|
|
14
|
-
"build": "npm run clean && tsc"
|
|
14
|
+
"build": "npm run clean && tsc",
|
|
15
|
+
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
|
|
15
16
|
},
|
|
16
17
|
"keywords": [],
|
|
17
18
|
"author": "",
|
|
@@ -21,5 +22,15 @@
|
|
|
21
22
|
"del-cli": "^7.0.0",
|
|
22
23
|
"rimraf": "^5.0.10",
|
|
23
24
|
"typescript": "^5.9.3"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@types/cookie-session": "^2.0.49",
|
|
28
|
+
"@types/express": "^5.0.6",
|
|
29
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
30
|
+
"cookie-session": "^2.1.1",
|
|
31
|
+
"express": "^5.2.1",
|
|
32
|
+
"express-validator": "^7.3.1",
|
|
33
|
+
"jsonwebtoken": "^9.0.3",
|
|
34
|
+
"package": "^1.0.1"
|
|
24
35
|
}
|
|
25
36
|
}
|