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

Improve middleware type definition #1918

Merged
merged 3 commits into from
Sep 20, 2024

Conversation

yicrotkd
Copy link
Contributor

@yicrotkd yicrotkd commented Sep 20, 2024

Changes

This PR updates the Middleware type definition to ensure that either onRequest or onResponse must be provided, as reflected in the error:

"Middleware must be an object with one of onRequest() or onResponse()"

(see #1916)

The new type definition introduces a union that enforces this behavior, allowing the absence of both methods to be detected at compile time.

How to Review

Please review the changes to ensure that the new type definition accurately reflects the expected behavior. The key point is that a type error should occur only if neither onRequest nor onResponse is provided. Additionally, please check that existing middleware implementations are unaffected by this change.

Checklist

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

Closes #1916

@yicrotkd yicrotkd requested a review from a team as a code owner September 20, 2024 15:40
Copy link

changeset-bot bot commented Sep 20, 2024

🦋 Changeset detected

Latest commit: 5a0c65e

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

@drwpow
Copy link
Contributor

drwpow commented Sep 20, 2024

Great fix, and great test! Happy to merge this, but first we need a patch changeset to release this (see comment for instructions)

@yicrotkd
Copy link
Contributor Author

Thank you! I've added a patch changeset.

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

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

Thank you!

@drwpow drwpow merged commit e39d11e into openapi-ts:main Sep 20, 2024
8 checks passed
@github-actions github-actions bot mentioned this pull request Sep 20, 2024
@topaxi
Copy link

topaxi commented Sep 23, 2024

With this change, it is no longer possible to have classes implement the Middleware interface.

class MyMiddleware implements Middleware { ... }

Results in:

error TS2422: A class can only implement an object type or intersection of object types with statically known members.

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.

Improvement suggestion for Middleware type definition
3 participants