Skip to content

Commit

Permalink
sync @orpc/react
Browse files Browse the repository at this point in the history
  • Loading branch information
unnoq committed Dec 29, 2024
1 parent 37be305 commit 6e2e386
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 215 deletions.
13 changes: 0 additions & 13 deletions packages/client/src/procedure-fetch-client.test-d.ts

This file was deleted.

85 changes: 0 additions & 85 deletions packages/client/src/procedure-fetch-client.test.ts

This file was deleted.

87 changes: 0 additions & 87 deletions packages/client/src/procedure-fetch-client.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/react/src/procedure-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export interface ProcedureUtils<TInput, TOutput> {
}

export interface CreateProcedureUtilsOptions<TInput, TOutput> {
client: ProcedureClient<TInput, TOutput>
client: ProcedureClient<TInput, TOutput, any>
queryClient: QueryClient

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/react-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import type { RouterClient } from '@orpc/server'
import type { QueryClient } from '@tanstack/react-query'
import { type Context, createContext, useContext } from 'react'

export interface ORPCContextValue<T extends RouterClient<any>> {
export interface ORPCContextValue<T extends RouterClient<any, any>> {
client: T
queryClient: QueryClient
}

export type ORPCContext<T extends RouterClient<any>> = Context<
export type ORPCContext<T extends RouterClient<any, any>> = Context<
ORPCContextValue<T> | undefined
>

export function createORPCContext<T extends RouterClient<any>>(): ORPCContext<T> {
export function createORPCContext<T extends RouterClient<any, any>>(): ORPCContext<T> {
return createContext(undefined as any)
}

export function useORPCContext<T extends RouterClient<any>>(
export function useORPCContext<T extends RouterClient<any, any>>(
context: ORPCContext<T>,
): ORPCContextValue<T> {
const value = useContext(context)
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/react-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { createGeneralHooks, type GeneralHooks } from './general-hooks'
import { orpcPathSymbol } from './orpc-path'
import { createProcedureHooks, type ProcedureHooks } from './procedure-hooks'

export type ORPCHooks<T extends RouterClient<any>> =
T extends ProcedureClient<infer TInput, infer TOutput>
export type ORPCHooks<T extends RouterClient<any, any>> =
T extends ProcedureClient<infer TInput, infer TOutput, any>
? ProcedureHooks<TInput, TOutput> & GeneralHooks<TInput, TOutput>
: {
[K in keyof T]: T[K] extends RouterClient<any> ? ORPCHooks<T[K]> : never
[K in keyof T]: T[K] extends RouterClient<any, any> ? ORPCHooks<T[K]> : never
} & GeneralHooks<unknown, unknown>

export interface CreateORPCHooksOptions<T extends RouterClient<any>> {
export interface CreateORPCHooksOptions<T extends RouterClient<any, any>> {
context: ORPCContext<T>

/**
Expand All @@ -22,7 +22,7 @@ export interface CreateORPCHooksOptions<T extends RouterClient<any>> {
path?: string[]
}

export function createORPCHooks<T extends RouterClient<any>>(
export function createORPCHooks<T extends RouterClient<any, any>>(
options: CreateORPCHooksOptions<T>,
): ORPCHooks<T> {
const path = options.path ?? []
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/react-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import type { ORPCContextValue } from './react-context'
import { createGeneralUtils, type GeneralUtils } from './general-utils'
import { createProcedureUtils, type ProcedureUtils } from './procedure-utils'

export type ORPCUtils<T extends RouterClient<any>> =
T extends ProcedureClient<infer TInput, infer TOutput>
export type ORPCUtils<T extends RouterClient<any, any>> =
T extends ProcedureClient<infer TInput, infer TOutput, any>
? ProcedureUtils<TInput, TOutput> & GeneralUtils<TInput, TOutput>
: {
[K in keyof T]: T[K] extends RouterClient<any> ? ORPCUtils<T[K]> : never
[K in keyof T]: T[K] extends RouterClient<any, any> ? ORPCUtils<T[K]> : never
} & GeneralUtils<unknown, unknown>

export interface CreateORPCUtilsOptions<T extends RouterClient<any>> {
export interface CreateORPCUtilsOptions<T extends RouterClient<any, any>> {
contextValue: ORPCContextValue<T>

/**
Expand All @@ -21,7 +21,7 @@ export interface CreateORPCUtilsOptions<T extends RouterClient<any>> {
path?: string[]
}

export function createORPCUtils<T extends RouterClient<any>>(
export function createORPCUtils<T extends RouterClient<any, any>>(
options: CreateORPCUtilsOptions<T>,
): ORPCUtils<T> {
const path = options.path ?? []
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { createORPCHooks } from './react-hooks'
import { createORPCUtils } from './react-utils'
import { useQueriesFactory } from './use-queries/hook'

export type ORPCReact<T extends RouterClient<any>> =
export type ORPCReact<T extends RouterClient<any, any>> =
ORPCHooks<T> & {
useContext: () => ORPCContextValue<T>
useUtils: () => ORPCUtils<T>
useQueries: UseQueries<T>
}

export function createORPCReact<T extends RouterClient<any>>(): {
export function createORPCReact<T extends RouterClient<any, any>>(): {
orpc: ORPCReact<T>
ORPCContext: ORPCContext<T>
} {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/use-queries/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface UseQueriesBuilder<TInput, TOutput> {
}

export interface CreateUseQueriesBuilderOptions<TInput, TOutput> {
client: ProcedureClient<TInput, TOutput>
client: ProcedureClient<TInput, TOutput, any>

/**
* The path of procedure on server
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/use-queries/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import type { ProcedureClient, RouterClient } from '@orpc/server'
import type {} from '@tanstack/react-query'
import { createUseQueriesBuilder, type UseQueriesBuilder } from './builder'

export type UseQueriesBuilders<T extends RouterClient<any>> =
T extends ProcedureClient<infer UInput, infer UOutput>
export type UseQueriesBuilders<T extends RouterClient<any, any>> =
T extends ProcedureClient<infer UInput, infer UOutput, any>
? UseQueriesBuilder<UInput, UOutput>
: {
[K in keyof T]: T[K] extends RouterClient<any> ? UseQueriesBuilders<T[K]> : never
[K in keyof T]: T[K] extends RouterClient<any, any> ? UseQueriesBuilders<T[K]> : never
}

export interface CreateUseQueriesBuildersOptions<T extends RouterClient<any>> {
export interface CreateUseQueriesBuildersOptions<T extends RouterClient<any, any>> {
client: T

/**
Expand All @@ -18,7 +18,7 @@ export interface CreateUseQueriesBuildersOptions<T extends RouterClient<any>> {
path?: string[]
}

export function createUseQueriesBuilders<T extends RouterClient<any>>(
export function createUseQueriesBuilders<T extends RouterClient<any, any>>(
options: CreateUseQueriesBuildersOptions<T>,
): UseQueriesBuilders<T> {
const path = options.path ?? []
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/use-queries/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useQueries } from '@tanstack/react-query'
import { useORPCContext } from '../react-context'
import { createUseQueriesBuilders } from './builders'

export interface UseQueries<T extends RouterClient<any>> {
export interface UseQueries<T extends RouterClient<any, any>> {
<U extends Array<any> = [], UCombinedResult = QueriesResults<U>>(
build: (
builders: UseQueriesBuilders<T>,
Expand All @@ -18,11 +18,11 @@ export interface UseQueries<T extends RouterClient<any>> {
): UCombinedResult
}

export interface UseQueriesFactoryOptions<T extends RouterClient<any>> {
export interface UseQueriesFactoryOptions<T extends RouterClient<any, any>> {
context: ORPCContext<T>
}

export function useQueriesFactory<T extends RouterClient<any>>(
export function useQueriesFactory<T extends RouterClient<any, any>>(
options: UseQueriesFactoryOptions<T>,
): UseQueries<T> {
const Hook = (build: any, combine?: any): any => {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/lazy-decorated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type DecoratedLazy<T> = T extends Lazy<infer U>
& (
T extends Procedure<infer UContext, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput>
? undefined extends UContext
? ProcedureClient<SchemaInput<UInputSchema>, SchemaOutput<UOutputSchema, UFuncOutput>>
? ProcedureClient<SchemaInput<UInputSchema>, SchemaOutput<UOutputSchema, UFuncOutput>, unknown>
: unknown
: {
[K in keyof T]: T[K] extends object ? DecoratedLazy<T[K]> : never
Expand Down
9 changes: 6 additions & 3 deletions playgrounds/nextjs/src/lib/orpc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { router } from '@/app/api/[...rest]/router'
import { createORPCFetchClient } from '@orpc/client'
import { createClient } from '@orpc/client'
import { ORPCLink } from '@orpc/client/fetch'
import { createORPCReact } from '@orpc/react'

export const orpcClient = createORPCFetchClient<typeof router>({
baseURL: 'http://localhost:3000/api',
const orpcLink = new ORPCLink({
url: 'http://localhost:3000/api',
headers: () => ({
Authorization: 'Bearer default-token',
}),
})

export const orpcClient = createClient<typeof router>(orpcLink)

export const { orpc, ORPCContext } = createORPCReact<typeof orpcClient>()

0 comments on commit 6e2e386

Please sign in to comment.