@mondart/nestjs-common-module 1.1.44 → 1.1.45
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/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +1 -0
- package/dist/lib/saga/constant/index.d.ts +1 -0
- package/dist/lib/saga/constant/index.js +17 -0
- package/dist/lib/saga/constant/saga-handler.const.d.ts +1 -0
- package/dist/lib/saga/constant/saga-handler.const.js +4 -0
- package/dist/lib/saga/decorators/index.d.ts +1 -0
- package/dist/lib/saga/decorators/index.js +17 -0
- package/dist/lib/saga/decorators/saga.decorator.d.ts +4 -0
- package/dist/lib/saga/decorators/saga.decorator.js +15 -0
- package/dist/lib/saga/enums/index.d.ts +1 -0
- package/dist/lib/saga/enums/index.js +17 -0
- package/dist/lib/saga/enums/saga-status.enum.d.ts +8 -0
- package/dist/lib/saga/enums/saga-status.enum.js +12 -0
- package/dist/lib/saga/exceptions/index.d.ts +2 -0
- package/dist/lib/saga/exceptions/index.js +18 -0
- package/dist/lib/saga/exceptions/saga-compensation.exception.d.ts +5 -0
- package/dist/lib/saga/exceptions/saga-compensation.exception.js +13 -0
- package/dist/lib/saga/exceptions/saga-invocation.exception.d.ts +5 -0
- package/dist/lib/saga/exceptions/saga-invocation.exception.js +13 -0
- package/dist/lib/saga/helpers/index.d.ts +1 -0
- package/dist/lib/saga/helpers/index.js +17 -0
- package/dist/lib/saga/helpers/wrap-if-not-error.helper.d.ts +1 -0
- package/dist/lib/saga/helpers/wrap-if-not-error.helper.js +9 -0
- package/dist/lib/saga/index.d.ts +6 -0
- package/dist/lib/saga/index.js +14 -0
- package/dist/lib/saga/interfaces/index.d.ts +1 -0
- package/dist/lib/saga/interfaces/index.js +17 -0
- package/dist/lib/saga/interfaces/saga-module-register.interface.d.ts +6 -0
- package/dist/lib/saga/interfaces/saga-module-register.interface.js +3 -0
- package/dist/lib/saga/saga.module.d.ts +5 -0
- package/dist/lib/saga/saga.module.js +49 -0
- package/dist/lib/saga/services/index.d.ts +4 -0
- package/dist/lib/saga/services/index.js +20 -0
- package/dist/lib/saga/services/saga-builder.service.d.ts +8 -0
- package/dist/lib/saga/services/saga-builder.service.js +40 -0
- package/dist/lib/saga/services/saga-flow.service.d.ts +14 -0
- package/dist/lib/saga/services/saga-flow.service.js +39 -0
- package/dist/lib/saga/services/saga-step.service.d.ts +16 -0
- package/dist/lib/saga/services/saga-step.service.js +37 -0
- package/dist/lib/saga/services/saga.service.d.ts +11 -0
- package/dist/lib/saga/services/saga.service.js +44 -0
- package/dist/lib/saga/types/builder-method-return-result.type.d.ts +4 -0
- package/dist/lib/saga/types/builder-method-return-result.type.js +2 -0
- package/dist/lib/saga/types/builder-method-step-result.type.d.ts +7 -0
- package/dist/lib/saga/types/builder-method-step-result.type.js +2 -0
- package/dist/lib/saga/types/condition.type.d.ts +1 -0
- package/dist/lib/saga/types/condition.type.js +2 -0
- package/dist/lib/saga/types/index.d.ts +6 -0
- package/dist/lib/saga/types/index.js +22 -0
- package/dist/lib/saga/types/next-options.type.d.ts +12 -0
- package/dist/lib/saga/types/next-options.type.js +2 -0
- package/dist/lib/saga/types/saga-fn.type.d.ts +1 -0
- package/dist/lib/saga/types/saga-fn.type.js +2 -0
- package/dist/lib/saga/types/saga.type.d.ts +4 -0
- package/dist/lib/saga/types/saga.type.js +2 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
package/dist/lib/index.d.ts
CHANGED
package/dist/lib/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './saga-handler.const';
|
|
@@ -0,0 +1,17 @@
|
|
|
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("./saga-handler.const"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const __SAGA_HANDLER__ = "__SAGA_HANDLER__";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './saga.decorator';
|
|
@@ -0,0 +1,17 @@
|
|
|
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("./saga.decorator"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Saga = Saga;
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const saga_handler_const_1 = require("../constant/saga-handler.const");
|
|
7
|
+
function Saga(command) {
|
|
8
|
+
return (target) => {
|
|
9
|
+
target.prototype.execute = function execute(cmd) {
|
|
10
|
+
return this.saga.execute(cmd);
|
|
11
|
+
};
|
|
12
|
+
Reflect.defineMetadata(saga_handler_const_1.__SAGA_HANDLER__, command, target);
|
|
13
|
+
return (0, common_1.Injectable)({ scope: common_1.Scope.REQUEST })(target);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './saga-status.enum';
|
|
@@ -0,0 +1,17 @@
|
|
|
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("./saga-status.enum"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SagaStatus = void 0;
|
|
4
|
+
var SagaStatus;
|
|
5
|
+
(function (SagaStatus) {
|
|
6
|
+
SagaStatus["New"] = "New";
|
|
7
|
+
SagaStatus["InProgress"] = "In progress";
|
|
8
|
+
SagaStatus["InCompensation"] = "In compensation";
|
|
9
|
+
SagaStatus["Complete"] = "Complete";
|
|
10
|
+
SagaStatus["CompensationComplete"] = "Compensation complete";
|
|
11
|
+
SagaStatus["CompensationError"] = "Compensation error";
|
|
12
|
+
})(SagaStatus || (exports.SagaStatus = SagaStatus = {}));
|
|
@@ -0,0 +1,18 @@
|
|
|
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("./saga-invocation.exception"), exports);
|
|
18
|
+
__exportStar(require("./saga-compensation.exception"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SagaCompensationException = void 0;
|
|
4
|
+
class SagaCompensationException extends Error {
|
|
5
|
+
constructor(originalError, step) {
|
|
6
|
+
super(originalError.message);
|
|
7
|
+
this.originalError = originalError;
|
|
8
|
+
this.step = step;
|
|
9
|
+
Object.setPrototypeOf(this, SagaCompensationException.prototype);
|
|
10
|
+
this.stack = originalError.stack;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.SagaCompensationException = SagaCompensationException;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SagaInvocationException = void 0;
|
|
4
|
+
class SagaInvocationException extends Error {
|
|
5
|
+
constructor(originalError, step) {
|
|
6
|
+
super(originalError.message);
|
|
7
|
+
this.originalError = originalError;
|
|
8
|
+
this.step = step;
|
|
9
|
+
Object.setPrototypeOf(this, SagaInvocationException.prototype);
|
|
10
|
+
this.stack = originalError.stack;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.SagaInvocationException = SagaInvocationException;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './wrap-if-not-error.helper';
|
|
@@ -0,0 +1,17 @@
|
|
|
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("./wrap-if-not-error.helper"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function wrapIfNotError(x: unknown): Error;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { SagaModuleRegisterParams } from './interfaces/saga-module-register.interface';
|
|
2
|
+
export { SagaCompensationException, SagaInvocationException, } from './exceptions';
|
|
3
|
+
export { SagaStatus } from './enums';
|
|
4
|
+
export { SagaBuilder } from './services';
|
|
5
|
+
export { SagaModule } from './saga.module';
|
|
6
|
+
export { Saga } from './decorators';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Saga = exports.SagaModule = exports.SagaBuilder = exports.SagaStatus = exports.SagaInvocationException = exports.SagaCompensationException = void 0;
|
|
4
|
+
var exceptions_1 = require("./exceptions");
|
|
5
|
+
Object.defineProperty(exports, "SagaCompensationException", { enumerable: true, get: function () { return exceptions_1.SagaCompensationException; } });
|
|
6
|
+
Object.defineProperty(exports, "SagaInvocationException", { enumerable: true, get: function () { return exceptions_1.SagaInvocationException; } });
|
|
7
|
+
var enums_1 = require("./enums");
|
|
8
|
+
Object.defineProperty(exports, "SagaStatus", { enumerable: true, get: function () { return enums_1.SagaStatus; } });
|
|
9
|
+
var services_1 = require("./services");
|
|
10
|
+
Object.defineProperty(exports, "SagaBuilder", { enumerable: true, get: function () { return services_1.SagaBuilder; } });
|
|
11
|
+
var saga_module_1 = require("./saga.module");
|
|
12
|
+
Object.defineProperty(exports, "SagaModule", { enumerable: true, get: function () { return saga_module_1.SagaModule; } });
|
|
13
|
+
var decorators_1 = require("./decorators");
|
|
14
|
+
Object.defineProperty(exports, "Saga", { enumerable: true, get: function () { return decorators_1.Saga; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './saga-module-register.interface';
|
|
@@ -0,0 +1,17 @@
|
|
|
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("./saga-module-register.interface"), exports);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SagaModule = void 0;
|
|
13
|
+
const cqrs_1 = require("@nestjs/cqrs");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const saga_handler_const_1 = require("./constant/saga-handler.const");
|
|
16
|
+
class SagaModule {
|
|
17
|
+
static register(params) {
|
|
18
|
+
return {
|
|
19
|
+
imports: [cqrs_1.CqrsModule, ...(params.imports || [])],
|
|
20
|
+
module: SagaModule,
|
|
21
|
+
providers: [
|
|
22
|
+
...(params.providers || []),
|
|
23
|
+
...params.sagas.flatMap((saga) => {
|
|
24
|
+
const cmdType = Reflect.getMetadata(saga_handler_const_1.__SAGA_HANDLER__, saga);
|
|
25
|
+
let SagaCommandHandler = class SagaCommandHandler {
|
|
26
|
+
constructor(moduleRef) {
|
|
27
|
+
this.moduleRef = moduleRef;
|
|
28
|
+
}
|
|
29
|
+
async execute(cmd) {
|
|
30
|
+
const s = await this.moduleRef.create(saga);
|
|
31
|
+
s.saga.bind(s);
|
|
32
|
+
return s.execute(cmd);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
SagaCommandHandler = __decorate([
|
|
36
|
+
(0, cqrs_1.CommandHandler)(cmdType),
|
|
37
|
+
__metadata("design:paramtypes", [core_1.ModuleRef])
|
|
38
|
+
], SagaCommandHandler);
|
|
39
|
+
Object.defineProperty(SagaCommandHandler, 'name', {
|
|
40
|
+
writable: true,
|
|
41
|
+
value: [cmdType.name, 'SagaDispatcher'].join(''),
|
|
42
|
+
});
|
|
43
|
+
return [saga, SagaCommandHandler];
|
|
44
|
+
}),
|
|
45
|
+
],
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.SagaModule = SagaModule;
|
|
@@ -0,0 +1,20 @@
|
|
|
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("./saga-builder.service"), exports);
|
|
18
|
+
__exportStar(require("./saga-flow.service"), exports);
|
|
19
|
+
__exportStar(require("./saga-step.service"), exports);
|
|
20
|
+
__exportStar(require("./saga.service"), exports);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SagaBuilder = void 0;
|
|
4
|
+
const saga_step_service_1 = require("./saga-step.service");
|
|
5
|
+
const saga_service_1 = require("./saga.service");
|
|
6
|
+
const saga_flow_service_1 = require("./saga-flow.service");
|
|
7
|
+
class SagaBuilder {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.steps = [];
|
|
10
|
+
this.returnFn = (() => void 0);
|
|
11
|
+
}
|
|
12
|
+
step(name = '') {
|
|
13
|
+
return {
|
|
14
|
+
invoke: (method) => {
|
|
15
|
+
const step = new saga_step_service_1.Step(name, this.steps.length, method);
|
|
16
|
+
this.steps.push(step);
|
|
17
|
+
const nextOptions = {
|
|
18
|
+
step: (name = '') => this.step(name),
|
|
19
|
+
return: (method) => this.return(method),
|
|
20
|
+
build: () => this.build(),
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
...nextOptions,
|
|
24
|
+
withCompensation: (method) => {
|
|
25
|
+
step.setCompensation(method);
|
|
26
|
+
return nextOptions;
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return(method) {
|
|
33
|
+
this.returnFn = method;
|
|
34
|
+
return { build: () => this.build() };
|
|
35
|
+
}
|
|
36
|
+
build() {
|
|
37
|
+
return new saga_service_1.SagaCore(new saga_flow_service_1.SagaFlow(this.steps, this.returnFn));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.SagaBuilder = SagaBuilder;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Step } from './saga-step.service';
|
|
2
|
+
import { Fn } from '../types';
|
|
3
|
+
export declare class SagaFlow<T, R> {
|
|
4
|
+
private readonly steps;
|
|
5
|
+
private returnFn;
|
|
6
|
+
private readonly compensationSteps;
|
|
7
|
+
private __current;
|
|
8
|
+
private __returnFnName;
|
|
9
|
+
constructor(steps: Step<T>[], returnFn: Fn<T, R>);
|
|
10
|
+
get current(): Step<T>;
|
|
11
|
+
invoke(params: T): Promise<R>;
|
|
12
|
+
compensate(params: T): Promise<void>;
|
|
13
|
+
bind(ctx: unknown): this;
|
|
14
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SagaFlow = void 0;
|
|
4
|
+
const saga_step_service_1 = require("./saga-step.service");
|
|
5
|
+
class SagaFlow {
|
|
6
|
+
constructor(steps, returnFn) {
|
|
7
|
+
this.steps = steps;
|
|
8
|
+
this.returnFn = returnFn;
|
|
9
|
+
this.compensationSteps = [];
|
|
10
|
+
this.__returnFnName = returnFn.name || 'return';
|
|
11
|
+
}
|
|
12
|
+
get current() {
|
|
13
|
+
return this.__current;
|
|
14
|
+
}
|
|
15
|
+
async invoke(params) {
|
|
16
|
+
for (const step of this.steps) {
|
|
17
|
+
this.__current = step;
|
|
18
|
+
await step.invoke(params);
|
|
19
|
+
this.compensationSteps.push(step);
|
|
20
|
+
}
|
|
21
|
+
const step = new saga_step_service_1.Step(this.__returnFnName, this.steps.length, this.returnFn);
|
|
22
|
+
this.__current = step;
|
|
23
|
+
return step.invoke(params);
|
|
24
|
+
}
|
|
25
|
+
async compensate(params) {
|
|
26
|
+
for (const step of this.compensationSteps.reverse()) {
|
|
27
|
+
if (!step.hasCompensation)
|
|
28
|
+
continue;
|
|
29
|
+
this.__current = step;
|
|
30
|
+
await step.compensate(params);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
bind(ctx) {
|
|
34
|
+
this.steps.forEach((step) => step.bind(ctx));
|
|
35
|
+
this.returnFn = this.returnFn.bind(ctx);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.SagaFlow = SagaFlow;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Fn } from '../types';
|
|
2
|
+
export declare class Step<T, R = void> {
|
|
3
|
+
private stepName;
|
|
4
|
+
private index;
|
|
5
|
+
invocation: Fn<T, R>;
|
|
6
|
+
private __compensate?;
|
|
7
|
+
private compensationName;
|
|
8
|
+
private finalName;
|
|
9
|
+
constructor(stepName: string, index: number, invocation: Fn<T, R>);
|
|
10
|
+
get name(): string;
|
|
11
|
+
get hasCompensation(): boolean;
|
|
12
|
+
invoke(params: T): Promise<R>;
|
|
13
|
+
compensate(i: T): Promise<R> | R;
|
|
14
|
+
setCompensation(value: Fn<T, R>): void;
|
|
15
|
+
bind(ctx: unknown): this;
|
|
16
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Step = void 0;
|
|
4
|
+
class Step {
|
|
5
|
+
constructor(stepName, index, invocation) {
|
|
6
|
+
this.stepName = stepName;
|
|
7
|
+
this.index = index;
|
|
8
|
+
this.invocation = invocation;
|
|
9
|
+
this.compensationName = '';
|
|
10
|
+
this.finalName = '';
|
|
11
|
+
this.finalName = this.stepName || invocation.name || `step${index}`;
|
|
12
|
+
}
|
|
13
|
+
get name() {
|
|
14
|
+
return this.finalName;
|
|
15
|
+
}
|
|
16
|
+
get hasCompensation() {
|
|
17
|
+
return !!this.__compensate;
|
|
18
|
+
}
|
|
19
|
+
async invoke(params) {
|
|
20
|
+
return this.invocation(params);
|
|
21
|
+
}
|
|
22
|
+
compensate(i) {
|
|
23
|
+
this.finalName = this.compensationName;
|
|
24
|
+
return this.__compensate(i);
|
|
25
|
+
}
|
|
26
|
+
setCompensation(value) {
|
|
27
|
+
this.compensationName = this.stepName || value.name || `step${this.index}`;
|
|
28
|
+
this.__compensate = value;
|
|
29
|
+
}
|
|
30
|
+
bind(ctx) {
|
|
31
|
+
this.invocation = this.invocation.bind(ctx);
|
|
32
|
+
if (this.__compensate)
|
|
33
|
+
this.__compensate = this.__compensate.bind(ctx);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.Step = Step;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SagaStatus } from '../enums';
|
|
2
|
+
import { SagaFlow } from './saga-flow.service';
|
|
3
|
+
export declare class SagaCore<T, R> {
|
|
4
|
+
private sagaFlow;
|
|
5
|
+
private __status;
|
|
6
|
+
constructor(sagaFlow: SagaFlow<T, R>);
|
|
7
|
+
get status(): SagaStatus;
|
|
8
|
+
execute(params: T): Promise<R>;
|
|
9
|
+
bind(ctx: unknown): this;
|
|
10
|
+
private runCompensationFlow;
|
|
11
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SagaCore = void 0;
|
|
4
|
+
const enums_1 = require("../enums");
|
|
5
|
+
const exceptions_1 = require("../exceptions");
|
|
6
|
+
const helpers_1 = require("../helpers");
|
|
7
|
+
class SagaCore {
|
|
8
|
+
constructor(sagaFlow) {
|
|
9
|
+
this.sagaFlow = sagaFlow;
|
|
10
|
+
this.__status = enums_1.SagaStatus.New;
|
|
11
|
+
}
|
|
12
|
+
get status() {
|
|
13
|
+
return this.__status;
|
|
14
|
+
}
|
|
15
|
+
async execute(params) {
|
|
16
|
+
this.__status = enums_1.SagaStatus.InProgress;
|
|
17
|
+
try {
|
|
18
|
+
const result = await this.sagaFlow.invoke(params);
|
|
19
|
+
this.__status = enums_1.SagaStatus.Complete;
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
const invokeErrStepName = this.sagaFlow.current.name;
|
|
24
|
+
this.__status = enums_1.SagaStatus.InCompensation;
|
|
25
|
+
await this.runCompensationFlow(params);
|
|
26
|
+
throw new exceptions_1.SagaInvocationException((0, helpers_1.wrapIfNotError)(e), invokeErrStepName);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
bind(ctx) {
|
|
30
|
+
this.sagaFlow.bind(ctx);
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
async runCompensationFlow(params) {
|
|
34
|
+
try {
|
|
35
|
+
await this.sagaFlow.compensate(params);
|
|
36
|
+
this.__status = enums_1.SagaStatus.CompensationComplete;
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
this.__status = enums_1.SagaStatus.CompensationError;
|
|
40
|
+
throw new exceptions_1.SagaCompensationException((0, helpers_1.wrapIfNotError)(e), this.sagaFlow.current.name);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.SagaCore = SagaCore;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Condition<IF, THEN, ELSE> = IF extends void ? ELSE : THEN;
|