Skip to content

[openapi-fetch] Provide client.request() to support dynamic method. #1808

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

Closed
1 task done
kerwanp opened this issue Aug 2, 2024 · 3 comments · Fixed by #2063
Closed
1 task done

[openapi-fetch] Provide client.request() to support dynamic method. #1808

kerwanp opened this issue Aug 2, 2024 · 3 comments · Fixed by #2063
Assignees
Labels
enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library

Comments

@kerwanp
Copy link
Contributor

kerwanp commented Aug 2, 2024

Description

Currently, openapi-fetch uses the method as the function name (ex: client.GET()) and there is no way to use the method as an argument. When building around openapi-fetch it requires a bit of gymnastic and manual typing to have a dynamic method.

          const mth = method.toUpperCase() as keyof typeof client;
          const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;

We can also find this pattern in libraries like axios, ky, etc.

Proposal

Internally, openapi-fetch already use coreFetch method to pass the method as an argument.

We could have a request (or REQUEST to respect casing) method on the client. (or FETCH).

  return {
    /** Call an endpoint */
    async REQUEST(method, url, init) {
          return coreFetch(url, { ...init, method });
    }
    /** Call a GET endpoint */
    async GET(url, init) {
      return coreFetch(url, { ...init, method: "GET" });
    },
    /** Call a PUT endpoint */
    async PUT(url, init) {
      return coreFetch(url, { ...init, method: "PUT" });
    },
    /** Call a POST endpoint */
    async POST(url, init) {
      return coreFetch(url, { ...init, method: "POST" });
    },
    [...]
  }

Checklist

@kerwanp kerwanp added enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library labels Aug 2, 2024
@kerwanp kerwanp changed the title [openapi-fetch] Supports client.request() to provide dynamic method. [openapi-fetch] Provide client.request() to support dynamic method. Aug 2, 2024
@drwpow
Copy link
Contributor

drwpow commented Aug 2, 2024

I’d be open to adding this! This should be pretty invisible. There’s no strong reason this wasn’t done initially. But one trickiness in all this will be requiring (or denying) requestBody based on the method. We’ll need lots of type tests to make sure this catches everything (negative type tests are as valuable as—if not moreso—positive type tests). But adding this should be very doable and shouldn’t impact performance at all.

@licongy
Copy link

licongy commented Oct 28, 2024

I’d be open to adding this as well! In the case where we need to isolate the backend for security reasons, we will make an api proxy on the React server side, so that all client's query goes through the api proxy to make the request. In the case where openapi-fetch is to be executed in the proxy instead of directly on the client, request method should be passed as a variable to achieve a generic proxy.

In this usage scenario, there is also the problem of not being able to dynamically assign a value to path parameter, because of its PathsWithMethod type definition. Perhaps the dynamic assignment of path can be achieved by throwing an error when path doesn't match paths with methods, while balancing the efficiency of checking with PathsWithMethod.

@darioackermann
Copy link

darioackermann commented Nov 3, 2024

I have been trying to implement

const mth = method.toUpperCase() as keyof typeof client;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;

Could you provide a full example with method, Paths and Media defined?
I have been trying to get it work, however typescript throws obscure errors at me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library
Projects
Development

Successfully merging a pull request may close this issue.

5 participants