integreat
Version:
Node.js integration layer
31 lines • 1.28 kB
JavaScript
import { isObject } from './is.js';
const isResponseObject = (response) => isObject(response) && Object.keys(response).length > 0;
const joinErrorMessages = (errors) => Array.isArray(errors)
? errors.length > 0
? errors.join(' | ')
: undefined
: errors;
const isOkOrEmpty = (status) => ['ok', null, undefined].includes(status);
function setStatusAndError({ error: responseError, ...response }, originalStatus = null) {
const error = joinErrorMessages(responseError);
const status = error && isOkOrEmpty(response.status) && isOkOrEmpty(originalStatus)
? 'error'
: response.status || originalStatus || undefined;
return error ? { ...response, status, error } : { ...response, status };
}
export function populateActionAfterMutation(action, mappedAction) {
if (!mappedAction) {
return action;
}
const { type, payload, meta } = mappedAction;
const response = mappedAction.response || action.response;
return {
type: type || action.type,
payload: payload || action.payload,
...(isResponseObject(response) && {
response: setStatusAndError(response, action.response?.status),
}),
meta: meta || action.meta,
};
}
//# sourceMappingURL=mutationHelpers.js.map