feat(client): support fetch headers in link options#946
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughAdds support for passing Fetch API Headers into standard RPC and OpenAPI link codecs by introducing a toStandardHeaders normalization helper, widening codec header option types to accept Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor App
participant Codec as Standard*LinkCodec
participant Utils as toStandardHeaders
participant HTTP as HTTP Transport
App->>Codec: encode(path, input, { headers: Headers | Record })
Codec->>Utils: toStandardHeaders(headers)
Utils-->>Codec: StandardHeaders (plain object)
alt last-event-id provided
Codec->>Codec: merge last-event-id into headers
end
Codec->>HTTP: Build request with normalized headers (cookie merged, set-cookie preserved)
HTTP-->>App: Prepared request
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. 📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Summary of Changes
Hello @unnoq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request enhances the client-side RPCLink configuration by enabling direct support for Headers objects in the headers option. This simplifies header management, eliminating the need for manual conversion from Headers to plain objects, and updates relevant documentation and test cases to reflect this improvement.
Highlights
- Direct Headers Support: The RPCLink headers option now directly accepts Headers objects, alongside existing plain object headers.
- Simplified Header Handling: Eliminates the need for Object.fromEntries when working with Headers instances from environments like Next.js or SolidStart.
- Internal Header Conversion: Introduces a new utility function toStandardHeaders to internally convert Headers objects to the library's StandardHeaders format.
- Documentation Updates: Examples in Next.js, Nuxt, and SolidStart documentation have been updated to leverage the new direct Headers support.
- Comprehensive Testing: New test cases have been added to ensure proper handling and conversion of Headers objects within the client and OpenAPI link codecs.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Code Review
This pull request adds support for using Headers objects directly in link options, which simplifies header management in various adapters like Next.js, Nuxt, and Solid Start. The changes are well-structured and include new tests for the functionality. My main feedback is regarding the handling of multiple cookie headers, which seems to be incorrect in a dependency and will cause the new tests to fail. I've provided a suggestion to fix this within the client utility function.
More templates
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/experimental-durable-event-iterator
@orpc/hey-api
@orpc/interop
@orpc/json-schema
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/otel
@orpc/react
@orpc/react-query
@orpc/experimental-react-swr
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (6)
apps/content/docs/adapters/nuxt.md (1)
60-60: Pass Headers instance directly: good; add a safe fallback.Using
event?.headersaligns with the PR intent. Consider a defensive fallback to avoid passingundefinedon the client.- headers: event?.headers, + headers: event?.headers ?? {},apps/content/docs/adapters/next.md (1)
104-106: Drop unnecessary await onheaders().
next/headers’sheaders()is synchronous; removingawaitavoids implying it’s async.- const { headers } = await import('next/headers') - return await headers() + const { headers } = await import('next/headers') + return headers()packages/client/src/adapters/standard/utils.ts (1)
3-3: Confirm client-bundle safety of@orpc/standard-server-fetchimport.Ensure this import is tree-shakeable and browser-safe (no Node-only side effects). If not, consider moving the converter to a shared, isomorphic utility.
packages/client/src/adapters/standard/rpc-link-codec.test.ts (1)
107-125: Add a case for lastEventId with Headers.Ensure mergeStandardHeaders runs when headers is a Headers instance.
Example test to add:
it('support fetch headers + last-event-id', async () => { const headers = new Headers([['cookie', 'a=1']]) const codec = new StandardRPCLinkCodec(serializer, { url: 'http://localhost:3000', headers }) const req = await codec.encode(['test'], 'input', { context: {}, lastEventId: '9' }) expect(req.headers['cookie']).toBe('a=1') expect(req.headers['last-event-id']).toBe('9') })packages/client/src/adapters/standard/utils.test.ts (1)
9-13: Broaden tests to cover Headers-like (cross-realm) objects.Next.js ReadonlyHeaders are not instanceof global Headers; current tests won’t catch that path.
Add:
it('toStandardHeaders with headers-like object', () => { const headersLike = { map: new Map<string, string[]>([['cookie', ['a=1','b=2']], ['set-cookie', ['a1=1','b1=2']]]), forEach(cb: any) { for (const [k, arr] of this.map) for (const v of arr) cb(v, k) }, [Symbol.iterator]() { return this.map[Symbol.iterator]() }, } as any expect(toStandardHeaders(headersLike)).toEqual({ cookie: 'a=1; b=2', 'set-cookie': ['a1=1', 'b1=2'] }) })packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts (1)
53-71: Add coverage for lastEventId + Headers and for headers function returning a Headers instance.Confirms both async Value and merge behavior.
Examples to add:
it('support fetch headers via function + lastEventId', async () => { const headers = new Headers([['cookie', 'a=1']]) const codec = new StandardOpenapiLinkCodec({ ping: oc }, serializer, { url: 'http://localhost:3000', headers: async () => headers }) const req = await codec.encode(['ping'], 'input', { context: {}, lastEventId: '7' }) expect(req.headers['cookie']).toBe('a=1') expect(req.headers['last-event-id']).toBe('7') })
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (10)
apps/content/docs/adapters/next.md(1 hunks)apps/content/docs/adapters/nuxt.md(1 hunks)apps/content/docs/adapters/solid-start.md(1 hunks)packages/client/src/adapters/standard/rpc-link-codec.test.ts(1 hunks)packages/client/src/adapters/standard/rpc-link-codec.ts(3 hunks)packages/client/src/adapters/standard/utils.test.ts(1 hunks)packages/client/src/adapters/standard/utils.ts(1 hunks)packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts(1 hunks)packages/openapi-client/src/adapters/standard/openapi-link-codec.ts(3 hunks)playgrounds/solid-start/src/lib/orpc.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (6)
packages/client/src/adapters/standard/utils.ts (2)
packages/client/src/types.ts (1)
HTTPPath(3-3)packages/standard-server/src/types.ts (1)
StandardHeaders(1-3)
packages/client/src/adapters/standard/rpc-link-codec.test.ts (3)
packages/standard-server-fetch/src/request.ts (2)
headers(13-17)headers(18-20)packages/standard-server-fetch/src/response.ts (2)
headers(27-31)headers(32-34)packages/client/src/adapters/standard/rpc-link-codec.ts (1)
StandardRPCLinkCodec(45-145)
packages/openapi-client/src/adapters/standard/openapi-link-codec.ts (4)
packages/shared/src/value.ts (2)
Value(1-1)value(3-12)packages/standard-server/src/types.ts (1)
StandardHeaders(1-3)packages/client/src/adapters/standard/utils.ts (1)
toStandardHeaders(10-19)packages/standard-server/src/utils.ts (1)
mergeStandardHeaders(28-49)
packages/client/src/adapters/standard/utils.test.ts (1)
packages/client/src/adapters/standard/utils.ts (2)
toHttpPath(6-8)toStandardHeaders(10-19)
packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts (2)
packages/openapi-client/src/adapters/standard/openapi-link-codec.ts (1)
StandardOpenapiLinkCodec(34-247)packages/contract/src/builder.ts (1)
oc(189-198)
packages/client/src/adapters/standard/rpc-link-codec.ts (7)
packages/shared/src/value.ts (2)
Value(1-1)value(3-12)packages/standard-server/src/types.ts (1)
StandardHeaders(1-3)packages/client/src/types.ts (1)
ClientOptions(8-12)packages/standard-server-fetch/src/request.ts (2)
headers(13-17)headers(18-20)packages/standard-server-fetch/src/response.ts (2)
headers(27-31)headers(32-34)packages/client/src/adapters/standard/utils.ts (1)
toStandardHeaders(10-19)packages/standard-server/src/utils.ts (1)
mergeStandardHeaders(28-49)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: test
- GitHub Check: publish-commit
- GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (10)
playgrounds/solid-start/src/lib/orpc.ts (1)
18-19: LGTM: forwards Fetch Headers (SSR) and{}in the browser.This matches the widened
headersoption and keeps SSR/browser parity.apps/content/docs/adapters/solid-start.md (1)
64-65: LGTM: pass through Headers directly.Forwarding
getRequestEvent()?.request.headerswithout converting to an object is consistent with the new API.packages/client/src/adapters/standard/rpc-link-codec.test.ts (1)
107-125: Good coverage for Fetch Headers normalization.Asserts match the expected cookie join and set-cookie array semantics.
packages/client/src/adapters/standard/utils.test.ts (1)
1-1: Import looks correct.Matches the new export surface.
packages/client/src/adapters/standard/rpc-link-codec.ts (2)
9-9: Importing toStandardHeaders is appropriate and consistent with OpenAPI codec.
42-42: Public option widened to Headers | StandardHeaders.API change is reasonable; ensure docs/changelog note this (minor).
packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts (1)
53-71: Good parity test with RPC: verifies cookie join and set-cookie array.packages/openapi-client/src/adapters/standard/openapi-link-codec.ts (3)
8-8: Import of toStandardHeaders aligns with client RPC codec.
27-27: Headers option widened to Headers | StandardHeaders.Looks good; consider noting that input.headers (in detailed mode) still expects StandardHeaders only.
53-53: Computing baseUrl after header normalization is fine; no behavior change.
Support fetch
Headersinheaderslink option - no moreObject.fromEntriesimport { RPCLink } from '@orpc/client/fetch' const link = new RPCLink({ url: `${typeof window !== 'undefined' ? window.location.origin : 'http://localhost:3000'}/rpc`, headers: async () => { if (typeof window !== 'undefined') { return {} } const { headers } = await import('next/headers') - return Object.fromEntries(await headers()) + return await headers() }, })Summary by CodeRabbit