react-server-actions
Version:
A package for working with actions in React and Next.js
69 lines • 1.94 kB
JavaScript
// ** Action result constructors helpers
// ** Public helpers
export const success = (data) => ({
success: true,
data,
});
export const invalid = (invalid) => ({
success: false,
invalid,
});
export const error = (error) => ({
success: false,
error: typeof error === 'string'
? error
: error instanceof Error
? error.message
: JSON.stringify(error),
});
// ** Private helpers
export function _actionSuccess(formData, actionResult) {
return {
...actionResult,
formData, // pass down the data to leave the form filled
invalid: undefined,
error: undefined,
};
}
export function _actionInvalid(formData, actionResult) {
return {
...actionResult,
formData, // pass down the data to leave the form filled
data: undefined,
error: undefined,
};
}
export function _actionError(formData, actionResult) {
return {
...actionResult,
formData, // pass down the data to leave the form filled
invalid: undefined,
data: undefined,
};
}
// ** Action result typeguards
export const isInvalidActionResult = (actionResult) => {
return (typeof actionResult === 'object' &&
actionResult !== null &&
'success' in actionResult &&
actionResult.success === false &&
'invalid' in actionResult &&
actionResult.invalid !== undefined);
};
export const isErrorActionResult = (actionResult) => {
return (typeof actionResult === 'object' &&
actionResult !== null &&
'success' in actionResult &&
actionResult.success === false &&
'error' in actionResult &&
actionResult.error !== undefined);
};
// ** Initial state helper
export const initialState = (formData) => ({
success: false,
formData,
data: undefined,
invalid: undefined,
error: undefined,
});
//# sourceMappingURL=helpers.js.map