@trpc/server 11.0.0-next-alpha.139 → 11.0.0-next-alpha.141
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/aws-lambda/index.js +2 -2
- package/dist/adapters/aws-lambda/index.mjs +2 -2
- package/dist/adapters/express.js +3 -3
- package/dist/adapters/express.mjs +3 -3
- package/dist/adapters/fastify/index.js +2 -2
- package/dist/adapters/fastify/index.mjs +2 -2
- package/dist/adapters/fetch/index.js +2 -2
- package/dist/adapters/fetch/index.mjs +2 -2
- package/dist/adapters/next.js +3 -3
- package/dist/adapters/next.mjs +3 -3
- package/dist/adapters/node-http/index.js +3 -3
- package/dist/adapters/node-http/index.mjs +3 -3
- package/dist/adapters/standalone.js +3 -3
- package/dist/adapters/standalone.mjs +3 -3
- package/dist/adapters/ws.js +1 -1
- package/dist/adapters/ws.mjs +1 -1
- package/dist/bundle-analysis.json +68 -67
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/initTRPC.d.ts +14 -0
- package/dist/core/initTRPC.d.ts.map +1 -1
- package/dist/core/router.d.ts +6 -1
- package/dist/core/router.d.ts.map +1 -1
- package/dist/http/index.js +2 -2
- package/dist/http/index.mjs +2 -2
- package/dist/index.js +12 -3
- package/dist/index.mjs +13 -5
- package/dist/{nodeHTTPRequestHandler-84248270.js → nodeHTTPRequestHandler-3f218e71.js} +1 -1
- package/dist/{nodeHTTPRequestHandler-fb26ba4e.mjs → nodeHTTPRequestHandler-c92e159b.mjs} +1 -1
- package/dist/{nodeHTTPRequestHandler-e24d9576.js → nodeHTTPRequestHandler-f04ea0b8.js} +1 -1
- package/dist/{procedureBuilder-e77bab71.js → procedureBuilder-51db231f.js} +2 -8
- package/dist/{procedureBuilder-5431b157.mjs → procedureBuilder-955ce9bc.mjs} +2 -8
- package/dist/{procedureBuilder-416387b5.js → procedureBuilder-f3a66e27.js} +2 -8
- package/dist/{resolveHTTPResponse-c5c776d4.js → resolveHTTPResponse-62594a39.js} +1 -1
- package/dist/{resolveHTTPResponse-cbea0b76.js → resolveHTTPResponse-e5110d33.js} +1 -1
- package/dist/{resolveHTTPResponse-6ce6731d.mjs → resolveHTTPResponse-fda30ede.mjs} +1 -1
- package/dist/{router-6e6a40b3.js → router-05a567ea.js} +19 -0
- package/dist/{router-2e54d451.js → router-40fcf5e5.js} +19 -1
- package/dist/{router-accbd60f.mjs → router-8564439c.mjs} +19 -1
- package/dist/shared/internal/serialize.d.ts +38 -8
- package/dist/shared/internal/serialize.d.ts.map +1 -1
- package/dist/unstableInternalsExport.js +2 -2
- package/dist/unstableInternalsExport.mjs +2 -2
- package/package.json +2 -2
- package/src/core/index.ts +3 -1
- package/src/core/initTRPC.ts +10 -1
- package/src/core/internals/procedureBuilder.ts +1 -7
- package/src/core/router.ts +30 -1
- package/src/shared/internal/serialize.ts +58 -23
package/src/core/router.ts
CHANGED
|
@@ -46,12 +46,16 @@ type DecoratedProcedureRecord<TProcedures extends ProcedureRouterRecord> = {
|
|
|
46
46
|
/**
|
|
47
47
|
* @internal
|
|
48
48
|
*/
|
|
49
|
-
type RouterCaller<TDef extends AnyRouterDef> = (
|
|
49
|
+
export type RouterCaller<TDef extends AnyRouterDef> = (
|
|
50
50
|
ctx: TDef['_config']['$types']['ctx'],
|
|
51
51
|
) => DecoratedProcedureRecord<TDef['record']>;
|
|
52
52
|
|
|
53
53
|
export interface Router<TDef extends AnyRouterDef> {
|
|
54
54
|
_def: TDef;
|
|
55
|
+
/**
|
|
56
|
+
* @deprecated use `t.createCallerFactory(router)` instead
|
|
57
|
+
* @see https://trpc.io/docs/server/server-side-calls
|
|
58
|
+
*/
|
|
55
59
|
createCaller: RouterCaller<TDef>;
|
|
56
60
|
}
|
|
57
61
|
|
|
@@ -164,6 +168,7 @@ export function createRouterFactory<TConfig extends AnyRootConfig>(
|
|
|
164
168
|
return proxy as ReturnType<RouterCaller<any>>;
|
|
165
169
|
},
|
|
166
170
|
};
|
|
171
|
+
|
|
167
172
|
return router as any;
|
|
168
173
|
};
|
|
169
174
|
}
|
|
@@ -190,3 +195,27 @@ export function callProcedure(
|
|
|
190
195
|
|
|
191
196
|
return proc(opts);
|
|
192
197
|
}
|
|
198
|
+
|
|
199
|
+
export function createCallerFactory<TConfig extends AnyRootConfig>() {
|
|
200
|
+
return function createCallerInner<
|
|
201
|
+
TRouter extends Router<AnyRouterDef<TConfig>>,
|
|
202
|
+
>(router: TRouter): RouterCaller<TRouter['_def']> {
|
|
203
|
+
const _def = router._def;
|
|
204
|
+
|
|
205
|
+
return function createCaller(ctx) {
|
|
206
|
+
const proxy = createRecursiveProxy(({ path, args }) => {
|
|
207
|
+
const fullPath = path.join('.');
|
|
208
|
+
const procedure = _def.procedures[fullPath] as AnyProcedure;
|
|
209
|
+
|
|
210
|
+
return procedure({
|
|
211
|
+
path: fullPath,
|
|
212
|
+
getRawInput: async () => args[0],
|
|
213
|
+
ctx,
|
|
214
|
+
type: procedure._def.type,
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
return proxy as ReturnType<RouterCaller<any>>;
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
}
|
|
@@ -40,48 +40,83 @@ export type Serialize<T> =
|
|
|
40
40
|
|
|
41
41
|
/** JSON serialize [tuples](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) */
|
|
42
42
|
type SerializeTuple<T extends [unknown, ...unknown[]]> = {
|
|
43
|
-
[
|
|
43
|
+
[K in keyof T]: T[K] extends NonJsonPrimitive ? null : Serialize<T[K]>;
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
// prettier-ignore
|
|
47
|
-
type SerializeObjectKey<T extends Record<any, any>,
|
|
47
|
+
type SerializeObjectKey<T extends Record<any, any>, K> =
|
|
48
48
|
// never include entries where the key is a symbol
|
|
49
|
-
|
|
49
|
+
K extends symbol ? never :
|
|
50
50
|
// always include entries where the value is any
|
|
51
|
-
IsAny<T[
|
|
51
|
+
IsAny<T[K]> extends true ? K :
|
|
52
52
|
// always include entries where the value is unknown
|
|
53
|
-
unknown extends T[
|
|
53
|
+
unknown extends T[K] ? K :
|
|
54
54
|
// never include entries where the value is a non-JSON primitive
|
|
55
|
-
T[
|
|
55
|
+
T[K] extends NonJsonPrimitive ? never :
|
|
56
56
|
// otherwise serialize the value
|
|
57
|
-
|
|
57
|
+
K;
|
|
58
58
|
/**
|
|
59
59
|
* JSON serialize objects (not including arrays) and classes
|
|
60
60
|
* @internal
|
|
61
61
|
**/
|
|
62
62
|
export type SerializeObject<T extends object> = {
|
|
63
|
-
[
|
|
63
|
+
[K in keyof T as SerializeObjectKey<T, K>]: Serialize<T[K]>;
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Extract keys from T where the value dosen't extend undefined
|
|
68
|
+
* Note: Can't parse IndexSignature or Record types
|
|
69
|
+
*/
|
|
70
|
+
type FilterDefinedKeys<T extends object> = Exclude<
|
|
67
71
|
{
|
|
68
|
-
[
|
|
69
|
-
}[keyof
|
|
72
|
+
[K in keyof T]: undefined extends T[K] ? never : K;
|
|
73
|
+
}[keyof T],
|
|
70
74
|
undefined
|
|
71
75
|
>;
|
|
72
76
|
|
|
73
|
-
|
|
74
|
-
*
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Get value of exactOptionalPropertyTypes config
|
|
79
|
+
*/
|
|
80
|
+
type ExactOptionalPropertyTypes = { a?: 0 | undefined } extends {
|
|
81
|
+
a?: 0;
|
|
82
|
+
}
|
|
83
|
+
? false
|
|
84
|
+
: true;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Check if T has an index signature
|
|
88
|
+
*/
|
|
89
|
+
type HasIndexSignature<T extends object> = string extends keyof T
|
|
90
|
+
? true
|
|
91
|
+
: false;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* { [key: string]: number | undefined } --> { [key: string]: number }
|
|
95
|
+
*/
|
|
96
|
+
type HandleIndexSignature<T extends object> = {
|
|
97
|
+
[K in keyof Omit<T, keyof WithoutIndexSignature<T>>]: Exclude<
|
|
98
|
+
T[K],
|
|
99
|
+
undefined
|
|
100
|
+
>;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* { a: number | undefined } --> { a?: number }
|
|
105
|
+
* Note: Can't parse IndexSignature or Record types
|
|
106
|
+
*/
|
|
107
|
+
type HandleUndefined<T extends object> = {
|
|
108
|
+
[K in keyof Omit<T, FilterDefinedKeys<T>>]?: Exclude<T[K], undefined>;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Handle undefined, index signature and records
|
|
78
113
|
*/
|
|
79
114
|
type UndefinedToOptional<T extends object> =
|
|
80
115
|
// Property is not a union with `undefined`, keep as-is
|
|
81
|
-
Pick<
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
116
|
+
Pick<WithoutIndexSignature<T>, FilterDefinedKeys<WithoutIndexSignature<T>>> &
|
|
117
|
+
// If following is true, don't merge undefined or optional into index signature if any in T
|
|
118
|
+
(ExactOptionalPropertyTypes extends true
|
|
119
|
+
? HandleIndexSignature<T> & HandleUndefined<WithoutIndexSignature<T>>
|
|
120
|
+
: HasIndexSignature<T> extends true
|
|
121
|
+
? HandleIndexSignature<T>
|
|
122
|
+
: HandleUndefined<T>);
|