@trpc/client 10.0.0-alpha.36 → 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.
package/README.md CHANGED
@@ -1,3 +1,60 @@
1
+ <p align="center">
2
+ <a href="https://trpc.io/"><img src="../../www/static/img/logo-text.svg" alt="tRPC" height="130"/></a>
3
+ </p>
4
+
5
+ <p align="center">
6
+ <strong>End-to-end typesafe APIs made easy</strong>
7
+ </p>
8
+
9
+ <p align="center">
10
+ <!-- TODO: replace with new version GIF -->
11
+ <img src="https://storage.googleapis.com/trpc/trpcgif.gif" alt="Demo" />
12
+ </p>
13
+
1
14
  # `@trpc/client`
2
15
 
3
- https://trpc.io
16
+ > Communicate with a tRPC server on the client side.
17
+
18
+ ## Documentation
19
+
20
+ Full documentation for `@trpc/client` can be found [here](https://trpc.io/docs/vanilla)
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ # npm
26
+ npm install @trpc/client
27
+
28
+ # Yarn
29
+ yarn add @trpc/client
30
+
31
+ # pnpm
32
+ pnpm add @trpc/client
33
+ ```
34
+
35
+ ## Basic Example
36
+
37
+ ```ts
38
+ import { createTRPCClient, createTRPCClientProxy } from '@trpc/client';
39
+ // Importing the router type from the server file
40
+ import type { AppRouter } from './server';
41
+
42
+ // Initializing the tRPC client
43
+ const client = createTRPCClient<AppRouter>({
44
+ url: 'http://localhost:2022',
45
+ });
46
+
47
+ // Creating a proxy, this allows for cmd+click to the backend function.
48
+ const proxy = createTRPCClientProxy(client);
49
+
50
+ async function main() {
51
+ // Querying the greeting
52
+ const helloResponse = await proxy.greeting.query({
53
+ name: 'world',
54
+ });
55
+
56
+ console.log('helloResponse', helloResponse); // Hello world
57
+ }
58
+
59
+ main();
60
+ ```
@@ -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
@@ -5,11 +5,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
6
6
  var _createClass = require('@babel/runtime/helpers/createClass');
7
7
  var observable = require('@trpc/server/observable');
8
- var links_wsLink = require('./wsLink-69f5adb8.js');
8
+ var links_wsLink = require('./wsLink-807f4ad9.js');
9
9
  var links_httpBatchLink = require('./links/httpBatchLink.js');
10
10
  var _defineProperty = require('@babel/runtime/helpers/defineProperty');
11
11
  require('@babel/runtime/helpers/slicedToArray');
12
- var links_splitLink = require('./splitLink-8202386e.js');
12
+ var links_splitLink = require('./splitLink-23c5ce5c.js');
13
13
  require('@babel/runtime/helpers/assertThisInitialized');
14
14
  require('@babel/runtime/helpers/inherits');
15
15
  require('@babel/runtime/helpers/possibleConstructorReturn');
@@ -70,11 +70,12 @@ function dedupeLink() {
70
70
  }
71
71
 
72
72
  var key = JSON.stringify([op.path, op.input]);
73
+ var obs$ = pending[key];
73
74
 
74
- if (pending[key]) {
75
+ if (obs$) {
75
76
  // console.log('hooking into pending', { op });
76
77
  return observable.observable(function (observer) {
77
- return pending[key].subscribe(observer);
78
+ return obs$.subscribe(observer);
78
79
  });
79
80
  }
80
81
 
@@ -415,6 +416,11 @@ function createTRPCClient(opts) {
415
416
  return client;
416
417
  } // Also the client created above needs to somehow be like `TRPCClient<Router> & Omit<Router, 'createCaller' | 'createProcedure' | '_def' | 'transformer' | 'errorFormatter' | 'getErrorShape>`
417
418
 
419
+ var clientCallTypeMap = {
420
+ query: 'query',
421
+ mutate: 'mutation',
422
+ subscribe: 'subscription'
423
+ };
418
424
  function createTRPCClientProxy(client) {
419
425
  var proxy = shared.createProxy(function (_ref) {
420
426
  var _ref2;
@@ -424,14 +430,10 @@ function createTRPCClientProxy(client) {
424
430
 
425
431
  var pathCopy = _toConsumableArray__default["default"](path);
426
432
 
427
- var type = pathCopy.pop();
428
-
429
- if (type === 'mutate') {
430
- type = 'mutation';
431
- }
432
-
433
+ var clientCallType = pathCopy.pop();
434
+ var procedureType = clientCallTypeMap[clientCallType];
433
435
  var fullPath = pathCopy.join('.');
434
- 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)));
435
437
  });
436
438
  return proxy;
437
439
  }
package/dist/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
1
  import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
2
2
  import _createClass from '@babel/runtime/helpers/createClass';
3
3
  import { observable, share, observableToPromise } from '@trpc/server/observable';
4
- import { T as TRPCClientError } from './wsLink-9468a563.mjs';
5
- export { T as TRPCClientError, c as createWSClient, w as wsLink } from './wsLink-9468a563.mjs';
4
+ import { T as TRPCClientError } from './wsLink-11ee4922.mjs';
5
+ export { T as TRPCClientError, c as createWSClient, w as wsLink } from './wsLink-11ee4922.mjs';
6
6
  import { httpBatchLink } from './links/httpBatchLink.mjs';
7
7
  export { httpBatchLink } from './links/httpBatchLink.mjs';
8
8
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
9
9
  import '@babel/runtime/helpers/slicedToArray';
10
- import { c as createChain } from './splitLink-62ff9db0.mjs';
11
- export { s as splitLink } from './splitLink-62ff9db0.mjs';
10
+ import { c as createChain } from './splitLink-0202cd7b.mjs';
11
+ export { s as splitLink } from './splitLink-0202cd7b.mjs';
12
12
  import '@babel/runtime/helpers/assertThisInitialized';
13
13
  import '@babel/runtime/helpers/inherits';
14
14
  import '@babel/runtime/helpers/possibleConstructorReturn';
@@ -62,11 +62,12 @@ function dedupeLink() {
62
62
  }
63
63
 
64
64
  var key = JSON.stringify([op.path, op.input]);
65
+ var obs$ = pending[key];
65
66
 
66
- if (pending[key]) {
67
+ if (obs$) {
67
68
  // console.log('hooking into pending', { op });
68
69
  return observable(function (observer) {
69
- return pending[key].subscribe(observer);
70
+ return obs$.subscribe(observer);
70
71
  });
71
72
  }
72
73
 
@@ -407,6 +408,11 @@ function createTRPCClient(opts) {
407
408
  return client;
408
409
  } // Also the client created above needs to somehow be like `TRPCClient<Router> & Omit<Router, 'createCaller' | 'createProcedure' | '_def' | 'transformer' | 'errorFormatter' | 'getErrorShape>`
409
410
 
411
+ var clientCallTypeMap = {
412
+ query: 'query',
413
+ mutate: 'mutation',
414
+ subscribe: 'subscription'
415
+ };
410
416
  function createTRPCClientProxy(client) {
411
417
  var proxy = createProxy(function (_ref) {
412
418
  var _ref2;
@@ -416,14 +422,10 @@ function createTRPCClientProxy(client) {
416
422
 
417
423
  var pathCopy = _toConsumableArray(path);
418
424
 
419
- var type = pathCopy.pop();
420
-
421
- if (type === 'mutate') {
422
- type = 'mutation';
423
- }
424
-
425
+ var clientCallType = pathCopy.pop();
426
+ var procedureType = clientCallTypeMap[clientCallType];
425
427
  var fullPath = pathCopy.join('.');
426
- return (_ref2 = client)[type].apply(_ref2, [fullPath].concat(_toConsumableArray(args)));
428
+ return (_ref2 = client)[procedureType].apply(_ref2, [fullPath].concat(_toConsumableArray(args)));
427
429
  });
428
430
  return proxy;
429
431
  }
@@ -1 +1 @@
1
- {"version":3,"file":"dedupeLink.d.ts","sourceRoot":"","sources":["../../src/links/dedupeLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,wBAAgB,UAAU,CACxB,OAAO,SAAS,SAAS,GAAG,SAAS,KAClC,QAAQ,CAAC,OAAO,CAAC,CA4CrB"}
1
+ {"version":3,"file":"dedupeLink.d.ts","sourceRoot":"","sources":["../../src/links/dedupeLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,wBAAgB,UAAU,CACxB,OAAO,SAAS,SAAS,GAAG,SAAS,KAClC,QAAQ,CAAC,OAAO,CAAC,CA6CrB"}
@@ -1 +1 @@
1
- {"version":3,"file":"createChain.d.ts","sourceRoot":"","sources":["../../../src/links/internals/createChain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAE/E,gBAAgB;AAChB,wBAAgB,WAAW,CACzB,OAAO,SAAS,SAAS,EACzB,MAAM,GAAG,OAAO,EAChB,OAAO,GAAG,OAAO,EACjB,IAAI,EAAE;IACN,KAAK,EAAE,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;IACjD,EAAE,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;CACvB,GAAG,yBAAyB,CAAC,OAAO,EAAE,OAAO,CAAC,CAkB9C"}
1
+ {"version":3,"file":"createChain.d.ts","sourceRoot":"","sources":["../../../src/links/internals/createChain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAE/E,gBAAgB;AAChB,wBAAgB,WAAW,CACzB,OAAO,SAAS,SAAS,EACzB,MAAM,GAAG,OAAO,EAChB,OAAO,GAAG,OAAO,EACjB,IAAI,EAAE;IACN,KAAK,EAAE,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;IACjD,EAAE,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;CACvB,GAAG,yBAAyB,CAAC,OAAO,EAAE,OAAO,CAAC,CAuB9C"}
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  require('@trpc/server/observable');
6
- var links_splitLink = require('../splitLink-8202386e.js');
6
+ var links_splitLink = require('../splitLink-23c5ce5c.js');
7
7
 
8
8
 
9
9
 
@@ -1,2 +1,2 @@
1
1
  import '@trpc/server/observable';
2
- export { s as splitLink } from '../splitLink-62ff9db0.mjs';
2
+ export { s as splitLink } from '../splitLink-0202cd7b.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"wsLink.d.ts","sourceRoot":"","sources":["../../src/links/wsLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAiB,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,aAAa,EAAc,MAAM,yBAAyB,CAAC;AASpE,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,uBAAuB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEvE,MAAM,WAAW,sBAAsB;IACrC,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,OAAO,SAAS,CAAC;IAC7B,YAAY,CAAC,EAAE,OAAO,UAAU,CAAC;CAClC;AACD,wBAAgB,cAAc,CAAC,IAAI,EAAE,sBAAsB;;kBA8LpC,SAAS,6DAA0B,aAAa;;EAgDtE;AACD,oBAAY,mBAAmB,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;AAEpE,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,mBAAmB,CAAC;CAC7B;AAiBD,wBAAgB,MAAM,CAAC,OAAO,SAAS,SAAS,EAC9C,IAAI,EAAE,oBAAoB,GACzB,QAAQ,CAAC,OAAO,CAAC,CAoDnB"}
1
+ {"version":3,"file":"wsLink.d.ts","sourceRoot":"","sources":["../../src/links/wsLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAiB,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,aAAa,EAAc,MAAM,yBAAyB,CAAC;AASpE,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,uBAAuB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEvE,MAAM,WAAW,sBAAsB;IACrC,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,OAAO,SAAS,CAAC;IAC7B,YAAY,CAAC,EAAE,OAAO,UAAU,CAAC;CAClC;AACD,wBAAgB,cAAc,CAAC,IAAI,EAAE,sBAAsB;;kBA6LpC,SAAS,6DAA0B,aAAa;;EAgDtE;AACD,oBAAY,mBAAmB,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;AAEpE,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,mBAAmB,CAAC;CAC7B;AAiBD,wBAAgB,MAAM,CAAC,OAAO,SAAS,SAAS,EAC9C,IAAI,EAAE,oBAAoB,GACzB,QAAQ,CAAC,OAAO,CAAC,CAoDnB"}
@@ -9,8 +9,9 @@ require('@babel/runtime/helpers/inherits');
9
9
  require('@babel/runtime/helpers/possibleConstructorReturn');
10
10
  require('@babel/runtime/helpers/getPrototypeOf');
11
11
  require('@babel/runtime/helpers/wrapNativeSuper');
12
+ require('@babel/runtime/helpers/slicedToArray');
12
13
  require('@trpc/server/observable');
13
- var links_wsLink = require('../wsLink-69f5adb8.js');
14
+ var links_wsLink = require('../wsLink-807f4ad9.js');
14
15
  require('@babel/runtime/helpers/defineProperty');
15
16
 
16
17
 
@@ -5,6 +5,7 @@ import '@babel/runtime/helpers/inherits';
5
5
  import '@babel/runtime/helpers/possibleConstructorReturn';
6
6
  import '@babel/runtime/helpers/getPrototypeOf';
7
7
  import '@babel/runtime/helpers/wrapNativeSuper';
8
+ import '@babel/runtime/helpers/slicedToArray';
8
9
  import '@trpc/server/observable';
9
- export { c as createWSClient, w as wsLink } from '../wsLink-9468a563.mjs';
10
+ export { c as createWSClient, w as wsLink } from '../wsLink-11ee4922.mjs';
10
11
  import '@babel/runtime/helpers/defineProperty';
@@ -7,6 +7,11 @@ function createChain(opts) {
7
7
  var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
8
8
  var op = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : opts.op;
9
9
  var next = opts.links[index];
10
+
11
+ if (!next) {
12
+ throw new Error('No more links to execute - did you forget to add an ending link?');
13
+ }
14
+
10
15
  var subscription = next({
11
16
  op: op,
12
17
  next: function next(nextOp) {
@@ -9,6 +9,11 @@ function createChain(opts) {
9
9
  var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
10
10
  var op = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : opts.op;
11
11
  var next = opts.links[index];
12
+
13
+ if (!next) {
14
+ throw new Error('No more links to execute - did you forget to add an ending link?');
15
+ }
16
+
12
17
  var subscription = next({
13
18
  op: op,
14
19
  next: function next(nextOp) {
@@ -5,6 +5,7 @@ import _inherits from '@babel/runtime/helpers/inherits';
5
5
  import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
6
6
  import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
7
7
  import _wrapNativeSuper from '@babel/runtime/helpers/wrapNativeSuper';
8
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
8
9
  import { observable } from '@trpc/server/observable';
9
10
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
10
11
 
@@ -262,10 +263,12 @@ function createWSClient(opts) {
262
263
  tryReconnect();
263
264
  }
264
265
 
265
- for (var key in pendingRequests) {
266
+ for (var _i2 = 0, _Object$entries = Object.entries(pendingRequests); _i2 < _Object$entries.length; _i2++) {
266
267
  var _req$callbacks$error, _req$callbacks2;
267
268
 
268
- var req = pendingRequests[key];
269
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i2], 2),
270
+ key = _Object$entries$_i[0],
271
+ req = _Object$entries$_i[1];
269
272
 
270
273
  if (req.ws !== conn) {
271
274
  continue;
@@ -7,6 +7,7 @@ var _inherits = require('@babel/runtime/helpers/inherits');
7
7
  var _possibleConstructorReturn = require('@babel/runtime/helpers/possibleConstructorReturn');
8
8
  var _getPrototypeOf = require('@babel/runtime/helpers/getPrototypeOf');
9
9
  var _wrapNativeSuper = require('@babel/runtime/helpers/wrapNativeSuper');
10
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
10
11
  var observable = require('@trpc/server/observable');
11
12
  var _defineProperty = require('@babel/runtime/helpers/defineProperty');
12
13
 
@@ -19,6 +20,7 @@ var _inherits__default = /*#__PURE__*/_interopDefaultLegacy(_inherits);
19
20
  var _possibleConstructorReturn__default = /*#__PURE__*/_interopDefaultLegacy(_possibleConstructorReturn);
20
21
  var _getPrototypeOf__default = /*#__PURE__*/_interopDefaultLegacy(_getPrototypeOf);
21
22
  var _wrapNativeSuper__default = /*#__PURE__*/_interopDefaultLegacy(_wrapNativeSuper);
23
+ var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
22
24
  var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
23
25
 
24
26
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
@@ -275,10 +277,12 @@ function createWSClient(opts) {
275
277
  tryReconnect();
276
278
  }
277
279
 
278
- for (var key in pendingRequests) {
280
+ for (var _i2 = 0, _Object$entries = Object.entries(pendingRequests); _i2 < _Object$entries.length; _i2++) {
279
281
  var _req$callbacks$error, _req$callbacks2;
280
282
 
281
- var req = pendingRequests[key];
283
+ var _Object$entries$_i = _slicedToArray__default["default"](_Object$entries[_i2], 2),
284
+ key = _Object$entries$_i[0],
285
+ req = _Object$entries$_i[1];
282
286
 
283
287
  if (req.ws !== conn) {
284
288
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "10.0.0-alpha.36",
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.36"
70
+ "@trpc/server": "10.0.0-alpha.39"
71
71
  },
72
72
  "devDependencies": {
73
- "@trpc/server": "10.0.0-alpha.36"
73
+ "@trpc/server": "10.0.0-alpha.39"
74
74
  },
75
75
  "publishConfig": {
76
76
  "access": "public"
77
77
  },
78
- "gitHead": "c45c7bddde215ec057b2695a5b458cac67451cf3"
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
  }
@@ -112,8 +112,8 @@ export function dataLoader<TKey, TValue>(
112
112
  promise
113
113
  .then((result) => {
114
114
  for (let i = 0; i < result.length; i++) {
115
- const value = result[i];
116
- const item = batch.items[i];
115
+ const value = result[i]!;
116
+ const item = batch.items[i]!;
117
117
  item.resolve(value);
118
118
  item.batch = null;
119
119
  }
@@ -17,9 +17,10 @@ export function dedupeLink<
17
17
  return next(op);
18
18
  }
19
19
  const key = JSON.stringify([op.path, op.input]);
20
- if (pending[key]) {
20
+ const obs$ = pending[key];
21
+ if (obs$) {
21
22
  // console.log('hooking into pending', { op });
22
- return observable((observer) => pending[key].subscribe(observer));
23
+ return observable((observer) => obs$.subscribe(observer));
23
24
  }
24
25
 
25
26
  const shared$ = observable((observer) => {
@@ -14,6 +14,11 @@ export function createChain<
14
14
  return observable((observer) => {
15
15
  function execute(index = 0, op = opts.op) {
16
16
  const next = opts.links[index];
17
+ if (!next) {
18
+ throw new Error(
19
+ 'No more links to execute - did you forget to add an ending link?',
20
+ );
21
+ }
17
22
  const subscription = next({
18
23
  op,
19
24
  next(nextOp) {
@@ -184,8 +184,7 @@ export function createWSClient(opts: WebSocketClientOptions) {
184
184
  tryReconnect();
185
185
  }
186
186
 
187
- for (const key in pendingRequests) {
188
- const req = pendingRequests[key];
187
+ for (const [key, req] of Object.entries(pendingRequests)) {
189
188
  if (req.ws !== conn) {
190
189
  continue;
191
190
  }