Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: trpc v11 #25

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.5.2",
"@trpc/client": "^10.45.2",
"@trpc/server": "^10.45.2",
"@trpc/client": "^11.0.0-rc.467",
"@trpc/server": "^11.0.0-rc.467",
"@types/node": "^20.12.12",
"@types/react": "^18.3.2",
"@types/react-dom": "^18.3.0",
Expand All @@ -84,8 +84,8 @@
"vitest": "^1.6.0"
},
"peerDependencies": {
"@trpc/client": ">=10.0.0",
"@trpc/server": ">=10.0.0",
"@trpc/client": ">=11.0.0",
"@trpc/server": ">=11.0.0",
"jotai": ">=2.0.0"
}
}
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 33 additions & 33 deletions src/createTRPCJotai.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { createTRPCProxyClient } from '@trpc/client';
import { createTRPCClient } from '@trpc/client';
import type { TRPCRequestOptions, CreateTRPCClientOptions } from '@trpc/client';
import type {
AnyMutationProcedure,
AnyProcedure,
AnyQueryProcedure,
AnySubscriptionProcedure,
AnyRouter,
ProcedureArgs,
ProcedureRouterRecord,
AnyTRPCMutationProcedure,
AnyTRPCProcedure,
AnyTRPCQueryProcedure,
AnyTRPCSubscriptionProcedure,
AnyTRPCRouter,
TRPCRouterRecord,
TRPCProcedureOptions,
inferProcedureInput,
inferProcedureOutput,
} from '@trpc/server';
Expand Down Expand Up @@ -39,7 +39,7 @@ export const DISABLED = Symbol();

type CustomOptions = { disabledOutput?: unknown };

const atomWithQuery = <TProcedure extends AnyQueryProcedure, TClient>(
const atomWithQuery = <TProcedure extends AnyTRPCQueryProcedure, TClient>(
path: string[],
getClient: (get: Getter) => TClient,
getInput: AsyncValueOrGetter<
Expand Down Expand Up @@ -69,11 +69,11 @@ const atomWithQuery = <TProcedure extends AnyQueryProcedure, TClient>(
return queryAtom;
};

const atomWithMutation = <TProcedure extends AnyMutationProcedure, TClient>(
const atomWithMutation = <TProcedure extends AnyTRPCMutationProcedure, TClient>(
path: string[],
getClient: (get: Getter) => TClient,
) => {
type Args = ProcedureArgs<TProcedure['_def']>;
type Args = [inferProcedureInput<TProcedure>, TRPCProcedureOptions];
type Output = inferProcedureOutput<TProcedure>;
const mutationAtom = atom(
null as Output | null,
Expand All @@ -88,7 +88,7 @@ const atomWithMutation = <TProcedure extends AnyMutationProcedure, TClient>(
};

const atomWithSubscription = <
TProcedure extends AnySubscriptionProcedure,
TProcedure extends AnyTRPCSubscriptionProcedure,
TClient,
>(
path: string[],
Expand Down Expand Up @@ -122,18 +122,18 @@ const atomWithSubscription = <
return subscriptionAtom;
};

type QueryResolver<TProcedure extends AnyProcedure, TClient> = {
type QueryResolver<TProcedure extends AnyTRPCProcedure, TClient> = {
(
getInput: AsyncValueOrGetter<ProcedureArgs<TProcedure['_def']>[0]>,
getOptions?: ValueOrGetter<ProcedureArgs<TProcedure['_def']>[1]>,
getInput: AsyncValueOrGetter<inferProcedureInput<TProcedure>>,
getOptions?: ValueOrGetter<TRPCProcedureOptions>,
getClient?: (get: Getter) => TClient,
): WritableAtom<Promise<inferProcedureOutput<TProcedure>>, [], void>;
(
getInput: AsyncValueOrGetter<
ProcedureArgs<TProcedure['_def']>[0] | typeof DISABLED
inferProcedureInput<TProcedure> | typeof DISABLED
>,
getOptions?: ValueOrGetter<
ProcedureArgs<TProcedure['_def']>[1] & { disabledOutput?: undefined }
TRPCProcedureOptions & { disabledOutput?: undefined }
>,
getClient?: (get: Getter) => TClient,
): WritableAtom<
Expand All @@ -143,10 +143,10 @@ type QueryResolver<TProcedure extends AnyProcedure, TClient> = {
>;
<DisabledOutput>(
getInput: AsyncValueOrGetter<
ProcedureArgs<TProcedure['_def']>[0] | typeof DISABLED
inferProcedureInput<TProcedure> | typeof DISABLED
>,
getOptions: ValueOrGetter<
ProcedureArgs<TProcedure['_def']>[1] & { disabledOutput: DisabledOutput }
TRPCProcedureOptions & { disabledOutput: DisabledOutput }
>,
getClient?: (get: Getter) => TClient,
): WritableAtom<
Expand All @@ -156,52 +156,52 @@ type QueryResolver<TProcedure extends AnyProcedure, TClient> = {
>;
};

type MutationResolver<TProcedure extends AnyProcedure, TClient> = (
type MutationResolver<TProcedure extends AnyTRPCProcedure, TClient> = (
getClient?: (get: Getter) => TClient,
) => WritableAtom<
inferProcedureOutput<TProcedure> | null,
[ProcedureArgs<TProcedure['_def']>],
[[inferProcedureInput<TProcedure>, TRPCProcedureOptions]],
Promise<inferProcedureOutput<TProcedure>>
>;

type SubscriptionResolver<TProcedure extends AnyProcedure, TClient> = (
getInput: ValueOrGetter<ProcedureArgs<TProcedure['_def']>[0]>,
getOptions?: ValueOrGetter<ProcedureArgs<TProcedure['_def']>[1]>,
type SubscriptionResolver<TProcedure extends AnyTRPCProcedure, TClient> = (
getInput: ValueOrGetter<inferProcedureInput<TProcedure>>,
getOptions?: ValueOrGetter<TRPCProcedureOptions>,
getClient?: (get: Getter) => TClient,
) => Atom<inferObservableValue<inferProcedureOutput<TProcedure>>>;

type DecorateProcedure<
TProcedure extends AnyProcedure,
TProcedure extends AnyTRPCProcedure,
TClient,
> = TProcedure extends AnyQueryProcedure
> = TProcedure extends AnyTRPCQueryProcedure
? {
atomWithQuery: QueryResolver<TProcedure, TClient>;
}
: TProcedure extends AnyMutationProcedure
: TProcedure extends AnyTRPCMutationProcedure
? {
atomWithMutation: MutationResolver<TProcedure, TClient>;
}
: TProcedure extends AnySubscriptionProcedure
: TProcedure extends AnyTRPCSubscriptionProcedure
? {
atomWithSubscription: SubscriptionResolver<TProcedure, TClient>;
}
: never;

type DecoratedProcedureRecord<
TProcedures extends ProcedureRouterRecord,
TProcedures extends TRPCRouterRecord,
TClient,
> = {
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyTRPCRouter
? DecoratedProcedureRecord<TProcedures[TKey]['_def']['record'], TClient>
: TProcedures[TKey] extends AnyProcedure
: TProcedures[TKey] extends AnyTRPCProcedure
? DecorateProcedure<TProcedures[TKey], TClient>
: never;
};

export function createTRPCJotai<TRouter extends AnyRouter>(
export function createTRPCJotai<TRouter extends AnyTRPCRouter>(
opts: CreateTRPCClientOptions<TRouter>,
) {
const client = createTRPCProxyClient<TRouter>(opts);
const client = createTRPCClient<TRouter>(opts);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const createProxy = (target: any, path: readonly string[] = []): any => {
Expand Down
Loading