integreat
Version:
Node.js integration layer
33 lines • 1.64 kB
JavaScript
import debugLib from 'debug';
import mutateAndSend from '../utils/mutateAndSend.js';
import { createErrorResponse, createUnknownServiceError, } from '../utils/response.js';
import { isTypedData } from '../utils/is.js';
const debug = debugLib('great');
const extractType = (action, data) => action.payload.type || (isTypedData(data) && data.$type) || undefined;
const extractId = (action, data) => action.payload.id || (isTypedData(data) && data.id) || undefined;
export function getTypeAndId(action, data) {
const type = extractType(action, data);
const id = extractId(action, data);
return { type, id };
}
export const setIdAndTypeOnAction = (action, id, type) => ({
...action,
payload: { ...action.payload, id, type },
});
export default async function set(action, { getService, dispatch }) {
const { data, targetService: serviceId, endpoint: endpointId, } = action.payload;
const { type, id } = getTypeAndId(action, data);
const service = getService(type, serviceId);
if (!service) {
return createUnknownServiceError(type, serviceId, 'SET');
}
const endpointDebug = endpointId ? `at endpoint '${endpointId}'` : '';
debug('SET: Send to service %s %s', service.id, endpointDebug);
const nextAction = setIdAndTypeOnAction(action, id, type);
const endpoint = await service.endpointFromAction(nextAction);
if (!endpoint) {
return createErrorResponse(`No endpoint matching ${action.type} request to service '${serviceId}'.`, 'handler:SET', 'badrequest');
}
return await mutateAndSend(service, endpoint, nextAction, dispatch);
}
//# sourceMappingURL=set.js.map