@nestjs-mod/two-factor
Version:
Two factor module with an error filter, guard, controller, database migrations and rest-sdk for work with module from other nodejs appliaction
123 lines • 5.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TwoFactorError = exports.TwoFactorErrorMetadata = exports.TwoFactorErrorMetadataConstraint = exports.TWO_FACTOR_ERROR_ENUM_TITLES = exports.TwoFactorErrorEnum = void 0;
const tslib_1 = require("tslib");
const swagger_1 = require("@nestjs/swagger");
const nestjs_translates_1 = require("nestjs-translates");
var TwoFactorErrorEnum;
(function (TwoFactorErrorEnum) {
TwoFactorErrorEnum["COMMON"] = "TWO_FACTOR-000";
TwoFactorErrorEnum["TwoFactorCodeNotSet"] = "TWO-FACTOR-001";
TwoFactorErrorEnum["TwoFactorCodeWrongCode"] = "TWO-FACTOR-002";
TwoFactorErrorEnum["TwoFactorCodeIsOutdated"] = "TWO-FACTOR-003";
TwoFactorErrorEnum["TwoFactorCodePleaseWait30Seconds"] = "TWO-FACTOR-004";
TwoFactorErrorEnum["TwoFactorCodeIsUsed"] = "TWO-FACTOR-005";
})(TwoFactorErrorEnum || (exports.TwoFactorErrorEnum = TwoFactorErrorEnum = {}));
exports.TWO_FACTOR_ERROR_ENUM_TITLES = {
[TwoFactorErrorEnum.COMMON]: (0, nestjs_translates_1.getText)('Two factor error'),
[TwoFactorErrorEnum.TwoFactorCodeNotSet]: (0, nestjs_translates_1.getText)('Two factor code not set'),
[TwoFactorErrorEnum.TwoFactorCodeWrongCode]: (0, nestjs_translates_1.getText)('Wrong two factor code'),
[TwoFactorErrorEnum.TwoFactorCodeIsOutdated]: (0, nestjs_translates_1.getText)('Two factor code is outdated'),
[TwoFactorErrorEnum.TwoFactorCodePleaseWait30Seconds]: (0, nestjs_translates_1.getText)('Please wait 30 seconds'),
[TwoFactorErrorEnum.TwoFactorCodeIsUsed]: (0, nestjs_translates_1.getText)('Two-factor code has already been used'),
};
class TwoFactorErrorMetadataConstraint {
constructor(options) {
Object.assign(this, options);
}
}
exports.TwoFactorErrorMetadataConstraint = TwoFactorErrorMetadataConstraint;
tslib_1.__decorate([
(0, swagger_1.ApiProperty)({
type: String,
}),
tslib_1.__metadata("design:type", String)
], TwoFactorErrorMetadataConstraint.prototype, "name", void 0);
tslib_1.__decorate([
(0, swagger_1.ApiProperty)({
type: String,
}),
tslib_1.__metadata("design:type", String)
], TwoFactorErrorMetadataConstraint.prototype, "description", void 0);
class TwoFactorErrorMetadata {
constructor(options) {
Object.assign(this, options);
}
static fromClassValidatorTwoFactorErrors(errors) {
return errors?.map((error) => new TwoFactorErrorMetadata({
property: error.property,
constraints: Object.entries(error.constraints || {}).map(([key, value]) => new TwoFactorErrorMetadataConstraint({
name: key,
description: value,
})),
...(error.children?.length
? {
children: this.fromClassValidatorTwoFactorErrors(error.children),
}
: {}),
}));
}
}
exports.TwoFactorErrorMetadata = TwoFactorErrorMetadata;
tslib_1.__decorate([
(0, swagger_1.ApiProperty)({
type: String,
}),
tslib_1.__metadata("design:type", String)
], TwoFactorErrorMetadata.prototype, "property", void 0);
tslib_1.__decorate([
(0, swagger_1.ApiProperty)({
type: () => TwoFactorErrorMetadataConstraint,
isArray: true,
}),
tslib_1.__metadata("design:type", Array)
], TwoFactorErrorMetadata.prototype, "constraints", void 0);
tslib_1.__decorate([
(0, swagger_1.ApiPropertyOptional)({
type: () => TwoFactorErrorMetadata,
isArray: true,
}),
tslib_1.__metadata("design:type", Array)
], TwoFactorErrorMetadata.prototype, "children", void 0);
class TwoFactorError extends Error {
constructor(message, code, metadata) {
const messageAsCode = Boolean(message &&
Object.values(TwoFactorErrorEnum).includes(message));
const preparedCode = messageAsCode ? message : code;
const preparedMessage = messageAsCode && preparedCode
? exports.TWO_FACTOR_ERROR_ENUM_TITLES[preparedCode]
: message;
code = preparedCode || TwoFactorErrorEnum.COMMON;
message = preparedMessage || exports.TWO_FACTOR_ERROR_ENUM_TITLES[code];
super(message);
this.code = TwoFactorErrorEnum.COMMON;
this.code = code;
this.message = message;
this.metadata =
TwoFactorErrorMetadata.fromClassValidatorTwoFactorErrors(metadata);
}
}
exports.TwoFactorError = TwoFactorError;
tslib_1.__decorate([
(0, swagger_1.ApiProperty)({
type: String,
description: Object.entries(exports.TWO_FACTOR_ERROR_ENUM_TITLES)
.map(([key, value]) => `${value} (${key})`)
.join(', '),
example: exports.TWO_FACTOR_ERROR_ENUM_TITLES[TwoFactorErrorEnum.COMMON],
}),
tslib_1.__metadata("design:type", String)
], TwoFactorError.prototype, "message", void 0);
tslib_1.__decorate([
(0, swagger_1.ApiProperty)({
enum: TwoFactorErrorEnum,
enumName: 'TwoFactorErrorEnum',
example: TwoFactorErrorEnum.COMMON,
}),
tslib_1.__metadata("design:type", Object)
], TwoFactorError.prototype, "code", void 0);
tslib_1.__decorate([
(0, swagger_1.ApiPropertyOptional)({ type: TwoFactorErrorMetadata, isArray: true }),
tslib_1.__metadata("design:type", Array)
], TwoFactorError.prototype, "metadata", void 0);
//# sourceMappingURL=two-factor.errors.js.map