orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
2.98k stars 327 forks source link

Mode: Splitting react hooks and requests into separate files #985

Open hasanaktas opened 1 year ago

hasanaktas commented 1 year ago

In Nextjs 13, in order to send requests to server components, there must be no hooks in the file containing the function to be used.

We need to be able to extract requests and hooks into separate files with a parameter added to the config.

In this way, we can use the hooks directory only on the client side and the requests directory everywhere.

We are happy to use Orval, and this is one of the things that makes us think the most when updating the dependencies of our project.

melloware commented 1 year ago

Cc @georgiev-anton has done some SSR work with prefetch may weigh in here.

georgiev-anton commented 1 year ago

hi @hasanaktas do you mean prefetch function?

hasanaktas commented 1 year ago

hi @georgiev-anton. In this structure (if hooks are created), it is not possible to not only prefetch function but also make API requests within server components.

melloware commented 1 year ago

@hasanaktas @georgiev-anton if you guys wuld like to work on a PR together that would be great. I don't use SSR so i know its not something I am going to work on.

hasanaktas commented 1 year ago

@melloware

Actually, all we need to do is create the hooks in a separate file.

Just as we create the schemas in a separate file when mode split or tag-split, we can also create the hooks for the react-query or swr in a separate hooks.ts file.

georgiev-anton commented 12 months ago

@hasanaktas can you show an example of why you can’t use requests from the same file with hooks? I don't see a problem

there is an export of requests

image
hasanaktas commented 12 months ago

@georgiev-anton

When Next js 13 was first released, if you used getCategories from the file below in the server component, it would cause an error.

image

Now, when I try it on an empty project in Next 14, it does not give an error. I do not know in which intermediate version this problem was solved.

That's why I wanted to extract the hooks to be used on the client side as a separate file.

I'm sorry for wasting your time.