nksaraf / vinxi

The Full Stack JavaScript SDK
https://vinxi.vercel.app
MIT License
1.32k stars 56 forks source link

Server Functions Middleware #290

Open frenzzy opened 1 month ago

frenzzy commented 1 month ago

Is it possible to add custom error handling logic or logging to all server functions (actions) at once, without the need to manually wrap each server function in a middleware function, similar to the example provided here: https://github.com/solidjs/solid-start/issues/1434#issuecomment-2097769617 ?

apatrida commented 1 month ago

Does vinxi even know about server functions from solid-start?

frenzzy commented 1 month ago

Here is how the server functions work: https://github.com/nksaraf/vinxi/blob/dbf6e8661f0a03f4161abce4afc8c9f22b747183/packages/vinxi-server-functions/server-handler.js#L14-L35

I think we need a way to adjust the action result, maybe via something like:

- const result = action.apply(null, json);
+ const result = middleware ? middleware(action, json) : action.apply(null, json);

This will allow us to handle server action errors, for example:

async function middleware(action, data) {
  try {
    return await action(data)
  } catch (error) {
    return "modified server function response here"
  }
}