solid-start
solid-start copied to clipboard
Added options to transform API event handlers
PR Checklist
Please check if your PR fulfills the following requirements:
- [ ] Addresses an existing open issue: fixes #000
- [x] Tests for the changes have been added (for bug fixes / features)
What is the current behavior?
N/A
What is the new behavior?
Add an additional options to the server configuration to allow for HTTP endpoints to be arbitrarily modified without.
Use Cases
These are just some examples why I personally would find this useful all though there are many other applications.
Say we had some service function that includes some fairly nested function calls say something like the following example of a transaction in drizzle.
Rather than having to drill back up the errors it's nice to be able to just throw them
async function createUser(values: SignupForm) {
return await database.transaction(async transaction => {
const user = await transaction
.insert(users)
.values(values)
.returning()
.catch(async error => {
if (error.constraint === "users_email_unique") {
throw new Response("This email is taken", { status: 409 });
}
if (error.constraint === "users_username_unique") {
throw new Response("This username is taken", { status: 409 });
}
throw error;
});
return user;
})
}
But we need to make sure that we are properly handling each and every endpoint, say something like so
export function METHOD() {
try {
// call some service
} catch (error) {
if (error instanceof Response) {
return error;
}
// handle the non response error, throw it again, etc...
}
}
Again though this still leads to a lot of boilerplate even if we break out this into it's own function instead we could use this new proposed API.
createHandler({
() => <StartServer/>,
{},
{
transformHandler: async (event, handler) => {
try {
await handler(event);
} catch (error) {
if (error instanceof Response) {
return error;
}
// handle the non response error, throw it again, etc...
}
}
}
);
This would then be applied to all event handlers. Additionally it opens the door for some additional DX niceties without having to make any breaking changes by being able to modify the APIEvent bespoke to the application. Perhaps exposing cookies, query parameters etc more directly but that's really up to the individual application
Some Additional Notes and Considerations
This API isn't perfect, especially taking two different options objects but that mostly stems from maintaining backwards compatibility and to only effect projects that make direct use of it. Any suggestions would be very welcome.
Notably this is distinct from middleware as we need to be able to access the transform function itself
⚠️ No Changeset found
Latest commit: ab3510f32b50d8a3ae4567bcc9bb66f9d6a9a70b
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Deploy Preview for solid-start-landing-page ready!
| Name | Link |
|---|---|
| Latest commit | ab3510f32b50d8a3ae4567bcc9bb66f9d6a9a70b |
| Latest deploy log | https://app.netlify.com/sites/solid-start-landing-page/deploys/67b3ae8ced672e000893c9fa |
| Deploy Preview | https://deploy-preview-1818--solid-start-landing-page.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
Is this still something we want to push forward? I'm still not 100% sure of the usecases