integreat
Version:
Node.js integration layer
46 lines • 2.4 kB
JavaScript
import validateFilters from '../../utils/validateFilters.js';
import { arrayIncludes } from '../../utils/array.js';
const validateConditions = (validators) => async function validateConditions(action) {
for (const validator of validators) {
if (!(await validator(action, { rev: false }))) {
return false;
}
}
return true;
};
function createConditionsValidator(conditions, mapOptions, mapTransform) {
if (!conditions) {
return async () => true;
}
const validators = conditions.map((condition) => mapTransform(condition, mapOptions));
return validateConditions(validators);
}
const matchValue = (match, value) => arrayIncludes(match, value);
const hasParam = (params, key) => params && params[key] !== undefined;
const matchId = (endpoint, { payload: { endpoint: endpointId } = {} }) => !endpointId || endpoint.id === endpointId;
const matchType = ({ match = {} }, { payload }) => !match.type || matchValue(match.type, payload.type);
const matchScope = ({ match = {} }, { payload }) => !match.scope ||
matchValue(match.scope, payload.id
? Array.isArray(payload.id)
? 'members'
: 'member'
: 'collection');
const matchAction = ({ match = {} }, { type }) => !match.action || matchValue(match.action, type);
const matchParams = ({ match: { params } = {} }, { payload }) => typeof params !== 'object' ||
params === null ||
Object.entries(params).every(([key, isRequired]) => !isRequired || hasParam(payload, key));
const matchIncoming = ({ match: { incoming: incomingEndpoint } = {} }, isIncoming) => incomingEndpoint === undefined || incomingEndpoint === isIncoming;
export default function isEndpointMatch(endpoint, mapTransform, mapOptions) {
const match = endpoint.match || {};
const matchFilters = match.filters ? validateFilters(match.filters) : () => [];
const matchConditions = createConditionsValidator(match.conditions, mapOptions, mapTransform);
return async (action, isIncoming = false) => matchId(endpoint, action) &&
matchType(endpoint, action) &&
matchScope(endpoint, action) &&
matchAction(endpoint, action) &&
matchParams(endpoint, action) &&
matchIncoming(endpoint, isIncoming) &&
(await matchConditions(action)) &&
matchFilters(action).length === 0;
}
//# sourceMappingURL=isEndpointMatch.js.map