@angstone/monostone
Version:
monolitic event-sourced framework
66 lines • 2.75 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fetch_1 = require("node-fetch");
const uuid_1 = require("uuid");
/**
* send an event to event store
* @param eventRecipe recipe with the command and the request
* @return eventNumber
*/
function send(eventRecipe) {
return __awaiter(this, void 0, void 0, function* () {
const evt = {
payload: eventRecipe.request || {},
type: eventRecipe.command.featureName + " " +
eventRecipe.command.commandName,
};
const url = (process.env.EVENT_SOURCE_GATEWAY || "http://localhost:2113") +
"/streams/" + (process.env.EVENT_STREAM_NAME || "mono");
const method = "POST";
const eventData = {
data: evt.payload,
eventId: uuid_1.v4(),
eventType: evt.type,
};
const body = JSON.stringify([eventData]);
const headers = { "Content-Type": "application/vnd.eventstore.events+json" };
const response = yield node_fetch_1.default(url, { method, headers, body });
if (response.status === 201) {
const location = response.headers.get("location");
if (location) {
return parseInt(location.split("/").slice(-1)[0], 10);
}
else {
throw new Error("no location field in response");
}
}
else {
throw new Error("event store could not get the event");
}
});
}
exports.send = send;
/**
* deletes all events
*/
function clearAllEvents() {
return __awaiter(this, void 0, void 0, function* () {
const url = (process.env.EVENT_SOURCE_GATEWAY || "http://localhost:2113") +
"/streams/" + (process.env.EVENT_STREAM_NAME || "mono");
const method = "DELETE";
const response = yield node_fetch_1.default(url, { method });
if (response.status !== 204 && response.status !== 404) {
throw new Error("the stream could not be deleted");
}
});
}
exports.clearAllEvents = clearAllEvents;
//# sourceMappingURL=event.tools.js.map