@trpc/client 10.4.3 → 10.5.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.
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var observable = require('@trpc/server/observable');
6
- var transformResult = require('./transformResult-f5705001.js');
6
+ var transformResult = require('./transformResult-e7a1e69e.js');
7
7
  var links_splitLink = require('./splitLink-f29e84be.js');
8
8
  var links_httpBatchLink = require('./links/httpBatchLink.js');
9
9
  var shared = require('@trpc/server/shared');
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { share, observableToPromise } from '@trpc/server/observable';
2
- import { T as TRPCClientError } from './transformResult-4e0db787.mjs';
3
- export { T as TRPCClientError } from './transformResult-4e0db787.mjs';
2
+ import { T as TRPCClientError } from './transformResult-6fb67924.mjs';
3
+ export { T as TRPCClientError } from './transformResult-6fb67924.mjs';
4
4
  import { c as createChain } from './splitLink-4c75f7be.mjs';
5
5
  export { s as splitLink } from './splitLink-4c75f7be.mjs';
6
6
  import { httpBatchLink } from './links/httpBatchLink.mjs';
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var observable = require('@trpc/server/observable');
6
- var transformResult = require('../transformResult-f5705001.js');
6
+ var transformResult = require('../transformResult-e7a1e69e.js');
7
7
  var httpUtils = require('../httpUtils-277fd63a.js');
8
8
 
9
9
  /* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
@@ -1,5 +1,5 @@
1
1
  import { observable } from '@trpc/server/observable';
2
- import { t as transformResult, T as TRPCClientError } from '../transformResult-4e0db787.mjs';
2
+ import { t as transformResult, T as TRPCClientError } from '../transformResult-6fb67924.mjs';
3
3
  import { r as resolveHTTPLinkOptions, a as getUrl, h as httpRequest } from '../httpUtils-939364d7.mjs';
4
4
 
5
5
  /* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var observable = require('@trpc/server/observable');
6
- var transformResult = require('../transformResult-f5705001.js');
6
+ var transformResult = require('../transformResult-e7a1e69e.js');
7
7
  var httpUtils = require('../httpUtils-277fd63a.js');
8
8
 
9
9
  function httpLink(opts) {
@@ -1,5 +1,5 @@
1
1
  import { observable } from '@trpc/server/observable';
2
- import { t as transformResult, T as TRPCClientError } from '../transformResult-4e0db787.mjs';
2
+ import { t as transformResult, T as TRPCClientError } from '../transformResult-6fb67924.mjs';
3
3
  import { r as resolveHTTPLinkOptions, h as httpRequest } from '../httpUtils-939364d7.mjs';
4
4
 
5
5
  function httpLink(opts) {
@@ -2,7 +2,7 @@ import { AnyRouter, inferRouterError } from '@trpc/server';
2
2
  import { TRPCResponse, TRPCResponseMessage } from '@trpc/server/rpc';
3
3
  import { TRPCClientRuntime } from '..';
4
4
  /** @internal */
5
- export declare function transformResult<TRouter extends AnyRouter, TOutput>(response: TRPCResponseMessage<TOutput, inferRouterError<TRouter>> | TRPCResponse<TOutput, inferRouterError<TRouter>>, runtime: TRPCClientRuntime): {
5
+ declare function transformResultInner<TRouter extends AnyRouter, TOutput>(response: TRPCResponseMessage<TOutput, inferRouterError<TRouter>> | TRPCResponse<TOutput, inferRouterError<TRouter>>, runtime: TRPCClientRuntime): {
6
6
  readonly ok: false;
7
7
  readonly error: {
8
8
  readonly error: inferRouterError<TRouter>;
@@ -25,4 +25,10 @@ export declare function transformResult<TRouter extends AnyRouter, TOutput>(resp
25
25
  });
26
26
  readonly error?: undefined;
27
27
  };
28
+ /**
29
+ * Transforms and validates that the result is a valid TRPCResponse
30
+ * @internal
31
+ */
32
+ export declare function transformResult<TRouter extends AnyRouter, TOutput>(response: TRPCResponseMessage<TOutput, inferRouterError<TRouter>> | TRPCResponse<TOutput, inferRouterError<TRouter>>, runtime: TRPCClientRuntime): ReturnType<typeof transformResultInner>;
33
+ export {};
28
34
  //# sourceMappingURL=transformResult.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transformResult.d.ts","sourceRoot":"","sources":["../../../src/links/internals/transformResult.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EACL,YAAY,EACZ,mBAAmB,EAEpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAC;AAKvC,gBAAgB;AAChB,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,EAChE,QAAQ,EACJ,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,GACvD,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EACpD,OAAO,EAAE,iBAAiB;;;;;;;;;;;;;;;;;;;;;;EA0B3B"}
1
+ {"version":3,"file":"transformResult.d.ts","sourceRoot":"","sources":["../../../src/links/internals/transformResult.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EACL,YAAY,EACZ,mBAAmB,EAEpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAC;AAMvC,gBAAgB;AAChB,iBAAS,oBAAoB,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,EAC9D,QAAQ,EACJ,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,GACvD,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EACpD,OAAO,EAAE,iBAAiB;;;;;;;;;;;;;;;;;;;;;;EAuB3B;AAOD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,EAChE,QAAQ,EACJ,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,GACvD,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EACpD,OAAO,EAAE,iBAAiB,GACzB,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAqBzC"}
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var observable = require('@trpc/server/observable');
6
- var transformResult = require('../transformResult-f5705001.js');
6
+ var transformResult = require('../transformResult-e7a1e69e.js');
7
7
 
8
8
  /* istanbul ignore next */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
9
9
 
@@ -1,5 +1,5 @@
1
1
  import { observable } from '@trpc/server/observable';
2
- import { T as TRPCClientError, t as transformResult } from '../transformResult-4e0db787.mjs';
2
+ import { T as TRPCClientError, t as transformResult } from '../transformResult-6fb67924.mjs';
3
3
 
4
4
  /* istanbul ignore next */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
5
5
 
@@ -35,7 +35,7 @@ class TRPCClientError extends Error {
35
35
  // FIXME:
36
36
  // - the generics here are probably unnecessary
37
37
  // - the RPC-spec could probably be simplified to combine HTTP + WS
38
- /** @internal */ function transformResult(response, runtime) {
38
+ /** @internal */ function transformResultInner(response, runtime) {
39
39
  if ('error' in response) {
40
40
  const error = runtime.transformer.deserialize(response.error);
41
41
  return {
@@ -46,9 +46,6 @@ class TRPCClientError extends Error {
46
46
  }
47
47
  };
48
48
  }
49
- if (!('result' in response)) {
50
- throw new Error('Badly formatted response from server');
51
- }
52
49
  const result = {
53
50
  ...response.result,
54
51
  ...(!response.result.type || response.result.type === 'data') && {
@@ -61,5 +58,29 @@ class TRPCClientError extends Error {
61
58
  result
62
59
  };
63
60
  }
61
+ function isObject(value) {
62
+ // check that value is object
63
+ return !!value && !Array.isArray(value) && typeof value === 'object';
64
+ }
65
+ /**
66
+ * Transforms and validates that the result is a valid TRPCResponse
67
+ * @internal
68
+ */ function transformResult(response, runtime) {
69
+ let result;
70
+ try {
71
+ // Use the data transformers on the JSON-response
72
+ result = transformResultInner(response, runtime);
73
+ } catch (err) {
74
+ throw new TRPCClientError('Unable to transform response from server');
75
+ }
76
+ // check that output of the transformers is a valid TRPCResponse
77
+ if (!result.ok && (!isObject(result.error.error) || typeof result.error.error.code !== 'number')) {
78
+ throw new TRPCClientError('Badly formatted response from server');
79
+ }
80
+ if (result.ok && !isObject(result.result)) {
81
+ throw new TRPCClientError('Badly formatted response from server');
82
+ }
83
+ return result;
84
+ }
64
85
 
65
86
  export { TRPCClientError as T, transformResult as t };
@@ -37,7 +37,7 @@ class TRPCClientError extends Error {
37
37
  // FIXME:
38
38
  // - the generics here are probably unnecessary
39
39
  // - the RPC-spec could probably be simplified to combine HTTP + WS
40
- /** @internal */ function transformResult(response, runtime) {
40
+ /** @internal */ function transformResultInner(response, runtime) {
41
41
  if ('error' in response) {
42
42
  const error = runtime.transformer.deserialize(response.error);
43
43
  return {
@@ -48,9 +48,6 @@ class TRPCClientError extends Error {
48
48
  }
49
49
  };
50
50
  }
51
- if (!('result' in response)) {
52
- throw new Error('Badly formatted response from server');
53
- }
54
51
  const result = {
55
52
  ...response.result,
56
53
  ...(!response.result.type || response.result.type === 'data') && {
@@ -63,6 +60,30 @@ class TRPCClientError extends Error {
63
60
  result
64
61
  };
65
62
  }
63
+ function isObject(value) {
64
+ // check that value is object
65
+ return !!value && !Array.isArray(value) && typeof value === 'object';
66
+ }
67
+ /**
68
+ * Transforms and validates that the result is a valid TRPCResponse
69
+ * @internal
70
+ */ function transformResult(response, runtime) {
71
+ let result;
72
+ try {
73
+ // Use the data transformers on the JSON-response
74
+ result = transformResultInner(response, runtime);
75
+ } catch (err) {
76
+ throw new TRPCClientError('Unable to transform response from server');
77
+ }
78
+ // check that output of the transformers is a valid TRPCResponse
79
+ if (!result.ok && (!isObject(result.error.error) || typeof result.error.error.code !== 'number')) {
80
+ throw new TRPCClientError('Badly formatted response from server');
81
+ }
82
+ if (result.ok && !isObject(result.result)) {
83
+ throw new TRPCClientError('Badly formatted response from server');
84
+ }
85
+ return result;
86
+ }
66
87
 
67
88
  exports.TRPCClientError = TRPCClientError;
68
89
  exports.transformResult = transformResult;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "10.4.3",
3
+ "version": "10.5.0",
4
4
  "description": "tRPC Client lib",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -66,11 +66,11 @@
66
66
  "links"
67
67
  ],
68
68
  "peerDependencies": {
69
- "@trpc/server": "10.4.3"
69
+ "@trpc/server": "10.5.0"
70
70
  },
71
71
  "devDependencies": {
72
72
  "@testing-library/dom": "^8.18.1",
73
- "@trpc/server": "10.4.3",
73
+ "@trpc/server": "10.5.0",
74
74
  "@types/node": "^18.7.20",
75
75
  "rollup": "^2.79.1",
76
76
  "tsx": "^3.9.0"
@@ -78,5 +78,5 @@
78
78
  "publishConfig": {
79
79
  "access": "public"
80
80
  },
81
- "gitHead": "c5b49adb1d4e875633bb88ce282a3573fa00b404"
81
+ "gitHead": "c7f2d301cb1ab5713bf8b4e7dcaa05d66311cc9f"
82
82
  }
@@ -5,12 +5,13 @@ import {
5
5
  TRPCResultMessage,
6
6
  } from '@trpc/server/rpc';
7
7
  import { TRPCClientRuntime } from '..';
8
+ import { TRPCClientError } from '../../TRPCClientError';
8
9
 
9
10
  // FIXME:
10
11
  // - the generics here are probably unnecessary
11
12
  // - the RPC-spec could probably be simplified to combine HTTP + WS
12
13
  /** @internal */
13
- export function transformResult<TRouter extends AnyRouter, TOutput>(
14
+ function transformResultInner<TRouter extends AnyRouter, TOutput>(
14
15
  response:
15
16
  | TRPCResponseMessage<TOutput, inferRouterError<TRouter>>
16
17
  | TRPCResponse<TOutput, inferRouterError<TRouter>>,
@@ -28,9 +29,6 @@ export function transformResult<TRouter extends AnyRouter, TOutput>(
28
29
  },
29
30
  } as const;
30
31
  }
31
- if (!('result' in response)) {
32
- throw new Error('Badly formatted response from server');
33
- }
34
32
 
35
33
  const result = {
36
34
  ...response.result,
@@ -41,3 +39,40 @@ export function transformResult<TRouter extends AnyRouter, TOutput>(
41
39
  } as TRPCResultMessage<TOutput>['result'];
42
40
  return { ok: true, result } as const;
43
41
  }
42
+
43
+ function isObject(value: unknown): value is Record<string, unknown> {
44
+ // check that value is object
45
+ return !!value && !Array.isArray(value) && typeof value === 'object';
46
+ }
47
+
48
+ /**
49
+ * Transforms and validates that the result is a valid TRPCResponse
50
+ * @internal
51
+ */
52
+ export function transformResult<TRouter extends AnyRouter, TOutput>(
53
+ response:
54
+ | TRPCResponseMessage<TOutput, inferRouterError<TRouter>>
55
+ | TRPCResponse<TOutput, inferRouterError<TRouter>>,
56
+ runtime: TRPCClientRuntime,
57
+ ): ReturnType<typeof transformResultInner> {
58
+ let result: ReturnType<typeof transformResultInner>;
59
+ try {
60
+ // Use the data transformers on the JSON-response
61
+ result = transformResultInner(response, runtime);
62
+ } catch (err) {
63
+ throw new TRPCClientError('Unable to transform response from server');
64
+ }
65
+
66
+ // check that output of the transformers is a valid TRPCResponse
67
+ if (
68
+ !result.ok &&
69
+ (!isObject(result.error.error) ||
70
+ typeof result.error.error.code !== 'number')
71
+ ) {
72
+ throw new TRPCClientError('Badly formatted response from server');
73
+ }
74
+ if (result.ok && !isObject(result.result)) {
75
+ throw new TRPCClientError('Badly formatted response from server');
76
+ }
77
+ return result;
78
+ }