@trpc/client 10.0.0-alpha.41 → 10.0.0-alpha.42

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,7 +1,14 @@
1
1
  import type { AnyRouter, OmitNeverKeys, Procedure, ProcedureArgs, ProcedureRouterRecord, inferProcedureOutput } from '@trpc/server';
2
- import { TRPCClient as Client } from './internals/TRPCClient';
2
+ import type { Unsubscribable, inferObservableValue } from '@trpc/server/observable';
3
+ import type { TRPCResultMessage } from '@trpc/server/rpc';
4
+ import { TRPCClientError } from './TRPCClientError';
5
+ import { TRPCClient as Client, TRPCSubscriptionObserver } from './internals/TRPCClient';
3
6
  declare type Resolver<TProcedure extends Procedure<any>> = (...args: ProcedureArgs<TProcedure['_def']>) => Promise<inferProcedureOutput<TProcedure>>;
4
- declare type DecorateProcedure<TProcedure extends Procedure<any>> = OmitNeverKeys<{
7
+ declare type SubscriptionResolver<TProcedure extends Procedure<any>, TRouter extends AnyRouter> = (...args: [
8
+ input: ProcedureArgs<TProcedure['_def']>[0],
9
+ opts: ProcedureArgs<TProcedure['_def']>[1] & Partial<TRPCSubscriptionObserver<TRPCResultMessage<inferObservableValue<inferProcedureOutput<TProcedure>>>, TRPCClientError<TRouter>>>
10
+ ]) => Unsubscribable;
11
+ declare type DecorateProcedure<TProcedure extends Procedure<any>, TRouter extends AnyRouter> = OmitNeverKeys<{
5
12
  query: TProcedure extends {
6
13
  _query: true;
7
14
  } ? Resolver<TProcedure> : never;
@@ -10,15 +17,15 @@ declare type DecorateProcedure<TProcedure extends Procedure<any>> = OmitNeverKey
10
17
  } ? Resolver<TProcedure> : never;
11
18
  subscribe: TProcedure extends {
12
19
  _subscription: true;
13
- } ? Resolver<TProcedure> : never;
20
+ } ? SubscriptionResolver<TProcedure, TRouter> : never;
14
21
  }>;
15
22
  declare type assertProcedure<T> = T extends Procedure<any> ? T : never;
16
23
  /**
17
24
  * @internal
18
25
  */
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]>>;
26
+ declare type DecoratedProcedureRecord<TProcedures extends ProcedureRouterRecord, TRouter extends AnyRouter> = {
27
+ [TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter ? DecoratedProcedureRecord<TProcedures[TKey]['_def']['record'], TProcedures[TKey]> : DecorateProcedure<assertProcedure<TProcedures[TKey]>, TRouter>;
21
28
  };
22
- export declare function createTRPCClientProxy<TRouter extends AnyRouter>(client: Client<TRouter>): DecoratedProcedureRecord<TRouter["_def"]["record"]>;
29
+ export declare function createTRPCClientProxy<TRouter extends AnyRouter>(client: Client<TRouter>): DecoratedProcedureRecord<TRouter["_def"]["record"], TRouter>;
23
30
  export {};
24
31
  //# sourceMappingURL=createTRPCClientProxy.d.ts.map
@@ -1 +1 @@
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,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/D;;GAEG;AACH,aAAK,wBAAwB,CAC3B,WAAW,SAAS,qBAAqB,EACzC,OAAO,SAAS,SAAS,IACvB;KACD,IAAI,IAAI,MAAM,WAAW,GAAG,WAAW,CAAC,IAAI,CAAC,SAAS,SAAS,GAC5D,wBAAwB,CACtB,WAAW,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,EACnC,WAAW,CAAC,IAAI,CAAC,CAClB,GACD,iBAAiB,CAAC,eAAe,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC;CACnE,CAAC;AAWF,wBAAgB,qBAAqB,CAAC,OAAO,SAAS,SAAS,EAC7D,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,gEAWxB"}
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ 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-807f4ad9.js');
8
+ var links_wsLink = require('./wsLink-44703a83.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');
@@ -343,7 +343,7 @@ var TRPCClient = /*#__PURE__*/function () {
343
343
  args[_key - 1] = arguments[_key];
344
344
  }
345
345
 
346
- var context = (_args$ = args[1]) === null || _args$ === void 0 ? void 0 : _args$.context;
346
+ var context = (_args$ = args[1]) === null || _args$ === void 0 ? void 0 : _args$.requestContext;
347
347
  return this.requestAsPromise({
348
348
  type: 'query',
349
349
  path: path,
@@ -360,7 +360,7 @@ var TRPCClient = /*#__PURE__*/function () {
360
360
  args[_key2 - 1] = arguments[_key2];
361
361
  }
362
362
 
363
- var context = (_args$2 = args[1]) === null || _args$2 === void 0 ? void 0 : _args$2.context;
363
+ var context = (_args$2 = args[1]) === null || _args$2 === void 0 ? void 0 : _args$2.requestContext;
364
364
  return this.requestAsPromise({
365
365
  type: 'mutation',
366
366
  path: path,
@@ -375,33 +375,44 @@ var TRPCClient = /*#__PURE__*/function () {
375
375
  type: 'subscription',
376
376
  path: path,
377
377
  input: input,
378
- context: opts.context
378
+ context: opts.requestContext
379
379
  });
380
380
  var runtime = this.runtime;
381
381
  return observable$.subscribe({
382
382
  next: function next(result) {
383
- var _opts$error;
383
+ var _opts$onError;
384
384
 
385
385
  var transformed = transformSubscriptionOperationResult(result, runtime);
386
386
 
387
387
  if (transformed.ok) {
388
- var _opts$next;
388
+ if (transformed.data.type === 'started') {
389
+ var _opts$onStarted;
390
+
391
+ (_opts$onStarted = opts.onStarted) === null || _opts$onStarted === void 0 ? void 0 : _opts$onStarted.call(opts);
392
+ } else if (transformed.data.type === 'stopped') {
393
+ var _opts$onStopped;
394
+
395
+ (_opts$onStopped = opts.onStopped) === null || _opts$onStopped === void 0 ? void 0 : _opts$onStopped.call(opts);
396
+ } else if (transformed.data.type === 'data') {
397
+ var _opts$onData;
398
+
399
+ (_opts$onData = opts.onData) === null || _opts$onData === void 0 ? void 0 : _opts$onData.call(opts, transformed.data.data);
400
+ }
389
401
 
390
- (_opts$next = opts.next) === null || _opts$next === void 0 ? void 0 : _opts$next.call(opts, transformed.data);
391
402
  return;
392
403
  }
393
404
 
394
- (_opts$error = opts.error) === null || _opts$error === void 0 ? void 0 : _opts$error.call(opts, transformed.error);
405
+ (_opts$onError = opts.onError) === null || _opts$onError === void 0 ? void 0 : _opts$onError.call(opts, transformed.error);
395
406
  },
396
407
  error: function error(err) {
397
- var _opts$error2;
408
+ var _opts$onError2;
398
409
 
399
- (_opts$error2 = opts.error) === null || _opts$error2 === void 0 ? void 0 : _opts$error2.call(opts, err);
410
+ (_opts$onError2 = opts.onError) === null || _opts$onError2 === void 0 ? void 0 : _opts$onError2.call(opts, err);
400
411
  },
401
412
  complete: function complete() {
402
- var _opts$complete;
413
+ var _opts$onComplete;
403
414
 
404
- (_opts$complete = opts.complete) === null || _opts$complete === void 0 ? void 0 : _opts$complete.call(opts);
415
+ (_opts$onComplete = opts.onComplete) === null || _opts$onComplete === void 0 ? void 0 : _opts$onComplete.call(opts);
405
416
  }
406
417
  });
407
418
  }
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
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-11ee4922.mjs';
5
- export { T as TRPCClientError, c as createWSClient, w as wsLink } from './wsLink-11ee4922.mjs';
4
+ import { T as TRPCClientError } from './wsLink-27b93bd0.mjs';
5
+ export { T as TRPCClientError, c as createWSClient, w as wsLink } from './wsLink-27b93bd0.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';
@@ -335,7 +335,7 @@ var TRPCClient = /*#__PURE__*/function () {
335
335
  args[_key - 1] = arguments[_key];
336
336
  }
337
337
 
338
- var context = (_args$ = args[1]) === null || _args$ === void 0 ? void 0 : _args$.context;
338
+ var context = (_args$ = args[1]) === null || _args$ === void 0 ? void 0 : _args$.requestContext;
339
339
  return this.requestAsPromise({
340
340
  type: 'query',
341
341
  path: path,
@@ -352,7 +352,7 @@ var TRPCClient = /*#__PURE__*/function () {
352
352
  args[_key2 - 1] = arguments[_key2];
353
353
  }
354
354
 
355
- var context = (_args$2 = args[1]) === null || _args$2 === void 0 ? void 0 : _args$2.context;
355
+ var context = (_args$2 = args[1]) === null || _args$2 === void 0 ? void 0 : _args$2.requestContext;
356
356
  return this.requestAsPromise({
357
357
  type: 'mutation',
358
358
  path: path,
@@ -367,33 +367,44 @@ var TRPCClient = /*#__PURE__*/function () {
367
367
  type: 'subscription',
368
368
  path: path,
369
369
  input: input,
370
- context: opts.context
370
+ context: opts.requestContext
371
371
  });
372
372
  var runtime = this.runtime;
373
373
  return observable$.subscribe({
374
374
  next: function next(result) {
375
- var _opts$error;
375
+ var _opts$onError;
376
376
 
377
377
  var transformed = transformSubscriptionOperationResult(result, runtime);
378
378
 
379
379
  if (transformed.ok) {
380
- var _opts$next;
380
+ if (transformed.data.type === 'started') {
381
+ var _opts$onStarted;
382
+
383
+ (_opts$onStarted = opts.onStarted) === null || _opts$onStarted === void 0 ? void 0 : _opts$onStarted.call(opts);
384
+ } else if (transformed.data.type === 'stopped') {
385
+ var _opts$onStopped;
386
+
387
+ (_opts$onStopped = opts.onStopped) === null || _opts$onStopped === void 0 ? void 0 : _opts$onStopped.call(opts);
388
+ } else if (transformed.data.type === 'data') {
389
+ var _opts$onData;
390
+
391
+ (_opts$onData = opts.onData) === null || _opts$onData === void 0 ? void 0 : _opts$onData.call(opts, transformed.data.data);
392
+ }
381
393
 
382
- (_opts$next = opts.next) === null || _opts$next === void 0 ? void 0 : _opts$next.call(opts, transformed.data);
383
394
  return;
384
395
  }
385
396
 
386
- (_opts$error = opts.error) === null || _opts$error === void 0 ? void 0 : _opts$error.call(opts, transformed.error);
397
+ (_opts$onError = opts.onError) === null || _opts$onError === void 0 ? void 0 : _opts$onError.call(opts, transformed.error);
387
398
  },
388
399
  error: function error(err) {
389
- var _opts$error2;
400
+ var _opts$onError2;
390
401
 
391
- (_opts$error2 = opts.error) === null || _opts$error2 === void 0 ? void 0 : _opts$error2.call(opts, err);
402
+ (_opts$onError2 = opts.onError) === null || _opts$onError2 === void 0 ? void 0 : _opts$onError2.call(opts, err);
392
403
  },
393
404
  complete: function complete() {
394
- var _opts$complete;
405
+ var _opts$onComplete;
395
406
 
396
- (_opts$complete = opts.complete) === null || _opts$complete === void 0 ? void 0 : _opts$complete.call(opts);
407
+ (_opts$onComplete = opts.onComplete) === null || _opts$onComplete === void 0 ? void 0 : _opts$onComplete.call(opts);
397
408
  }
398
409
  });
399
410
  }
@@ -1,6 +1,5 @@
1
1
  import { AnyRouter, ClientDataTransformerOptions, inferHandlerInput, inferProcedureInput, inferProcedureOutput, inferSubscriptionOutput } from '@trpc/server';
2
- import { Observer, Unsubscribable } from '@trpc/server/observable';
3
- import { TRPCResultMessage } from '@trpc/server/rpc';
2
+ import { Unsubscribable } from '@trpc/server/observable';
4
3
  import { CancelFn } from '..';
5
4
  import { TRPCClientError } from '../TRPCClientError';
6
5
  import { HTTPHeaders, OperationContext, TRPCClientRuntime, TRPCLink } from '../links/types';
@@ -44,7 +43,14 @@ export interface TRPCRequestOptions {
44
43
  /**
45
44
  * Pass additional context to links
46
45
  */
47
- context?: OperationContext;
46
+ requestContext?: OperationContext;
47
+ }
48
+ export interface TRPCSubscriptionObserver<TValue, TError> {
49
+ onStarted: () => void;
50
+ onData: (value: TValue) => void;
51
+ onError: (err: TError) => void;
52
+ onStopped: () => void;
53
+ onComplete: () => void;
48
54
  }
49
55
  /** @internal */
50
56
  export declare type CreateTRPCClientOptions<TRouter extends AnyRouter> = CreateTRPCClientWithLinksOptions<TRouter> | CreateTRPCClientWithURLOptions;
@@ -56,7 +62,7 @@ export declare class TRPCClient<TRouter extends AnyRouter> {
56
62
  private requestAsPromise;
57
63
  query<TQueries extends TRouter['_def']['queries'], TPath extends string & keyof TQueries>(path: TPath, ...args: [...inferHandlerInput<TQueries[TPath]>, TRPCRequestOptions?]): CancellablePromise<inferProcedureOutput<TQueries[TPath]>>;
58
64
  mutation<TMutations extends TRouter['_def']['mutations'], TPath extends string & keyof TMutations>(path: TPath, ...args: [...inferHandlerInput<TMutations[TPath]>, TRPCRequestOptions?]): CancellablePromise<inferProcedureOutput<TMutations[TPath]>>;
59
- subscription<TSubscriptions extends TRouter['_def']['subscriptions'], TPath extends string & keyof TSubscriptions, TOutput extends inferSubscriptionOutput<TRouter, TPath>, TInput extends inferProcedureInput<TSubscriptions[TPath]>>(path: TPath, input: TInput, opts: TRPCRequestOptions & Partial<Observer<TRPCResultMessage<TOutput>, TRPCClientError<TRouter>>>): Unsubscribable;
65
+ subscription<TSubscriptions extends TRouter['_def']['subscriptions'], TPath extends string & keyof TSubscriptions, TOutput extends inferSubscriptionOutput<TRouter, TPath>, TInput extends inferProcedureInput<TSubscriptions[TPath]>>(path: TPath, input: TInput, opts: TRPCRequestOptions & Partial<TRPCSubscriptionObserver<TOutput, TRPCClientError<TRouter>>>): Unsubscribable;
60
66
  }
61
67
  export {};
62
68
  //# sourceMappingURL=TRPCClient.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"TRPCClient.d.ts","sourceRoot":"","sources":["../../src/internals/TRPCClient.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,4BAA4B,EAE5B,iBAAiB,EACjB,mBAAmB,EACnB,oBAAoB,EACpB,uBAAuB,EACxB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,QAAQ,EACR,cAAc,EAIf,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAQrD,OAAO,EACL,WAAW,EACX,gBAAgB,EAEhB,iBAAiB,EACjB,QAAQ,EACT,MAAM,gBAAgB,CAAC;AAGxB,aAAK,kBAAkB,CAAC,CAAC,GAAG,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG;IAClD,MAAM,EAAE,QAAQ,CAAC;CAClB,CAAC;AAMF,UAAU,2BAA2B;IACnC;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB;;OAEG;IACH,eAAe,CAAC,EAAE,OAAO,eAAe,CAAC;IACzC;;OAEG;IACH,OAAO,CAAC,EAAE,WAAW,GAAG,CAAC,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;IACnE;;;QAGI;IACJ,WAAW,CAAC,EAAE,4BAA4B,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AAE,kBAAkB,CAAC,CAAC;IAahE,QAAQ,CACb,UAAU,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC,EAC/C,KAAK,SAAS,MAAM,GAAG,MAAM,UAAU,EAEvC,IAAI,EAAE,KAAK,EACX,GAAG,IAAI,EAAE,CAAC,GAAG,iBAAiB,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC;IAalE,YAAY,CACjB,cAAc,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,eAAe,CAAC,EACvD,KAAK,SAAS,MAAM,GAAG,MAAM,cAAc,EAC3C,OAAO,SAAS,uBAAuB,CAAC,OAAO,EAAE,KAAK,CAAC,EACvD,MAAM,SAAS,mBAAmB,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,EAEzD,IAAI,EAAE,KAAK,EACX,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,kBAAkB,GACtB,OAAO,CAAC,wBAAwB,CAAC,OAAO,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,GACrE,cAAc;CAkClB"}
@@ -6,6 +6,8 @@ export interface WebSocketClientOptions {
6
6
  url: string;
7
7
  WebSocket?: typeof WebSocket;
8
8
  retryDelayMs?: typeof retryDelay;
9
+ onOpen?: () => void;
10
+ onClose?: () => void;
9
11
  }
10
12
  export declare function createWSClient(opts: WebSocketClientOptions): {
11
13
  close: () => void;
@@ -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,G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SAAS,SAAS,EAC9C,IAAI,EAAE,oBAAoB,GACzB,QAAQ,CAAC,OAAO,CAAC,CAoDnB"}
@@ -11,7 +11,7 @@ require('@babel/runtime/helpers/getPrototypeOf');
11
11
  require('@babel/runtime/helpers/wrapNativeSuper');
12
12
  require('@babel/runtime/helpers/slicedToArray');
13
13
  require('@trpc/server/observable');
14
- var links_wsLink = require('../wsLink-807f4ad9.js');
14
+ var links_wsLink = require('../wsLink-44703a83.js');
15
15
  require('@babel/runtime/helpers/defineProperty');
16
16
 
17
17
 
@@ -7,5 +7,5 @@ import '@babel/runtime/helpers/getPrototypeOf';
7
7
  import '@babel/runtime/helpers/wrapNativeSuper';
8
8
  import '@babel/runtime/helpers/slicedToArray';
9
9
  import '@trpc/server/observable';
10
- export { c as createWSClient, w as wsLink } from '../wsLink-11ee4922.mjs';
10
+ export { c as createWSClient, w as wsLink } from '../wsLink-27b93bd0.mjs';
11
11
  import '@babel/runtime/helpers/defineProperty';
@@ -89,7 +89,9 @@ function createWSClient(opts) {
89
89
  _opts$WebSocket = opts.WebSocket,
90
90
  WebSocketImpl = _opts$WebSocket === void 0 ? WebSocket : _opts$WebSocket,
91
91
  _opts$retryDelayMs = opts.retryDelayMs,
92
- retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs;
92
+ retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs,
93
+ onOpen = opts.onOpen,
94
+ onClose = opts.onClose;
93
95
  /* istanbul ignore next */
94
96
 
95
97
  if (!WebSocketImpl) {
@@ -194,6 +196,7 @@ function createWSClient(opts) {
194
196
 
195
197
  connectAttempt = 0;
196
198
  state = 'open';
199
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
197
200
  dispatch();
198
201
  });
199
202
  conn.addEventListener('error', function () {
@@ -204,6 +207,10 @@ function createWSClient(opts) {
204
207
 
205
208
  var handleIncomingRequest = function handleIncomingRequest(req) {
206
209
  if (req.method === 'reconnect' && conn === activeConnection) {
210
+ if (state === 'open') {
211
+ onClose === null || onClose === void 0 ? void 0 : onClose();
212
+ }
213
+
207
214
  reconnect(); // notify subscribers
208
215
 
209
216
  for (var _i = 0, _Object$values = Object.values(pendingRequests); _i < _Object$values.length; _i++) {
@@ -258,6 +265,10 @@ function createWSClient(opts) {
258
265
  }
259
266
  });
260
267
  conn.addEventListener('close', function () {
268
+ if (state === 'open') {
269
+ onClose === null || onClose === void 0 ? void 0 : onClose();
270
+ }
271
+
261
272
  if (activeConnection === conn) {
262
273
  // connection might have been replaced already
263
274
  tryReconnect();
@@ -335,6 +346,7 @@ function createWSClient(opts) {
335
346
  return {
336
347
  close: function close() {
337
348
  state = 'closed';
349
+ onClose === null || onClose === void 0 ? void 0 : onClose();
338
350
  closeIfNoPending(activeConnection);
339
351
  clearTimeout(connectTimer);
340
352
  connectTimer = null;
@@ -103,7 +103,9 @@ function createWSClient(opts) {
103
103
  _opts$WebSocket = opts.WebSocket,
104
104
  WebSocketImpl = _opts$WebSocket === void 0 ? WebSocket : _opts$WebSocket,
105
105
  _opts$retryDelayMs = opts.retryDelayMs,
106
- retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs;
106
+ retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs,
107
+ onOpen = opts.onOpen,
108
+ onClose = opts.onClose;
107
109
  /* istanbul ignore next */
108
110
 
109
111
  if (!WebSocketImpl) {
@@ -208,6 +210,7 @@ function createWSClient(opts) {
208
210
 
209
211
  connectAttempt = 0;
210
212
  state = 'open';
213
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
211
214
  dispatch();
212
215
  });
213
216
  conn.addEventListener('error', function () {
@@ -218,6 +221,10 @@ function createWSClient(opts) {
218
221
 
219
222
  var handleIncomingRequest = function handleIncomingRequest(req) {
220
223
  if (req.method === 'reconnect' && conn === activeConnection) {
224
+ if (state === 'open') {
225
+ onClose === null || onClose === void 0 ? void 0 : onClose();
226
+ }
227
+
221
228
  reconnect(); // notify subscribers
222
229
 
223
230
  for (var _i = 0, _Object$values = Object.values(pendingRequests); _i < _Object$values.length; _i++) {
@@ -272,6 +279,10 @@ function createWSClient(opts) {
272
279
  }
273
280
  });
274
281
  conn.addEventListener('close', function () {
282
+ if (state === 'open') {
283
+ onClose === null || onClose === void 0 ? void 0 : onClose();
284
+ }
285
+
275
286
  if (activeConnection === conn) {
276
287
  // connection might have been replaced already
277
288
  tryReconnect();
@@ -349,6 +360,7 @@ function createWSClient(opts) {
349
360
  return {
350
361
  close: function close() {
351
362
  state = 'closed';
363
+ onClose === null || onClose === void 0 ? void 0 : onClose();
352
364
  closeIfNoPending(activeConnection);
353
365
  clearTimeout(connectTimer);
354
366
  connectTimer = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "10.0.0-alpha.41",
3
+ "version": "10.0.0-alpha.42",
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.41"
70
+ "@trpc/server": "10.0.0-alpha.42"
71
71
  },
72
72
  "devDependencies": {
73
- "@trpc/server": "10.0.0-alpha.41"
73
+ "@trpc/server": "10.0.0-alpha.42"
74
74
  },
75
75
  "publishConfig": {
76
76
  "access": "public"
77
77
  },
78
- "gitHead": "927344f2dd01312598720f0165a8f7ad3972761d"
78
+ "gitHead": "d5909785e8d024ef7869d9f5423dbe3bfd80cd59"
79
79
  }
@@ -10,20 +10,50 @@ import type {
10
10
  ProcedureType,
11
11
  inferProcedureOutput,
12
12
  } from '@trpc/server';
13
+ import type {
14
+ Unsubscribable,
15
+ inferObservableValue,
16
+ } from '@trpc/server/observable';
17
+ import type { TRPCResultMessage } from '@trpc/server/rpc';
13
18
  import { createProxy } from '@trpc/server/shared';
14
- import { TRPCClient as Client } from './internals/TRPCClient';
19
+ import { TRPCClientError } from './TRPCClientError';
20
+ import {
21
+ TRPCClient as Client,
22
+ TRPCSubscriptionObserver,
23
+ } from './internals/TRPCClient';
15
24
 
16
25
  type Resolver<TProcedure extends Procedure<any>> = (
17
26
  ...args: ProcedureArgs<TProcedure['_def']>
18
27
  ) => Promise<inferProcedureOutput<TProcedure>>;
19
28
 
20
- type DecorateProcedure<TProcedure extends Procedure<any>> = OmitNeverKeys<{
29
+ type SubscriptionResolver<
30
+ TProcedure extends Procedure<any>,
31
+ TRouter extends AnyRouter,
32
+ > = (
33
+ ...args: [
34
+ input: ProcedureArgs<TProcedure['_def']>[0],
35
+ opts: ProcedureArgs<TProcedure['_def']>[1] &
36
+ Partial<
37
+ TRPCSubscriptionObserver<
38
+ TRPCResultMessage<
39
+ inferObservableValue<inferProcedureOutput<TProcedure>>
40
+ >,
41
+ TRPCClientError<TRouter>
42
+ >
43
+ >,
44
+ ]
45
+ ) => Unsubscribable;
46
+
47
+ type DecorateProcedure<
48
+ TProcedure extends Procedure<any>,
49
+ TRouter extends AnyRouter,
50
+ > = OmitNeverKeys<{
21
51
  query: TProcedure extends { _query: true } ? Resolver<TProcedure> : never;
22
52
 
23
53
  mutate: TProcedure extends { _mutation: true } ? Resolver<TProcedure> : never;
24
54
 
25
55
  subscribe: TProcedure extends { _subscription: true }
26
- ? Resolver<TProcedure>
56
+ ? SubscriptionResolver<TProcedure, TRouter>
27
57
  : never;
28
58
  }>;
29
59
 
@@ -32,13 +62,22 @@ type assertProcedure<T> = T extends Procedure<any> ? T : never;
32
62
  /**
33
63
  * @internal
34
64
  */
35
- type DecoratedProcedureRecord<TProcedures extends ProcedureRouterRecord> = {
65
+ type DecoratedProcedureRecord<
66
+ TProcedures extends ProcedureRouterRecord,
67
+ TRouter extends AnyRouter,
68
+ > = {
36
69
  [TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
37
- ? DecoratedProcedureRecord<TProcedures[TKey]['_def']['record']>
38
- : DecorateProcedure<assertProcedure<TProcedures[TKey]>>;
70
+ ? DecoratedProcedureRecord<
71
+ TProcedures[TKey]['_def']['record'],
72
+ TProcedures[TKey]
73
+ >
74
+ : DecorateProcedure<assertProcedure<TProcedures[TKey]>, TRouter>;
39
75
  };
40
76
 
41
- const clientCallTypeMap: Record<keyof DecorateProcedure<any>, ProcedureType> = {
77
+ const clientCallTypeMap: Record<
78
+ keyof DecorateProcedure<any, any>,
79
+ ProcedureType
80
+ > = {
42
81
  query: 'query',
43
82
  mutate: 'mutation',
44
83
  subscribe: 'subscription',
@@ -49,11 +88,11 @@ export function createTRPCClientProxy<TRouter extends AnyRouter>(
49
88
  ) {
50
89
  const proxy = createProxy(({ path, args }) => {
51
90
  const pathCopy = [...path];
52
- const clientCallType = pathCopy.pop()! as keyof DecorateProcedure<any>;
91
+ const clientCallType = pathCopy.pop()! as keyof DecorateProcedure<any, any>;
53
92
  const procedureType = clientCallTypeMap[clientCallType];
54
93
 
55
94
  const fullPath = pathCopy.join('.');
56
95
  return (client as any)[procedureType](fullPath, ...args);
57
96
  });
58
- return proxy as DecoratedProcedureRecord<TRouter['_def']['record']>;
97
+ return proxy as DecoratedProcedureRecord<TRouter['_def']['record'], TRouter>;
59
98
  }
@@ -8,13 +8,11 @@ import {
8
8
  inferSubscriptionOutput,
9
9
  } from '@trpc/server';
10
10
  import {
11
- Observer,
12
11
  Unsubscribable,
13
12
  inferObservableValue,
14
13
  observableToPromise,
15
14
  share,
16
15
  } from '@trpc/server/observable';
17
- import { TRPCResultMessage } from '@trpc/server/rpc';
18
16
  import { CancelFn } from '..';
19
17
  import { TRPCClientError } from '../TRPCClientError';
20
18
  import { getFetch } from '../getFetch';
@@ -84,7 +82,15 @@ export interface TRPCRequestOptions {
84
82
  /**
85
83
  * Pass additional context to links
86
84
  */
87
- context?: OperationContext;
85
+ requestContext?: OperationContext;
86
+ }
87
+
88
+ export interface TRPCSubscriptionObserver<TValue, TError> {
89
+ onStarted: () => void;
90
+ onData: (value: TValue) => void;
91
+ onError: (err: TError) => void;
92
+ onStopped: () => void;
93
+ onComplete: () => void;
88
94
  }
89
95
 
90
96
  /** @internal */
@@ -197,7 +203,7 @@ export class TRPCClient<TRouter extends AnyRouter> {
197
203
  path: TPath,
198
204
  ...args: [...inferHandlerInput<TQueries[TPath]>, TRPCRequestOptions?]
199
205
  ) {
200
- const context = (args[1] as TRPCRequestOptions | undefined)?.context;
206
+ const context = (args[1] as TRPCRequestOptions | undefined)?.requestContext;
201
207
  return this.requestAsPromise<
202
208
  inferHandlerInput<TQueries[TPath]>,
203
209
  inferProcedureOutput<TQueries[TPath]>
@@ -215,7 +221,7 @@ export class TRPCClient<TRouter extends AnyRouter> {
215
221
  path: TPath,
216
222
  ...args: [...inferHandlerInput<TMutations[TPath]>, TRPCRequestOptions?]
217
223
  ) {
218
- const context = (args[1] as TRPCRequestOptions | undefined)?.context;
224
+ const context = (args[1] as TRPCRequestOptions | undefined)?.requestContext;
219
225
  return this.requestAsPromise<
220
226
  inferHandlerInput<TMutations[TPath]>,
221
227
  inferProcedureOutput<TMutations[TPath]>
@@ -235,13 +241,13 @@ export class TRPCClient<TRouter extends AnyRouter> {
235
241
  path: TPath,
236
242
  input: TInput,
237
243
  opts: TRPCRequestOptions &
238
- Partial<Observer<TRPCResultMessage<TOutput>, TRPCClientError<TRouter>>>,
244
+ Partial<TRPCSubscriptionObserver<TOutput, TRPCClientError<TRouter>>>,
239
245
  ): Unsubscribable {
240
246
  const observable$ = this.$request<TInput, TOutput>({
241
247
  type: 'subscription',
242
248
  path,
243
249
  input,
244
- context: opts.context,
250
+ context: opts.requestContext,
245
251
  });
246
252
  const runtime = this.runtime;
247
253
  return observable$.subscribe({
@@ -251,16 +257,22 @@ export class TRPCClient<TRouter extends AnyRouter> {
251
257
  runtime,
252
258
  );
253
259
  if (transformed.ok) {
254
- opts.next?.(transformed.data);
260
+ if (transformed.data.type === 'started') {
261
+ opts.onStarted?.();
262
+ } else if (transformed.data.type === 'stopped') {
263
+ opts.onStopped?.();
264
+ } else if (transformed.data.type === 'data') {
265
+ opts.onData?.(transformed.data.data);
266
+ }
255
267
  return;
256
268
  }
257
- opts.error?.(transformed.error);
269
+ opts.onError?.(transformed.error);
258
270
  },
259
271
  error(err) {
260
- opts.error?.(err);
272
+ opts.onError?.(err);
261
273
  },
262
274
  complete() {
263
- opts.complete?.();
275
+ opts.onComplete?.();
264
276
  },
265
277
  });
266
278
  }
@@ -15,12 +15,17 @@ export interface WebSocketClientOptions {
15
15
  url: string;
16
16
  WebSocket?: typeof WebSocket;
17
17
  retryDelayMs?: typeof retryDelay;
18
+ onOpen?: () => void;
19
+ onClose?: () => void;
18
20
  }
21
+
19
22
  export function createWSClient(opts: WebSocketClientOptions) {
20
23
  const {
21
24
  url,
22
25
  WebSocket: WebSocketImpl = WebSocket,
23
26
  retryDelayMs: retryDelayFn = retryDelay,
27
+ onOpen,
28
+ onClose,
24
29
  } = opts;
25
30
  /* istanbul ignore next */
26
31
  if (!WebSocketImpl) {
@@ -123,6 +128,7 @@ export function createWSClient(opts: WebSocketClientOptions) {
123
128
  }
124
129
  connectAttempt = 0;
125
130
  state = 'open';
131
+ onOpen?.();
126
132
  dispatch();
127
133
  });
128
134
  conn.addEventListener('error', () => {
@@ -132,6 +138,9 @@ export function createWSClient(opts: WebSocketClientOptions) {
132
138
  });
133
139
  const handleIncomingRequest = (req: TRPCClientIncomingRequest) => {
134
140
  if (req.method === 'reconnect' && conn === activeConnection) {
141
+ if (state === 'open') {
142
+ onClose?.();
143
+ }
135
144
  reconnect();
136
145
  // notify subscribers
137
146
  for (const pendingReq of Object.values(pendingRequests)) {
@@ -179,6 +188,10 @@ export function createWSClient(opts: WebSocketClientOptions) {
179
188
  });
180
189
 
181
190
  conn.addEventListener('close', () => {
191
+ if (state === 'open') {
192
+ onClose?.();
193
+ }
194
+
182
195
  if (activeConnection === conn) {
183
196
  // connection might have been replaced already
184
197
  tryReconnect();
@@ -244,6 +257,7 @@ export function createWSClient(opts: WebSocketClientOptions) {
244
257
  return {
245
258
  close: () => {
246
259
  state = 'closed';
260
+ onClose?.();
247
261
  closeIfNoPending(activeConnection);
248
262
  clearTimeout(connectTimer as any);
249
263
  connectTimer = null;