-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrpc-compat.ts
More file actions
66 lines (56 loc) · 1.96 KB
/
trpc-compat.ts
File metadata and controls
66 lines (56 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { AnyRouter as TRPCAnyRouter, AnyProcedure as TRPCAnyProcedure } from '@trpc/server';
/**
* Types that approximate a tRPC v11 router and procedure to infer types correctly.
* Written to avoid direct dependency on @trpc/server v11+.
*/
export type Trpc11RouterLike = {
_def: {
_config: {
$types: { meta: unknown; ctx: unknown };
};
procedures: Record<
string,
Trpc11ProcedureLike | Trpc11RouterLike | Record<string, Trpc11ProcedureLike>
>;
};
};
export type Trpc11ProcedureLike = {
_def: {
type: 'mutation' | 'query' | 'subscription';
meta?: unknown;
inputs?: unknown[]; // Not exposed by tRPC v11 as of 11.0.0-rc.502
$types: { input: unknown; output: unknown };
};
};
export type Trpc10RouterLike = {
_def: {
_config: {
$types: { meta: unknown; ctx: unknown };
};
procedures: Record<string, Trpc10ProcedureLike | Trpc10RouterLike>;
};
};
export type Trpc10ProcedureLike = {
_def: {
mutation?: boolean;
query?: boolean;
subscription?: boolean;
meta?: unknown;
inputs: unknown[];
_input_in: unknown;
_output_out: unknown;
};
};
export type CreateCallerFactoryLike = (
router: unknown
) => (context: unknown) => Record<string, (input: unknown) => unknown>;
export type AnyRouter = Trpc10RouterLike | Trpc11RouterLike | TRPCAnyRouter | { link: any };
export type AnyProcedure = Trpc10ProcedureLike | Trpc11ProcedureLike | TRPCAnyProcedure;
export type InferRouterContext<R extends AnyRouter> = R['_def']['_config']['$types']['ctx'];
export const isTrpc11Procedure = (procedure: AnyProcedure): procedure is Trpc11ProcedureLike => {
return '_def' in procedure && 'type' in procedure._def && typeof procedure._def.type === 'string';
};
export const isTrpc11Router = (router: AnyRouter): router is Trpc11RouterLike => {
const procedure = Object.values(router._def.procedures)[0] as AnyProcedure | undefined;
return procedure ? isTrpc11Procedure(procedure) : false;
};