-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitTRPC.ts
158 lines (145 loc) · 4.59 KB
/
initTRPC.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { mergeRoutersGeneric } from './internals/__generated__/mergeRoutersGeneric';
import {
DefaultErrorShape,
ErrorFormatter,
ErrorFormatterShape,
defaultFormatter,
} from '../error/formatter';
import { createFlatProxy } from '../shared';
import {
CombinedDataTransformer,
DataTransformerOptions,
DefaultDataTransformer,
defaultTransformer,
getDataTransformer,
} from '../transformer';
import { FlatOverwrite } from '../types';
import {
CreateRootConfigTypes,
RootConfig,
RootConfigTypes,
RuntimeConfig,
isServerDefault,
} from './internals/config';
import { createBuilder } from './internals/procedureBuilder';
import { PickFirstDefined, ValidateShape } from './internals/utils';
import { createMiddlewareFactory } from './middleware';
import { createRouterFactory } from './router';
type PartialRootConfigTypes = Partial<RootConfigTypes>;
type CreateRootConfigTypesFromPartial<TTypes extends PartialRootConfigTypes> =
CreateRootConfigTypes<{
ctx: TTypes['ctx'] extends RootConfigTypes['ctx'] ? TTypes['ctx'] : {};
meta: TTypes['meta'] extends RootConfigTypes['meta'] ? TTypes['meta'] : {};
errorShape: TTypes['errorShape'];
transformer: DataTransformerOptions;
}>;
/**
* TODO: This can be improved:
* - We should be able to chain `.meta()`/`.context()` only once
* - Simplify typings
* - Doesn't need to be a class but it doesn't really hurt either
*/
class TRPCBuilder<TParams extends PartialRootConfigTypes = {}> {
context<TNewContext extends RootConfigTypes['ctx']>() {
return new TRPCBuilder<FlatOverwrite<TParams, { ctx: TNewContext }>>();
}
meta<TNewMeta extends RootConfigTypes['meta']>() {
return new TRPCBuilder<FlatOverwrite<TParams, { meta: TNewMeta }>>();
}
create<
TOptions extends Partial<
RuntimeConfig<CreateRootConfigTypesFromPartial<TParams>>
>,
>(
options?:
| ValidateShape<
TOptions,
Partial<RuntimeConfig<CreateRootConfigTypesFromPartial<TParams>>>
>
| undefined,
) {
return createTRPCInner<TParams>()<TOptions>(options);
}
}
/**
* Initialize tRPC - be done exactly once per backend
*/
export const initTRPC = new TRPCBuilder();
function createTRPCInner<TParams extends PartialRootConfigTypes>() {
type $Generics = CreateRootConfigTypesFromPartial<TParams>;
type $Context = $Generics['ctx'];
type $Meta = $Generics['meta'];
type $Runtime = Partial<RuntimeConfig<$Generics>>;
return function initTRPCInner<TOptions extends $Runtime>(
runtime?: ValidateShape<TOptions, $Runtime>,
) {
type $Formatter = PickFirstDefined<
TOptions['errorFormatter'],
ErrorFormatter<$Context, DefaultErrorShape>
>;
type $Transformer = TOptions['transformer'] extends DataTransformerOptions
? TOptions['transformer'] extends DataTransformerOptions
? CombinedDataTransformer
: DefaultDataTransformer
: DefaultDataTransformer;
type $ErrorShape = ErrorFormatterShape<$Formatter>;
type $Config = RootConfig<{
ctx: $Context;
meta: $Meta;
errorShape: $ErrorShape;
transformer: $Transformer;
}>;
const errorFormatter = runtime?.errorFormatter ?? defaultFormatter;
const transformer = getDataTransformer(
runtime?.transformer ?? defaultTransformer,
) as $Transformer;
const config: $Config = {
transformer,
isDev:
runtime?.isDev ?? globalThis.process?.env?.NODE_ENV !== 'production',
allowOutsideOfServer: runtime?.allowOutsideOfServer ?? false,
errorFormatter,
isServer: runtime?.isServer ?? isServerDefault,
/**
* @internal
*/
$types: createFlatProxy((key) => {
throw new Error(
`Tried to access "$types.${key}" which is not available at runtime`,
);
}),
};
{
// Server check
const isServer: boolean = runtime?.isServer ?? isServerDefault;
if (!isServer && runtime?.allowOutsideOfServer !== true) {
throw new Error(
`You're trying to use @trpc/server in a non-server environment. This is not supported by default.`,
);
}
}
return {
/**
* These are just types, they can't be used
* @internal
*/
_config: config,
/**
* Builder object for creating procedures
*/
procedure: createBuilder<$Config>(),
/**
* Create reusable middlewares
*/
middleware: createMiddlewareFactory<$Config>(),
/**
* Create a router
*/
router: createRouterFactory<$Config>(config),
/**
* Merge Routers
*/
mergeRouters: mergeRoutersGeneric,
};
};
}