@trpc/server 10.26.0 → 10.27.1

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 (77) hide show
  1. package/dist/adapters/aws-lambda/index.js +5 -5
  2. package/dist/adapters/aws-lambda/index.mjs +5 -5
  3. package/dist/adapters/express.js +6 -6
  4. package/dist/adapters/express.mjs +6 -6
  5. package/dist/adapters/fastify/index.js +5 -5
  6. package/dist/adapters/fastify/index.mjs +5 -5
  7. package/dist/adapters/fetch/index.js +5 -5
  8. package/dist/adapters/fetch/index.mjs +5 -5
  9. package/dist/adapters/next.d.ts.map +1 -1
  10. package/dist/adapters/next.js +8 -7
  11. package/dist/adapters/next.mjs +8 -7
  12. package/dist/adapters/node-http/index.js +6 -6
  13. package/dist/adapters/node-http/index.mjs +6 -6
  14. package/dist/adapters/standalone.js +6 -6
  15. package/dist/adapters/standalone.mjs +6 -6
  16. package/dist/adapters/ws.d.ts.map +1 -1
  17. package/dist/adapters/ws.js +13 -9
  18. package/dist/adapters/ws.mjs +13 -9
  19. package/dist/{codes-0a561c20.js → codes-0eeb600d.js} +3 -1
  20. package/dist/{codes-e5d244b6.mjs → codes-24aa1ce1.mjs} +3 -1
  21. package/dist/{codes-dddd9731.js → codes-b3c262d8.js} +3 -0
  22. package/dist/{config-2d85b8f0.js → config-91ab0b1d.js} +3 -47
  23. package/dist/{config-f0387de5.mjs → config-d1cf9de0.mjs} +3 -49
  24. package/dist/{config-e6e74385.js → config-f355c4d8.js} +3 -52
  25. package/dist/core/router.d.ts +4 -0
  26. package/dist/core/router.d.ts.map +1 -1
  27. package/dist/core/types.d.ts +1 -1
  28. package/dist/core/types.d.ts.map +1 -1
  29. package/dist/http/index.js +8 -8
  30. package/dist/http/index.mjs +5 -5
  31. package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
  32. package/dist/index-315a9f08.mjs +96 -0
  33. package/dist/index-8e243a74.js +102 -0
  34. package/dist/index-cddfa242.js +93 -0
  35. package/dist/index.js +4 -4
  36. package/dist/index.mjs +4 -4
  37. package/dist/internals/invert.d.ts +3 -0
  38. package/dist/internals/invert.d.ts.map +1 -1
  39. package/dist/internals/types.d.ts +4 -0
  40. package/dist/internals/types.d.ts.map +1 -1
  41. package/dist/{nodeHTTPRequestHandler-ace6dac5.js → nodeHTTPRequestHandler-7762c7d4.js} +1 -1
  42. package/dist/{nodeHTTPRequestHandler-f0efcff4.mjs → nodeHTTPRequestHandler-8a261b10.mjs} +1 -1
  43. package/dist/{nodeHTTPRequestHandler-54aea883.js → nodeHTTPRequestHandler-fc509b47.js} +1 -1
  44. package/dist/{resolveHTTPResponse-a7abb046.js → resolveHTTPResponse-27fe1108.js} +9 -6
  45. package/dist/{resolveHTTPResponse-e1286cb3.mjs → resolveHTTPResponse-36f4e729.mjs} +8 -5
  46. package/dist/{resolveHTTPResponse-9a9f3436.js → resolveHTTPResponse-5f8945d7.js} +8 -5
  47. package/dist/rpc/index.js +1 -1
  48. package/dist/rpc/index.mjs +1 -1
  49. package/dist/shared/getErrorShape.d.ts +15 -0
  50. package/dist/shared/getErrorShape.d.ts.map +1 -0
  51. package/dist/shared/index.d.ts +2 -1
  52. package/dist/shared/index.d.ts.map +1 -1
  53. package/dist/shared/index.js +4 -2
  54. package/dist/shared/index.mjs +3 -2
  55. package/dist/shared/transformTRPCResponse.d.ts +2 -2
  56. package/dist/shared/transformTRPCResponse.d.ts.map +1 -1
  57. package/dist/transformTRPCResponse-6887ae6d.mjs +53 -0
  58. package/dist/transformTRPCResponse-8cb7e890.js +54 -0
  59. package/dist/transformTRPCResponse-f7b2ffd1.js +56 -0
  60. package/package.json +4 -3
  61. package/src/adapters/next.ts +3 -1
  62. package/src/adapters/ws.ts +12 -5
  63. package/src/core/initTRPC.ts +1 -1
  64. package/src/core/router.ts +6 -2
  65. package/src/core/types.ts +1 -1
  66. package/src/http/resolveHTTPResponse.ts +10 -4
  67. package/src/internals/invert.ts +3 -0
  68. package/src/internals/types.ts +4 -0
  69. package/src/shared/getErrorShape.ts +35 -0
  70. package/src/shared/index.ts +3 -1
  71. package/src/shared/transformTRPCResponse.ts +7 -9
  72. package/dist/index-4d2d31b6.js +0 -51
  73. package/dist/index-972002da.mjs +0 -48
  74. package/dist/index-f38f12ba.js +0 -47
  75. package/dist/transformTRPCResponse-0ea8c0ca.mjs +0 -25
  76. package/dist/transformTRPCResponse-560a1eb1.js +0 -28
  77. package/dist/transformTRPCResponse-8afb9a5c.js +0 -27
@@ -1,7 +1,8 @@
1
- import { e as callProcedure, f as getHTTPStatusCode } from './config-f0387de5.mjs';
1
+ import { b as callProcedure } from './config-d1cf9de0.mjs';
2
2
  import { T as TRPCError, a as getTRPCErrorFromUnknown } from './TRPCError-2b10c8d2.mjs';
3
- import { t as transformTRPCResponse } from './transformTRPCResponse-0ea8c0ca.mjs';
3
+ import { g as getErrorShape, t as transformTRPCResponse } from './transformTRPCResponse-6887ae6d.mjs';
4
4
  import { g as getJsonContentTypeInputs } from './contentType-acc3be52.mjs';
5
+ import { b as getHTTPStatusCode } from './index-315a9f08.mjs';
5
6
 
6
7
  const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
7
8
  GET: 'query',
@@ -44,7 +45,7 @@ async function resolveHTTPResponse(opts) {
44
45
  if (meta.status) {
45
46
  status = meta.status;
46
47
  }
47
- const transformedJSON = transformTRPCResponse(router, untransformedJSON);
48
+ const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
48
49
  const body = JSON.stringify(transformedJSON);
49
50
  return {
50
51
  body,
@@ -120,7 +121,8 @@ async function resolveHTTPResponse(opts) {
120
121
  const { path , input } = obj;
121
122
  if (obj.error) {
122
123
  return {
123
- error: router.getErrorShape({
124
+ error: getErrorShape({
125
+ config: router._def._config,
124
126
  error: obj.error,
125
127
  type,
126
128
  path,
@@ -155,7 +157,8 @@ async function resolveHTTPResponse(opts) {
155
157
  req
156
158
  });
157
159
  return endResponse({
158
- error: router.getErrorShape({
160
+ error: getErrorShape({
161
+ config: router._def._config,
159
162
  error,
160
163
  type,
161
164
  path: undefined,
@@ -1,7 +1,8 @@
1
- import { e as callProcedure, f as getHTTPStatusCode } from './config-2d85b8f0.js';
1
+ import { b as callProcedure } from './config-91ab0b1d.js';
2
2
  import { T as TRPCError, a as getTRPCErrorFromUnknown } from './TRPCError-61026c9a.js';
3
- import { t as transformTRPCResponse } from './transformTRPCResponse-560a1eb1.js';
3
+ import { g as getErrorShape, t as transformTRPCResponse } from './transformTRPCResponse-8cb7e890.js';
4
4
  import { g as getJsonContentTypeInputs } from './contentType-a75c990c.js';
5
+ import { b as getHTTPStatusCode } from './index-cddfa242.js';
5
6
 
6
7
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
7
8
  const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
@@ -45,7 +46,7 @@ async function resolveHTTPResponse(opts) {
45
46
  if (meta.status) {
46
47
  status = meta.status;
47
48
  }
48
- const transformedJSON = transformTRPCResponse(router, untransformedJSON);
49
+ const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
49
50
  const body = JSON.stringify(transformedJSON);
50
51
  return {
51
52
  body,
@@ -112,7 +113,8 @@ async function resolveHTTPResponse(opts) {
112
113
  const { path, input } = obj;
113
114
  if (obj.error) {
114
115
  return {
115
- error: router.getErrorShape({
116
+ error: getErrorShape({
117
+ config: router._def._config,
116
118
  error: obj.error,
117
119
  type,
118
120
  path,
@@ -149,7 +151,8 @@ async function resolveHTTPResponse(opts) {
149
151
  req,
150
152
  });
151
153
  return endResponse({
152
- error: router.getErrorShape({
154
+ error: getErrorShape({
155
+ config: router._def._config,
153
156
  error,
154
157
  type,
155
158
  path: undefined,
package/dist/rpc/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var codes = require('../codes-0a561c20.js');
5
+ var codes = require('../codes-0eeb600d.js');
6
6
 
7
7
 
8
8
 
@@ -1 +1 @@
1
- export { T as TRPC_ERROR_CODES_BY_KEY, a as TRPC_ERROR_CODES_BY_NUMBER } from '../codes-e5d244b6.mjs';
1
+ export { T as TRPC_ERROR_CODES_BY_KEY, a as TRPC_ERROR_CODES_BY_NUMBER } from '../codes-24aa1ce1.mjs';
@@ -0,0 +1,15 @@
1
+ import { ProcedureType } from '../core';
2
+ import { TRPCError } from '../error/TRPCError';
3
+ import { AnyRootConfig } from '../internals';
4
+ /**
5
+ * @internal
6
+ */
7
+ export declare function getErrorShape<TConfig extends AnyRootConfig>(opts: {
8
+ config: TConfig;
9
+ error: TRPCError;
10
+ type: ProcedureType | 'unknown';
11
+ path: string | undefined;
12
+ input: unknown;
13
+ ctx: undefined | TConfig['$types']['ctx'];
14
+ }): TConfig['$types']['errorShape'];
15
+ //# sourceMappingURL=getErrorShape.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getErrorShape.d.ts","sourceRoot":"","sources":["../../src/shared/getErrorShape.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C,OAAO,EAAE,aAAa,EAAqB,MAAM,cAAc,CAAC;AAGhE;;GAEG;AACH,wBAAgB,aAAa,CAAC,OAAO,SAAS,aAAa,EAAE,IAAI,EAAE;IACjE,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,SAAS,CAAC;IACjB,IAAI,EAAE,aAAa,GAAG,SAAS,CAAC;IAChC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,KAAK,EAAE,OAAO,CAAC;IACf,GAAG,EAAE,SAAS,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC;CAC3C,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,YAAY,CAAC,CAkBlC"}
@@ -1,5 +1,6 @@
1
1
  export * from './createProxy';
2
2
  export * from './jsonify';
3
3
  export * from './transformTRPCResponse';
4
- export type { SerializeObject } from './internal/serialize';
4
+ export type { SerializeObject, Serialize } from './internal/serialize';
5
+ export { getErrorShape } from './getErrorShape';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/shared/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,yBAAyB,CAAC;AAGxC,YAAY,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/shared/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,yBAAyB,CAAC;AAGxC,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEvE,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC"}
@@ -2,11 +2,13 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('../index-4d2d31b6.js');
6
- var transformTRPCResponse = require('../transformTRPCResponse-8afb9a5c.js');
5
+ var index = require('../index-8e243a74.js');
6
+ var transformTRPCResponse = require('../transformTRPCResponse-f7b2ffd1.js');
7
+ require('../codes-0eeb600d.js');
7
8
 
8
9
 
9
10
 
10
11
  exports.createFlatProxy = index.createFlatProxy;
11
12
  exports.createRecursiveProxy = index.createRecursiveProxy;
13
+ exports.getErrorShape = transformTRPCResponse.getErrorShape;
12
14
  exports.transformTRPCResponse = transformTRPCResponse.transformTRPCResponse;
@@ -1,2 +1,3 @@
1
- export { c as createFlatProxy, a as createRecursiveProxy } from '../index-972002da.mjs';
2
- export { t as transformTRPCResponse } from '../transformTRPCResponse-0ea8c0ca.mjs';
1
+ export { c as createFlatProxy, a as createRecursiveProxy } from '../index-315a9f08.mjs';
2
+ export { g as getErrorShape, t as transformTRPCResponse } from '../transformTRPCResponse-6887ae6d.mjs';
3
+ import '../codes-24aa1ce1.mjs';
@@ -1,9 +1,9 @@
1
- import { AnyRouter } from '../core/router';
1
+ import { AnyRootConfig } from '../internals';
2
2
  import { TRPCResponse, TRPCResponseMessage } from '../rpc';
3
3
  /**
4
4
  * Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
5
5
  **/
6
- export declare function transformTRPCResponse<TResponse extends TRPCResponse | TRPCResponse[] | TRPCResponseMessage | TRPCResponseMessage[]>(router: AnyRouter, itemOrItems: TResponse): import("../rpc").TRPCSuccessResponse<unknown> | import("../rpc").TRPCErrorResponse<import("../rpc").TRPCErrorShape<import("../rpc").TRPC_ERROR_CODE_NUMBER, Record<string, unknown>>> | ({
6
+ export declare function transformTRPCResponse<TResponse extends TRPCResponse | TRPCResponse[] | TRPCResponseMessage | TRPCResponseMessage[]>(config: AnyRootConfig, itemOrItems: TResponse): import("../rpc").TRPCSuccessResponse<unknown> | import("../rpc").TRPCErrorResponse<import("../rpc").TRPCErrorShape<import("../rpc").TRPC_ERROR_CODE_NUMBER, Record<string, unknown>>> | ({
7
7
  id: import("../rpc").JSONRPC2.RequestId;
8
8
  } & import("../rpc").TRPCResultMessage<unknown>) | (TRPCResponse<unknown, import("../rpc").TRPCErrorShape<import("../rpc").TRPC_ERROR_CODE_NUMBER, Record<string, unknown>>> | TRPCResponseMessage<unknown, import("../rpc").TRPCErrorShape<import("../rpc").TRPC_ERROR_CODE_NUMBER, Record<string, unknown>>>)[];
9
9
  //# sourceMappingURL=transformTRPCResponse.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transformTRPCResponse.d.ts","sourceRoot":"","sources":["../../src/shared/transformTRPCResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AA2B3D;;IAEI;AACJ,wBAAgB,qBAAqB,CACnC,SAAS,SACL,YAAY,GACZ,YAAY,EAAE,GACd,mBAAmB,GACnB,mBAAmB,EAAE,EACzB,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,SAAS;;kTAI1C"}
1
+ {"version":3,"file":"transformTRPCResponse.d.ts","sourceRoot":"","sources":["../../src/shared/transformTRPCResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAyB3D;;IAEI;AACJ,wBAAgB,qBAAqB,CACnC,SAAS,SACL,YAAY,GACZ,YAAY,EAAE,GACd,mBAAmB,GACnB,mBAAmB,EAAE,EACzB,MAAM,EAAE,aAAa,EAAE,WAAW,EAAE,SAAS;;kTAI9C"}
@@ -0,0 +1,53 @@
1
+ import { g as getHTTPStatusCodeFromError } from './index-315a9f08.mjs';
2
+ import { T as TRPC_ERROR_CODES_BY_KEY } from './codes-24aa1ce1.mjs';
3
+
4
+ /**
5
+ * @internal
6
+ */ function getErrorShape(opts) {
7
+ const { path , error , config } = opts;
8
+ const { code } = opts.error;
9
+ const shape = {
10
+ message: error.message,
11
+ code: TRPC_ERROR_CODES_BY_KEY[code],
12
+ data: {
13
+ code,
14
+ httpStatus: getHTTPStatusCodeFromError(error)
15
+ }
16
+ };
17
+ if (config.isDev && typeof opts.error.stack === 'string') {
18
+ shape.data.stack = opts.error.stack;
19
+ }
20
+ if (typeof path === 'string') {
21
+ shape.data.path = path;
22
+ }
23
+ return config.errorFormatter({
24
+ ...opts,
25
+ shape
26
+ });
27
+ }
28
+
29
+ function transformTRPCResponseItem(config, item) {
30
+ if ('error' in item) {
31
+ return {
32
+ ...item,
33
+ error: config.transformer.output.serialize(item.error)
34
+ };
35
+ }
36
+ if ('data' in item.result) {
37
+ return {
38
+ ...item,
39
+ result: {
40
+ ...item.result,
41
+ data: config.transformer.output.serialize(item.result.data)
42
+ }
43
+ };
44
+ }
45
+ return item;
46
+ }
47
+ /**
48
+ * Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
49
+ **/ function transformTRPCResponse(config, itemOrItems) {
50
+ return Array.isArray(itemOrItems) ? itemOrItems.map((item)=>transformTRPCResponseItem(config, item)) : transformTRPCResponseItem(config, itemOrItems);
51
+ }
52
+
53
+ export { getErrorShape as g, transformTRPCResponse as t };
@@ -0,0 +1,54 @@
1
+ import { g as getHTTPStatusCodeFromError } from './index-cddfa242.js';
2
+ import { T as TRPC_ERROR_CODES_BY_KEY } from './codes-b3c262d8.js';
3
+
4
+ /**
5
+ * @internal
6
+ */
7
+ function getErrorShape(opts) {
8
+ const { path, error, config } = opts;
9
+ const { code } = opts.error;
10
+ const shape = {
11
+ message: error.message,
12
+ code: TRPC_ERROR_CODES_BY_KEY[code],
13
+ data: {
14
+ code,
15
+ httpStatus: getHTTPStatusCodeFromError(error),
16
+ },
17
+ };
18
+ if (config.isDev && typeof opts.error.stack === 'string') {
19
+ shape.data.stack = opts.error.stack;
20
+ }
21
+ if (typeof path === 'string') {
22
+ shape.data.path = path;
23
+ }
24
+ return config.errorFormatter({ ...opts, shape });
25
+ }
26
+
27
+ function transformTRPCResponseItem(config, item) {
28
+ if ('error' in item) {
29
+ return {
30
+ ...item,
31
+ error: config.transformer.output.serialize(item.error),
32
+ };
33
+ }
34
+ if ('data' in item.result) {
35
+ return {
36
+ ...item,
37
+ result: {
38
+ ...item.result,
39
+ data: config.transformer.output.serialize(item.result.data),
40
+ },
41
+ };
42
+ }
43
+ return item;
44
+ }
45
+ /**
46
+ * Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
47
+ **/
48
+ function transformTRPCResponse(config, itemOrItems) {
49
+ return Array.isArray(itemOrItems)
50
+ ? itemOrItems.map((item) => transformTRPCResponseItem(config, item))
51
+ : transformTRPCResponseItem(config, itemOrItems);
52
+ }
53
+
54
+ export { getErrorShape as g, transformTRPCResponse as t };
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ var index = require('./index-8e243a74.js');
4
+ var codes = require('./codes-0eeb600d.js');
5
+
6
+ /**
7
+ * @internal
8
+ */ function getErrorShape(opts) {
9
+ const { path , error , config } = opts;
10
+ const { code } = opts.error;
11
+ const shape = {
12
+ message: error.message,
13
+ code: codes.TRPC_ERROR_CODES_BY_KEY[code],
14
+ data: {
15
+ code,
16
+ httpStatus: index.getHTTPStatusCodeFromError(error)
17
+ }
18
+ };
19
+ if (config.isDev && typeof opts.error.stack === 'string') {
20
+ shape.data.stack = opts.error.stack;
21
+ }
22
+ if (typeof path === 'string') {
23
+ shape.data.path = path;
24
+ }
25
+ return config.errorFormatter({
26
+ ...opts,
27
+ shape
28
+ });
29
+ }
30
+
31
+ function transformTRPCResponseItem(config, item) {
32
+ if ('error' in item) {
33
+ return {
34
+ ...item,
35
+ error: config.transformer.output.serialize(item.error)
36
+ };
37
+ }
38
+ if ('data' in item.result) {
39
+ return {
40
+ ...item,
41
+ result: {
42
+ ...item.result,
43
+ data: config.transformer.output.serialize(item.result.data)
44
+ }
45
+ };
46
+ }
47
+ return item;
48
+ }
49
+ /**
50
+ * Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
51
+ **/ function transformTRPCResponse(config, itemOrItems) {
52
+ return Array.isArray(itemOrItems) ? itemOrItems.map((item)=>transformTRPCResponseItem(config, item)) : transformTRPCResponseItem(config, itemOrItems);
53
+ }
54
+
55
+ exports.getErrorShape = getErrorShape;
56
+ exports.transformTRPCResponse = transformTRPCResponse;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "10.26.0",
3
+ "version": "10.27.1",
4
4
  "description": "The tRPC server library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -155,7 +155,7 @@
155
155
  "fastify-plugin": "^4.5.0",
156
156
  "hash-sum": "^2.0.0",
157
157
  "myzod": "^1.3.1",
158
- "next": "^13.3.4",
158
+ "next": "^13.4.3",
159
159
  "react": "^18.2.0",
160
160
  "react-dom": "^18.2.0",
161
161
  "rollup": "^2.79.1",
@@ -168,5 +168,6 @@
168
168
  "ws": "^8.0.0",
169
169
  "yup": "^1.0.0",
170
170
  "zod": "^3.0.0"
171
- }
171
+ },
172
+ "gitHead": "dffd2e72cc40f70156adebbb6a2d1e1df39d9808"
172
173
  }
@@ -5,6 +5,7 @@ import type {
5
5
  } from 'next/types';
6
6
  import { AnyRouter } from '../core';
7
7
  import { TRPCError } from '../error/TRPCError';
8
+ import { getErrorShape } from '../shared/getErrorShape';
8
9
  import { nodeHTTPRequestHandler } from './node-http';
9
10
  import {
10
11
  NodeHTTPCreateContextFnOptions,
@@ -32,7 +33,8 @@ export function createNextApiHandler<TRouter extends AnyRouter>(
32
33
  const path = getPath();
33
34
 
34
35
  if (path === null) {
35
- const error = opts.router.getErrorShape({
36
+ const error = getErrorShape({
37
+ config: opts.router._def._config,
36
38
  error: new TRPCError({
37
39
  message:
38
40
  'Query "trpc" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?',
@@ -16,6 +16,7 @@ import {
16
16
  TRPCReconnectNotification,
17
17
  TRPCResponseMessage,
18
18
  } from '../rpc';
19
+ import { getErrorShape } from '../shared/getErrorShape';
19
20
  import { transformTRPCResponse } from '../shared/transformTRPCResponse';
20
21
  import { CombinedDataTransformer } from '../transformer';
21
22
  import {
@@ -121,7 +122,9 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
121
122
 
122
123
  function respond(untransformedJSON: TRPCResponseMessage) {
123
124
  client.send(
124
- JSON.stringify(transformTRPCResponse(router, untransformedJSON)),
125
+ JSON.stringify(
126
+ transformTRPCResponse(router._def._config, untransformedJSON),
127
+ ),
125
128
  );
126
129
  }
127
130
 
@@ -211,7 +214,8 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
211
214
  respond({
212
215
  id,
213
216
  jsonrpc,
214
- error: router.getErrorShape({
217
+ error: getErrorShape({
218
+ config: router._def._config,
215
219
  error,
216
220
  type,
217
221
  path,
@@ -263,7 +267,8 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
263
267
  respond({
264
268
  id,
265
269
  jsonrpc,
266
- error: router.getErrorShape({
270
+ error: getErrorShape({
271
+ config: router._def._config,
267
272
  error,
268
273
  type,
269
274
  path,
@@ -289,7 +294,8 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
289
294
 
290
295
  respond({
291
296
  id: null,
292
- error: router.getErrorShape({
297
+ error: getErrorShape({
298
+ config: router._def._config,
293
299
  error,
294
300
  type: 'unknown',
295
301
  path: undefined,
@@ -336,7 +342,8 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
336
342
  });
337
343
  respond({
338
344
  id: null,
339
- error: router.getErrorShape({
345
+ error: getErrorShape({
346
+ config: router._def._config,
340
347
  error,
341
348
  type: 'unknown',
342
349
  path: undefined,
@@ -4,7 +4,7 @@ import {
4
4
  ErrorFormatterShape,
5
5
  defaultFormatter,
6
6
  } from '../error/formatter';
7
- import { createFlatProxy } from '../shared';
7
+ import { createFlatProxy } from '../shared/createProxy';
8
8
  import {
9
9
  DataTransformerOptions,
10
10
  DefaultDataTransformer,
@@ -2,7 +2,7 @@ import { TRPCError } from '../error/TRPCError';
2
2
  import { DefaultErrorShape, defaultFormatter } from '../error/formatter';
3
3
  import { getHTTPStatusCodeFromError } from '../http/getHTTPStatusCode';
4
4
  import { TRPC_ERROR_CODES_BY_KEY } from '../rpc';
5
- import { createRecursiveProxy } from '../shared';
5
+ import { createRecursiveProxy } from '../shared/createProxy';
6
6
  import { defaultTransformer } from '../transformer';
7
7
  import { AnyRootConfig } from './internals/config';
8
8
  import { omitPrototype } from './internals/omitPrototype';
@@ -129,7 +129,11 @@ type RouterCaller<TDef extends AnyRouterDef> = (
129
129
  export interface Router<TDef extends AnyRouterDef> {
130
130
  _def: TDef;
131
131
  createCaller: RouterCaller<TDef>;
132
- // FIXME rename me and deprecate
132
+
133
+ /**
134
+ * @deprecated
135
+ * FIXME: use the new standalone `getErrorShape` instead
136
+ */
133
137
  getErrorShape(opts: {
134
138
  error: TRPCError;
135
139
  type: ProcedureType | 'unknown';
package/src/core/types.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { inferObservableValue } from '../observable';
2
- import { inferTransformedProcedureOutput } from '../shared';
2
+ import { inferTransformedProcedureOutput } from '../shared/jsonify';
3
3
  import { AnyProcedure, ProcedureArgs } from './procedure';
4
4
  import { AnyRouter, AnyRouterDef, Router } from './router';
5
5
 
@@ -8,7 +8,8 @@ import {
8
8
  } from '../core';
9
9
  import { TRPCError, getTRPCErrorFromUnknown } from '../error/TRPCError';
10
10
  import { TRPCResponse } from '../rpc';
11
- import { transformTRPCResponse } from '../shared';
11
+ import { getErrorShape } from '../shared/getErrorShape';
12
+ import { transformTRPCResponse } from '../shared/transformTRPCResponse';
12
13
  import { Maybe } from '../types';
13
14
  import {
14
15
  BaseContentTypeHandler,
@@ -93,7 +94,10 @@ export async function resolveHTTPResponse<
93
94
  status = meta.status;
94
95
  }
95
96
 
96
- const transformedJSON = transformTRPCResponse(router, untransformedJSON);
97
+ const transformedJSON = transformTRPCResponse(
98
+ router._def._config,
99
+ untransformedJSON,
100
+ );
97
101
 
98
102
  const body = JSON.stringify(transformedJSON);
99
103
 
@@ -170,7 +174,8 @@ export async function resolveHTTPResponse<
170
174
 
171
175
  if (obj.error) {
172
176
  return {
173
- error: router.getErrorShape({
177
+ error: getErrorShape({
178
+ config: router._def._config,
174
179
  error: obj.error,
175
180
  type,
176
181
  path,
@@ -208,7 +213,8 @@ export async function resolveHTTPResponse<
208
213
  });
209
214
  return endResponse(
210
215
  {
211
- error: router.getErrorShape({
216
+ error: getErrorShape({
217
+ config: router._def._config,
212
218
  error,
213
219
  type,
214
220
  path: undefined,
@@ -6,6 +6,9 @@ type Invert<TType extends Record<PropertyKey, PropertyKey>> = {
6
6
  [TValue in TType[keyof TType]]: KeyFromValue<TValue, TType>;
7
7
  };
8
8
 
9
+ /**
10
+ * @internal
11
+ */
9
12
  export function invert<TRecord extends Record<PropertyKey, PropertyKey>>(
10
13
  obj: TRecord,
11
14
  ): Invert<TRecord> {
@@ -4,6 +4,7 @@ import { TRPCError } from '../error/TRPCError';
4
4
 
5
5
  /**
6
6
  * Base interface for any response handler
7
+ * @internal
7
8
  */
8
9
  export interface BaseHandlerOptions<TRouter extends AnyRouter, TRequest> {
9
10
  onError?: OnErrorFunction<TRouter, TRequest>;
@@ -13,6 +14,9 @@ export interface BaseHandlerOptions<TRouter extends AnyRouter, TRequest> {
13
14
  router: TRouter;
14
15
  }
15
16
 
17
+ /**
18
+ * @internal
19
+ */
16
20
  export type OnErrorFunction<TRouter extends AnyRouter, TRequest> = (opts: {
17
21
  error: TRPCError;
18
22
  type: ProcedureType | 'unknown';
@@ -0,0 +1,35 @@
1
+ import { ProcedureType } from '../core';
2
+ import { TRPCError } from '../error/TRPCError';
3
+ import { getHTTPStatusCodeFromError } from '../http/getHTTPStatusCode';
4
+ import { AnyRootConfig, DefaultErrorShape } from '../internals';
5
+ import { TRPC_ERROR_CODES_BY_KEY } from '../rpc';
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export function getErrorShape<TConfig extends AnyRootConfig>(opts: {
11
+ config: TConfig;
12
+ error: TRPCError;
13
+ type: ProcedureType | 'unknown';
14
+ path: string | undefined;
15
+ input: unknown;
16
+ ctx: undefined | TConfig['$types']['ctx'];
17
+ }): TConfig['$types']['errorShape'] {
18
+ const { path, error, config } = opts;
19
+ const { code } = opts.error;
20
+ const shape: DefaultErrorShape = {
21
+ message: error.message,
22
+ code: TRPC_ERROR_CODES_BY_KEY[code],
23
+ data: {
24
+ code,
25
+ httpStatus: getHTTPStatusCodeFromError(error),
26
+ },
27
+ };
28
+ if (config.isDev && typeof opts.error.stack === 'string') {
29
+ shape.data.stack = opts.error.stack;
30
+ }
31
+ if (typeof path === 'string') {
32
+ shape.data.path = path;
33
+ }
34
+ return config.errorFormatter({ ...opts, shape });
35
+ }
@@ -3,4 +3,6 @@ export * from './jsonify';
3
3
  export * from './transformTRPCResponse';
4
4
 
5
5
  // For `.d.ts` files https://github.com/trpc/trpc/issues/3943
6
- export type { SerializeObject } from './internal/serialize';
6
+ export type { SerializeObject, Serialize } from './internal/serialize';
7
+
8
+ export { getErrorShape } from './getErrorShape';
@@ -1,13 +1,13 @@
1
- import { AnyRouter } from '../core/router';
1
+ import { AnyRootConfig } from '../internals';
2
2
  import { TRPCResponse, TRPCResponseMessage } from '../rpc';
3
3
 
4
4
  function transformTRPCResponseItem<
5
5
  TResponseItem extends TRPCResponse | TRPCResponseMessage,
6
- >(router: AnyRouter, item: TResponseItem): TResponseItem {
6
+ >(config: AnyRootConfig, item: TResponseItem): TResponseItem {
7
7
  if ('error' in item) {
8
8
  return {
9
9
  ...item,
10
- error: router._def._config.transformer.output.serialize(item.error),
10
+ error: config.transformer.output.serialize(item.error),
11
11
  };
12
12
  }
13
13
 
@@ -16,9 +16,7 @@ function transformTRPCResponseItem<
16
16
  ...item,
17
17
  result: {
18
18
  ...item.result,
19
- data: router._def._config.transformer.output.serialize(
20
- item.result.data,
21
- ),
19
+ data: config.transformer.output.serialize(item.result.data),
22
20
  },
23
21
  };
24
22
  }
@@ -35,8 +33,8 @@ export function transformTRPCResponse<
35
33
  | TRPCResponse[]
36
34
  | TRPCResponseMessage
37
35
  | TRPCResponseMessage[],
38
- >(router: AnyRouter, itemOrItems: TResponse) {
36
+ >(config: AnyRootConfig, itemOrItems: TResponse) {
39
37
  return Array.isArray(itemOrItems)
40
- ? itemOrItems.map((item) => transformTRPCResponseItem(router, item))
41
- : transformTRPCResponseItem(router, itemOrItems);
38
+ ? itemOrItems.map((item) => transformTRPCResponseItem(config, item))
39
+ : transformTRPCResponseItem(config, itemOrItems);
42
40
  }