remix-utils-rt
Version:
This package contains simple utility functions to use with [React Router](https://reactrouter.com/home).
30 lines • 982 B
JavaScript
/**
* Runs an action based on the FormData's action name
* @param formData The FormData to parse for an action name
* @param actions The map of actions to run
* @returns The response from the action
* @throws {ReferenceError} Action name not found
* @throws {ReferenceError} Action "${name}" not found
*/
export async function namedAction(formData, actions) {
let name = findNameInFormData(formData);
if (name && name in actions) {
let fn = actions[name];
if (fn) {
return fn();
}
}
if (name === null && "default" in actions) {
return actions.default();
}
if (name === null)
throw new ReferenceError("Action name not found");
throw new ReferenceError(`Action "${name}" not found`);
}
function findNameInFormData(formData) {
let actionName = formData.get("intent");
if (typeof actionName === "string")
return actionName;
return null;
}
//# sourceMappingURL=named-action.js.map