Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/content/blog/nextjs-ssr-orpc-tanstack-query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function createQueryClient(): QueryClient {
queries: {
staleTime: 60 * 1000, // > 0 to prevent immediate refetching on mount
},
...createHydrationOptions(serializer),
...createHydrationOptions({ serializer }),
},
})
}
Expand Down Expand Up @@ -341,7 +341,7 @@ import { orpc } from '@/lib/orpc'
export function DiscoveryFeed() {
const { data: events = [] } = useQuery(
orpc.planet.feed.streamedOptions({
queryFnOptions: { maxChunks: 10 }, // keep only the 10 newest events
fnOptions: { maxChunks: 10 }, // keep only the 10 newest events
retry: true, // reconnect forever if the stream drops
}),
)
Expand Down
12 changes: 6 additions & 6 deletions apps/content/docs/integrations/tanstack-query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Use `.streamedOptions` to build query options for an [AsyncIteratorObject](/docs
const query = useQuery(
orpc.streamed.streamedOptions({
input: { id: 123 },
queryFnOptions: {
fnOptions: {
refetchMode: 'reset',
maxChunks: 3, // Keep only the last 3 events
},
Expand All @@ -99,10 +99,10 @@ For a loading state, call `queryClient.resetQueries({ queryKey })` before refetc
:::

:::warning
`queryFnOptions` is part of the query key, except for default values:
`fnOptions` is part of the query key, except for default values:

- Queries with different options get separate cache entries and separate streams. To share one, use the same options, and show fewer chunks with `select`, e.g. `select: data => data.slice(-3)`.
- To read or write the cache by key, pass the same options: `streamedKey({ input, queryFnOptions })`.
- To read or write the cache by key, pass the same options: `streamedKey({ input, fnOptions })`.
:::

## Live Query Options
Expand Down Expand Up @@ -561,15 +561,15 @@ const queryClient = new QueryClient({
})
```

With an RPC [key serializer](#key-serializer), equal instances of [custom types](/docs/rpc/serializer#custom-serializers) are kept too, compared by what their handler serializes them to. For queries not built with oRPC utils, pass it to `createStructuralSharing(serializer)`.
With an RPC [key serializer](#key-serializer), equal instances of [custom types](/docs/rpc/serializer#custom-serializers) are kept too, compared by what their handler serializes them to. For queries not built with oRPC utils, pass it to `createStructuralSharing({ serializer })`.

## Server-Side Rendering (SSR)

When [server-side rendering](/docs/recipes/optimizing-ssr) with [TanStack Query hydration](https://tanstack.com/query/latest/docs/framework/react/guides/ssr), two things need special care: oRPC types that JSON cannot represent, and streamed or live queries that never complete.

### Custom Serializers

TanStack Query dehydrates query data as plain JSON, which cannot represent oRPC types such as `Date`, `BigInt`, `Set`, or `Map`. Pass your [RPC Serializer](/docs/rpc/serializer#custom-serializers) to `createHydrationOptions` to keep them intact.
TanStack Query dehydrates query data as plain JSON, which cannot represent oRPC types such as `Date`, `BigInt`, `Set`, or `Map`. Pass your [RPC Serializer](/docs/rpc/serializer#custom-serializers) to `createHydrationOptions` as the `serializer` option to keep them intact.

```ts
import { createHydrationOptions } from '@orpc/tanstack-query'
Expand All @@ -580,7 +580,7 @@ const queryClient = new QueryClient({
queries: {
staleTime: 60 * 1000, // > 0 to prevent immediate refetching on mount
},
...createHydrationOptions(serializer), // dehydrate.serializeData and hydrate.deserializeData
...createHydrationOptions({ serializer }), // dehydrate.serializeData and hydrate.deserializeData
},
})
```
Expand Down
1 change: 1 addition & 0 deletions apps/content/docs/migrations/from-v1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,7 @@ orpc.live.experimental_liveOptions({ input: {} })
Streamed queries changed (see [Streamed Query Options](/docs/integrations/tanstack-query#streamed-query-options)):

- `refetchMode: 'reset'` keeps the old data until the new stream connects, then sets it to `[]`. (v1 cleared it immediately, returning the query to `pending`.)
- `queryFnOptions` is now `fnOptions`, the same name `.key({ type: 'streamed', fnOptions })` uses.
- `refetchMode: 'replace'` was removed. To keep the old data until the stream ends, collect the stream in a custom `queryFn`:

```ts
Expand Down
6 changes: 6 additions & 0 deletions packages/msw/src/http-router-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export type HTTPRouterUtils<
TContext extends Context,
> = HTTPRouterUtilsInternal<T, TContext>

/**
* The recursion behind {@link HTTPRouterUtils}. Exported only so TypeScript can name it
* in declarations emitted for generic code, do not use it directly.
*
* @internal
*/
export type HTTPRouterUtilsInternal<T, TContext extends Context> =
T extends ProcedureContract<infer UInputSchema, infer UOutputSchema, infer UErrorMap>
? HTTPProcedureUtils<TContext, UInputSchema, UOutputSchema, UErrorMap>
Expand Down
5 changes: 4 additions & 1 deletion packages/msw/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
it('exports createHTTPUtils', async () => {
await expect(import('./index')).resolves.toMatchObject({
const exports = await import('./index')

expect(exports).toMatchObject({
createHTTPUtils: expect.any(Function),
HTTPProcedureUtils: expect.any(Function),
})
})
22 changes: 15 additions & 7 deletions packages/pinia-colada/src/contract-utils.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { PromiseWithError } from '@orpc/shared'
import { createContractJsonifiedUtilsFactory, createContractUtilsFactory } from './contract-utils'
import type { ProcedureUtils } from './procedure-utils'
import type { SharedUtils } from './shared-utils'
import type { OperationContext } from './types'
import type { PiniaColadaOperationContext } from './types'

const contract = {
ping: oc.meta(meta.path(['ping'])),
Expand All @@ -29,7 +29,7 @@ describe('createContractUtilsFactory', () => {
mutationInterceptors: [
async ({ context, next, input }) => {
expectTypeOf(input).toEqualTypeOf<unknown>()
expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>()
expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & PiniaColadaOperationContext>()

const result = next()

Expand All @@ -43,7 +43,9 @@ describe('createContractUtilsFactory', () => {
queryInterceptors: [
async ({ context, next, input }) => {
expectTypeOf(input).toEqualTypeOf<unknown>()
expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>()
expectTypeOf(context).toEqualTypeOf<
{ cache?: boolean } & PiniaColadaOperationContext
>()

const result = next()

Expand All @@ -60,7 +62,9 @@ describe('createContractUtilsFactory', () => {
mutationInterceptors: [
async ({ context, next, input }) => {
expectTypeOf(input).toEqualTypeOf<unknown>()
expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>()
expectTypeOf(context).toEqualTypeOf<
{ cache?: boolean } & PiniaColadaOperationContext
>()

const result = next()

Expand Down Expand Up @@ -110,7 +114,7 @@ describe('createContractJsonifiedUtilsFactory', () => {
mutationInterceptors: [
async ({ context, next, input }) => {
expectTypeOf(input).toEqualTypeOf<unknown>()
expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>()
expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & PiniaColadaOperationContext>()

const result = next()

Expand All @@ -124,7 +128,9 @@ describe('createContractJsonifiedUtilsFactory', () => {
queryInterceptors: [
async ({ context, next, input }) => {
expectTypeOf(input).toEqualTypeOf<unknown>()
expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>()
expectTypeOf(context).toEqualTypeOf<
{ cache?: boolean } & PiniaColadaOperationContext
>()

const result = next()

Expand All @@ -141,7 +147,9 @@ describe('createContractJsonifiedUtilsFactory', () => {
mutationInterceptors: [
async ({ context, next, input }) => {
expectTypeOf(input).toEqualTypeOf<unknown>()
expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>()
expectTypeOf(context).toEqualTypeOf<
{ cache?: boolean } & PiniaColadaOperationContext
>()

const result = next()

Expand Down
7 changes: 6 additions & 1 deletion packages/pinia-colada/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
it('exports createPiniaColadaUtils, PINIA_COLADA_OPERATION_CONTEXT_SYMBOL, StreamQuerySSRUtilsPlugin', async () => {
await expect(import('./index')).resolves.toMatchObject({
const exports = await import('./index')

expect(exports).toMatchObject({
createPiniaColadaUtils: expect.any(Function),
createRouterUtils: expect.any(Function),
ProcedureUtils: expect.any(Function),
SharedUtils: expect.any(Function),
PINIA_COLADA_OPERATION_CONTEXT_SYMBOL: expect.any(Symbol),
OPERATION_CONTEXT_SYMBOL: expect.any(Symbol),
StreamQuerySSRUtilsPlugin: expect.any(Function),
})
})
12 changes: 9 additions & 3 deletions packages/pinia-colada/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type {
export * from './router-utils'
export {
/**
* @deprecated Use `createRouterUtils` instead.
* @deprecated Use `createPiniaColadaUtils` instead.
*/
createRouterUtils as createORPCVueColadaUtils,
} from './router-utils'
Expand All @@ -35,8 +35,14 @@ export * from './ssr'
export type { SerializableStreamedQueryOptions, SettleOnFirstDataOptions } from './stream-query'
export * from './types'
export {
OPERATION_CONTEXT_SYMBOL as PINIA_COLADA_OPERATION_CONTEXT_SYMBOL,
type OperationContext as PiniaColadaOperationContext,
/**
* @deprecated Use `PINIA_COLADA_OPERATION_CONTEXT_SYMBOL` instead.
*/
PINIA_COLADA_OPERATION_CONTEXT_SYMBOL as OPERATION_CONTEXT_SYMBOL,
/**
* @deprecated Use `PiniaColadaOperationContext` instead.
*/
type PiniaColadaOperationContext as OperationContext,
} from './types'

export type { AnyNestedClient } from '@orpc/client'
Expand Down
4 changes: 2 additions & 2 deletions packages/pinia-colada/src/meta.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { oc, type } from '@orpc/contract'
import type { PromiseWithError } from '@orpc/shared'

import { piniaColada } from './meta'
import type { OperationContext } from './types'
import type { PiniaColadaOperationContext } from './types'

describe('piniaColada', () => {
it('infers input, output and error types from the contract', () => {
Expand All @@ -16,7 +16,7 @@ describe('piniaColada', () => {
queryInterceptors: [
async ({ context, input, next }) => {
expectTypeOf(input).toEqualTypeOf<string>()
expectTypeOf(context).toEqualTypeOf<ClientContext & OperationContext>()
expectTypeOf(context).toEqualTypeOf<ClientContext & PiniaColadaOperationContext>()

const result = next()

Expand Down
Loading
Loading