Skip to content

fix: allow use of PathBasedClient with generated paths #1842

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

Merged
merged 1 commit into from
Aug 17, 2024

Conversation

gzm0
Copy link
Contributor

@gzm0 gzm0 commented Aug 12, 2024

To achieve this, we remove unnecessary type bounds.

This fixes #1840.

Changes

  • Remove unnecessary type bounds.
  • Add tests for client types.

How to Review

  • Double check the removal of type bounds does not break anything.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

@gzm0 gzm0 requested a review from a team as a code owner August 12, 2024 07:19
Copy link

changeset-bot bot commented Aug 12, 2024

🦋 Changeset detected

Latest commit: a88ee19

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
openapi-fetch Patch
openapi-react-query Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

To achieve this, we remove unnecessary type bounds.

This fixes openapi-ts#1840.
@@ -142,6 +144,11 @@ describe("client", () => {
}
});

it("provides a Client type", () => {
const client = createClient<paths>({ baseUrl });
expectTypeOf(client).toEqualTypeOf<Client<paths>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are great, thank you. I’d like to have more of these in general. And since these can run inside a test-d.ts test, I’ve been thinking it would be good to separate the type vs runtime tests (and have more of these granular type assertions).

What are your thoughts on splitting runtime vs type tests? And could these be good additions to get that rolling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an excellent question.

Libraries I've been working on so far (notably Scala.js) had to split runtime / type tests because of infrastructure restrictions (or more specifically test cases that should compile and test cases that shouldn't compile).

Personally, I often found that annoying. Because you end up with test cases like (pseudocode):

assert(max([1]) == 1)
assert(max([1,2]) == 2)

And somewhere completely different:

assertFailsCompile(max([]))

What we can do with TS is much nicer IMO:

assert(max([1]) == 1)
assert(max([1,2]) == 2)
assertThrows(
  // @ts-expect-error
  max([])
)

That all being said: IMO it does make sense to split tests by some axis (e.g. the functionality offered, independent of whether the functionality is offered by the type system or the runtime system or a combination thereof).

So in this case, it is true we are testing the Client type function and its inference (w/o inference issues, this test would not be necessary, one could simply read the return type of createClient). So this could / should indeed go into a different "describe" (and/or even a different file).

Now, along which axes to split tests (exported elements, "features", ...) is not easy. However, it is also something that can be done very ad-hoc, so there is no need to "get it right" the first time.

Splitting what is in index.test.js into multiple files is IMO definitely a good idea (merely based on the LOC count of index.test.js).

In summary:

  • Splitting tests into different categories / units / ... is IMO a good thing.
  • I'm not sure typing-time / run-time is the best splitting axis though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In summary:

  • Splitting tests into different categories / units / ... is IMO a good thing.
  • I'm not sure typing-time / run-time is the best splitting axis though.

You’ve convinced me 🙂. Let’s split up the tests into more meaningful divisions then. I think the test suite “feels” complete currently because it’s just too big a file. But in reality is missing several cases still.

Can be a followup obviously! Thanks for weighing in.

@drwpow
Copy link
Contributor

drwpow commented Aug 14, 2024

Great work! In favor of this merging, but see comment on type vs runtime tests

@drwpow drwpow merged commit 0e42cbb into openapi-ts:main Aug 17, 2024
7 checks passed
@gzm0 gzm0 deleted the fix-type branch August 21, 2024 07:22
@gzm0
Copy link
Contributor Author

gzm0 commented Aug 21, 2024

For posterity: I have been working around this using an intersection type:

// BAD!!! See explanation below.
type Client = PathBasedClient<paths & Record<string, Record<string, string>>>;

However, this breaks inference on usage-site for wrong paths: They get accepted by the typechecker rather than flagged as non-existent.

A correct workaround is:

type Client = ReturnType<typeof createPathBasedClient<paths>>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PathBasedClient cannot be (directly) used with generated paths
2 participants