@trpc/client 10.0.0-alpha.38 → 10.0.0-alpha.39

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.
@@ -1,10 +1,24 @@
1
- import type { AnyRouter } from '@trpc/server';
1
+ import type { AnyRouter, OmitNeverKeys, Procedure, ProcedureArgs, ProcedureRouterRecord, inferProcedureOutput } from '@trpc/server';
2
2
  import { TRPCClient as Client } from './internals/TRPCClient';
3
+ declare type Resolver<TProcedure extends Procedure<any>> = (...args: ProcedureArgs<TProcedure['_def']>) => Promise<inferProcedureOutput<TProcedure>>;
4
+ declare type DecorateProcedure<TProcedure extends Procedure<any>> = OmitNeverKeys<{
5
+ query: TProcedure extends {
6
+ _query: true;
7
+ } ? Resolver<TProcedure> : never;
8
+ mutate: TProcedure extends {
9
+ _mutation: true;
10
+ } ? Resolver<TProcedure> : never;
11
+ subscribe: TProcedure extends {
12
+ _subscription: true;
13
+ } ? Resolver<TProcedure> : never;
14
+ }>;
15
+ declare type assertProcedure<T> = T extends Procedure<any> ? T : never;
3
16
  /**
4
17
  * @internal
5
18
  */
6
- export declare type FlattenRouter<TRouter extends AnyRouter> = {
7
- [Key in keyof TRouter['_def']['record']]: TRouter['_def']['record'][Key] extends AnyRouter ? FlattenRouter<TRouter['_def']['record'][Key]> : TRouter['_def']['record'][Key];
19
+ declare type DecoratedProcedureRecord<TProcedures extends ProcedureRouterRecord> = {
20
+ [TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter ? DecoratedProcedureRecord<TProcedures[TKey]['_def']['record']> : DecorateProcedure<assertProcedure<TProcedures[TKey]>>;
8
21
  };
9
- export declare function createTRPCClientProxy<TRouter extends AnyRouter>(client: Client<TRouter>): FlattenRouter<TRouter>;
22
+ export declare function createTRPCClientProxy<TRouter extends AnyRouter>(client: Client<TRouter>): DecoratedProcedureRecord<TRouter["_def"]["record"]>;
23
+ export {};
10
24
  //# sourceMappingURL=createTRPCClientProxy.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createTRPCClientProxy.d.ts","sourceRoot":"","sources":["../src/createTRPCClientProxy.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAE,UAAU,IAAI,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAE9D;;GAEG;AACH,oBAAY,aAAa,CAAC,OAAO,SAAS,SAAS,IAAI;KACpD,GAAG,IAAI,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,SAAS,SAAS,GACtF,aAAa,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,GAC7C,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC;CACnC,CAAC;AAEF,wBAAgB,qBAAqB,CAAC,OAAO,SAAS,SAAS,EAC7D,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,0BAYxB"}
1
+ {"version":3,"file":"createTRPCClientProxy.d.ts","sourceRoot":"","sources":["../src/createTRPCClientProxy.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,SAAS,EACT,aAAa,EACb,SAAS,EACT,aAAa,EACb,qBAAqB,EAErB,oBAAoB,EACrB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,UAAU,IAAI,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAE9D,aAAK,QAAQ,CAAC,UAAU,SAAS,SAAS,CAAC,GAAG,CAAC,IAAI,CACjD,GAAG,IAAI,EAAE,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,KACvC,OAAO,CAAC,oBAAoB,CAAC,UAAU,CAAC,CAAC,CAAC;AAE/C,aAAK,iBAAiB,CAAC,UAAU,SAAS,SAAS,CAAC,GAAG,CAAC,IAAI,aAAa,CAAC;IACxE,KAAK,EAAE,UAAU,SAAS;QAAE,MAAM,EAAE,IAAI,CAAA;KAAE,GAAG,QAAQ,CAAC,UAAU,CAAC,GAAG,KAAK,CAAC;IAE1E,MAAM,EAAE,UAAU,SAAS;QAAE,SAAS,EAAE,IAAI,CAAA;KAAE,GAAG,QAAQ,CAAC,UAAU,CAAC,GAAG,KAAK,CAAC;IAE9E,SAAS,EAAE,UAAU,SAAS;QAAE,aAAa,EAAE,IAAI,CAAA;KAAE,GACjD,QAAQ,CAAC,UAAU,CAAC,GACpB,KAAK,CAAC;CACX,CAAC,CAAC;AAEH,aAAK,eAAe,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAE/D;;GAEG;AACH,aAAK,wBAAwB,CAAC,WAAW,SAAS,qBAAqB,IAAI;KACxE,IAAI,IAAI,MAAM,WAAW,GAAG,WAAW,CAAC,IAAI,CAAC,SAAS,SAAS,GAC5D,wBAAwB,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,GAC7D,iBAAiB,CAAC,eAAe,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;CAC1D,CAAC;AAQF,wBAAgB,qBAAqB,CAAC,OAAO,SAAS,SAAS,EAC7D,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,uDAWxB"}
package/dist/index.js CHANGED
@@ -416,6 +416,11 @@ function createTRPCClient(opts) {
416
416
  return client;
417
417
  } // Also the client created above needs to somehow be like `TRPCClient<Router> & Omit<Router, 'createCaller' | 'createProcedure' | '_def' | 'transformer' | 'errorFormatter' | 'getErrorShape>`
418
418
 
419
+ var clientCallTypeMap = {
420
+ query: 'query',
421
+ mutate: 'mutation',
422
+ subscribe: 'subscription'
423
+ };
419
424
  function createTRPCClientProxy(client) {
420
425
  var proxy = shared.createProxy(function (_ref) {
421
426
  var _ref2;
@@ -425,14 +430,10 @@ function createTRPCClientProxy(client) {
425
430
 
426
431
  var pathCopy = _toConsumableArray__default["default"](path);
427
432
 
428
- var type = pathCopy.pop();
429
-
430
- if (type === 'mutate') {
431
- type = 'mutation';
432
- }
433
-
433
+ var clientCallType = pathCopy.pop();
434
+ var procedureType = clientCallTypeMap[clientCallType];
434
435
  var fullPath = pathCopy.join('.');
435
- return (_ref2 = client)[type].apply(_ref2, [fullPath].concat(_toConsumableArray__default["default"](args)));
436
+ return (_ref2 = client)[procedureType].apply(_ref2, [fullPath].concat(_toConsumableArray__default["default"](args)));
436
437
  });
437
438
  return proxy;
438
439
  }
package/dist/index.mjs CHANGED
@@ -408,6 +408,11 @@ function createTRPCClient(opts) {
408
408
  return client;
409
409
  } // Also the client created above needs to somehow be like `TRPCClient<Router> & Omit<Router, 'createCaller' | 'createProcedure' | '_def' | 'transformer' | 'errorFormatter' | 'getErrorShape>`
410
410
 
411
+ var clientCallTypeMap = {
412
+ query: 'query',
413
+ mutate: 'mutation',
414
+ subscribe: 'subscription'
415
+ };
411
416
  function createTRPCClientProxy(client) {
412
417
  var proxy = createProxy(function (_ref) {
413
418
  var _ref2;
@@ -417,14 +422,10 @@ function createTRPCClientProxy(client) {
417
422
 
418
423
  var pathCopy = _toConsumableArray(path);
419
424
 
420
- var type = pathCopy.pop();
421
-
422
- if (type === 'mutate') {
423
- type = 'mutation';
424
- }
425
-
425
+ var clientCallType = pathCopy.pop();
426
+ var procedureType = clientCallTypeMap[clientCallType];
426
427
  var fullPath = pathCopy.join('.');
427
- return (_ref2 = client)[type].apply(_ref2, [fullPath].concat(_toConsumableArray(args)));
428
+ return (_ref2 = client)[procedureType].apply(_ref2, [fullPath].concat(_toConsumableArray(args)));
428
429
  });
429
430
  return proxy;
430
431
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "10.0.0-alpha.38",
3
+ "version": "10.0.0-alpha.39",
4
4
  "description": "tRPC Client lib",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -67,13 +67,13 @@
67
67
  "@babel/runtime": "^7.9.0"
68
68
  },
69
69
  "peerDependencies": {
70
- "@trpc/server": "10.0.0-alpha.38"
70
+ "@trpc/server": "10.0.0-alpha.39"
71
71
  },
72
72
  "devDependencies": {
73
- "@trpc/server": "10.0.0-alpha.38"
73
+ "@trpc/server": "10.0.0-alpha.39"
74
74
  },
75
75
  "publishConfig": {
76
76
  "access": "public"
77
77
  },
78
- "gitHead": "0c40cb7ebfd8762ef482ff72cc54c2cf3ef4a0a8"
78
+ "gitHead": "a9cde42919d1a6b761fe25f18410c7270cb2cc2e"
79
79
  }
@@ -1,17 +1,47 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
2
 
3
3
  /* eslint-disable @typescript-eslint/no-explicit-any */
4
- import type { AnyRouter } from '@trpc/server';
4
+ import type {
5
+ AnyRouter,
6
+ OmitNeverKeys,
7
+ Procedure,
8
+ ProcedureArgs,
9
+ ProcedureRouterRecord,
10
+ ProcedureType,
11
+ inferProcedureOutput,
12
+ } from '@trpc/server';
5
13
  import { createProxy } from '@trpc/server/shared';
6
14
  import { TRPCClient as Client } from './internals/TRPCClient';
7
15
 
16
+ type Resolver<TProcedure extends Procedure<any>> = (
17
+ ...args: ProcedureArgs<TProcedure['_def']>
18
+ ) => Promise<inferProcedureOutput<TProcedure>>;
19
+
20
+ type DecorateProcedure<TProcedure extends Procedure<any>> = OmitNeverKeys<{
21
+ query: TProcedure extends { _query: true } ? Resolver<TProcedure> : never;
22
+
23
+ mutate: TProcedure extends { _mutation: true } ? Resolver<TProcedure> : never;
24
+
25
+ subscribe: TProcedure extends { _subscription: true }
26
+ ? Resolver<TProcedure>
27
+ : never;
28
+ }>;
29
+
30
+ type assertProcedure<T> = T extends Procedure<any> ? T : never;
31
+
8
32
  /**
9
33
  * @internal
10
34
  */
11
- export type FlattenRouter<TRouter extends AnyRouter> = {
12
- [Key in keyof TRouter['_def']['record']]: TRouter['_def']['record'][Key] extends AnyRouter
13
- ? FlattenRouter<TRouter['_def']['record'][Key]>
14
- : TRouter['_def']['record'][Key];
35
+ type DecoratedProcedureRecord<TProcedures extends ProcedureRouterRecord> = {
36
+ [TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
37
+ ? DecoratedProcedureRecord<TProcedures[TKey]['_def']['record']>
38
+ : DecorateProcedure<assertProcedure<TProcedures[TKey]>>;
39
+ };
40
+
41
+ const clientCallTypeMap: Record<keyof DecorateProcedure<any>, ProcedureType> = {
42
+ query: 'query',
43
+ mutate: 'mutation',
44
+ subscribe: 'subscription',
15
45
  };
16
46
 
17
47
  export function createTRPCClientProxy<TRouter extends AnyRouter>(
@@ -19,12 +49,11 @@ export function createTRPCClientProxy<TRouter extends AnyRouter>(
19
49
  ) {
20
50
  const proxy = createProxy(({ path, args }) => {
21
51
  const pathCopy = [...path];
22
- let type = pathCopy.pop()!;
23
- if (type === 'mutate') {
24
- type = 'mutation';
25
- }
52
+ const clientCallType = pathCopy.pop()! as keyof DecorateProcedure<any>;
53
+ const procedureType = clientCallTypeMap[clientCallType];
54
+
26
55
  const fullPath = pathCopy.join('.');
27
- return (client as any)[type](fullPath, ...args);
56
+ return (client as any)[procedureType](fullPath, ...args);
28
57
  });
29
- return proxy as FlattenRouter<TRouter>;
58
+ return proxy as DecoratedProcedureRecord<TRouter['_def']['record']>;
30
59
  }