Skip to content

Added options to transform API event handlers #1818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Rabbitminers
Copy link

PR Checklist

Please check if your PR fulfills the following requirements:

  • Addresses an existing open issue: fixes #000
  • 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

Copy link

changeset-bot bot commented Feb 17, 2025

⚠️ No Changeset found

Latest commit: ab3510f

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

Copy link

netlify bot commented Feb 17, 2025

Deploy Preview for solid-start-landing-page ready!

Name Link
🔨 Latest commit ab3510f
🔍 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...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant