integreat
Version:
Node.js integration layer
32 lines • 1.5 kB
JavaScript
import debugLib from 'debug';
import mutateAndSend from '../utils/mutateAndSend.js';
import { isTypedData } from '../utils/is.js';
import { setDataOnActionPayload } from '../utils/action.js';
import { createErrorResponse, createUnknownServiceError, } from '../utils/response.js';
const debug = debugLib('great');
const prepareData = ({ type, id, data }) => type && id
? { id, $type: type }
: Array.isArray(data)
? data.filter(isTypedData)
: isTypedData(data)
? data
: undefined;
export default async function deleteFn(action, { getService, dispatch }) {
const { type, id, targetService: serviceId, endpoint: endpointId, } = action.payload;
const service = getService(type, serviceId);
if (!service) {
return createUnknownServiceError(type, serviceId, 'DELETE');
}
const data = prepareData(action.payload);
const endpointDebug = endpointId
? `endpoint '${endpointId}'`
: `endpoint matching ${type} and ${id}`;
debug("DELETE: Delete from service '%s' at %s.", service.id, endpointDebug);
const nextAction = setDataOnActionPayload(action, data);
const endpoint = await service.endpointFromAction(nextAction);
if (!endpoint) {
return createErrorResponse(`No endpoint matching ${nextAction.type} request to service '${serviceId}'.`, 'handler:DELETE', 'badrequest');
}
return await mutateAndSend(service, endpoint, nextAction, dispatch);
}
//# sourceMappingURL=delete.js.map