@trpc/server 10.44.2-canary.37 → 10.45.0

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.
Files changed (46) hide show
  1. package/adapters/zodFileSchema/index.d.ts +1 -0
  2. package/adapters/zodFileSchema/index.js +1 -0
  3. package/dist/adapters/aws-lambda/index.js +2 -2
  4. package/dist/adapters/aws-lambda/index.mjs +2 -2
  5. package/dist/adapters/express.js +3 -3
  6. package/dist/adapters/express.mjs +3 -3
  7. package/dist/adapters/fastify/index.js +2 -2
  8. package/dist/adapters/fastify/index.mjs +2 -2
  9. package/dist/adapters/fetch/index.js +2 -2
  10. package/dist/adapters/fetch/index.mjs +2 -2
  11. package/dist/adapters/next.js +3 -3
  12. package/dist/adapters/next.mjs +3 -3
  13. package/dist/adapters/node-http/index.js +3 -3
  14. package/dist/adapters/node-http/index.mjs +3 -3
  15. package/dist/adapters/standalone.js +3 -3
  16. package/dist/adapters/standalone.mjs +3 -3
  17. package/dist/adapters/ws.js +1 -1
  18. package/dist/adapters/ws.mjs +1 -1
  19. package/dist/{config-01d3fe30.js → config-930036df.js} +36 -27
  20. package/dist/{config-b1b43f4e.mjs → config-e3143f14.mjs} +36 -28
  21. package/dist/{config-7e6a54b5.js → config-efa2782d.js} +38 -30
  22. package/dist/core/index.d.ts +2 -2
  23. package/dist/core/index.d.ts.map +1 -1
  24. package/dist/core/initTRPC.d.ts +14 -0
  25. package/dist/core/initTRPC.d.ts.map +1 -1
  26. package/dist/core/router.d.ts +6 -1
  27. package/dist/core/router.d.ts.map +1 -1
  28. package/dist/http/index.js +2 -2
  29. package/dist/http/index.mjs +2 -2
  30. package/dist/index.js +12 -9
  31. package/dist/index.mjs +12 -10
  32. package/dist/{nodeHTTPRequestHandler-b4601529.js → nodeHTTPRequestHandler-60853005.js} +1 -1
  33. package/dist/{nodeHTTPRequestHandler-fc4c2130.js → nodeHTTPRequestHandler-cd03d007.js} +1 -1
  34. package/dist/{nodeHTTPRequestHandler-ba6bf8fd.mjs → nodeHTTPRequestHandler-f5efadf5.mjs} +1 -1
  35. package/dist/{resolveHTTPResponse-ab0691a1.js → resolveHTTPResponse-268da4ac.js} +1 -1
  36. package/dist/{resolveHTTPResponse-28b78a87.mjs → resolveHTTPResponse-37afa02e.mjs} +1 -1
  37. package/dist/{resolveHTTPResponse-fe5ab387.js → resolveHTTPResponse-b2ba9325.js} +1 -1
  38. package/dist/shared/internal/serialize.d.ts +38 -8
  39. package/dist/shared/internal/serialize.d.ts.map +1 -1
  40. package/package.json +2 -2
  41. package/src/core/index.ts +3 -1
  42. package/src/core/initTRPC.ts +10 -1
  43. package/src/core/internals/procedureBuilder.ts +1 -7
  44. package/src/core/router.ts +53 -36
  45. package/src/shared/internal/serialize.ts +58 -23
  46. package/dist/bundle-analysis.json +0 -1011
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var config = require('./config-01d3fe30.js');
5
+ var config = require('./config-930036df.js');
6
6
  var TRPCError = require('./TRPCError-0b220715.js');
7
7
  var index = require('./index-784ff647.js');
8
8
  var codes = require('./codes-87f6824b.js');
@@ -433,14 +433,8 @@ function createResolver(_def, resolver) {
433
433
  return createProcedureCaller(finalBuilder._def);
434
434
  }
435
435
  const codeblock = `
436
- If you want to call this function on the server, you do the following:
437
436
  This is a client-only function.
438
-
439
- const caller = appRouter.createCaller({
440
- /* ... your context */
441
- });
442
-
443
- const result = await caller.call('myProcedure', input);
437
+ If you want to call this function on the server, see https://trpc.io/docs/server/server-side-calls
444
438
  `.trim();
445
439
  function createProcedureCaller(_def) {
446
440
  const procedure = async function resolve(opts) {
@@ -852,23 +846,32 @@ function createTRPCInner() {
852
846
  */ _config: config$1,
853
847
  /**
854
848
  * Builder object for creating procedures
849
+ * @see https://trpc.io/docs/server/procedures
855
850
  */ procedure: createBuilder({
856
851
  meta: runtime?.defaultMeta
857
852
  }),
858
853
  /**
859
854
  * Create reusable middlewares
855
+ * @see https://trpc.io/docs/server/middlewares
860
856
  */ middleware: createMiddlewareFactory(),
861
857
  /**
862
858
  * Create a router
859
+ * @see https://trpc.io/docs/server/routers
863
860
  */ router: config.createRouterFactory(config$1),
864
861
  /**
865
862
  * Merge Routers
866
- */ mergeRouters
863
+ * @see https://trpc.io/docs/server/merging-routers
864
+ */ mergeRouters,
865
+ /**
866
+ * Create a server-side caller for a router
867
+ * @see https://trpc.io/docs/server/server-side-calls
868
+ */ createCallerFactory: config.createCallerFactory()
867
869
  };
868
870
  };
869
871
  }
870
872
 
871
873
  exports.callProcedure = config.callProcedure;
874
+ exports.createCallerFactory = config.createCallerFactory;
872
875
  exports.defaultTransformer = config.defaultTransformer;
873
876
  exports.getDataTransformer = config.getDataTransformer;
874
877
  exports.procedureTypes = config.procedureTypes;
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { c as createRouterFactory, d as defaultFormatter, a as defaultTransformer, g as getDataTransformer$1, i as isServerDefault } from './config-b1b43f4e.mjs';
2
- export { b as callProcedure, a as defaultTransformer, g as getDataTransformer, p as procedureTypes } from './config-b1b43f4e.mjs';
1
+ import { c as createRouterFactory, d as defaultFormatter, a as defaultTransformer, g as getDataTransformer$1, i as isServerDefault, b as createCallerFactory } from './config-e3143f14.mjs';
2
+ export { e as callProcedure, b as createCallerFactory, a as defaultTransformer, g as getDataTransformer, p as procedureTypes } from './config-e3143f14.mjs';
3
3
  import { T as TRPCError, g as getTRPCErrorFromUnknown } from './TRPCError-7e8e2a1b.mjs';
4
4
  export { T as TRPCError, g as getTRPCErrorFromUnknown } from './TRPCError-7e8e2a1b.mjs';
5
5
  import { g as getHTTPStatusCodeFromError, c as createFlatProxy } from './index-f91d720c.mjs';
@@ -431,14 +431,8 @@ function createResolver(_def, resolver) {
431
431
  return createProcedureCaller(finalBuilder._def);
432
432
  }
433
433
  const codeblock = `
434
- If you want to call this function on the server, you do the following:
435
434
  This is a client-only function.
436
-
437
- const caller = appRouter.createCaller({
438
- /* ... your context */
439
- });
440
-
441
- const result = await caller.call('myProcedure', input);
435
+ If you want to call this function on the server, see https://trpc.io/docs/server/server-side-calls
442
436
  `.trim();
443
437
  function createProcedureCaller(_def) {
444
438
  const procedure = async function resolve(opts) {
@@ -850,18 +844,26 @@ function createTRPCInner() {
850
844
  */ _config: config,
851
845
  /**
852
846
  * Builder object for creating procedures
847
+ * @see https://trpc.io/docs/server/procedures
853
848
  */ procedure: createBuilder({
854
849
  meta: runtime?.defaultMeta
855
850
  }),
856
851
  /**
857
852
  * Create reusable middlewares
853
+ * @see https://trpc.io/docs/server/middlewares
858
854
  */ middleware: createMiddlewareFactory(),
859
855
  /**
860
856
  * Create a router
857
+ * @see https://trpc.io/docs/server/routers
861
858
  */ router: createRouterFactory(config),
862
859
  /**
863
860
  * Merge Routers
864
- */ mergeRouters
861
+ * @see https://trpc.io/docs/server/merging-routers
862
+ */ mergeRouters,
863
+ /**
864
+ * Create a server-side caller for a router
865
+ * @see https://trpc.io/docs/server/server-side-calls
866
+ */ createCallerFactory: createCallerFactory()
865
867
  };
866
868
  };
867
869
  }
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  require('./index-784ff647.js');
4
- var resolveHTTPResponse = require('./resolveHTTPResponse-fe5ab387.js');
4
+ var resolveHTTPResponse = require('./resolveHTTPResponse-b2ba9325.js');
5
5
  var batchStreamFormatter = require('./batchStreamFormatter-93cdcdd4.js');
6
6
  var adapters_nodeHttp_contentType_json_index = require('./adapters/node-http/content-type/json/index.js');
7
7
 
@@ -1,5 +1,5 @@
1
1
  import './index-41f6f0b3.js';
2
- import { r as resolveHTTPResponse } from './resolveHTTPResponse-ab0691a1.js';
2
+ import { r as resolveHTTPResponse } from './resolveHTTPResponse-268da4ac.js';
3
3
  import { g as getBatchStreamFormatter } from './batchStreamFormatter-2c1405a1.js';
4
4
  import { nodeHTTPJSONContentTypeHandler } from './adapters/node-http/content-type/json/index.js';
5
5
 
@@ -1,5 +1,5 @@
1
1
  import './index-f91d720c.mjs';
2
- import { r as resolveHTTPResponse } from './resolveHTTPResponse-28b78a87.mjs';
2
+ import { r as resolveHTTPResponse } from './resolveHTTPResponse-37afa02e.mjs';
3
3
  import { g as getBatchStreamFormatter } from './batchStreamFormatter-fc1ffb26.mjs';
4
4
  import { nodeHTTPJSONContentTypeHandler } from './adapters/node-http/content-type/json/index.mjs';
5
5
 
@@ -1,4 +1,4 @@
1
- import { b as callProcedure } from './config-7e6a54b5.js';
1
+ import { e as callProcedure } from './config-efa2782d.js';
2
2
  import { T as TRPCError, g as getTRPCErrorFromUnknown } from './TRPCError-2bb6a7d0.js';
3
3
  import { t as transformTRPCResponse, g as getErrorShape } from './transformTRPCResponse-23bdf37c.js';
4
4
  import { g as getJsonContentTypeInputs } from './contentType-0447b256.js';
@@ -1,4 +1,4 @@
1
- import { b as callProcedure } from './config-b1b43f4e.mjs';
1
+ import { e as callProcedure } from './config-e3143f14.mjs';
2
2
  import { T as TRPCError, g as getTRPCErrorFromUnknown } from './TRPCError-7e8e2a1b.mjs';
3
3
  import { t as transformTRPCResponse, g as getErrorShape } from './transformTRPCResponse-1153b421.mjs';
4
4
  import { g as getJsonContentTypeInputs } from './contentType-31223e57.mjs';
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var config = require('./config-01d3fe30.js');
3
+ var config = require('./config-930036df.js');
4
4
  var TRPCError = require('./TRPCError-0b220715.js');
5
5
  var transformTRPCResponse = require('./transformTRPCResponse-e65f34e9.js');
6
6
  var contentType = require('./contentType-a229790b.js');
@@ -12,21 +12,51 @@ export type Serialize<T> = IsAny<T> extends true ? any : unknown extends T ? unk
12
12
  } ? U : T extends [] ? [] : T extends [unknown, ...unknown[]] ? SerializeTuple<T> : T extends readonly (infer U)[] ? (U extends NonJsonPrimitive ? null : Serialize<U>)[] : T extends object ? IsRecord<T> extends true ? Record<keyof T, Serialize<T[keyof T]>> : Simplify<SerializeObject<UndefinedToOptional<T>>> : never;
13
13
  /** JSON serialize [tuples](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) */
14
14
  type SerializeTuple<T extends [unknown, ...unknown[]]> = {
15
- [k in keyof T]: T[k] extends NonJsonPrimitive ? null : Serialize<T[k]>;
15
+ [K in keyof T]: T[K] extends NonJsonPrimitive ? null : Serialize<T[K]>;
16
16
  };
17
- type SerializeObjectKey<T extends Record<any, any>, TKey> = TKey extends symbol ? never : IsAny<T[TKey]> extends true ? TKey : unknown extends T[TKey] ? TKey : T[TKey] extends NonJsonPrimitive ? never : TKey;
17
+ type SerializeObjectKey<T extends Record<any, any>, K> = K extends symbol ? never : IsAny<T[K]> extends true ? K : unknown extends T[K] ? K : T[K] extends NonJsonPrimitive ? never : K;
18
18
  /**
19
19
  * JSON serialize objects (not including arrays) and classes
20
20
  * @internal
21
21
  **/
22
22
  export type SerializeObject<T extends object> = {
23
- [$Key in keyof T as SerializeObjectKey<T, $Key>]: Serialize<T[$Key]>;
23
+ [K in keyof T as SerializeObjectKey<T, K>]: Serialize<T[K]>;
24
24
  };
25
- type FilterDefinedKeys<TObj extends object> = Exclude<{
26
- [TKey in keyof TObj]: undefined extends TObj[TKey] ? never : TKey;
27
- }[keyof TObj], undefined>;
28
- type UndefinedToOptional<T extends object> = Pick<WithoutIndexSignature<T>, FilterDefinedKeys<WithoutIndexSignature<T>>> & {
29
- [k in keyof Omit<T, FilterDefinedKeys<T>>]?: Exclude<T[k], undefined>;
25
+ /**
26
+ * Extract keys from T where the value dosen't extend undefined
27
+ * Note: Can't parse IndexSignature or Record types
28
+ */
29
+ type FilterDefinedKeys<T extends object> = Exclude<{
30
+ [K in keyof T]: undefined extends T[K] ? never : K;
31
+ }[keyof T], undefined>;
32
+ /**
33
+ * Get value of exactOptionalPropertyTypes config
34
+ */
35
+ type ExactOptionalPropertyTypes = {
36
+ a?: 0 | undefined;
37
+ } extends {
38
+ a?: 0;
39
+ } ? false : true;
40
+ /**
41
+ * Check if T has an index signature
42
+ */
43
+ type HasIndexSignature<T extends object> = string extends keyof T ? true : false;
44
+ /**
45
+ * { [key: string]: number | undefined } --> { [key: string]: number }
46
+ */
47
+ type HandleIndexSignature<T extends object> = {
48
+ [K in keyof Omit<T, keyof WithoutIndexSignature<T>>]: Exclude<T[K], undefined>;
49
+ };
50
+ /**
51
+ * { a: number | undefined } --> { a?: number }
52
+ * Note: Can't parse IndexSignature or Record types
53
+ */
54
+ type HandleUndefined<T extends object> = {
55
+ [K in keyof Omit<T, FilterDefinedKeys<T>>]?: Exclude<T[K], undefined>;
30
56
  };
57
+ /**
58
+ * Handle undefined, index signature and records
59
+ */
60
+ type UndefinedToOptional<T extends object> = Pick<WithoutIndexSignature<T>, FilterDefinedKeys<WithoutIndexSignature<T>>> & (ExactOptionalPropertyTypes extends true ? HandleIndexSignature<T> & HandleUndefined<WithoutIndexSignature<T>> : HasIndexSignature<T> extends true ? HandleIndexSignature<T> : HandleUndefined<T>);
31
61
  export {};
32
62
  //# sourceMappingURL=serialize.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../../../src/shared/internal/serialize.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAE9D;;GAEG;AACH,KAAK,aAAa,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;AAEtD,KAAK,gBAAgB,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAC;AAKtD,KAAK,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;AAK/C,KAAK,cAAc,GAAG,aAAa,GAAG,SAAS,CAAC;AAEhD,KAAK,QAAQ,CAAC,CAAC,SAAS,MAAM,IAAI,MAAM,qBAAqB,CAAC,CAAC,CAAC,SAAS,KAAK,GAC1E,IAAI,GACJ,KAAK,CAAC;AAGV,MAAM,MAAM,SAAS,CAAC,CAAC,IACr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C,SAAS,MAAM,IAAI;KAC3C,CAAC,IAAI,MAAM,IAAI,CAAC,CAAC,EAAE,MAAM,qBAAqB,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAC3D,CAAC,CAAC,CAAC,CAAC,EACJ,SAAS,CACV;CACF,CAAC;AAEF;;;GAGG;AACH,KAAK,eAAe,CAAC,CAAC,SAAS,MAAM,IAAI;KACtC,CAAC,IAAI,MAAM,IAAI,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC;CACtE,CAAC;AAEF;;GAEG;AACH,KAAK,mBAAmB,CAAC,CAAC,SAAS,MAAM,IAEvC,IAAI,CAAC,qBAAqB,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC,GAEzE,CAAC,0BAA0B,SAAS,IAAI,GACpC,oBAAoB,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,GACnE,iBAAiB,CAAC,CAAC,CAAC,SAAS,IAAI,GACjC,oBAAoB,CAAC,CAAC,CAAC,GACvB,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "10.44.2-canary.37+db1fd4424",
3
+ "version": "10.45.0",
4
4
  "description": "The tRPC server library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -173,5 +173,5 @@
173
173
  "funding": [
174
174
  "https://trpc.io/sponsor"
175
175
  ],
176
- "gitHead": "db1fd44242e076174a83084a075ccfdc0d577c5d"
176
+ "gitHead": "06fd40d491e22ddba9951d4459a12df2b08d8615"
177
177
  }
package/src/core/index.ts CHANGED
@@ -4,8 +4,10 @@ export type {
4
4
  ProcedureRouterRecord,
5
5
  CreateRouterInner,
6
6
  Router,
7
+ RouterCaller,
8
+ AnyRouterDef,
7
9
  } from './router';
8
- export { callProcedure } from './router';
10
+ export { callProcedure, createCallerFactory } from './router';
9
11
  export type {
10
12
  Procedure,
11
13
  AnyProcedure,
@@ -23,7 +23,7 @@ import { mergeRouters } from './internals/mergeRouters';
23
23
  import { createBuilder } from './internals/procedureBuilder';
24
24
  import { Overwrite, PickFirstDefined, ValidateShape } from './internals/utils';
25
25
  import { createMiddlewareFactory } from './middleware';
26
- import { createRouterFactory } from './router';
26
+ import { createCallerFactory, createRouterFactory } from './router';
27
27
 
28
28
  type PartialRootConfigTypes = Partial<RootConfigTypes>;
29
29
 
@@ -148,22 +148,31 @@ function createTRPCInner<TParams extends PartialRootConfigTypes>() {
148
148
  _config: config,
149
149
  /**
150
150
  * Builder object for creating procedures
151
+ * @see https://trpc.io/docs/server/procedures
151
152
  */
152
153
  procedure: createBuilder<$Config>({
153
154
  meta: runtime?.defaultMeta,
154
155
  }),
155
156
  /**
156
157
  * Create reusable middlewares
158
+ * @see https://trpc.io/docs/server/middlewares
157
159
  */
158
160
  middleware: createMiddlewareFactory<$Config>(),
159
161
  /**
160
162
  * Create a router
163
+ * @see https://trpc.io/docs/server/routers
161
164
  */
162
165
  router: createRouterFactory<$Config>(config),
163
166
  /**
164
167
  * Merge Routers
168
+ * @see https://trpc.io/docs/server/merging-routers
165
169
  */
166
170
  mergeRouters,
171
+ /**
172
+ * Create a server-side caller for a router
173
+ * @see https://trpc.io/docs/server/server-side-calls
174
+ */
175
+ createCallerFactory: createCallerFactory<$Config>(),
167
176
  };
168
177
  };
169
178
  }
@@ -316,14 +316,8 @@ export interface ProcedureCallOptions {
316
316
  }
317
317
 
318
318
  const codeblock = `
319
- If you want to call this function on the server, you do the following:
320
319
  This is a client-only function.
321
-
322
- const caller = appRouter.createCaller({
323
- /* ... your context */
324
- });
325
-
326
- const result = await caller.call('myProcedure', input);
320
+ If you want to call this function on the server, see https://trpc.io/docs/server/server-side-calls
327
321
  `.trim();
328
322
 
329
323
  function createProcedureCaller(_def: AnyProcedureBuilderDef): AnyProcedure {
@@ -107,7 +107,7 @@ type DecoratedProcedureRecord<TProcedures extends ProcedureRouterRecord> = {
107
107
  /**
108
108
  * @internal
109
109
  */
110
- type RouterCaller<TDef extends AnyRouterDef> = (
110
+ export type RouterCaller<TDef extends AnyRouterDef> = (
111
111
  ctx: TDef['_config']['$types']['ctx'],
112
112
  ) => DecoratedProcedureRecord<TDef['record']> & {
113
113
  /**
@@ -126,6 +126,10 @@ type RouterCaller<TDef extends AnyRouterDef> = (
126
126
 
127
127
  export interface Router<TDef extends AnyRouterDef> {
128
128
  _def: TDef;
129
+ /**
130
+ * @deprecated use `t.createCallerFactory(router)` instead
131
+ * @see https://trpc.io/docs/server/server-side-calls
132
+ */
129
133
  createCaller: RouterCaller<TDef>;
130
134
 
131
135
  /**
@@ -243,41 +247,8 @@ export function createRouterFactory<TConfig extends AnyRootConfig>(
243
247
  const router: AnyRouter = {
244
248
  ...procedures,
245
249
  _def,
246
- createCaller(ctx) {
247
- const proxy = createRecursiveProxy(({ path, args }) => {
248
- // interop mode
249
- if (
250
- path.length === 1 &&
251
- procedureTypes.includes(path[0] as ProcedureType)
252
- ) {
253
- return callProcedure({
254
- procedures: _def.procedures,
255
- path: args[0] as string,
256
- rawInput: args[1],
257
- ctx,
258
- type: path[0] as ProcedureType,
259
- });
260
- }
261
-
262
- const fullPath = path.join('.');
263
- const procedure = _def.procedures[fullPath] as AnyProcedure;
264
-
265
- let type: ProcedureType = 'query';
266
- if (procedure._def.mutation) {
267
- type = 'mutation';
268
- } else if (procedure._def.subscription) {
269
- type = 'subscription';
270
- }
271
-
272
- return procedure({
273
- path: fullPath,
274
- rawInput: args[0],
275
- ctx,
276
- type,
277
- });
278
- });
279
-
280
- return proxy as ReturnType<RouterCaller<any>>;
250
+ createCaller(ctx: TConfig['$types']['ctx']) {
251
+ return createCallerFactory<TConfig>()(router as any)(ctx);
281
252
  },
282
253
  getErrorShape(opts) {
283
254
  const { path, error } = opts;
@@ -299,6 +270,7 @@ export function createRouterFactory<TConfig extends AnyRootConfig>(
299
270
  return this._def._config.errorFormatter({ ...opts, shape });
300
271
  },
301
272
  };
273
+
302
274
  return router as any;
303
275
  };
304
276
  }
@@ -322,3 +294,48 @@ export function callProcedure(
322
294
 
323
295
  return procedure(opts);
324
296
  }
297
+
298
+ export function createCallerFactory<TConfig extends AnyRootConfig>() {
299
+ return function createCallerInner<
300
+ TRouter extends Router<AnyRouterDef<TConfig>>,
301
+ >(router: TRouter): RouterCaller<TRouter['_def']> {
302
+ const def = router._def;
303
+
304
+ return function createCaller(ctx) {
305
+ const proxy = createRecursiveProxy(({ path, args }) => {
306
+ // interop mode
307
+ if (
308
+ path.length === 1 &&
309
+ procedureTypes.includes(path[0] as ProcedureType)
310
+ ) {
311
+ return callProcedure({
312
+ procedures: def.procedures,
313
+ path: args[0] as string,
314
+ rawInput: args[1],
315
+ ctx,
316
+ type: path[0] as ProcedureType,
317
+ });
318
+ }
319
+
320
+ const fullPath = path.join('.');
321
+ const procedure = def.procedures[fullPath] as AnyProcedure;
322
+
323
+ let type: ProcedureType = 'query';
324
+ if (procedure._def.mutation) {
325
+ type = 'mutation';
326
+ } else if (procedure._def.subscription) {
327
+ type = 'subscription';
328
+ }
329
+
330
+ return procedure({
331
+ path: fullPath,
332
+ rawInput: args[0],
333
+ ctx,
334
+ type,
335
+ });
336
+ });
337
+
338
+ return proxy as ReturnType<RouterCaller<any>>;
339
+ };
340
+ };
341
+ }
@@ -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
- [k in keyof T]: T[k] extends NonJsonPrimitive ? null : Serialize<T[k]>;
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>, TKey> =
47
+ type SerializeObjectKey<T extends Record<any, any>, K> =
48
48
  // never include entries where the key is a symbol
49
- TKey extends symbol ? never :
49
+ K extends symbol ? never :
50
50
  // always include entries where the value is any
51
- IsAny<T[TKey]> extends true ? TKey :
51
+ IsAny<T[K]> extends true ? K :
52
52
  // always include entries where the value is unknown
53
- unknown extends T[TKey] ? TKey :
53
+ unknown extends T[K] ? K :
54
54
  // never include entries where the value is a non-JSON primitive
55
- T[TKey] extends NonJsonPrimitive ? never :
55
+ T[K] extends NonJsonPrimitive ? never :
56
56
  // otherwise serialize the value
57
- TKey;
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
- [$Key in keyof T as SerializeObjectKey<T, $Key>]: Serialize<T[$Key]>;
63
+ [K in keyof T as SerializeObjectKey<T, K>]: Serialize<T[K]>;
64
64
  };
65
65
 
66
- type FilterDefinedKeys<TObj extends object> = Exclude<
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
- [TKey in keyof TObj]: undefined extends TObj[TKey] ? never : TKey;
69
- }[keyof TObj],
72
+ [K in keyof T]: undefined extends T[K] ? never : K;
73
+ }[keyof T],
70
74
  undefined
71
75
  >;
72
76
 
73
- /*
74
- * For an object T, if it has any properties that are a union with `undefined`,
75
- * make those into optional properties instead.
76
- *
77
- * Example: { a: string | undefined} --> { a?: string}
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
- WithoutIndexSignature<T>,
83
- FilterDefinedKeys<WithoutIndexSignature<T>>
84
- > & {
85
- // Property _is_ a union with `defined`. Set as optional (via `?`) and remove `undefined` from the union
86
- [k in keyof Omit<T, FilterDefinedKeys<T>>]?: Exclude<T[k], undefined>;
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>);