@villedemontreal/workit-core
Version:
This package provides default and no-op implementations of the WorkIt types for client packages.
54 lines • 2.03 kB
JavaScript
;
/*
* Copyright (c) 2025 Ville de Montreal. All rights reserved.
* Licensed under the MIT license.
* See LICENSE file in the project root for full license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Interceptors = void 0;
const utils_1 = require("./utils/utils");
class Interceptors {
/**
* It will execute all interceptors concurrently (limited by INTERCEPTOR_ASYNC_LIMIT env, default 3)
* body message will be untouched
* properties will be merged from all returned messages
*/
static async execute(interceptors, message) {
const interceptorsSanitized = Interceptors.sanitize(interceptors);
if (!interceptorsSanitized) {
return message;
}
return Interceptors._internalExecute(interceptorsSanitized, message);
}
static async _internalExecute(interceptors, message) {
let msg = message;
// eslint-disable-next-line no-restricted-syntax
for (const interceptor of interceptors) {
msg = await interceptor(msg);
}
Interceptors._validateMessage(msg);
return msg;
}
static _validateMessage(msg) {
// light validation, perhaps we should check properties
const isObj = (0, utils_1.isObject)(msg);
if (!isObj || !(0, utils_1.isObject)(msg.body) || !(0, utils_1.isObject)(msg.properties)) {
throw Error(`interceptor must return IMessage object`);
}
}
}
exports.Interceptors = Interceptors;
Interceptors.sanitize = (interceptors) => {
if (!interceptors || (0, utils_1.isEmptyArray)(interceptors)) {
return undefined;
}
const isInterceptor = (0, utils_1.isFunction)(interceptors);
if (!isInterceptor && !(0, utils_1.isArrayOfFunctions)(interceptors)) {
throw new Error('interceptors passed in parameter are not valid.');
}
if (isInterceptor) {
return [interceptors];
}
return interceptors;
};
//# sourceMappingURL=interceptors.js.map