Extend Astro.getActionResult
and/or getActionContext
to surface inputs of the latest request
#1215
Replies: 1 comment 4 replies
-
Regarding types, the request might have come with a single string or number in place of the whole object, or even just malformed in general. So the only type that could be provided in case of error would be It would be incorrect to even give It should be mostly trivial to implement, so contributions are welcome. But since there is really no guarantees about it in case of errors and reusing the values in case of success for something seems like an design mistake or an XY problem, I don't see much benefit to this. The action is an abstraction that includes validation, deciding whether it really is an error or not based on the input shouldn't be leaking to the page. For the given example, you can customize the errors emitted by Zod with something like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Allow consumers of action results to easily access the inputs of the action, just like they can now access results and errors.
Background & Motivation
Broadly, my question was: if my form action has an error and I want to know what data was originally submitted, how can I get this information back? This is what I came up with:
There are multiple ways one can use this value, like more complex error messages that mention the original value, link to relevant data (e.g. in this case, a link to the original URI to manually check if it matches expectations), etc.
My particular use case is that I like to have actions handling self-contained within a component, and so I want each component to be able to tell if the action started from itself. That is:
Here's a slightly different variation, where the action originated from the component, but I want to be able to have a global message.
Note that when I do this for every Card I get:
Obviously doing it in a middleware and passing it down would avoid this, but I do believe this should be an easy thing to access—result data and errors are there already, after all!
Goals
Example
What I'd like is to write my components like this:
And similarly
Much cleaner!
Now, it was also suggested to surface this from
getActionContext
. I'm not going to say no to that. I think it should be in both.Typing
Obviously you couldn't really make this type safe if there's an error (after all it did fail validation), but even then I'd be very happy to get the data the action received, including undeclared one, as for example:
This would be different for form actions vs json, but it shouldn't be impossible to figure out.
Beta Was this translation helpful? Give feedback.
All reactions