@trpc/server 10.11.1 → 10.13.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 (75) hide show
  1. package/dist/TRPCError-ef1ceb74.js +55 -0
  2. package/dist/adapters/aws-lambda/index.js +2 -2
  3. package/dist/adapters/aws-lambda/index.mjs +2 -2
  4. package/dist/adapters/express.js +3 -3
  5. package/dist/adapters/express.mjs +3 -3
  6. package/dist/adapters/fastify/index.js +3 -3
  7. package/dist/adapters/fastify/index.mjs +3 -3
  8. package/dist/adapters/fetch/index.js +3 -3
  9. package/dist/adapters/fetch/index.mjs +3 -3
  10. package/dist/adapters/next.js +3 -3
  11. package/dist/adapters/next.mjs +3 -3
  12. package/dist/adapters/node-http/index.js +3 -3
  13. package/dist/adapters/node-http/index.mjs +3 -3
  14. package/dist/adapters/standalone.js +3 -3
  15. package/dist/adapters/standalone.mjs +3 -3
  16. package/dist/adapters/ws.js +10 -10
  17. package/dist/adapters/ws.mjs +10 -10
  18. package/dist/codes-ec2bad91.js +45 -0
  19. package/dist/{config-d356ff77.js → config-cc5b2d45.js} +1 -1
  20. package/dist/{config-4ca0221b.mjs → config-cd794c2a.mjs} +1 -1
  21. package/dist/config-e8f70019.js +225 -0
  22. package/dist/core/index.d.ts +1 -0
  23. package/dist/core/index.d.ts.map +1 -1
  24. package/dist/core/internals/config.d.ts +4 -1
  25. package/dist/core/internals/config.d.ts.map +1 -1
  26. package/dist/core/internals/procedureBuilder.d.ts +3 -6
  27. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  28. package/dist/error/TRPCError.d.ts +1 -1
  29. package/dist/http/index.js +2 -2
  30. package/dist/http/index.mjs +2 -2
  31. package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
  32. package/dist/index-f38f12ba.js +47 -0
  33. package/dist/index.js +1 -1
  34. package/dist/index.mjs +2 -2
  35. package/dist/internals.d.ts +2 -3
  36. package/dist/internals.d.ts.map +1 -1
  37. package/dist/{nodeHTTPRequestHandler-34404221.mjs → nodeHTTPRequestHandler-85d2b636.mjs} +1 -1
  38. package/dist/{nodeHTTPRequestHandler-64cf5f55.js → nodeHTTPRequestHandler-a649e036.js} +1 -1
  39. package/dist/nodeHTTPRequestHandler-fa0b13d4.js +76 -0
  40. package/dist/observable-a1c2103f.js +83 -0
  41. package/dist/{resolveHTTPResponse-3408bbf7.js → resolveHTTPResponse-6cffb0ce.js} +4 -3
  42. package/dist/{resolveHTTPResponse-b831e644.mjs → resolveHTTPResponse-7a4f75b3.mjs} +4 -3
  43. package/dist/resolveHTTPResponse-fed650e4.js +205 -0
  44. package/dist/subscription.js +3 -3
  45. package/dist/subscription.mjs +3 -3
  46. package/dist/transformTRPCResponse-896669e0.js +28 -0
  47. package/dist/transformer.d.ts +35 -8
  48. package/dist/transformer.d.ts.map +1 -1
  49. package/package.json +10 -14
  50. package/src/adapters/fastify/fastifyRequestHandler.ts +1 -1
  51. package/src/adapters/fetch/fetchRequestHandler.ts +1 -1
  52. package/src/adapters/ws.ts +9 -9
  53. package/src/core/index.ts +1 -0
  54. package/src/core/internals/config.ts +7 -2
  55. package/src/core/internals/procedureBuilder.ts +9 -20
  56. package/src/http/resolveHTTPResponse.ts +3 -2
  57. package/src/internals.ts +2 -3
  58. package/src/observable/observable.test.ts +15 -15
  59. package/src/observable/operators/map.test.ts +2 -2
  60. package/src/observable/operators/share.test.ts +9 -9
  61. package/src/subscription.ts +3 -3
  62. package/src/transformer.ts +36 -8
  63. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.d.ts +0 -1
  64. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +0 -190
  65. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.js +0 -7
  66. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +0 -190
  67. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +0 -186
  68. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.d.ts +0 -1
  69. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +0 -109
  70. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.js +0 -7
  71. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +0 -109
  72. package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +0 -104
  73. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +0 -202
  74. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +0 -1
  75. package/src/core/internals/__generated__/mergeRoutersGeneric.ts +0 -306
@@ -0,0 +1,55 @@
1
+ function getMessageFromUnknownError(err, fallback) {
2
+ if (typeof err === 'string') {
3
+ return err;
4
+ }
5
+ if (err instanceof Error && typeof err.message === 'string') {
6
+ return err.message;
7
+ }
8
+ return fallback;
9
+ }
10
+ function getErrorFromUnknown(cause) {
11
+ if (cause instanceof Error) {
12
+ return cause;
13
+ }
14
+ const message = getMessageFromUnknownError(cause, 'Unknown error');
15
+ return new Error(message);
16
+ }
17
+ function getTRPCErrorFromUnknown(cause) {
18
+ const error = getErrorFromUnknown(cause);
19
+ // this should ideally be an `instanceof TRPCError` but for some reason that isn't working
20
+ // ref https://github.com/trpc/trpc/issues/331
21
+ if (error.name === 'TRPCError') {
22
+ return cause;
23
+ }
24
+ const trpcError = new TRPCError({
25
+ code: 'INTERNAL_SERVER_ERROR',
26
+ cause: error,
27
+ message: error.message,
28
+ });
29
+ // Inherit stack from error
30
+ trpcError.stack = error.stack;
31
+ return trpcError;
32
+ }
33
+ function getCauseFromUnknown(cause) {
34
+ if (cause instanceof Error) {
35
+ return cause;
36
+ }
37
+ return undefined;
38
+ }
39
+
40
+ class TRPCError extends Error {
41
+ constructor(opts) {
42
+ const code = opts.code;
43
+ const message = opts.message ?? getMessageFromUnknownError(opts.cause, code);
44
+ const cause = opts.cause !== undefined ? getErrorFromUnknown(opts.cause) : undefined;
45
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
46
+ // @ts-ignore https://github.com/tc39/proposal-error-cause
47
+ super(message, { cause });
48
+ this.code = code;
49
+ this.cause = cause;
50
+ this.name = 'TRPCError';
51
+ Object.setPrototypeOf(this, new.target.prototype);
52
+ }
53
+ }
54
+
55
+ export { TRPCError as T, getTRPCErrorFromUnknown as a, getCauseFromUnknown as g };
@@ -3,9 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var TRPCError = require('../../TRPCError-bd37df84.js');
6
- require('../../config-d356ff77.js');
6
+ require('../../config-cc5b2d45.js');
7
7
  require('../../codes-e0ea0f4c.js');
8
- var resolveHTTPResponse = require('../../resolveHTTPResponse-3408bbf7.js');
8
+ var resolveHTTPResponse = require('../../resolveHTTPResponse-6cffb0ce.js');
9
9
  require('../../index-4d2d31b6.js');
10
10
  require('../../transformTRPCResponse-d2700b72.js');
11
11
 
@@ -1,7 +1,7 @@
1
1
  import { T as TRPCError } from '../../TRPCError-7de0a793.mjs';
2
- import '../../config-4ca0221b.mjs';
2
+ import '../../config-cd794c2a.mjs';
3
3
  import '../../codes-52c11119.mjs';
4
- import { r as resolveHTTPResponse } from '../../resolveHTTPResponse-b831e644.mjs';
4
+ import { r as resolveHTTPResponse } from '../../resolveHTTPResponse-7a4f75b3.mjs';
5
5
  import '../../index-972002da.mjs';
6
6
  import '../../transformTRPCResponse-7a73a2df.mjs';
7
7
 
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-64cf5f55.js');
6
- require('../resolveHTTPResponse-3408bbf7.js');
7
- require('../config-d356ff77.js');
5
+ var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-a649e036.js');
6
+ require('../resolveHTTPResponse-6cffb0ce.js');
7
+ require('../config-cc5b2d45.js');
8
8
  require('../TRPCError-bd37df84.js');
9
9
  require('../codes-e0ea0f4c.js');
10
10
  require('../index-4d2d31b6.js');
@@ -1,6 +1,6 @@
1
- import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-34404221.mjs';
2
- import '../resolveHTTPResponse-b831e644.mjs';
3
- import '../config-4ca0221b.mjs';
1
+ import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-85d2b636.mjs';
2
+ import '../resolveHTTPResponse-7a4f75b3.mjs';
3
+ import '../config-cd794c2a.mjs';
4
4
  import '../TRPCError-7de0a793.mjs';
5
5
  import '../codes-52c11119.mjs';
6
6
  import '../index-972002da.mjs';
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var resolveHTTPResponse = require('../../resolveHTTPResponse-3408bbf7.js');
5
+ var resolveHTTPResponse = require('../../resolveHTTPResponse-6cffb0ce.js');
6
6
  var adapters_ws = require('../ws.js');
7
- require('../../config-d356ff77.js');
7
+ require('../../config-cc5b2d45.js');
8
8
  require('../../TRPCError-bd37df84.js');
9
9
  require('../../codes-e0ea0f4c.js');
10
10
  require('../../index-4d2d31b6.js');
@@ -41,7 +41,7 @@ async function fastifyRequestHandler(opts) {
41
41
  res.statusCode = result.status;
42
42
  }
43
43
  for (const [key, value] of Object.entries(result.headers ?? {})){
44
- /* istanbul ignore if */ if (typeof value === 'undefined') {
44
+ /* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
45
45
  continue;
46
46
  }
47
47
  void res.header(key, value);
@@ -1,6 +1,6 @@
1
- import { r as resolveHTTPResponse } from '../../resolveHTTPResponse-b831e644.mjs';
1
+ import { r as resolveHTTPResponse } from '../../resolveHTTPResponse-7a4f75b3.mjs';
2
2
  import { applyWSSHandler } from '../ws.mjs';
3
- import '../../config-4ca0221b.mjs';
3
+ import '../../config-cd794c2a.mjs';
4
4
  import '../../TRPCError-7de0a793.mjs';
5
5
  import '../../codes-52c11119.mjs';
6
6
  import '../../index-972002da.mjs';
@@ -37,7 +37,7 @@ async function fastifyRequestHandler(opts) {
37
37
  res.statusCode = result.status;
38
38
  }
39
39
  for (const [key, value] of Object.entries(result.headers ?? {})){
40
- /* istanbul ignore if */ if (typeof value === 'undefined') {
40
+ /* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
41
41
  continue;
42
42
  }
43
43
  void res.header(key, value);
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var resolveHTTPResponse = require('../../resolveHTTPResponse-3408bbf7.js');
6
- require('../../config-d356ff77.js');
5
+ var resolveHTTPResponse = require('../../resolveHTTPResponse-6cffb0ce.js');
6
+ require('../../config-cc5b2d45.js');
7
7
  require('../../TRPCError-bd37df84.js');
8
8
  require('../../codes-e0ea0f4c.js');
9
9
  require('../../index-4d2d31b6.js');
@@ -40,7 +40,7 @@ async function fetchRequestHandler(opts) {
40
40
  }
41
41
  });
42
42
  for (const [key, value] of Object.entries(result.headers ?? {})){
43
- /* istanbul ignore if */ if (typeof value === 'undefined') {
43
+ /* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
44
44
  continue;
45
45
  }
46
46
  if (typeof value === 'string') {
@@ -1,5 +1,5 @@
1
- import { r as resolveHTTPResponse } from '../../resolveHTTPResponse-b831e644.mjs';
2
- import '../../config-4ca0221b.mjs';
1
+ import { r as resolveHTTPResponse } from '../../resolveHTTPResponse-7a4f75b3.mjs';
2
+ import '../../config-cd794c2a.mjs';
3
3
  import '../../TRPCError-7de0a793.mjs';
4
4
  import '../../codes-52c11119.mjs';
5
5
  import '../../index-972002da.mjs';
@@ -36,7 +36,7 @@ async function fetchRequestHandler(opts) {
36
36
  }
37
37
  });
38
38
  for (const [key, value] of Object.entries(result.headers ?? {})){
39
- /* istanbul ignore if */ if (typeof value === 'undefined') {
39
+ /* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
40
40
  continue;
41
41
  }
42
42
  if (typeof value === 'string') {
@@ -3,9 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var TRPCError = require('../TRPCError-bd37df84.js');
6
- var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-64cf5f55.js');
7
- require('../resolveHTTPResponse-3408bbf7.js');
8
- require('../config-d356ff77.js');
6
+ var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-a649e036.js');
7
+ require('../resolveHTTPResponse-6cffb0ce.js');
8
+ require('../config-cc5b2d45.js');
9
9
  require('../codes-e0ea0f4c.js');
10
10
  require('../index-4d2d31b6.js');
11
11
  require('../transformTRPCResponse-d2700b72.js');
@@ -1,7 +1,7 @@
1
1
  import { T as TRPCError } from '../TRPCError-7de0a793.mjs';
2
- import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-34404221.mjs';
3
- import '../resolveHTTPResponse-b831e644.mjs';
4
- import '../config-4ca0221b.mjs';
2
+ import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-85d2b636.mjs';
3
+ import '../resolveHTTPResponse-7a4f75b3.mjs';
4
+ import '../config-cd794c2a.mjs';
5
5
  import '../codes-52c11119.mjs';
6
6
  import '../index-972002da.mjs';
7
7
  import '../transformTRPCResponse-7a73a2df.mjs';
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var nodeHTTPRequestHandler = require('../../nodeHTTPRequestHandler-64cf5f55.js');
6
- require('../../resolveHTTPResponse-3408bbf7.js');
7
- require('../../config-d356ff77.js');
5
+ var nodeHTTPRequestHandler = require('../../nodeHTTPRequestHandler-a649e036.js');
6
+ require('../../resolveHTTPResponse-6cffb0ce.js');
7
+ require('../../config-cc5b2d45.js');
8
8
  require('../../TRPCError-bd37df84.js');
9
9
  require('../../codes-e0ea0f4c.js');
10
10
  require('../../index-4d2d31b6.js');
@@ -1,6 +1,6 @@
1
- export { n as nodeHTTPRequestHandler } from '../../nodeHTTPRequestHandler-34404221.mjs';
2
- import '../../resolveHTTPResponse-b831e644.mjs';
3
- import '../../config-4ca0221b.mjs';
1
+ export { n as nodeHTTPRequestHandler } from '../../nodeHTTPRequestHandler-85d2b636.mjs';
2
+ import '../../resolveHTTPResponse-7a4f75b3.mjs';
3
+ import '../../config-cd794c2a.mjs';
4
4
  import '../../TRPCError-7de0a793.mjs';
5
5
  import '../../codes-52c11119.mjs';
6
6
  import '../../index-972002da.mjs';
@@ -3,9 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var http = require('node:http');
6
- var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-64cf5f55.js');
7
- require('../resolveHTTPResponse-3408bbf7.js');
8
- require('../config-d356ff77.js');
6
+ var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-a649e036.js');
7
+ require('../resolveHTTPResponse-6cffb0ce.js');
8
+ require('../config-cc5b2d45.js');
9
9
  require('../TRPCError-bd37df84.js');
10
10
  require('../codes-e0ea0f4c.js');
11
11
  require('../index-4d2d31b6.js');
@@ -1,7 +1,7 @@
1
1
  import http from 'node:http';
2
- import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-34404221.mjs';
3
- import '../resolveHTTPResponse-b831e644.mjs';
4
- import '../config-4ca0221b.mjs';
2
+ import { n as nodeHTTPRequestHandler } from '../nodeHTTPRequestHandler-85d2b636.mjs';
3
+ import '../resolveHTTPResponse-7a4f75b3.mjs';
4
+ import '../config-cd794c2a.mjs';
5
5
  import '../TRPCError-7de0a793.mjs';
6
6
  import '../codes-52c11119.mjs';
7
7
  import '../index-972002da.mjs';
@@ -2,34 +2,34 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var config = require('../config-d356ff77.js');
5
+ var config = require('../config-cc5b2d45.js');
6
6
  var TRPCError = require('../TRPCError-bd37df84.js');
7
7
  var transformTRPCResponse = require('../transformTRPCResponse-d2700b72.js');
8
8
  var observable = require('../observable-464116ac.js');
9
9
  require('../codes-e0ea0f4c.js');
10
10
  require('../index-4d2d31b6.js');
11
11
 
12
- /* istanbul ignore next */ function assertIsObject(obj) {
12
+ /* istanbul ignore next -- @preserve */ function assertIsObject(obj) {
13
13
  if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
14
14
  throw new Error('Not an object');
15
15
  }
16
16
  }
17
- /* istanbul ignore next */ function assertIsProcedureType(obj) {
17
+ /* istanbul ignore next -- @preserve */ function assertIsProcedureType(obj) {
18
18
  if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
19
19
  throw new Error('Invalid procedure type');
20
20
  }
21
21
  }
22
- /* istanbul ignore next */ function assertIsRequestId(obj) {
22
+ /* istanbul ignore next -- @preserve */ function assertIsRequestId(obj) {
23
23
  if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
24
24
  throw new Error('Invalid request id');
25
25
  }
26
26
  }
27
- /* istanbul ignore next */ function assertIsString(obj) {
27
+ /* istanbul ignore next -- @preserve */ function assertIsString(obj) {
28
28
  if (typeof obj !== 'string') {
29
29
  throw new Error('Invalid string');
30
30
  }
31
31
  }
32
- /* istanbul ignore next */ function assertIsJSONRPC2OrUndefined(obj) {
32
+ /* istanbul ignore next -- @preserve */ function assertIsJSONRPC2OrUndefined(obj) {
33
33
  if (typeof obj !== 'undefined' && obj !== '2.0') {
34
34
  throw new Error('Must be JSONRPC 2.0');
35
35
  }
@@ -86,7 +86,7 @@ function applyWSSHandler(opts) {
86
86
  let ctx = undefined;
87
87
  async function handleRequest(msg) {
88
88
  const { id , jsonrpc } = msg;
89
- /* istanbul ignore next */ if (id === null) {
89
+ /* istanbul ignore next -- @preserve */ if (id === null) {
90
90
  throw new TRPCError.TRPCError({
91
91
  code: 'BAD_REQUEST',
92
92
  message: '`id` is required'
@@ -177,13 +177,13 @@ function applyWSSHandler(opts) {
177
177
  });
178
178
  }
179
179
  });
180
- /* istanbul ignore next */ if (client.readyState !== client.OPEN) {
180
+ /* istanbul ignore next -- @preserve */ if (client.readyState !== client.OPEN) {
181
181
  // if the client got disconnected whilst initializing the subscription
182
182
  // no need to send stopped message if the client is disconnected
183
183
  sub1.unsubscribe();
184
184
  return;
185
185
  }
186
- /* istanbul ignore next */ if (clientSubscriptions.has(id)) {
186
+ /* istanbul ignore next -- @preserve */ if (clientSubscriptions.has(id)) {
187
187
  // duplicate request ids for client
188
188
  stopSubscription(sub1, {
189
189
  id,
@@ -202,7 +202,7 @@ function applyWSSHandler(opts) {
202
202
  type: 'started'
203
203
  }
204
204
  });
205
- } catch (cause) /* istanbul ignore next */ {
205
+ } catch (cause) /* istanbul ignore next -- @preserve */ {
206
206
  // procedure threw an error
207
207
  const error = TRPCError.getTRPCErrorFromUnknown(cause);
208
208
  opts.onError?.({
@@ -1,31 +1,31 @@
1
- import { e as callProcedure } from '../config-4ca0221b.mjs';
1
+ import { e as callProcedure } from '../config-cd794c2a.mjs';
2
2
  import { T as TRPCError, g as getCauseFromUnknown, a as getTRPCErrorFromUnknown } from '../TRPCError-7de0a793.mjs';
3
3
  import { t as transformTRPCResponse } from '../transformTRPCResponse-7a73a2df.mjs';
4
4
  import { i as isObservable } from '../observable-ade1bad8.mjs';
5
5
  import '../codes-52c11119.mjs';
6
6
  import '../index-972002da.mjs';
7
7
 
8
- /* istanbul ignore next */ function assertIsObject(obj) {
8
+ /* istanbul ignore next -- @preserve */ function assertIsObject(obj) {
9
9
  if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
10
10
  throw new Error('Not an object');
11
11
  }
12
12
  }
13
- /* istanbul ignore next */ function assertIsProcedureType(obj) {
13
+ /* istanbul ignore next -- @preserve */ function assertIsProcedureType(obj) {
14
14
  if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
15
15
  throw new Error('Invalid procedure type');
16
16
  }
17
17
  }
18
- /* istanbul ignore next */ function assertIsRequestId(obj) {
18
+ /* istanbul ignore next -- @preserve */ function assertIsRequestId(obj) {
19
19
  if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
20
20
  throw new Error('Invalid request id');
21
21
  }
22
22
  }
23
- /* istanbul ignore next */ function assertIsString(obj) {
23
+ /* istanbul ignore next -- @preserve */ function assertIsString(obj) {
24
24
  if (typeof obj !== 'string') {
25
25
  throw new Error('Invalid string');
26
26
  }
27
27
  }
28
- /* istanbul ignore next */ function assertIsJSONRPC2OrUndefined(obj) {
28
+ /* istanbul ignore next -- @preserve */ function assertIsJSONRPC2OrUndefined(obj) {
29
29
  if (typeof obj !== 'undefined' && obj !== '2.0') {
30
30
  throw new Error('Must be JSONRPC 2.0');
31
31
  }
@@ -82,7 +82,7 @@ function applyWSSHandler(opts) {
82
82
  let ctx = undefined;
83
83
  async function handleRequest(msg) {
84
84
  const { id , jsonrpc } = msg;
85
- /* istanbul ignore next */ if (id === null) {
85
+ /* istanbul ignore next -- @preserve */ if (id === null) {
86
86
  throw new TRPCError({
87
87
  code: 'BAD_REQUEST',
88
88
  message: '`id` is required'
@@ -173,13 +173,13 @@ function applyWSSHandler(opts) {
173
173
  });
174
174
  }
175
175
  });
176
- /* istanbul ignore next */ if (client.readyState !== client.OPEN) {
176
+ /* istanbul ignore next -- @preserve */ if (client.readyState !== client.OPEN) {
177
177
  // if the client got disconnected whilst initializing the subscription
178
178
  // no need to send stopped message if the client is disconnected
179
179
  sub1.unsubscribe();
180
180
  return;
181
181
  }
182
- /* istanbul ignore next */ if (clientSubscriptions.has(id)) {
182
+ /* istanbul ignore next -- @preserve */ if (clientSubscriptions.has(id)) {
183
183
  // duplicate request ids for client
184
184
  stopSubscription(sub1, {
185
185
  id,
@@ -198,7 +198,7 @@ function applyWSSHandler(opts) {
198
198
  type: 'started'
199
199
  }
200
200
  });
201
- } catch (cause) /* istanbul ignore next */ {
201
+ } catch (cause) /* istanbul ignore next -- @preserve */ {
202
202
  // procedure threw an error
203
203
  const error = getTRPCErrorFromUnknown(cause);
204
204
  opts.onError?.({
@@ -0,0 +1,45 @@
1
+ function invert(obj) {
2
+ const newObj = Object.create(null);
3
+ for (const key in obj) {
4
+ const v = obj[key];
5
+ newObj[v] = key;
6
+ }
7
+ return newObj;
8
+ }
9
+
10
+ // reference: https://www.jsonrpc.org/specification
11
+ /**
12
+ * JSON-RPC 2.0 Error codes
13
+ *
14
+ * `-32000` to `-32099` are reserved for implementation-defined server-errors.
15
+ * For tRPC we're copying the last digits of HTTP 4XX errors.
16
+ */
17
+ const TRPC_ERROR_CODES_BY_KEY = {
18
+ /**
19
+ * Invalid JSON was received by the server.
20
+ * An error occurred on the server while parsing the JSON text.
21
+ */
22
+ PARSE_ERROR: -32700,
23
+ /**
24
+ * The JSON sent is not a valid Request object.
25
+ */
26
+ BAD_REQUEST: -32600,
27
+ /**
28
+ * Internal JSON-RPC error.
29
+ */
30
+ INTERNAL_SERVER_ERROR: -32603,
31
+ // Implementation specific errors
32
+ UNAUTHORIZED: -32001,
33
+ FORBIDDEN: -32003,
34
+ NOT_FOUND: -32004,
35
+ METHOD_NOT_SUPPORTED: -32005,
36
+ TIMEOUT: -32008,
37
+ CONFLICT: -32009,
38
+ PRECONDITION_FAILED: -32012,
39
+ PAYLOAD_TOO_LARGE: -32013,
40
+ TOO_MANY_REQUESTS: -32029,
41
+ CLIENT_CLOSED_REQUEST: -32099, // 499
42
+ };
43
+ const TRPC_ERROR_CODES_BY_NUMBER = invert(TRPC_ERROR_CODES_BY_KEY);
44
+
45
+ export { TRPC_ERROR_CODES_BY_KEY as T, TRPC_ERROR_CODES_BY_NUMBER as a, invert as i };
@@ -230,7 +230,7 @@ const emptyRouter = {
230
230
 
231
231
  /**
232
232
  * The default check to see if we're in a server
233
- */ const isServerDefault = typeof window === 'undefined' || 'Deno' in window || globalThis.process?.env?.NODE_ENV === 'test' || !!globalThis.process?.env?.JEST_WORKER_ID;
233
+ */ const isServerDefault = typeof window === 'undefined' || 'Deno' in window || globalThis.process?.env?.NODE_ENV === 'test' || !!globalThis.process?.env?.JEST_WORKER_ID || !!globalThis.process?.env?.VITEST_WORKER_ID;
234
234
 
235
235
  exports.TRPC_ERROR_CODES_BY_NUMBER = TRPC_ERROR_CODES_BY_NUMBER;
236
236
  exports.callProcedure = callProcedure;
@@ -228,6 +228,6 @@ const emptyRouter = {
228
228
 
229
229
  /**
230
230
  * The default check to see if we're in a server
231
- */ const isServerDefault = typeof window === 'undefined' || 'Deno' in window || globalThis.process?.env?.NODE_ENV === 'test' || !!globalThis.process?.env?.JEST_WORKER_ID;
231
+ */ const isServerDefault = typeof window === 'undefined' || 'Deno' in window || globalThis.process?.env?.NODE_ENV === 'test' || !!globalThis.process?.env?.JEST_WORKER_ID || !!globalThis.process?.env?.VITEST_WORKER_ID;
232
232
 
233
233
  export { TRPC_ERROR_CODES_BY_NUMBER as T, defaultTransformer as a, getDataTransformer as b, createRouterFactory as c, defaultFormatter as d, callProcedure as e, getHTTPStatusCode as f, getHTTPStatusCodeFromError as g, isServerDefault as i, procedureTypes as p };