Skip to content
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

Hoping to wrap the client for single arg calls #1620

Open
1 task
morleytatro opened this issue Apr 18, 2024 · 0 comments
Open
1 task

Hoping to wrap the client for single arg calls #1620

morleytatro opened this issue Apr 18, 2024 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed openapi-fetch Relevant to the openapi-fetch library

Comments

@morleytatro
Copy link

Description

This library is super helpful. I'm looking to essentially pass in all options in one shot vs. creating the client, specifying the method, and then the path, as the generated OpenAPI spec spans a ton of files. Also, I'd like to throw on error vs. having to check data/errors.

So far, I've been using this workaround, which is nice, but not as simple as I'd like.

export function getClient<Paths extends {}>() {
  const client = createClient<Paths>({ baseUrl });
  client.use({
    async onRequest(req) {
      req.headers.set('Authorization', `Bearer ${await getToken()}`);
      return req;
    },
  });
  return client;
}

export function transformResult<T, E extends Error>({
  data,
  error,
}: { data: T; error: undefined } | { data: undefined; error: E }) {
  if (data) {
    return data;
  }
  throw error;
}

const result = await getClient<paths>()
  .GET('/some-path', {
    params: //...
  })
  .then(transformResult);

Proposal

What I'm going for is something like this syntax, but I can't for the life of me seem to get the types to line up:

export async function makeCall<
  TPaths extends {},
  TMethod extends HttpMethod = 'get',
  TEndpoint extends keyof PathsWithMethod<TPaths, TMethod> = string,
>({
  endpoint,
  method,
  args,
}: {
  endpoint: TEndpoint;
  method: TMethod;
  args: MaybeOptionalInit<TPaths[TEndpoint][TMethod], TMethod>;
}): Promise<SuccessResponseJSON<TPaths[TEndpoint][TMethod]>> {
  const client = getClient<TPaths>();
  return await client[method.toUpperCase()](endpoint, args).then(
    transformResult,
  );
}

const result = await makeCall<paths>({
  endpoint: '/some-endpoint',
  method: 'get',
  args: {
    params: {
      // ...
    },
  },
});

Checklist

I'm definitely open to helping, but like I said I've struck out thus far. Not even sure it's possible given the current method/path approach in play.

@morleytatro morleytatro added enhancement New feature or request help wanted Extra attention is needed openapi-fetch Relevant to the openapi-fetch library labels Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

1 participant