diff --git a/apps/content/blog/nextjs-ssr-orpc-tanstack-query.mdx b/apps/content/blog/nextjs-ssr-orpc-tanstack-query.mdx index 5b6ea7795..3dc3b0b60 100644 --- a/apps/content/blog/nextjs-ssr-orpc-tanstack-query.mdx +++ b/apps/content/blog/nextjs-ssr-orpc-tanstack-query.mdx @@ -214,7 +214,7 @@ function createQueryClient(): QueryClient { queries: { staleTime: 60 * 1000, // > 0 to prevent immediate refetching on mount }, - ...createHydrationOptions(serializer), + ...createHydrationOptions({ serializer }), }, }) } @@ -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 }), ) diff --git a/apps/content/docs/integrations/tanstack-query.mdx b/apps/content/docs/integrations/tanstack-query.mdx index df70f55a2..8e0b352c9 100644 --- a/apps/content/docs/integrations/tanstack-query.mdx +++ b/apps/content/docs/integrations/tanstack-query.mdx @@ -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 }, @@ -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 @@ -561,7 +561,7 @@ 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) @@ -569,7 +569,7 @@ When [server-side rendering](/docs/recipes/optimizing-ssr) with [TanStack Query ### 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' @@ -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 }, }) ``` diff --git a/apps/content/docs/migrations/from-v1.mdx b/apps/content/docs/migrations/from-v1.mdx index fee840ee1..80a7c256b 100644 --- a/apps/content/docs/migrations/from-v1.mdx +++ b/apps/content/docs/migrations/from-v1.mdx @@ -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 diff --git a/packages/msw/src/http-router-utils.ts b/packages/msw/src/http-router-utils.ts index bf4d72b9f..916bffef4 100644 --- a/packages/msw/src/http-router-utils.ts +++ b/packages/msw/src/http-router-utils.ts @@ -11,6 +11,12 @@ export type HTTPRouterUtils< TContext extends Context, > = HTTPRouterUtilsInternal +/** + * 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 extends ProcedureContract ? HTTPProcedureUtils diff --git a/packages/msw/src/index.test.ts b/packages/msw/src/index.test.ts index d72bfb7e3..d30cf2cc4 100644 --- a/packages/msw/src/index.test.ts +++ b/packages/msw/src/index.test.ts @@ -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), }) }) diff --git a/packages/pinia-colada/src/contract-utils.test-d.ts b/packages/pinia-colada/src/contract-utils.test-d.ts index 6371c6d87..41af8260e 100644 --- a/packages/pinia-colada/src/contract-utils.test-d.ts +++ b/packages/pinia-colada/src/contract-utils.test-d.ts @@ -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'])), @@ -29,7 +29,7 @@ describe('createContractUtilsFactory', () => { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & PiniaColadaOperationContext>() const result = next() @@ -43,7 +43,9 @@ describe('createContractUtilsFactory', () => { queryInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf< + { cache?: boolean } & PiniaColadaOperationContext + >() const result = next() @@ -60,7 +62,9 @@ describe('createContractUtilsFactory', () => { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf< + { cache?: boolean } & PiniaColadaOperationContext + >() const result = next() @@ -110,7 +114,7 @@ describe('createContractJsonifiedUtilsFactory', () => { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & PiniaColadaOperationContext>() const result = next() @@ -124,7 +128,9 @@ describe('createContractJsonifiedUtilsFactory', () => { queryInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf< + { cache?: boolean } & PiniaColadaOperationContext + >() const result = next() @@ -141,7 +147,9 @@ describe('createContractJsonifiedUtilsFactory', () => { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf< + { cache?: boolean } & PiniaColadaOperationContext + >() const result = next() diff --git a/packages/pinia-colada/src/index.test.ts b/packages/pinia-colada/src/index.test.ts index b561a7647..deed51796 100644 --- a/packages/pinia-colada/src/index.test.ts +++ b/packages/pinia-colada/src/index.test.ts @@ -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), }) }) diff --git a/packages/pinia-colada/src/index.ts b/packages/pinia-colada/src/index.ts index 189595acd..5209086de 100644 --- a/packages/pinia-colada/src/index.ts +++ b/packages/pinia-colada/src/index.ts @@ -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' @@ -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' diff --git a/packages/pinia-colada/src/meta.test-d.ts b/packages/pinia-colada/src/meta.test-d.ts index ce671784a..c7b11b80f 100644 --- a/packages/pinia-colada/src/meta.test-d.ts +++ b/packages/pinia-colada/src/meta.test-d.ts @@ -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', () => { @@ -16,7 +16,7 @@ describe('piniaColada', () => { queryInterceptors: [ async ({ context, input, next }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf() + expectTypeOf(context).toEqualTypeOf() const result = next() diff --git a/packages/pinia-colada/src/procedure-utils.test.ts b/packages/pinia-colada/src/procedure-utils.test.ts index 89a616139..45545642d 100644 --- a/packages/pinia-colada/src/procedure-utils.test.ts +++ b/packages/pinia-colada/src/procedure-utils.test.ts @@ -8,7 +8,7 @@ import { ProcedureUtils, } from './procedure-utils' import * as StreamQueryModule from './stream-query' -import { OPERATION_CONTEXT_SYMBOL } from './types' +import { PINIA_COLADA_OPERATION_CONTEXT_SYMBOL } from './types' const generateOperationKeySpy = vi.spyOn(KeyModule, 'generateOperationKey') const streamedQuerySpy = vi.spyOn(StreamQueryModule, 'serializableStreamedQuery') @@ -174,7 +174,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith(1, { signal: expect.any(AbortSignal), context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'streamed', }, @@ -265,7 +265,7 @@ describe('procedureUtils', () => { fnContext, context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'streamed', }, @@ -281,7 +281,7 @@ describe('procedureUtils', () => { signal: expect.any(AbortSignal), context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'streamed', }, @@ -398,7 +398,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith(1, { signal: expect.any(AbortSignal), context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'live', }, @@ -480,7 +480,7 @@ describe('procedureUtils', () => { fnContext, context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'live', }, @@ -496,7 +496,7 @@ describe('procedureUtils', () => { signal: expect.any(AbortSignal), context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'live', }, @@ -680,7 +680,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith(1, { signal, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'query', }, @@ -704,7 +704,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith(undefined, { signal, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'query', }, @@ -722,7 +722,7 @@ describe('procedureUtils', () => { signal, context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'query', }, @@ -747,7 +747,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith(undefined, { signal, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: ['__custom__'], type: 'query', }, @@ -803,7 +803,7 @@ describe('procedureUtils', () => { fnContext, context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'query', }, @@ -821,7 +821,7 @@ describe('procedureUtils', () => { context: { batch: true, extra: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'query', }, @@ -881,7 +881,7 @@ describe('procedureUtils', () => { { signal, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'infinite', }, @@ -919,7 +919,7 @@ describe('procedureUtils', () => { signal, context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'infinite', }, @@ -952,7 +952,7 @@ describe('procedureUtils', () => { { signal, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: ['__custom__'], type: 'infinite', }, @@ -1008,7 +1008,7 @@ describe('procedureUtils', () => { fnContext, context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'infinite', }, @@ -1025,7 +1025,7 @@ describe('procedureUtils', () => { signal, context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: options.key, type: 'infinite', }, @@ -1077,7 +1077,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledTimes(1) expect(client).toHaveBeenCalledWith(1, { context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: generateOperationKeySpy.mock.results[1]!.value, type: 'mutation', }, @@ -1105,7 +1105,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith(1, { context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: generateOperationKeySpy.mock.results[1]!.value, type: 'mutation', }, @@ -1130,7 +1130,7 @@ describe('procedureUtils', () => { await expect(options.mutation(1, {})).resolves.toEqual('__mocked__') expect(client).toHaveBeenCalledWith(1, { context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: ['__custom__'], type: 'mutation', }, @@ -1182,7 +1182,7 @@ describe('procedureUtils', () => { fnContext, context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: generateOperationKeySpy.mock.results[0]!.value, type: 'mutation', }, @@ -1198,7 +1198,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith('__override__', { context: { batch: true, - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: generateOperationKeySpy.mock.results[0]!.value, type: 'mutation', }, diff --git a/packages/pinia-colada/src/procedure-utils.ts b/packages/pinia-colada/src/procedure-utils.ts index cad4cf758..f914f5e3a 100644 --- a/packages/pinia-colada/src/procedure-utils.ts +++ b/packages/pinia-colada/src/procedure-utils.ts @@ -27,7 +27,7 @@ import type { MutationKeyOptions, MutationOptionsIn, MutationOptionsOut, - OperationContext, + PiniaColadaOperationContext, QueryKeyOptions, QueryOptionsIn, QueryOptionsOut, @@ -37,14 +37,14 @@ import type { UseMutationFnContext, UseQueryFnContext, } from './types' -import { OPERATION_CONTEXT_SYMBOL } from './types' +import { PINIA_COLADA_OPERATION_CONTEXT_SYMBOL } from './types' export interface ProcedureUtilsQueryInterceptorOptions< TClientContext extends ClientContext, TInput, > { path: string[] - context: TClientContext & OperationContext + context: TClientContext & PiniaColadaOperationContext input: TInput fnContext: UseQueryFnContext } @@ -91,7 +91,7 @@ export interface ProcedureUtilsInfiniteInterceptorOptions< TInput, > { path: string[] - context: TClientContext & OperationContext + context: TClientContext & PiniaColadaOperationContext input: TInput fnContext: UseInfiniteQueryFnContext } @@ -110,7 +110,7 @@ export interface ProcedureUtilsMutationInterceptorOptions< TInput, > { path: string[] - context: TClientContext & OperationContext + context: TClientContext & PiniaColadaOperationContext input: TInput fnContext: UseMutationFnContext } @@ -473,12 +473,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key, type: 'query', }, ...context, - } satisfies OperationContext as any, + } satisfies PiniaColadaOperationContext as any, input: input as TInput, fnContext, }, @@ -567,12 +567,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key, type: 'streamed', }, ...context, - } satisfies OperationContext as any, + } satisfies PiniaColadaOperationContext as any, input: input as TInput, fnContext, }, @@ -662,12 +662,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key, type: 'live', }, ...context, - } satisfies OperationContext as any, + } satisfies PiniaColadaOperationContext as any, input: input as TInput, fnContext, }, @@ -758,12 +758,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key, type: 'infinite', }, ...context, - } satisfies OperationContext as any, + } satisfies PiniaColadaOperationContext as any, input: input(fnContext.pageParam) as TInput, fnContext, }, @@ -837,12 +837,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [PINIA_COLADA_OPERATION_CONTEXT_SYMBOL]: { key: typeof key === 'function' ? key(input) : key, type: 'mutation', }, ...context, - } satisfies OperationContext as any, + } satisfies PiniaColadaOperationContext as any, input, fnContext, }, diff --git a/packages/pinia-colada/src/router-utils.ts b/packages/pinia-colada/src/router-utils.ts index 20c25d079..678d47dbb 100644 --- a/packages/pinia-colada/src/router-utils.ts +++ b/packages/pinia-colada/src/router-utils.ts @@ -29,6 +29,12 @@ import { SharedUtils } from './shared-utils' */ export type RouterUtils = RouterUtilsInternal +/** + * The recursion behind {@link RouterUtils}. Exported only so TypeScript can name it + * in declarations emitted for generic code, do not use it directly. + * + * @internal + */ export type RouterUtilsInternal = T extends Client ? Public> @@ -38,6 +44,12 @@ export type RouterUtilsInternal = export type RouterUtilsScoped = RouterUtilsScopedInternal +/** + * The recursion behind {@link RouterUtilsScoped}. Exported only so TypeScript can name it + * in declarations emitted for generic code, do not use it directly. + * + * @internal + */ export type RouterUtilsScopedInternal = T extends Client ? ProcedureUtilsOptions diff --git a/packages/pinia-colada/src/types.ts b/packages/pinia-colada/src/types.ts index 9b40dc697..d5521d757 100644 --- a/packages/pinia-colada/src/types.ts +++ b/packages/pinia-colada/src/types.ts @@ -31,7 +31,7 @@ export type UseMutationFnContext = Parameters, Error> } @@ -44,7 +44,7 @@ describe.each([ return server.stream() }), } as unknown as StreamClient - const utils = createRouterUtils(client, { + const utils = createPiniaColadaUtils(client, { plugins: [new StreamQuerySSRUtilsPlugin({ isServer: true })], }) const pinia = createPinia() @@ -72,7 +72,7 @@ describe.each([ expect(started).toHaveBeenCalledTimes(1) const client = { stream: vi.fn(() => browser.stream()) } as unknown as StreamClient - const utils = createRouterUtils(client, { + const utils = createPiniaColadaUtils(client, { plugins: [new StreamQuerySSRUtilsPlugin({ isServer: false })], }) diff --git a/packages/swr/src/index.test.ts b/packages/swr/src/index.test.ts index ef976db9a..c5035a661 100644 --- a/packages/swr/src/index.test.ts +++ b/packages/swr/src/index.test.ts @@ -1,7 +1,12 @@ it('exports createSWRUtils, SWR_OPERATION_CONTEXT_SYMBOL', async () => { - await expect(import('./index')).resolves.toMatchObject({ + const exports = await import('./index') + + expect(exports).toMatchObject({ createSWRUtils: expect.any(Function), createRouterUtils: expect.any(Function), + ProcedureUtils: expect.any(Function), + SharedUtils: expect.any(Function), SWR_OPERATION_CONTEXT_SYMBOL: expect.any(Symbol), + OPERATION_CONTEXT_SYMBOL: expect.any(Symbol), }) }) diff --git a/packages/swr/src/index.ts b/packages/swr/src/index.ts index 44c2bdf6f..ac13cba44 100644 --- a/packages/swr/src/index.ts +++ b/packages/swr/src/index.ts @@ -6,8 +6,14 @@ export { createRouterUtils as createSWRUtils } from './router-utils' export { SharedUtils } from './shared-utils' export * from './types' export { - OPERATION_CONTEXT_SYMBOL as SWR_OPERATION_CONTEXT_SYMBOL, - type OperationContext as SWROperationContext, + /** + * @deprecated Use `SWR_OPERATION_CONTEXT_SYMBOL` instead. + */ + SWR_OPERATION_CONTEXT_SYMBOL as OPERATION_CONTEXT_SYMBOL, + /** + * @deprecated Use `SWROperationContext` instead. + */ + type SWROperationContext as OperationContext, } from './types' export type { Public } from '@orpc/shared' diff --git a/packages/swr/src/procedure-utils.test.ts b/packages/swr/src/procedure-utils.test.ts index 52ae95577..6ec9e2bff 100644 --- a/packages/swr/src/procedure-utils.test.ts +++ b/packages/swr/src/procedure-utils.test.ts @@ -2,7 +2,7 @@ import { promiseWithResolvers } from '@orpc/shared' import type { OperationKey } from './key' import { ProcedureUtils } from './procedure-utils' -import { OPERATION_CONTEXT_SYMBOL } from './types' +import { SWR_OPERATION_CONTEXT_SYMBOL } from './types' beforeEach(() => { vi.clearAllMocks() @@ -45,7 +45,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledTimes(1) expect(client).toHaveBeenCalledWith( { search: '__search__' }, - { context: { batch: true, [OPERATION_CONTEXT_SYMBOL]: { key, type: 'fetcher' } } }, + { context: { batch: true, [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'fetcher' } } }, ) }) @@ -64,7 +64,10 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith( { search: '__search__' }, { - context: { batch: true, [OPERATION_CONTEXT_SYMBOL]: { key: prefixedKey, type: 'fetcher' } }, + context: { + batch: true, + [SWR_OPERATION_CONTEXT_SYMBOL]: { key: prefixedKey, type: 'fetcher' }, + }, }, ) }) @@ -90,7 +93,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith( { search: '__search__' }, { - context: { batch: true, [OPERATION_CONTEXT_SYMBOL]: { key, type: 'subscriber' } }, + context: { batch: true, [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'subscriber' } }, signal: expect.any(AbortSignal), }, ) @@ -129,7 +132,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith( { search: '__search__' }, { - context: { batch: true, [OPERATION_CONTEXT_SYMBOL]: { key, type: 'subscriber' } }, + context: { batch: true, [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'subscriber' } }, signal: expect.any(AbortSignal), }, ) @@ -161,7 +164,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith( { search: '__search__' }, { - context: { batch: true, [OPERATION_CONTEXT_SYMBOL]: { key, type: 'subscriber' } }, + context: { batch: true, [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'subscriber' } }, signal: expect.any(AbortSignal), }, ) @@ -322,7 +325,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith( { search: '__search__' }, { - context: { batch: true, [OPERATION_CONTEXT_SYMBOL]: { key, type: 'liveSubscriber' } }, + context: { batch: true, [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'liveSubscriber' } }, signal: expect.any(AbortSignal), }, ) @@ -351,7 +354,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith( { search: '__search__' }, { - context: { batch: true, [OPERATION_CONTEXT_SYMBOL]: { key, type: 'liveSubscriber' } }, + context: { batch: true, [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'liveSubscriber' } }, signal: expect.any(AbortSignal), }, ) @@ -461,7 +464,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledTimes(1) expect(client).toHaveBeenCalledWith( { search: '__search__' }, - { context: { batch: true, [OPERATION_CONTEXT_SYMBOL]: { key: 'key', type: 'mutator' } } }, + { context: { batch: true, [SWR_OPERATION_CONTEXT_SYMBOL]: { key: 'key', type: 'mutator' } } }, ) }) }) diff --git a/packages/swr/src/procedure-utils.ts b/packages/swr/src/procedure-utils.ts index 0a2c2c0b1..d8817e4f6 100644 --- a/packages/swr/src/procedure-utils.ts +++ b/packages/swr/src/procedure-utils.ts @@ -13,11 +13,11 @@ import type { KeyOptions, Mutator, MutatorOptions, - OperationContext, + SWROperationContext, Subscriber, SubscriberOptions, } from './types' -import { OPERATION_CONTEXT_SYMBOL } from './types' +import { SWR_OPERATION_CONTEXT_SYMBOL } from './types' export interface ProcedureUtilsOptions extends OperationKeyPrefixOptions {} @@ -74,9 +74,9 @@ export class ProcedureUtils< return this.call(input, { context: { - [OPERATION_CONTEXT_SYMBOL]: { key, type: 'fetcher' }, + [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'fetcher' }, ...options.context, - } satisfies OperationContext as any, + } satisfies SWROperationContext as any, }) } } @@ -104,9 +104,9 @@ export class ProcedureUtils< const iterator = await this.call(input, { context: { - [OPERATION_CONTEXT_SYMBOL]: { key, type: 'subscriber' }, + [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'subscriber' }, ...options.context, - } satisfies OperationContext as any, + } satisfies SWROperationContext as any, signal: controller.signal, }) @@ -175,9 +175,9 @@ export class ProcedureUtils< const iterator = await this.call(input, { context: { - [OPERATION_CONTEXT_SYMBOL]: { key, type: 'liveSubscriber' }, + [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'liveSubscriber' }, ...options.context, - } satisfies OperationContext as any, + } satisfies SWROperationContext as any, signal: controller.signal, }) @@ -224,9 +224,9 @@ export class ProcedureUtils< return (key, { arg }) => this.call(arg, { context: { - [OPERATION_CONTEXT_SYMBOL]: { key, type: 'mutator' }, + [SWR_OPERATION_CONTEXT_SYMBOL]: { key, type: 'mutator' }, ...options.context, - } satisfies OperationContext as any, + } satisfies SWROperationContext as any, }) } } diff --git a/packages/swr/src/router-utils.ts b/packages/swr/src/router-utils.ts index 39a2da086..4310f7db4 100644 --- a/packages/swr/src/router-utils.ts +++ b/packages/swr/src/router-utils.ts @@ -9,6 +9,12 @@ import { SharedUtils } from './shared-utils' export type RouterUtils = RouterUtilsInternal +/** + * The recursion behind {@link RouterUtils}. Exported only so TypeScript can name it + * in declarations emitted for generic code, do not use it directly. + * + * @internal + */ export type RouterUtilsInternal = T extends Client ? Public> diff --git a/packages/swr/src/types.ts b/packages/swr/src/types.ts index 99eda0954..b911ad620 100644 --- a/packages/swr/src/types.ts +++ b/packages/swr/src/types.ts @@ -15,7 +15,7 @@ export type OperationType = 'fetcher' | 'mutator' | 'subscriber' | 'liveSubscrib * * @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context} */ -export const OPERATION_CONTEXT_SYMBOL: unique symbol = /* @__PURE__ */ Symbol.for( +export const SWR_OPERATION_CONTEXT_SYMBOL: unique symbol = /* @__PURE__ */ Symbol.for( 'ORPC_SWR_OPERATION_CONTEXT', ) @@ -25,8 +25,8 @@ export const OPERATION_CONTEXT_SYMBOL: unique symbol = /* @__PURE__ */ Symbol.fo * * @see {@link https://orpc.dev/docs/integrations/swr#operation-context | SWR Integration - Operation Context} */ -export interface OperationContext { - [OPERATION_CONTEXT_SYMBOL]?: { +export interface SWROperationContext { + [SWR_OPERATION_CONTEXT_SYMBOL]?: { key: unknown type: OperationType } diff --git a/packages/tanstack-query/src/contract-utils.test-d.ts b/packages/tanstack-query/src/contract-utils.test-d.ts index 6371c6d87..ee1603e79 100644 --- a/packages/tanstack-query/src/contract-utils.test-d.ts +++ b/packages/tanstack-query/src/contract-utils.test-d.ts @@ -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 { TanstackQueryOperationContext } from './types' const contract = { ping: oc.meta(meta.path(['ping'])), @@ -29,7 +29,7 @@ describe('createContractUtilsFactory', () => { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & TanstackQueryOperationContext>() const result = next() @@ -43,7 +43,9 @@ describe('createContractUtilsFactory', () => { queryInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf< + { cache?: boolean } & TanstackQueryOperationContext + >() const result = next() @@ -60,7 +62,9 @@ describe('createContractUtilsFactory', () => { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf< + { cache?: boolean } & TanstackQueryOperationContext + >() const result = next() @@ -110,7 +114,7 @@ describe('createContractJsonifiedUtilsFactory', () => { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & TanstackQueryOperationContext>() const result = next() @@ -124,7 +128,9 @@ describe('createContractJsonifiedUtilsFactory', () => { queryInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf< + { cache?: boolean } & TanstackQueryOperationContext + >() const result = next() @@ -141,7 +147,9 @@ describe('createContractJsonifiedUtilsFactory', () => { mutationInterceptors: [ async ({ context, next, input }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf<{ cache?: boolean } & OperationContext>() + expectTypeOf(context).toEqualTypeOf< + { cache?: boolean } & TanstackQueryOperationContext + >() const result = next() diff --git a/packages/tanstack-query/src/index.test.ts b/packages/tanstack-query/src/index.test.ts index cf9f87c51..7a3348172 100644 --- a/packages/tanstack-query/src/index.test.ts +++ b/packages/tanstack-query/src/index.test.ts @@ -1,6 +1,12 @@ it('exports createTanstackQueryUtils, TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL', async () => { - await expect(import('./index')).resolves.toMatchObject({ + const exports = await import('./index') + + expect(exports).toMatchObject({ createTanstackQueryUtils: expect.any(Function), + createRouterUtils: expect.any(Function), + ProcedureUtils: expect.any(Function), + SharedUtils: expect.any(Function), TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL: expect.any(Symbol), + OPERATION_CONTEXT_SYMBOL: expect.any(Symbol), }) }) diff --git a/packages/tanstack-query/src/index.ts b/packages/tanstack-query/src/index.ts index 59f6271d0..7e795ca31 100644 --- a/packages/tanstack-query/src/index.ts +++ b/packages/tanstack-query/src/index.ts @@ -30,8 +30,14 @@ export type { SerializableStreamedQueryOptions, SettleOnFirstDataOptions } from export * from './structural-sharing' export * from './types' export { - OPERATION_CONTEXT_SYMBOL as TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL, - type OperationContext as TanstackQueryOperationContext, + /** + * @deprecated Use `TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL` instead. + */ + TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL as OPERATION_CONTEXT_SYMBOL, + /** + * @deprecated Use `TanstackQueryOperationContext` instead. + */ + type TanstackQueryOperationContext as OperationContext, } from './types' export type { AnyNestedClient } from '@orpc/client' diff --git a/packages/tanstack-query/src/meta.test-d.ts b/packages/tanstack-query/src/meta.test-d.ts index a79a8b643..af957b6fe 100644 --- a/packages/tanstack-query/src/meta.test-d.ts +++ b/packages/tanstack-query/src/meta.test-d.ts @@ -5,7 +5,7 @@ import type { PromiseWithError } from '@orpc/shared' import type { SkipToken } from '@tanstack/query-core' import { tanstackQuery } from './meta' -import type { OperationContext } from './types' +import type { TanstackQueryOperationContext } from './types' describe('tanstackQuery', () => { it('infers input, output and error types from the contract', () => { @@ -18,7 +18,7 @@ describe('tanstackQuery', () => { queryInterceptors: [ async ({ context, input, next }) => { expectTypeOf(input).toEqualTypeOf() - expectTypeOf(context).toEqualTypeOf() + expectTypeOf(context).toEqualTypeOf() const result = next() diff --git a/packages/tanstack-query/src/procedure-utils.test-d.ts b/packages/tanstack-query/src/procedure-utils.test-d.ts index 409d00a3d..66cd58c93 100644 --- a/packages/tanstack-query/src/procedure-utils.test-d.ts +++ b/packages/tanstack-query/src/procedure-utils.test-d.ts @@ -293,7 +293,7 @@ describe('ProcedureUtils', () => { expectTypeOf( optionalUtils.streamedKey({ input: { search: 'search' }, - queryFnOptions: { + fnOptions: { maxChunks: 1, refetchMode: 'append', }, @@ -301,7 +301,7 @@ describe('ProcedureUtils', () => { ).toExtend() // @ts-expect-error - 'replace' is no longer a refetch mode - optionalUtils.streamedKey({ queryFnOptions: { refetchMode: 'replace' } }) + optionalUtils.streamedKey({ fnOptions: { refetchMode: 'replace' } }) // @ts-expect-error - settleOnFirstData is not part of the key optionalUtils.streamedKey({ settleOnFirstData: true }) }) @@ -375,7 +375,7 @@ describe('ProcedureUtils', () => { streamUtils.streamedOptions({ initialData: () => 'invalid' }) }) - it('accepts settleOnFirstData and serializable queryFnOptions', () => { + it('accepts settleOnFirstData and serializable fnOptions', () => { streamUtils.streamedOptions({ settleOnFirstData: true }) streamUtils.streamedOptions({ settleOnFirstData: false }) // @ts-expect-error - invalid settleOnFirstData @@ -390,7 +390,7 @@ describe('ProcedureUtils', () => { settleOnFirstData: true, }) - streamUtils.streamedOptions({ queryFnOptions: { refetchMode: 'reset', maxChunks: 10 } }) + streamUtils.streamedOptions({ fnOptions: { refetchMode: 'reset', maxChunks: 10 } }) }) it('return valid streamed options', () => { @@ -1116,7 +1116,7 @@ describe('ProcedureUtilsOptions', () => { const _defaults: TestOptions = { streamedKey: { input: { search: 'test' }, - queryFnOptions: { maxChunks: 10 }, + fnOptions: { maxChunks: 10 }, }, } @@ -1133,7 +1133,7 @@ describe('ProcedureUtilsOptions', () => { return { input: { search: 'test' }, - queryFnOptions: { maxChunks: 10 }, + fnOptions: { maxChunks: 10 }, } }, } diff --git a/packages/tanstack-query/src/procedure-utils.test.ts b/packages/tanstack-query/src/procedure-utils.test.ts index 24057912f..c5cc750c8 100644 --- a/packages/tanstack-query/src/procedure-utils.test.ts +++ b/packages/tanstack-query/src/procedure-utils.test.ts @@ -9,7 +9,7 @@ import { ProcedureUtils, } from './procedure-utils' import * as streamQueryModule from './stream-query' -import { OPERATION_CONTEXT_SYMBOL } from './types' +import { TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL } from './types' const streamedQuerySpy = vi.spyOn(streamQueryModule, 'serializableStreamedQuery') @@ -60,7 +60,7 @@ describe('procedureUtils', () => { input: 1, }) - keyedUtils.streamedKey({ input: 1, queryFnOptions: { maxChunks: 1 } } as any) + keyedUtils.streamedKey({ input: 1, fnOptions: { maxChunks: 1 } } as any) expect(generateOperationKeySpy).toHaveBeenNthCalledWith(2, ['ping'], { prefix: '__prefix__', serializer, @@ -122,7 +122,7 @@ describe('procedureUtils', () => { signal, context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'query', }, @@ -184,7 +184,7 @@ describe('procedureUtils', () => { const options = utils.streamedOptions({ input: { search: '__search__' }, context: { batch: '__batch__' }, - queryFnOptions: { + fnOptions: { refetchMode: 'append', }, }) @@ -224,7 +224,7 @@ describe('procedureUtils', () => { signal: expect.any(AbortSignal), context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'streamed', }, @@ -236,7 +236,7 @@ describe('procedureUtils', () => { it('passes settleOnFirstData to the query function, not to the key or the query options', async () => { const options = utils.streamedOptions({ input: { search: '__search__' }, - queryFnOptions: { maxChunks: 2 }, + fnOptions: { maxChunks: 2 }, settleOnFirstData: true, }) @@ -292,7 +292,7 @@ describe('procedureUtils', () => { signal: expect.any(AbortSignal), context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'streamed', }, @@ -370,7 +370,7 @@ describe('procedureUtils', () => { signal: expect.any(AbortSignal), context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'live', }, @@ -433,7 +433,7 @@ describe('procedureUtils', () => { signal: expect.any(AbortSignal), context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'live', }, @@ -513,7 +513,7 @@ describe('procedureUtils', () => { signal, context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'infinite', }, @@ -592,7 +592,7 @@ describe('procedureUtils', () => { expect(client).toHaveBeenCalledWith('__input__', { context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.mutationKey, type: 'mutation', }, @@ -1296,7 +1296,7 @@ describe('createProcedureUtils with interceptors', () => { input: { search: '__search__' }, context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'query', }, @@ -1309,7 +1309,7 @@ describe('createProcedureUtils with interceptors', () => { signal, context: { batch: '__intercepted_batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'query', }, @@ -1354,7 +1354,7 @@ describe('createProcedureUtils with interceptors', () => { input: { search: '__search__' }, context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'streamed', }, @@ -1371,7 +1371,7 @@ describe('createProcedureUtils with interceptors', () => { signal: expect.any(AbortSignal), context: { batch: '__intercepted_batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'streamed', }, @@ -1416,7 +1416,7 @@ describe('createProcedureUtils with interceptors', () => { input: { search: '__search__' }, context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'live', }, @@ -1433,7 +1433,7 @@ describe('createProcedureUtils with interceptors', () => { signal: expect.any(AbortSignal), context: { batch: '__intercepted_batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'live', }, @@ -1478,7 +1478,7 @@ describe('createProcedureUtils with interceptors', () => { input: { search: '__search__', pageParam: '__pageParam__' }, context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'infinite', }, @@ -1491,7 +1491,7 @@ describe('createProcedureUtils with interceptors', () => { signal, context: { batch: '__intercepted_batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.queryKey, type: 'infinite', }, @@ -1529,7 +1529,7 @@ describe('createProcedureUtils with interceptors', () => { input: '__input__', context: { batch: '__batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.mutationKey, type: 'mutation', }, @@ -1539,7 +1539,7 @@ describe('createProcedureUtils with interceptors', () => { expect(client).toHaveBeenCalledWith('__intercepted_input__', { context: { batch: '__intercepted_batch__', - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: options.mutationKey, type: 'mutation', }, diff --git a/packages/tanstack-query/src/procedure-utils.ts b/packages/tanstack-query/src/procedure-utils.ts index 80584b920..9c6fe4971 100644 --- a/packages/tanstack-query/src/procedure-utils.ts +++ b/packages/tanstack-query/src/procedure-utils.ts @@ -34,7 +34,7 @@ import type { MutationOptionsIn, MutationOptionsOut, MutationOptionsInRest, - OperationContext, + TanstackQueryOperationContext, QueryKeyOptions, QueryOptionsIn, QueryOptionsOut, @@ -44,7 +44,7 @@ import type { StreamedOptionsOut, StreamedOptionsInRest, } from './types' -import { OPERATION_CONTEXT_SYMBOL } from './types' +import { TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL } from './types' function resolveOptionsInRest(rest: [options?: T]): T { return rest[0] ?? ({} as T) // only undefined when options are optional @@ -55,7 +55,7 @@ export interface ProcedureUtilsQueryInterceptorOptions< TInput, > { path: string[] - context: TClientContext & OperationContext + context: TClientContext & TanstackQueryOperationContext input: TInput | SkipToken fnContext: QueryFunctionContext } @@ -451,7 +451,7 @@ export class ProcedureUtils< const queryKey = this.queryKey(optionsIn) return { - structuralSharing: createStructuralSharing(this.options.serializer), + structuralSharing: createStructuralSharing({ serializer: this.options.serializer }), ...(optionsIn as any), queryKey, queryFn: (fnContext) => { @@ -460,12 +460,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: queryKey, type: 'query', }, ...optionsIn.context, - } satisfies OperationContext as any, + } satisfies TanstackQueryOperationContext as any, input: optionsIn.input as TInput | SkipToken, fnContext, }, @@ -507,7 +507,7 @@ export class ProcedureUtils< this.key({ type: 'streamed', input: (optionsIn as any).input, - fnOptions: (optionsIn as any).queryFnOptions, + fnOptions: (optionsIn as any).fnOptions, }) return queryKey as DataTag, TError> @@ -551,12 +551,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: queryKey, type: 'streamed', }, ...optionsIn.context, - } satisfies OperationContext as any, + } satisfies TanstackQueryOperationContext as any, input: optionsIn.input as TInput | SkipToken, fnContext, }, @@ -579,7 +579,7 @@ export class ProcedureUtils< return output }, - { ...optionsIn.queryFnOptions, settleOnFirstData }, + { ...optionsIn.fnOptions, settleOnFirstData }, )(fnContext) }, ) @@ -637,7 +637,7 @@ export class ProcedureUtils< const { settleOnFirstData, ...queryOptions } = optionsIn return { - structuralSharing: createStructuralSharing(this.options.serializer), + structuralSharing: createStructuralSharing({ serializer: this.options.serializer }), ...(queryOptions as any), queryKey, queryFn: (fnContext) => { @@ -646,12 +646,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: queryKey, type: 'live', }, ...optionsIn.context, - } satisfies OperationContext as any, + } satisfies TanstackQueryOperationContext as any, input: optionsIn.input as TInput | SkipToken, fnContext, }, @@ -740,7 +740,7 @@ export class ProcedureUtils< const queryKey = this.infiniteKey(optionsIn) return { - structuralSharing: createStructuralSharing(this.options.serializer), + structuralSharing: createStructuralSharing({ serializer: this.options.serializer }), ...(optionsIn as any), queryKey, queryFn: (fnContext) => { @@ -749,12 +749,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: queryKey, type: 'infinite', }, ...optionsIn.context, - } satisfies OperationContext as any, + } satisfies TanstackQueryOperationContext as any, input: optionsIn.input === skipToken ? skipToken @@ -819,12 +819,12 @@ export class ProcedureUtils< { path: this.path, context: { - [OPERATION_CONTEXT_SYMBOL]: { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]: { key: mutationKey, type: 'mutation', }, ...optionsIn.context, - } satisfies OperationContext as any, + } satisfies TanstackQueryOperationContext as any, fnContext, input, }, diff --git a/packages/tanstack-query/src/router-utils.ts b/packages/tanstack-query/src/router-utils.ts index a674da31c..87ba7aeea 100644 --- a/packages/tanstack-query/src/router-utils.ts +++ b/packages/tanstack-query/src/router-utils.ts @@ -29,6 +29,12 @@ import { SharedUtils } from './shared-utils' */ export type RouterUtils = RouterUtilsInternal +/** + * The recursion behind {@link RouterUtils}. Exported only so TypeScript can name it + * in declarations emitted for generic code, do not use it directly. + * + * @internal + */ export type RouterUtilsInternal = T extends Client ? Public> @@ -38,6 +44,12 @@ export type RouterUtilsInternal = export type RouterUtilsScoped = RouterUtilsScopedInternal +/** + * The recursion behind {@link RouterUtilsScoped}. Exported only so TypeScript can name it + * in declarations emitted for generic code, do not use it directly. + * + * @internal + */ export type RouterUtilsScopedInternal = T extends Client ? ProcedureUtilsOptions diff --git a/packages/tanstack-query/src/ssr.test-d.ts b/packages/tanstack-query/src/ssr.test-d.ts index 7e6c9320a..3fa171e5b 100644 --- a/packages/tanstack-query/src/ssr.test-d.ts +++ b/packages/tanstack-query/src/ssr.test-d.ts @@ -5,10 +5,14 @@ import { createRouterUtils } from './router-utils' import { createHydrationOptions, StreamQuerySSRUtilsPlugin } from './ssr' it('accepts an RPCSerializer', () => { - createHydrationOptions(new RPCSerializer()) + createHydrationOptions() + createHydrationOptions({}) + createHydrationOptions({ serializer: new RPCSerializer() }) // @ts-expect-error not a serializer - createHydrationOptions({}) + createHydrationOptions({ serializer: {} }) + // @ts-expect-error the serializer is no longer a positional argument + createHydrationOptions(new RPCSerializer()) }) it('StreamQuerySSRUtilsPlugin infers the client', () => { diff --git a/packages/tanstack-query/src/ssr.test.ts b/packages/tanstack-query/src/ssr.test.ts index c6acdf5ea..4c6bc74ae 100644 --- a/packages/tanstack-query/src/ssr.test.ts +++ b/packages/tanstack-query/src/ssr.test.ts @@ -45,8 +45,8 @@ describe('createHydrationOptions', () => { constructor(public name: string) {} } - const options = createHydrationOptions( - new RPCJsonSerializer({ + const options = createHydrationOptions({ + serializer: new RPCJsonSerializer({ handlers: { person: { condition: (value) => value instanceof Person, @@ -55,7 +55,7 @@ describe('createHydrationOptions', () => { }, }, }), - ) + }) expect(roundTrip({ person: new Person('Earth') }, options)).toEqual({ person: new Person('Earth'), diff --git a/packages/tanstack-query/src/ssr.ts b/packages/tanstack-query/src/ssr.ts index a461ca543..14aa1fee4 100644 --- a/packages/tanstack-query/src/ssr.ts +++ b/packages/tanstack-query/src/ssr.ts @@ -12,17 +12,27 @@ import type { RouterUtilsPlugin } from './plugin' import type { ProcedureUtilsOptions } from './procedure-utils' import { mergeProcedureUtilsOptions } from './procedure-utils' +export interface CreateHydrationOptionsOptions { + /** + * Serializes query data when dehydrating, and deserializes it when hydrating. + * Pass the serializer your link uses so custom types are supported. + * + * @default new RPCJsonSerializer() + */ + serializer?: Public | undefined +} + /** * Creates `dehydrate` and `hydrate` query client defaults that preserve oRPC types. * * @see {@link https://orpc.dev/docs/integrations/tanstack-query#custom-serializers | TanStack Query Integration - Custom Serializers} */ -export function createHydrationOptions( - serializer: Public = new RPCJsonSerializer(), -): { +export function createHydrationOptions(options: CreateHydrationOptionsOptions = {}): { dehydrate: { serializeData: (data: unknown) => RPCJsonSerialization } hydrate: { deserializeData: (data: RPCJsonSerialization) => unknown } } { + const serializer = options.serializer ?? new RPCJsonSerializer() + return { dehydrate: { serializeData(data) { diff --git a/packages/tanstack-query/src/structural-sharing.test.ts b/packages/tanstack-query/src/structural-sharing.test.ts index f13deb441..162ce7d04 100644 --- a/packages/tanstack-query/src/structural-sharing.test.ts +++ b/packages/tanstack-query/src/structural-sharing.test.ts @@ -158,8 +158,8 @@ describe('structuralSharing', () => { }) describe('with a serializer', () => { - const sharing = createStructuralSharing( - new RPCJsonSerializer({ + const sharing = createStructuralSharing({ + serializer: new RPCJsonSerializer({ handlers: { person: { condition: (value) => value instanceof Person, @@ -173,7 +173,7 @@ describe('structuralSharing', () => { }, }, }), - ) + }) it('keeps equal instances of types with custom handlers', () => { const old = { people: [new Person('Earth', new Date('2025-01-01'))] } @@ -199,8 +199,8 @@ describe('structuralSharing', () => { }) it('compares dates with own fields through a custom date handler', () => { - const zoned = createStructuralSharing( - new RPCJsonSerializer({ + const zoned = createStructuralSharing({ + serializer: new RPCJsonSerializer({ handlers: { date: { condition: (value) => value instanceof Date, @@ -210,7 +210,7 @@ describe('structuralSharing', () => { }, }, }), - ) + }) const old = Object.assign(new Date(0), { zone: 'UTC' }) const next = Object.assign(new Date(0), { zone: 'PST' }) @@ -244,8 +244,8 @@ describe('structuralSharing', () => { }) describe('with an OpenAPI serializer', () => { - const sharing = createStructuralSharing( - new OpenAPIJsonSerializer({ + const sharing = createStructuralSharing({ + serializer: new OpenAPIJsonSerializer({ handlers: { person: { condition: (value) => value instanceof Person, @@ -253,7 +253,7 @@ describe('structuralSharing', () => { }, }, }), - ) + }) it.each([ ['bigint and string members', new Set([1n]), new Set(['1'])], diff --git a/packages/tanstack-query/src/structural-sharing.ts b/packages/tanstack-query/src/structural-sharing.ts index b4af07c27..187091382 100644 --- a/packages/tanstack-query/src/structural-sharing.ts +++ b/packages/tanstack-query/src/structural-sharing.ts @@ -4,6 +4,14 @@ import { setOwn, stringifyJSON } from '@orpc/shared' export type StructuralSharing = (oldData: unknown, newData: T) => T +export interface CreateStructuralSharingOptions { + /** + * Compares custom types by what their handler serializes them to. + * Without it, only the built-in types are compared. + */ + serializer?: Public | undefined +} + /** * Creates a `structuralSharing` function that works like TanStack Query's default, but also keeps * the previous reference for equal `Date`, `URL`, `Map` and `Set` values, which oRPC's serializer @@ -14,7 +22,11 @@ export type StructuralSharing = (oldData: unknown, newData: T) => T * * @see {@link https://orpc.dev/docs/integrations/tanstack-query#structural-sharing | TanStack Query Integration - Structural Sharing} */ -export function createStructuralSharing(serializer?: Public): StructuralSharing { +export function createStructuralSharing( + options: CreateStructuralSharingOptions = {}, +): StructuralSharing { + const serializer = options.serializer + return (oldData, newData) => replaceEqualDeep(oldData, newData, 0, serializer) } diff --git a/packages/tanstack-query/src/types.ts b/packages/tanstack-query/src/types.ts index 2e6d4012e..80724b633 100644 --- a/packages/tanstack-query/src/types.ts +++ b/packages/tanstack-query/src/types.ts @@ -23,7 +23,7 @@ export type OperationType = 'query' | 'streamed' | 'live' | 'infinite' | 'mutati * * @see {@link https://orpc.dev/docs/integrations/tanstack-query#client-context | TanStack Query Integration - Client Context} */ -export const OPERATION_CONTEXT_SYMBOL: unique symbol = /* @__PURE__ */ Symbol.for( +export const TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL: unique symbol = /* @__PURE__ */ Symbol.for( 'ORPC_TANSTACK_QUERY_OPERATION_CONTEXT', ) as any @@ -33,8 +33,8 @@ export const OPERATION_CONTEXT_SYMBOL: unique symbol = /* @__PURE__ */ Symbol.fo * * @see {@link https://orpc.dev/docs/integrations/tanstack-query#client-context | TanStack Query Integration - Client Context} */ -export interface OperationContext { - [OPERATION_CONTEXT_SYMBOL]?: { +export interface TanstackQueryOperationContext { + [TANSTACK_QUERY_OPERATION_CONTEXT_SYMBOL]?: { key: QueryKey type: OperationType } @@ -87,7 +87,7 @@ export type QueryOptionsOut = Pick< export type StreamedKeyOptions = | ((undefined extends TInput ? { input?: TInput | SkipToken } : { input: TInput | SkipToken }) & { - queryFnOptions?: SerializableStreamedQueryOptions | undefined + fnOptions?: SerializableStreamedQueryOptions | undefined }) | { queryKey: QueryKey } @@ -99,7 +99,7 @@ export type StreamedOptionsIn< TSelectData, TInitialData, > = QueryOptionsIn & { - queryFnOptions?: SerializableStreamedQueryOptions | undefined + fnOptions?: SerializableStreamedQueryOptions | undefined } & SettleOnFirstDataOptions /** diff --git a/packages/tanstack-query/tests/e2e.test.tsx b/packages/tanstack-query/tests/e2e.test.tsx index 7a95a5b52..deb03ee37 100644 --- a/packages/tanstack-query/tests/e2e.test.tsx +++ b/packages/tanstack-query/tests/e2e.test.tsx @@ -94,7 +94,7 @@ it('case: with streamed/useQuery', async () => { const { result } = renderHook(() => useQuery( orpc.stream.streamedOptions({ - queryFnOptions: { + fnOptions: { refetchMode: 'append', maxChunks: 3, }, @@ -132,7 +132,7 @@ it('case: with streamed/useQuery', async () => { queryClient.getQueryData( orpc.stream.streamedKey({ input: { input: 2 }, - queryFnOptions: { refetchMode: 'append', maxChunks: 3 }, + fnOptions: { refetchMode: 'append', maxChunks: 3 }, }), ), ).toEqual([{ output: '0' }, { output: '1' }]) diff --git a/playgrounds/bun/src/components/chat-room.tsx b/playgrounds/bun/src/components/chat-room.tsx index eec281f1e..47cd156e5 100644 --- a/playgrounds/bun/src/components/chat-room.tsx +++ b/playgrounds/bun/src/components/chat-room.tsx @@ -11,7 +11,7 @@ export function ChatRoom() { orpc.message.subscribe.streamedOptions({ input: { channel: CHANNEL }, context: { retry: Infinity }, - queryFnOptions: { maxChunks: 10 }, + fnOptions: { maxChunks: 10 }, }), ) diff --git a/playgrounds/bun/src/lib/orpc.ts b/playgrounds/bun/src/lib/orpc.ts index b9b739500..90f496286 100644 --- a/playgrounds/bun/src/lib/orpc.ts +++ b/playgrounds/bun/src/lib/orpc.ts @@ -3,7 +3,7 @@ import type { RetryLinkPluginContext } from '@orpc/client/plugins' import { RetryLinkPlugin } from '@orpc/client/plugins' import { RPCLink } from '@orpc/client/websocket' import type { RouterClient } from '@orpc/server' -import { createRouterUtils } from '@orpc/tanstack-query' +import { createTanstackQueryUtils } from '@orpc/tanstack-query' import type { router } from '../routers' @@ -16,4 +16,4 @@ const link = new RPCLink({ export const client: RouterClient = createORPCClient(link) -export const orpc = createRouterUtils(client) +export const orpc = createTanstackQueryUtils(client) diff --git a/playgrounds/cloudflare/src/components/chat-room.tsx b/playgrounds/cloudflare/src/components/chat-room.tsx index eec281f1e..47cd156e5 100644 --- a/playgrounds/cloudflare/src/components/chat-room.tsx +++ b/playgrounds/cloudflare/src/components/chat-room.tsx @@ -11,7 +11,7 @@ export function ChatRoom() { orpc.message.subscribe.streamedOptions({ input: { channel: CHANNEL }, context: { retry: Infinity }, - queryFnOptions: { maxChunks: 10 }, + fnOptions: { maxChunks: 10 }, }), ) diff --git a/playgrounds/cloudflare/src/lib/orpc.ts b/playgrounds/cloudflare/src/lib/orpc.ts index bdd802fac..558bd5be5 100644 --- a/playgrounds/cloudflare/src/lib/orpc.ts +++ b/playgrounds/cloudflare/src/lib/orpc.ts @@ -3,7 +3,7 @@ import { RPCLink } from '@orpc/client/fetch' import type { RetryLinkPluginContext } from '@orpc/client/plugins' import { BatchLinkPlugin, RetryLinkPlugin } from '@orpc/client/plugins' import type { RouterClient } from '@orpc/server' -import { createRouterUtils } from '@orpc/tanstack-query' +import { createTanstackQueryUtils } from '@orpc/tanstack-query' import type { router } from '../../worker/routers' @@ -27,4 +27,4 @@ const link = new RPCLink({ export const client: RouterClient = createORPCClient(link) -export const orpc = createRouterUtils(client) +export const orpc = createTanstackQueryUtils(client) diff --git a/playgrounds/next/src/components/chat-room.tsx b/playgrounds/next/src/components/chat-room.tsx index d5c6a0901..8426fea3b 100644 --- a/playgrounds/next/src/components/chat-room.tsx +++ b/playgrounds/next/src/components/chat-room.tsx @@ -13,7 +13,7 @@ export function ChatRoom() { orpc.message.subscribe.streamedOptions({ input: { channel: CHANNEL }, context: { retry: Infinity }, - queryFnOptions: { maxChunks: 10 }, + fnOptions: { maxChunks: 10 }, }), ) diff --git a/playgrounds/next/src/lib/orpc.ts b/playgrounds/next/src/lib/orpc.ts index 4abc9101b..8b924e507 100644 --- a/playgrounds/next/src/lib/orpc.ts +++ b/playgrounds/next/src/lib/orpc.ts @@ -3,7 +3,7 @@ import { RPCLink } from '@orpc/client/fetch' import type { RetryLinkPluginContext } from '@orpc/client/plugins' import { BatchLinkPlugin, RetryLinkPlugin } from '@orpc/client/plugins' import type { RouterClient } from '@orpc/server' -import { createRouterUtils, StreamQuerySSRUtilsPlugin } from '@orpc/tanstack-query' +import { createTanstackQueryUtils, StreamQuerySSRUtilsPlugin } from '@orpc/tanstack-query' import type { router } from '@/routers' @@ -52,6 +52,6 @@ const link = new RPCLink({ export const client: RouterClient = globalThis.$client ?? createORPCClient(link) -export const orpc = createRouterUtils(client, { +export const orpc = createTanstackQueryUtils(client, { plugins: [new StreamQuerySSRUtilsPlugin()], }) diff --git a/playgrounds/next/src/lib/query-client.ts b/playgrounds/next/src/lib/query-client.ts index 68c55b00c..e40e5e15f 100644 --- a/playgrounds/next/src/lib/query-client.ts +++ b/playgrounds/next/src/lib/query-client.ts @@ -31,7 +31,7 @@ function createQueryClient(): QueryClient { queries: { staleTime: 60 * 1000, // > 0 to prevent immediate refetching on mount }, - ...createHydrationOptions(serializer), + ...createHydrationOptions({ serializer }), }, }) }