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

feat(client): Add WebSocket Provider Integration Tests and Enhance WebSocket Initialization #3213

Merged

Conversation

naporin0624
Copy link
Contributor

@naporin0624 naporin0624 commented Jul 30, 2024

Background

I use hono/client and wanted to reconnect websockets. However, the $ws method in hono/client internally creates a new WebSocket, so in order to use a websocket client with reconnection, it was necessary to define the url directly. This would lose the type safety of Hono RPC.

This change allows the use of clients that conform to WebSocket interfaces with reconnection, such as reconnecting-websocket, internally.

example usage

import ReconnectingWebSocket from 'reconnecting-websocket';

const client = hc<AppType>(url, {
  webSocket(url, options) {
    return new ReconnectingWebSocket(url, options)
  },
})
client.index.$ws({ query })

What was done

  • Added new test cases for WebSocket provider integration to ensure correct initialization and handling of query parameters.
  • Enhanced WebSocket initialization logic to support custom WebSocket implementations via client options.
  • Updated hc function to use establishWebSocket method for flexible WebSocket creation.
  • Modified ClientRequestOptions to include webSocket for custom WebSocket handling.

How to Test

  • Run the new and existing test suite to verify functionality and integration.
  • Verify the correct inclusion and handling of query parameters in WebSocket URLs.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Jul 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.21%. Comparing base (1fafc7a) to head (45740cd).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #3213    +/-   ##
========================================
  Coverage   96.21%   96.21%            
========================================
  Files         151      151            
  Lines       15096    15102     +6     
  Branches     2620     2757   +137     
========================================
+ Hits        14524    14530     +6     
  Misses        572      572            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe
Copy link
Member

Hi @naporin0624 ! Thank you for the PR.

Enhanced WebSocket initialization logic to support custom WebSocket implementations via client options.

Before I review the details, I would like to know the real-world use case of this option. Can you share it?

@naporin0624
Copy link
Contributor Author

@yusukebe
I didn't explain the background. I apologize.
I use hono/client and wanted to reconnect websockets. However, the $ws method in hono/client internally creates a new WebSocket, so in order to use a websocket client with reconnection, it was necessary to define the url directly. This would lose the type safety of Hono RPC.

This change allows the use of clients that conform to WebSocket interfaces with reconnection, such as reconnecting-websocket, internally.

@yusukebe
Copy link
Member

@naporin0624

That makes sense. Thanks! I'll review this.

@yusukebe yusukebe changed the title Add WebSocket Provider Integration Tests and Enhance WebSocket Initialization feat(client): Add WebSocket Provider Integration Tests and Enhance WebSocket Initialization Jul 30, 2024
@yusukebe
Copy link
Member

Hi @naporin0624

One thing I want to consider. You've added an option for webSocket in two places:

// 1
const client = hc<AppType>(url, {
  webSocket() {}, // <==
})

// 2
const client = hc<AppType>(url)
client.index.$ws(
  undefiend,
  {
    webSocket() {}, // <==
  }
)

It's good, but in my opinion, it's better to have one than two, a few changes as possible. This is because it is easy to add options but difficult to delete existing ones.

If your needs can be satisfied with 1, using only 1 is best. This would simplify the argument checking. What do you think?

@naporin0624
Copy link
Contributor Author

hi @yusukebe

Thank you for your review.
I specified two types in the same way as fetch, but I think it's not too late to add the implementation of 2 once the need arises. Given the nature of OSS, I understand that it is difficult to delete an API once it has been added, so I will change it to only implement 1, which is necessary this time.

This review has been a very valuable conversation for me. Thank you.

@nakasyou
Copy link
Contributor

nakasyou commented Aug 1, 2024

Hi @naporin0624, I think it's a nice idea. You have to import WebSocket from ws module if you use old Node.js version, so it's helpful for that.

Then, what do you think about receiving a class, not a function?

const client = hc<AppType>(url, {
  WebSocket: ReconnectingWebSocket
})

Because WebSocket is just class, and ReconnectingWebSocket has an API compatible with WebSocket. I think it more intuitive for WebSocket to receive WebSocket compatible APIs just as fetch receives fetch compatible functions.

@naporin0624
Copy link
Contributor Author

@nakasyou
Thank you for your review. I also considered taking the class as an option and creating a new one at runtime, but I wanted to avoid limiting the options to classes, so I made it a function-based interface.

I'm also thinking about making it so that it can accept retry like retry(new WebSocket()).

@nakasyou
Copy link
Contributor

nakasyou commented Aug 2, 2024

@naporin0624 I agree. As you said you can easily change options.

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

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

LGTM!

@yusukebe
Copy link
Member

yusukebe commented Aug 4, 2024

@naporin0624

Thank you for changing it. This is simpler and looks good. I'll merge it later. This is actually a feat change but is not a so big feature for users, so we can release it in a patch release. Thanks!

@naporin0624
Copy link
Contributor Author

@yusukebe Okay! Looking forward to the release!

@yusukebe
Copy link
Member

yusukebe commented Aug 6, 2024

Hi @naporin0624

I'll merge this into the main and release a new patch version that includes this change!

@yusukebe yusukebe merged commit b3b1e8a into honojs:main Aug 6, 2024
14 checks passed
@naporin0624 naporin0624 deleted the feat/hono-client-websocket-provider branch August 6, 2024 12:50
adamnolte pushed a commit to autoblocksai/cli that referenced this pull request Aug 9, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [hono](https://hono.dev/) ([source](https://github.com/honojs/hono))
| [`4.5.1` ->
`4.5.4`](https://renovatebot.com/diffs/npm/hono/4.5.1/4.5.4) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/hono/4.5.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/hono/4.5.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/hono/4.5.1/4.5.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/hono/4.5.1/4.5.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>honojs/hono (hono)</summary>

### [`v4.5.4`](https://github.com/honojs/hono/releases/tag/v4.5.4)

[Compare
Source](https://github.com/honojs/hono/compare/v4.5.3...v4.5.4)

##### What's Changed

- fix(jsx): corrects the type of 'draggable' attribute in
intrinsic-elements.ts by
[@&#8203;yasuaki640](https://github.com/yasuaki640) in
[honojs/hono#3224
- feat(jsx): allow to merge CSSProperties declaration by
[@&#8203;jonasnobile](https://github.com/jonasnobile) in
[honojs/hono#3228
- feat(client): Add WebSocket Provider Integration Tests and Enhance
WebSocket Initialization by
[@&#8203;naporin0624](https://github.com/naporin0624) in
[honojs/hono#3213
- fix(types): `param` in `ValidationTargets` supports optional param by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3229

##### New Contributors

- [@&#8203;jonasnobile](https://github.com/jonasnobile) made their
first contribution in
[honojs/hono#3228

**Full Changelog**:
honojs/hono@v4.5.3...v4.5.4

### [`v4.5.3`](https://github.com/honojs/hono/releases/tag/v4.5.3)

[Compare
Source](https://github.com/honojs/hono/compare/v4.5.2...v4.5.3)

#### What's Changed

- fix(validator): Add double quotation marks to multipart checker regex
by [@&#8203;CPlusPatch](https://github.com/CPlusPatch) in
[honojs/hono#3195
- fix(validator): support `application/json` with a charset as JSON by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3199
- fix(jsx): fix handling of SVG elements in JSX. by
[@&#8203;usualoma](https://github.com/usualoma) in
[honojs/hono#3204
- fix(jsx/dom): fix performance issue with adding many new node listings
by [@&#8203;usualoma](https://github.com/usualoma) in
[honojs/hono#3205
- fix(service-worker): refer to `self.fetch` correctly by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3200

#### New Contributors

- [@&#8203;CPlusPatch](https://github.com/CPlusPatch) made their first
contribution in
[honojs/hono#3195

**Full Changelog**:
honojs/hono@v4.5.2...v4.5.3

### [`v4.5.2`](https://github.com/honojs/hono/releases/tag/v4.5.2)

[Compare
Source](https://github.com/honojs/hono/compare/v4.5.1...v4.5.2)

#### What's Changed

- fix(helper/adapter): don't check `navigator` is `undefined` by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3171
- fix(types): handle readonly array correctly by
[@&#8203;m-shaka](https://github.com/m-shaka) in
[honojs/hono#3172
- Revert "fix(helper/adapter): don't check `navigator` is `undefined` by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3173
- fix(type): degradation of generic type handling by
[@&#8203;m-shaka](https://github.com/m-shaka) in
[honojs/hono#3138
- fix:(csrf) fix typo of csrf middleware by
[@&#8203;yasuaki640](https://github.com/yasuaki640) in
[honojs/hono#3178
- feat(secure-headers): remove "X-Powered-By" should be an option by
[@&#8203;EdamAme-x](https://github.com/EdamAme-x) in
[honojs/hono#3177

**Full Changelog**:
honojs/hono@v4.5.1...v4.5.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on Monday" in timezone
America/Chicago, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job log](https://developer.mend.io/github/autoblocksai/cli).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40NDAuNyIsInVwZGF0ZWRJblZlciI6IjM4LjE4LjE3IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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.

3 participants