ap0nia / eden-query

🙏 type-safe Elysia.js client with powerful asynchronous state management.
https://ap0nia.github.io/eden-query/
MIT License
34 stars 3 forks source link

Adding `onSuccess` property in example of `useMutation` throws `TypeError: rootHooks[hook] is not a function` #35

Closed hunterwilhelm closed 1 month ago

hunterwilhelm commented 1 month ago

Adding onSuccess property in example of useMutation throws TypeError: rootHooks[hook] is not a function

Steps to reproduce

Change addTodo in eden-query/examples/eden-react-query-basic/src/routes/mutation/+page.tsx to this:

  const addTodo = eden.api.todos.post.useMutation({
    onSuccess: () => {
      console.log('Success')
    }
  })

I get the following error on index.ts:275

 TypeError: rootHooks[hook] is not a function
    at Object.apply (index.ts:275:45)
    at handleAddTodo (+page.tsx:23:19)

image

ap0nia commented 1 month ago

The root cause of this can be attributed to #28 as well as the fact that the heuristic currently does not perform any of the following checks:

  1. It does not check that the only value in the object is either a string or a number. This is only specified at the type level for now. The official eden implementation does not perform any runtime type-checks, so I wasn't sure if I should either.
  2. It does not check whether a key is a designated hook. i.e. "useMutation," "useQuery," etc. It looks like the official eden implementation does check if the key is an HTTP method. I wasn't sure whether to do the equivalent with hooks because it would mean you can't have routes called useMutation/a/b/c.
  3. I actually already useMutation specifically here, so maybe I can just modify that.

Would you have any thoughts? I think I could go with option 3 for the moment.

hunterwilhelm commented 1 month ago

I think that is a valid approach. I am exploring options in my fork as I make the eden-vue-query adapter.

So far I found that in treaty/index.ts in createEdenTreatyVueQueryProxy, changing if (pathParam?.key != null) to if (pathParam?.key != null && pathParam.key !== 'onSuccess') provides a band-aid solution for what I want to do.

However, I don't consider it a permanent solution because we would have to check for all possible options on it. So, I'll keep you posted with what I find to fix this.

ap0nia commented 1 month ago

Thanks so much for considering contributing, let me know if there's any way I could help!

I apologize for the lack of additional developer assistance; I think I wrote a blog about how the type system generally works on the svelte-query readme.