@serialized/serialized-client
Advanced tools
Comparing version 4.11.0 to 4.12.0
@@ -8,7 +8,11 @@ import { BaseClient, DomainEvent } from './'; | ||
} | ||
export interface CommitOptions { | ||
export interface AggregateRequest { | ||
tenantId?: string; | ||
aggregateId: AggregateId; | ||
expectedVersion: number; | ||
events: DomainEvent<any>[]; | ||
} | ||
export interface RecordEventOptions { | ||
export interface AggregateBulkRequest { | ||
tenantId?: string; | ||
batches: EventBatch[]; | ||
} | ||
@@ -29,9 +33,7 @@ export interface CreateAggregateOptions { | ||
} | ||
export interface AggregateRequest { | ||
aggregateId: AggregateId; | ||
} | ||
export interface DeleteAggregateResponse { | ||
deleteToken?: string; | ||
} | ||
export interface LoadAggregateResponse extends AggregateRequest { | ||
export interface LoadAggregateResponse { | ||
aggregateId: AggregateId; | ||
aggregateVersion: number; | ||
@@ -41,3 +43,4 @@ events: DomainEvent<any>[]; | ||
} | ||
export interface CheckAggregateExistsRequest extends AggregateRequest { | ||
export interface CheckAggregateExistsRequest { | ||
aggregateId: AggregateId; | ||
} | ||
@@ -47,3 +50,4 @@ export interface DeleteAggregateOptions { | ||
} | ||
export interface DeleteAggregateRequest extends AggregateRequest { | ||
export interface DeleteAggregateRequest { | ||
aggregateId: AggregateId; | ||
} | ||
@@ -61,6 +65,2 @@ export interface DeleteAggregateTypeRequest { | ||
} | ||
export interface Commit { | ||
events: DomainEvent<any>[]; | ||
expectedVersion?: number; | ||
} | ||
declare class AggregatesClient<A> extends BaseClient { | ||
@@ -73,3 +73,5 @@ private aggregateTypeConstructor; | ||
constructor(aggregateTypeConstructor: any, serializedConfig: any, aggregateClientConfig?: AggregatesClientConfig); | ||
checkExists(request: CheckAggregateExistsRequest, options?: CheckAggregateExistsOptions): Promise<boolean>; | ||
exists(request: CheckAggregateExistsRequest, options?: CheckAggregateExistsOptions): Promise<boolean>; | ||
save(request: AggregateRequest): Promise<number>; | ||
saveBulk(request: AggregateBulkRequest): Promise<number>; | ||
update(aggregateId: string, commandHandler: (s: A) => DomainEvent<any>[], options?: UpdateAggregateOptions): Promise<number>; | ||
@@ -79,6 +81,2 @@ bulkUpdate(aggregateIds: string[], commandHandler: (s: A) => DomainEvent<any>[], tenantId?: string): Promise<number>; | ||
create(aggregateId: string, commandHandler: (s: A) => DomainEvent<any>[], options?: CreateAggregateOptions): Promise<number>; | ||
commit(aggregateId: string, commandHandler: (s: A) => Commit, options?: CommitOptions): Promise<number>; | ||
recordEvent(aggregateId: string, event: DomainEvent<any>, options?: RecordEventOptions): Promise<number>; | ||
recordEvents(aggregateId: string, events: DomainEvent<any>[], tenantId?: string): Promise<number>; | ||
load<T extends A>(aggregateId: string, options?: LoadAggregateOptions): Promise<T>; | ||
private loadInternal; | ||
@@ -93,4 +91,5 @@ deleteAggregate(request: DeleteAggregateRequest, options?: DeleteAggregateOptions): Promise<DeleteAggregateResponse | void>; | ||
static aggregateTypeEventsUrlPath(aggregateType: string): string; | ||
static aggregateTypeBulkEventsUrlPath(aggregateType: string): string; | ||
get initialState(): any; | ||
} | ||
export { AggregatesClient }; |
@@ -73,3 +73,3 @@ "use strict"; | ||
} | ||
AggregatesClient.prototype.checkExists = function (request, options) { | ||
AggregatesClient.prototype.exists = function (request, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -99,2 +99,28 @@ var url, error_2; | ||
}; | ||
AggregatesClient.prototype.save = function (request) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var events, aggregateId, expectedVersion, tenantId; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
events = request.events, aggregateId = request.aggregateId, expectedVersion = request.expectedVersion, tenantId = request.tenantId; | ||
return [4 /*yield*/, this.saveInternal({ aggregateId: aggregateId, events: events, expectedVersion: expectedVersion }, tenantId)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); | ||
}; | ||
AggregatesClient.prototype.saveBulk = function (request) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var batches, tenantId; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
batches = request.batches, tenantId = request.tenantId; | ||
return [4 /*yield*/, this.saveBulkInternal(batches, tenantId)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); | ||
}; | ||
AggregatesClient.prototype.update = function (aggregateId, commandHandler, options) { | ||
@@ -120,3 +146,4 @@ return __awaiter(this, void 0, void 0, function () { | ||
eventsToSave = commandHandler(response.aggregate); | ||
return [4 /*yield*/, this.saveInternal(aggregateId, { | ||
return [4 /*yield*/, this.saveInternal({ | ||
aggregateId: aggregateId, | ||
events: eventsToSave, | ||
@@ -245,3 +272,3 @@ expectedVersion: currentVersion | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.saveInternal(aggregateId, { events: eventsToSave, expectedVersion: 0 }, tenantId)]; | ||
case 0: return [4 /*yield*/, this.saveInternal({ aggregateId: aggregateId, events: eventsToSave, expectedVersion: 0 }, tenantId)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
@@ -265,64 +292,2 @@ } | ||
}; | ||
AggregatesClient.prototype.commit = function (aggregateId, commandHandler, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var aggregate, commit, tenantId; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
aggregate = new this.aggregateTypeConstructor.prototype.constructor(this.initialState()); | ||
commit = commandHandler(aggregate); | ||
tenantId = options === null || options === void 0 ? void 0 : options.tenantId; | ||
return [4 /*yield*/, this.saveInternal(aggregateId, commit, tenantId)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); | ||
}; | ||
AggregatesClient.prototype.recordEvent = function (aggregateId, event, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var tenantId; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
tenantId = options === null || options === void 0 ? void 0 : options.tenantId; | ||
return [4 /*yield*/, this.recordEvents(aggregateId, [event], tenantId)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); | ||
}; | ||
AggregatesClient.prototype.recordEvents = function (aggregateId, events, tenantId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.saveInternal(aggregateId, { events: events }, tenantId)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); | ||
}; | ||
AggregatesClient.prototype.load = function (aggregateId, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response, error_7; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, this.loadInternal(aggregateId, options)]; | ||
case 1: | ||
response = _a.sent(); | ||
return [2 /*return*/, response.aggregate]; | ||
case 2: | ||
error_7 = _a.sent(); | ||
if ((0, error_1.isSerializedApiError)(error_7)) { | ||
if (error_7.statusCode === 404) { | ||
throw new error_1.AggregateNotFound(this.aggregateType, aggregateId); | ||
} | ||
} | ||
throw error_7; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
AggregatesClient.prototype.loadInternal = function (aggregateId, options) { | ||
@@ -408,3 +373,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
} | ||
url = "".concat(AggregatesClient.aggregateTypeEventsUrlPath(this.aggregateType)); | ||
url = "".concat(AggregatesClient.aggregateTypeBulkEventsUrlPath(this.aggregateType)); | ||
return [4 /*yield*/, this.axiosClient.post(url, { batches: batches }, config)]; | ||
@@ -418,5 +383,5 @@ case 1: | ||
}; | ||
AggregatesClient.prototype.saveInternal = function (aggregateId, commit, tenantId) { | ||
AggregatesClient.prototype.saveInternal = function (eventBatch, tenantId) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var config; | ||
var config, events, aggregateId, expectedVersion; | ||
return __generator(this, function (_a) { | ||
@@ -426,9 +391,13 @@ switch (_a.label) { | ||
config = tenantId ? this.axiosConfig(tenantId) : this.axiosConfig(); | ||
if (commit.events.length === 0) { | ||
events = eventBatch.events, aggregateId = eventBatch.aggregateId, expectedVersion = eventBatch.expectedVersion; | ||
if (events.length === 0) { | ||
return [2 /*return*/, 0]; | ||
} | ||
return [4 /*yield*/, this.axiosClient.post(AggregatesClient.aggregateEventsUrlPath(this.aggregateType, aggregateId), commit, config)]; | ||
return [4 /*yield*/, this.axiosClient.post(AggregatesClient.aggregateEventsUrlPath(this.aggregateType, aggregateId), { | ||
events: events, | ||
expectedVersion: expectedVersion | ||
}, config)]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, commit.events.length]; | ||
return [2 /*return*/, events.length]; | ||
} | ||
@@ -450,2 +419,5 @@ }); | ||
}; | ||
AggregatesClient.aggregateTypeBulkEventsUrlPath = function (aggregateType) { | ||
return "/aggregates/".concat(aggregateType, "/events"); | ||
}; | ||
Object.defineProperty(AggregatesClient.prototype, "initialState", { | ||
@@ -452,0 +424,0 @@ get: function () { |
@@ -6,3 +6,3 @@ { | ||
"author": "Serialized", | ||
"version": "4.11.0", | ||
"version": "4.12.0", | ||
"main": "dist/index.js", | ||
@@ -9,0 +9,0 @@ "types": "dist/index.d.ts", |
119285
2511