@segment/analytics-core
Version:
This package represents core 'shared' functionality that is shared by analytics packages. This is not designed to be used directly, but internal to analytics-node and analytics-browser.
74 lines • 3.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateEvent = exports.assertMessageId = exports.assertTraits = exports.assertTrackEventProperties = exports.assertTrackEventName = exports.assertEventType = exports.assertEventExists = exports.assertUserIdentity = void 0;
var errors_1 = require("./errors");
var helpers_1 = require("./helpers");
var stringError = 'is not a string';
var objError = 'is not an object';
var nilError = 'is nil';
// user identity check could hypothetically could be used in the browser event factory, but not 100% sure -- so this is node only for now
function assertUserIdentity(event) {
var USER_FIELD_NAME = '.userId/anonymousId/previousId/groupId';
var getAnyUserId = function (event) { var _a, _b, _c; return (_c = (_b = (_a = event.userId) !== null && _a !== void 0 ? _a : event.anonymousId) !== null && _b !== void 0 ? _b : event.groupId) !== null && _c !== void 0 ? _c : event.previousId; };
var id = getAnyUserId(event);
if (!(0, helpers_1.exists)(id)) {
throw new errors_1.ValidationError(USER_FIELD_NAME, nilError);
}
else if (!(0, helpers_1.isString)(id)) {
throw new errors_1.ValidationError(USER_FIELD_NAME, stringError);
}
}
exports.assertUserIdentity = assertUserIdentity;
function assertEventExists(event) {
if (!(0, helpers_1.exists)(event)) {
throw new errors_1.ValidationError('Event', nilError);
}
if (typeof event !== 'object') {
throw new errors_1.ValidationError('Event', objError);
}
}
exports.assertEventExists = assertEventExists;
function assertEventType(event) {
if (!(0, helpers_1.isString)(event.type)) {
throw new errors_1.ValidationError('.type', stringError);
}
}
exports.assertEventType = assertEventType;
function assertTrackEventName(event) {
if (!(0, helpers_1.isString)(event.event)) {
throw new errors_1.ValidationError('.event', stringError);
}
}
exports.assertTrackEventName = assertTrackEventName;
function assertTrackEventProperties(event) {
if (!(0, helpers_1.isPlainObject)(event.properties)) {
throw new errors_1.ValidationError('.properties', objError);
}
}
exports.assertTrackEventProperties = assertTrackEventProperties;
function assertTraits(event) {
if (!(0, helpers_1.isPlainObject)(event.traits)) {
throw new errors_1.ValidationError('.traits', objError);
}
}
exports.assertTraits = assertTraits;
function assertMessageId(event) {
if (!(0, helpers_1.isString)(event.messageId)) {
throw new errors_1.ValidationError('.messageId', stringError);
}
}
exports.assertMessageId = assertMessageId;
function validateEvent(event) {
assertEventExists(event);
assertEventType(event);
assertMessageId(event);
if (event.type === 'track') {
assertTrackEventName(event);
assertTrackEventProperties(event);
}
if (['group', 'identify'].includes(event.type)) {
assertTraits(event);
}
}
exports.validateEvent = validateEvent;
//# sourceMappingURL=assertions.js.map