@trpc/server 10.0.0-proxy-beta.13 → 10.0.0-proxy-beta.15

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 (63) hide show
  1. package/dist/{TRPCError-281ac25a.mjs → TRPCError-e99e7cbe.mjs} +1 -1
  2. package/dist/adapters/aws-lambda/index.js +2 -3
  3. package/dist/adapters/aws-lambda/index.mjs +3 -4
  4. package/dist/adapters/express.js +3 -3
  5. package/dist/adapters/express.mjs +4 -4
  6. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  7. package/dist/adapters/fastify/index.js +2 -3
  8. package/dist/adapters/fastify/index.mjs +3 -4
  9. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  10. package/dist/adapters/fetch/index.js +2 -3
  11. package/dist/adapters/fetch/index.mjs +3 -4
  12. package/dist/adapters/next.js +3 -3
  13. package/dist/adapters/next.mjs +4 -4
  14. package/dist/adapters/node-http/index.js +3 -3
  15. package/dist/adapters/node-http/index.mjs +4 -4
  16. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  17. package/dist/adapters/standalone.js +3 -3
  18. package/dist/adapters/standalone.mjs +4 -4
  19. package/dist/adapters/ws.d.ts.map +1 -1
  20. package/dist/adapters/ws.js +10 -3
  21. package/dist/adapters/ws.mjs +11 -4
  22. package/dist/core/initTRPC.d.ts +16 -4
  23. package/dist/core/initTRPC.d.ts.map +1 -1
  24. package/dist/core/internals/config.d.ts +13 -0
  25. package/dist/core/internals/config.d.ts.map +1 -1
  26. package/dist/core/middleware.d.ts +1 -1
  27. package/dist/core/middleware.d.ts.map +1 -1
  28. package/dist/deprecated/internals/procedure.d.ts.map +1 -1
  29. package/dist/deprecated/router.d.ts.map +1 -1
  30. package/dist/http/index.d.ts.map +1 -1
  31. package/dist/index.d.ts +0 -1
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +739 -25
  34. package/dist/index.mjs +724 -9
  35. package/dist/{nodeHTTPRequestHandler-1bf460cc.mjs → nodeHTTPRequestHandler-205f7545.mjs} +2 -3
  36. package/dist/{nodeHTTPRequestHandler-71717219.js → nodeHTTPRequestHandler-efc38711.js} +1 -2
  37. package/dist/resolveHTTPResponse-4ceb1785.mjs +205 -0
  38. package/dist/resolveHTTPResponse-bf4c6ceb.js +207 -0
  39. package/dist/subscription.mjs +1 -1
  40. package/dist/{resolveHTTPResponse-c6e31b55.js → transformTRPCResponse-05aa84c1.js} +22 -201
  41. package/dist/{resolveHTTPResponse-f2b378b6.mjs → transformTRPCResponse-959de787.mjs} +21 -201
  42. package/dist/types.d.ts +9 -2
  43. package/dist/types.d.ts.map +1 -1
  44. package/package.json +6 -6
  45. package/src/adapters/fastify/fastifyRequestHandler.ts +0 -3
  46. package/src/adapters/fetch/fetchRequestHandler.ts +0 -3
  47. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +0 -3
  48. package/src/adapters/ws.ts +13 -3
  49. package/src/core/initTRPC.ts +12 -0
  50. package/src/core/internals/config.ts +19 -0
  51. package/src/core/middleware.ts +1 -1
  52. package/src/deprecated/internals/procedure.ts +0 -3
  53. package/src/deprecated/router.ts +0 -3
  54. package/src/http/index.ts +0 -4
  55. package/src/index.ts +0 -1
  56. package/src/types.ts +28 -7
  57. package/dist/assertNotBrowser.d.ts +0 -2
  58. package/dist/assertNotBrowser.d.ts.map +0 -1
  59. package/dist/router-15f9618e.js +0 -724
  60. package/dist/router-28a2c129.mjs +0 -718
  61. package/dist/transformTRPCResponse-11ac69cc.js +0 -27
  62. package/dist/transformTRPCResponse-c8138d5f.mjs +0 -25
  63. package/src/assertNotBrowser.ts +0 -12
@@ -1,5 +1,5 @@
1
- import { b as assertNotBrowser, r as resolveHTTPResponse } from './resolveHTTPResponse-f2b378b6.mjs';
2
- import { T as TRPCError } from './TRPCError-281ac25a.mjs';
1
+ import { r as resolveHTTPResponse } from './resolveHTTPResponse-4ceb1785.mjs';
2
+ import { T as TRPCError } from './TRPCError-e99e7cbe.mjs';
3
3
 
4
4
  async function getPostBody({ req , maxBodySize }) {
5
5
  return new Promise((resolve)=>{
@@ -34,7 +34,6 @@ async function getPostBody({ req , maxBodySize }) {
34
34
  });
35
35
  }
36
36
 
37
- assertNotBrowser();
38
37
  async function nodeHTTPRequestHandler(opts) {
39
38
  const createContext = async function _createContext() {
40
39
  return await opts.createContext?.(opts);
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var resolveHTTPResponse = require('./resolveHTTPResponse-c6e31b55.js');
3
+ var resolveHTTPResponse = require('./resolveHTTPResponse-bf4c6ceb.js');
4
4
  var TRPCError = require('./TRPCError-fe9113e5.js');
5
5
 
6
6
  async function getPostBody({ req , maxBodySize }) {
@@ -36,7 +36,6 @@ async function getPostBody({ req , maxBodySize }) {
36
36
  });
37
37
  }
38
38
 
39
- resolveHTTPResponse.assertNotBrowser();
40
39
  async function nodeHTTPRequestHandler(opts) {
41
40
  const createContext = async function _createContext() {
42
41
  return await opts.createContext?.(opts);
@@ -0,0 +1,205 @@
1
+ import { e as callProcedure, f as getHTTPStatusCode, t as transformTRPCResponse } from './transformTRPCResponse-959de787.mjs';
2
+ import { T as TRPCError, a as getTRPCErrorFromUnknown, g as getCauseFromUnknown } from './TRPCError-e99e7cbe.mjs';
3
+
4
+ const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
5
+ GET: 'query',
6
+ POST: 'mutation'
7
+ };
8
+ function getRawProcedureInputOrThrow(req) {
9
+ try {
10
+ if (req.method === 'GET') {
11
+ if (!req.query.has('input')) {
12
+ return undefined;
13
+ }
14
+ const raw = req.query.get('input');
15
+ return JSON.parse(raw);
16
+ }
17
+ if (typeof req.body === 'string') {
18
+ // A mutation with no inputs will have req.body === ''
19
+ return req.body.length === 0 ? undefined : JSON.parse(req.body);
20
+ }
21
+ return req.body;
22
+ } catch (err) {
23
+ throw new TRPCError({
24
+ code: 'PARSE_ERROR',
25
+ cause: getCauseFromUnknown(err)
26
+ });
27
+ }
28
+ }
29
+ async function resolveHTTPResponse(opts) {
30
+ const { createContext , onError , router , req } = opts;
31
+ const batchingEnabled = opts.batching?.enabled ?? true;
32
+ if (req.method === 'HEAD') {
33
+ // can be used for lambda warmup
34
+ return {
35
+ status: 204
36
+ };
37
+ }
38
+ const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
39
+ let ctx = undefined;
40
+ let paths = undefined;
41
+ const isBatchCall = !!req.query.get('batch');
42
+ function endResponse(untransformedJSON, errors) {
43
+ let status = getHTTPStatusCode(untransformedJSON);
44
+ const headers = {
45
+ 'Content-Type': 'application/json'
46
+ };
47
+ const meta = opts.responseMeta?.({
48
+ ctx,
49
+ paths,
50
+ type,
51
+ data: Array.isArray(untransformedJSON) ? untransformedJSON : [
52
+ untransformedJSON
53
+ ],
54
+ errors
55
+ }) ?? {};
56
+ for (const [key, value] of Object.entries(meta.headers ?? {})){
57
+ headers[key] = value;
58
+ }
59
+ if (meta.status) {
60
+ status = meta.status;
61
+ }
62
+ const transformedJSON = transformTRPCResponse(router, untransformedJSON);
63
+ const body = JSON.stringify(transformedJSON);
64
+ return {
65
+ body,
66
+ status,
67
+ headers
68
+ };
69
+ }
70
+ try {
71
+ if (opts.error) {
72
+ throw opts.error;
73
+ }
74
+ if (isBatchCall && !batchingEnabled) {
75
+ throw new Error(`Batching is not enabled on the server`);
76
+ }
77
+ if (type === 'subscription') {
78
+ throw new TRPCError({
79
+ message: 'Subscriptions should use wsLink',
80
+ code: 'METHOD_NOT_SUPPORTED'
81
+ });
82
+ }
83
+ if (type === 'unknown') {
84
+ throw new TRPCError({
85
+ message: `Unexpected request method ${req.method}`,
86
+ code: 'METHOD_NOT_SUPPORTED'
87
+ });
88
+ }
89
+ const rawInput = getRawProcedureInputOrThrow(req);
90
+ paths = isBatchCall ? opts.path.split(',') : [
91
+ opts.path
92
+ ];
93
+ ctx = await createContext();
94
+ const deserializeInputValue = (rawValue)=>{
95
+ return typeof rawValue !== 'undefined' ? router._def.transformer.input.deserialize(rawValue) : rawValue;
96
+ };
97
+ const getInputs = ()=>{
98
+ if (!isBatchCall) {
99
+ return {
100
+ 0: deserializeInputValue(rawInput)
101
+ };
102
+ }
103
+ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
104
+ throw new TRPCError({
105
+ code: 'BAD_REQUEST',
106
+ message: '"input" needs to be an object when doing a batch call'
107
+ });
108
+ }
109
+ const input = {};
110
+ for(const key in rawInput){
111
+ const k = key;
112
+ const rawValue = rawInput[k];
113
+ const value = deserializeInputValue(rawValue);
114
+ input[k] = value;
115
+ }
116
+ return input;
117
+ };
118
+ const inputs = getInputs();
119
+ const rawResults = await Promise.all(paths.map(async (path, index)=>{
120
+ const input = inputs[index];
121
+ try {
122
+ const output = await callProcedure({
123
+ procedures: router._def.procedures,
124
+ path,
125
+ rawInput: input,
126
+ ctx,
127
+ type
128
+ });
129
+ return {
130
+ input,
131
+ path,
132
+ data: output
133
+ };
134
+ } catch (cause) {
135
+ const error = getTRPCErrorFromUnknown(cause);
136
+ onError?.({
137
+ error,
138
+ path,
139
+ input,
140
+ ctx,
141
+ type: type,
142
+ req
143
+ });
144
+ return {
145
+ input,
146
+ path,
147
+ error
148
+ };
149
+ }
150
+ }));
151
+ const errors = rawResults.flatMap((obj)=>obj.error ? [
152
+ obj.error
153
+ ] : []);
154
+ const resultEnvelopes = rawResults.map((obj)=>{
155
+ const { path , input } = obj;
156
+ if (obj.error) {
157
+ return {
158
+ error: router.getErrorShape({
159
+ error: obj.error,
160
+ type,
161
+ path,
162
+ input,
163
+ ctx
164
+ })
165
+ };
166
+ } else {
167
+ return {
168
+ result: {
169
+ data: obj.data
170
+ }
171
+ };
172
+ }
173
+ });
174
+ const result = isBatchCall ? resultEnvelopes : resultEnvelopes[0];
175
+ return endResponse(result, errors);
176
+ } catch (cause) {
177
+ // we get here if
178
+ // - batching is called when it's not enabled
179
+ // - `createContext()` throws
180
+ // - post body is too large
181
+ // - input deserialization fails
182
+ const error = getTRPCErrorFromUnknown(cause);
183
+ onError?.({
184
+ error,
185
+ path: undefined,
186
+ input: undefined,
187
+ ctx,
188
+ type: type,
189
+ req
190
+ });
191
+ return endResponse({
192
+ error: router.getErrorShape({
193
+ error,
194
+ type,
195
+ path: undefined,
196
+ input: undefined,
197
+ ctx
198
+ })
199
+ }, [
200
+ error
201
+ ]);
202
+ }
203
+ }
204
+
205
+ export { resolveHTTPResponse as r };
@@ -0,0 +1,207 @@
1
+ 'use strict';
2
+
3
+ var transformTRPCResponse = require('./transformTRPCResponse-05aa84c1.js');
4
+ var TRPCError = require('./TRPCError-fe9113e5.js');
5
+
6
+ const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
7
+ GET: 'query',
8
+ POST: 'mutation'
9
+ };
10
+ function getRawProcedureInputOrThrow(req) {
11
+ try {
12
+ if (req.method === 'GET') {
13
+ if (!req.query.has('input')) {
14
+ return undefined;
15
+ }
16
+ const raw = req.query.get('input');
17
+ return JSON.parse(raw);
18
+ }
19
+ if (typeof req.body === 'string') {
20
+ // A mutation with no inputs will have req.body === ''
21
+ return req.body.length === 0 ? undefined : JSON.parse(req.body);
22
+ }
23
+ return req.body;
24
+ } catch (err) {
25
+ throw new TRPCError.TRPCError({
26
+ code: 'PARSE_ERROR',
27
+ cause: TRPCError.getCauseFromUnknown(err)
28
+ });
29
+ }
30
+ }
31
+ async function resolveHTTPResponse(opts) {
32
+ const { createContext , onError , router , req } = opts;
33
+ const batchingEnabled = opts.batching?.enabled ?? true;
34
+ if (req.method === 'HEAD') {
35
+ // can be used for lambda warmup
36
+ return {
37
+ status: 204
38
+ };
39
+ }
40
+ const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
41
+ let ctx = undefined;
42
+ let paths = undefined;
43
+ const isBatchCall = !!req.query.get('batch');
44
+ function endResponse(untransformedJSON, errors) {
45
+ let status = transformTRPCResponse.getHTTPStatusCode(untransformedJSON);
46
+ const headers = {
47
+ 'Content-Type': 'application/json'
48
+ };
49
+ const meta = opts.responseMeta?.({
50
+ ctx,
51
+ paths,
52
+ type,
53
+ data: Array.isArray(untransformedJSON) ? untransformedJSON : [
54
+ untransformedJSON
55
+ ],
56
+ errors
57
+ }) ?? {};
58
+ for (const [key, value] of Object.entries(meta.headers ?? {})){
59
+ headers[key] = value;
60
+ }
61
+ if (meta.status) {
62
+ status = meta.status;
63
+ }
64
+ const transformedJSON = transformTRPCResponse.transformTRPCResponse(router, untransformedJSON);
65
+ const body = JSON.stringify(transformedJSON);
66
+ return {
67
+ body,
68
+ status,
69
+ headers
70
+ };
71
+ }
72
+ try {
73
+ if (opts.error) {
74
+ throw opts.error;
75
+ }
76
+ if (isBatchCall && !batchingEnabled) {
77
+ throw new Error(`Batching is not enabled on the server`);
78
+ }
79
+ if (type === 'subscription') {
80
+ throw new TRPCError.TRPCError({
81
+ message: 'Subscriptions should use wsLink',
82
+ code: 'METHOD_NOT_SUPPORTED'
83
+ });
84
+ }
85
+ if (type === 'unknown') {
86
+ throw new TRPCError.TRPCError({
87
+ message: `Unexpected request method ${req.method}`,
88
+ code: 'METHOD_NOT_SUPPORTED'
89
+ });
90
+ }
91
+ const rawInput = getRawProcedureInputOrThrow(req);
92
+ paths = isBatchCall ? opts.path.split(',') : [
93
+ opts.path
94
+ ];
95
+ ctx = await createContext();
96
+ const deserializeInputValue = (rawValue)=>{
97
+ return typeof rawValue !== 'undefined' ? router._def.transformer.input.deserialize(rawValue) : rawValue;
98
+ };
99
+ const getInputs = ()=>{
100
+ if (!isBatchCall) {
101
+ return {
102
+ 0: deserializeInputValue(rawInput)
103
+ };
104
+ }
105
+ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
106
+ throw new TRPCError.TRPCError({
107
+ code: 'BAD_REQUEST',
108
+ message: '"input" needs to be an object when doing a batch call'
109
+ });
110
+ }
111
+ const input = {};
112
+ for(const key in rawInput){
113
+ const k = key;
114
+ const rawValue = rawInput[k];
115
+ const value = deserializeInputValue(rawValue);
116
+ input[k] = value;
117
+ }
118
+ return input;
119
+ };
120
+ const inputs = getInputs();
121
+ const rawResults = await Promise.all(paths.map(async (path, index)=>{
122
+ const input = inputs[index];
123
+ try {
124
+ const output = await transformTRPCResponse.callProcedure({
125
+ procedures: router._def.procedures,
126
+ path,
127
+ rawInput: input,
128
+ ctx,
129
+ type
130
+ });
131
+ return {
132
+ input,
133
+ path,
134
+ data: output
135
+ };
136
+ } catch (cause) {
137
+ const error = TRPCError.getTRPCErrorFromUnknown(cause);
138
+ onError?.({
139
+ error,
140
+ path,
141
+ input,
142
+ ctx,
143
+ type: type,
144
+ req
145
+ });
146
+ return {
147
+ input,
148
+ path,
149
+ error
150
+ };
151
+ }
152
+ }));
153
+ const errors = rawResults.flatMap((obj)=>obj.error ? [
154
+ obj.error
155
+ ] : []);
156
+ const resultEnvelopes = rawResults.map((obj)=>{
157
+ const { path , input } = obj;
158
+ if (obj.error) {
159
+ return {
160
+ error: router.getErrorShape({
161
+ error: obj.error,
162
+ type,
163
+ path,
164
+ input,
165
+ ctx
166
+ })
167
+ };
168
+ } else {
169
+ return {
170
+ result: {
171
+ data: obj.data
172
+ }
173
+ };
174
+ }
175
+ });
176
+ const result = isBatchCall ? resultEnvelopes : resultEnvelopes[0];
177
+ return endResponse(result, errors);
178
+ } catch (cause) {
179
+ // we get here if
180
+ // - batching is called when it's not enabled
181
+ // - `createContext()` throws
182
+ // - post body is too large
183
+ // - input deserialization fails
184
+ const error = TRPCError.getTRPCErrorFromUnknown(cause);
185
+ onError?.({
186
+ error,
187
+ path: undefined,
188
+ input: undefined,
189
+ ctx,
190
+ type: type,
191
+ req
192
+ });
193
+ return endResponse({
194
+ error: router.getErrorShape({
195
+ error,
196
+ type,
197
+ path: undefined,
198
+ input: undefined,
199
+ ctx
200
+ })
201
+ }, [
202
+ error
203
+ ]);
204
+ }
205
+ }
206
+
207
+ exports.resolveHTTPResponse = resolveHTTPResponse;
@@ -1,4 +1,4 @@
1
- import { g as getTRPCErrorFromUnknown } from './TRPCError-281ac25a.mjs';
1
+ import { a as getTRPCErrorFromUnknown } from './TRPCError-e99e7cbe.mjs';
2
2
  import { o as observable } from './observable-e129ac9d.mjs';
3
3
 
4
4
  function subscriptionPullFactory(opts) {
@@ -3,13 +3,6 @@
3
3
  var TRPCError = require('./TRPCError-fe9113e5.js');
4
4
  var codes = require('./codes-76c38a34.js');
5
5
  var index = require('./index-62d46c97.js');
6
- var transformTRPCResponse = require('./transformTRPCResponse-11ac69cc.js');
7
-
8
- /* istanbul ignore file */ function assertNotBrowser() {
9
- if (typeof window !== 'undefined' && !('Deno' in window) && process.env.NODE_ENV !== 'test' && process.env.JEST_WORKER_ID === undefined) {
10
- throw new Error('Imported server-only code in the browser');
11
- }
12
- }
13
6
 
14
7
  const defaultFormatter = ({ shape })=>{
15
8
  return shape;
@@ -255,214 +248,42 @@ const emptyRouter = {
255
248
  return procedure(opts);
256
249
  }
257
250
 
258
- const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
259
- GET: 'query',
260
- POST: 'mutation'
261
- };
262
- function getRawProcedureInputOrThrow(req) {
263
- try {
264
- if (req.method === 'GET') {
265
- if (!req.query.has('input')) {
266
- return undefined;
267
- }
268
- const raw = req.query.get('input');
269
- return JSON.parse(raw);
270
- }
271
- if (typeof req.body === 'string') {
272
- // A mutation with no inputs will have req.body === ''
273
- return req.body.length === 0 ? undefined : JSON.parse(req.body);
274
- }
275
- return req.body;
276
- } catch (err) {
277
- throw new TRPCError.TRPCError({
278
- code: 'PARSE_ERROR',
279
- cause: TRPCError.getCauseFromUnknown(err)
280
- });
281
- }
282
- }
283
- async function resolveHTTPResponse(opts) {
284
- const { createContext , onError , router , req } = opts;
285
- const batchingEnabled = opts.batching?.enabled ?? true;
286
- if (req.method === 'HEAD') {
287
- // can be used for lambda warmup
251
+ /**
252
+ * The default check to see if we're in a server
253
+ */ const isServerDefault = typeof window === 'undefined' || 'Deno' in window || process.env.NODE_ENV === 'test' || !!process.env.JEST_WORKER_ID;
254
+
255
+ function transformTRPCResponseItem(router, item) {
256
+ if ('error' in item) {
288
257
  return {
289
- status: 204
258
+ ...item,
259
+ error: router._def.transformer.output.serialize(item.error)
290
260
  };
291
261
  }
292
- const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
293
- let ctx = undefined;
294
- let paths = undefined;
295
- const isBatchCall = !!req.query.get('batch');
296
- function endResponse(untransformedJSON, errors) {
297
- let status = getHTTPStatusCode(untransformedJSON);
298
- const headers = {
299
- 'Content-Type': 'application/json'
300
- };
301
- const meta = opts.responseMeta?.({
302
- ctx,
303
- paths,
304
- type,
305
- data: Array.isArray(untransformedJSON) ? untransformedJSON : [
306
- untransformedJSON
307
- ],
308
- errors
309
- }) ?? {};
310
- for (const [key, value] of Object.entries(meta.headers ?? {})){
311
- headers[key] = value;
312
- }
313
- if (meta.status) {
314
- status = meta.status;
315
- }
316
- const transformedJSON = transformTRPCResponse.transformTRPCResponse(router, untransformedJSON);
317
- const body = JSON.stringify(transformedJSON);
262
+ if ('data' in item.result) {
318
263
  return {
319
- body,
320
- status,
321
- headers
322
- };
323
- }
324
- try {
325
- if (opts.error) {
326
- throw opts.error;
327
- }
328
- if (isBatchCall && !batchingEnabled) {
329
- throw new Error(`Batching is not enabled on the server`);
330
- }
331
- if (type === 'subscription') {
332
- throw new TRPCError.TRPCError({
333
- message: 'Subscriptions should use wsLink',
334
- code: 'METHOD_NOT_SUPPORTED'
335
- });
336
- }
337
- if (type === 'unknown') {
338
- throw new TRPCError.TRPCError({
339
- message: `Unexpected request method ${req.method}`,
340
- code: 'METHOD_NOT_SUPPORTED'
341
- });
342
- }
343
- const rawInput = getRawProcedureInputOrThrow(req);
344
- paths = isBatchCall ? opts.path.split(',') : [
345
- opts.path
346
- ];
347
- ctx = await createContext();
348
- const deserializeInputValue = (rawValue)=>{
349
- return typeof rawValue !== 'undefined' ? router._def.transformer.input.deserialize(rawValue) : rawValue;
350
- };
351
- const getInputs = ()=>{
352
- if (!isBatchCall) {
353
- return {
354
- 0: deserializeInputValue(rawInput)
355
- };
356
- }
357
- if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
358
- throw new TRPCError.TRPCError({
359
- code: 'BAD_REQUEST',
360
- message: '"input" needs to be an object when doing a batch call'
361
- });
362
- }
363
- const input = {};
364
- for(const key in rawInput){
365
- const k = key;
366
- const rawValue = rawInput[k];
367
- const value = deserializeInputValue(rawValue);
368
- input[k] = value;
264
+ ...item,
265
+ result: {
266
+ ...item.result,
267
+ data: router._def.transformer.output.serialize(item.result.data)
369
268
  }
370
- return input;
371
269
  };
372
- const inputs = getInputs();
373
- const rawResults = await Promise.all(paths.map(async (path, index)=>{
374
- const input = inputs[index];
375
- try {
376
- const output = await callProcedure({
377
- procedures: router._def.procedures,
378
- path,
379
- rawInput: input,
380
- ctx,
381
- type
382
- });
383
- return {
384
- input,
385
- path,
386
- data: output
387
- };
388
- } catch (cause) {
389
- const error = TRPCError.getTRPCErrorFromUnknown(cause);
390
- onError?.({
391
- error,
392
- path,
393
- input,
394
- ctx,
395
- type: type,
396
- req
397
- });
398
- return {
399
- input,
400
- path,
401
- error
402
- };
403
- }
404
- }));
405
- const errors = rawResults.flatMap((obj)=>obj.error ? [
406
- obj.error
407
- ] : []);
408
- const resultEnvelopes = rawResults.map((obj)=>{
409
- const { path , input } = obj;
410
- if (obj.error) {
411
- return {
412
- error: router.getErrorShape({
413
- error: obj.error,
414
- type,
415
- path,
416
- input,
417
- ctx
418
- })
419
- };
420
- } else {
421
- return {
422
- result: {
423
- data: obj.data
424
- }
425
- };
426
- }
427
- });
428
- const result = isBatchCall ? resultEnvelopes : resultEnvelopes[0];
429
- return endResponse(result, errors);
430
- } catch (cause) {
431
- // we get here if
432
- // - batching is called when it's not enabled
433
- // - `createContext()` throws
434
- // - post body is too large
435
- // - input deserialization fails
436
- const error = TRPCError.getTRPCErrorFromUnknown(cause);
437
- onError?.({
438
- error,
439
- path: undefined,
440
- input: undefined,
441
- ctx,
442
- type: type,
443
- req
444
- });
445
- return endResponse({
446
- error: router.getErrorShape({
447
- error,
448
- type,
449
- path: undefined,
450
- input: undefined,
451
- ctx
452
- })
453
- }, [
454
- error
455
- ]);
456
270
  }
271
+ return item;
272
+ }
273
+ /**
274
+ * Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
275
+ **/ function transformTRPCResponse(router, itemOrItems) {
276
+ return Array.isArray(itemOrItems) ? itemOrItems.map((item)=>transformTRPCResponseItem(router, item)) : transformTRPCResponseItem(router, itemOrItems);
457
277
  }
458
278
 
459
- exports.assertNotBrowser = assertNotBrowser;
460
279
  exports.callProcedure = callProcedure;
461
280
  exports.createRouterFactory = createRouterFactory;
462
281
  exports.defaultFormatter = defaultFormatter;
463
282
  exports.defaultTransformer = defaultTransformer;
464
283
  exports.getDataTransformer = getDataTransformer;
284
+ exports.getHTTPStatusCode = getHTTPStatusCode;
465
285
  exports.getHTTPStatusCodeFromError = getHTTPStatusCodeFromError;
286
+ exports.isServerDefault = isServerDefault;
466
287
  exports.mergeWithoutOverrides = mergeWithoutOverrides;
467
288
  exports.procedureTypes = procedureTypes;
468
- exports.resolveHTTPResponse = resolveHTTPResponse;
289
+ exports.transformTRPCResponse = transformTRPCResponse;