@aimee-blue/ab-service-kit
Version:
Aimee Blue Service Template
62 lines (43 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.actionStreamFromSocket = void 0;
var _rxjs = require("rxjs");
var _operators = require("rxjs/operators");
var _abContracts = require("@aimee-blue/ab-contracts");
var _helpers = require("./helpers");
var _logging = require("../logging");
function defaultActionSchemaByType(type) {
const schema = _abContracts.Channels.actionSchemaByType(type);
if (!schema) {
return null;
}
const fullSchema = _abContracts.Channels.partialMessageSchema(schema);
return fullSchema;
}
const actionStreamFromSocket = (data, actionSchemaByType = defaultActionSchemaByType, logger = _logging.defaultLogger) => {
return data.pipe((0, _operators.filter)(_helpers.isString), (0, _operators.mergeMap)(nonParsed => {
const value = (0, _helpers.tryParse)(nonParsed, logger);
if (value === null) {
return _rxjs.EMPTY;
}
if (typeof value !== 'object' || !('type' in value)) {
logger.error('💥 No type property in incoming message');
return _rxjs.EMPTY;
}
const schema = actionSchemaByType(value.type);
if (!schema) {
logger.error('💥 No schema found for type', value.type);
return _rxjs.EMPTY;
}
const result = schema.validate(value);
if (result.error) {
logger.error('💥 Invalid message of type', value.type, result.error);
return _rxjs.EMPTY;
}
return (0, _rxjs.of)(result.value);
}));
};
exports.actionStreamFromSocket = actionStreamFromSocket;
//# sourceMappingURL=actionStreamFromSocket.js.map