@trpc/server 11.0.0-next-alpha.153 → 11.0.0-next-alpha.155

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 (310) hide show
  1. package/dist/adapters/aws-lambda/index.d.ts +2 -2
  2. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  3. package/dist/adapters/aws-lambda/index.js +6 -13
  4. package/dist/adapters/aws-lambda/index.mjs +2 -9
  5. package/dist/adapters/aws-lambda/utils.d.ts +2 -4
  6. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
  7. package/dist/adapters/express.d.ts +2 -2
  8. package/dist/adapters/express.d.ts.map +1 -1
  9. package/dist/adapters/express.js +3 -11
  10. package/dist/adapters/express.mjs +3 -11
  11. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +4 -4
  12. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  13. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +4 -4
  14. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  15. package/dist/adapters/fastify/index.js +6 -14
  16. package/dist/adapters/fastify/index.mjs +4 -12
  17. package/dist/adapters/fetch/fetchRequestHandler.d.ts +2 -2
  18. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  19. package/dist/adapters/fetch/index.js +3 -12
  20. package/dist/adapters/fetch/index.mjs +1 -10
  21. package/dist/adapters/fetch/types.d.ts +2 -2
  22. package/dist/adapters/fetch/types.d.ts.map +1 -1
  23. package/dist/adapters/next.d.ts +4 -3
  24. package/dist/adapters/next.d.ts.map +1 -1
  25. package/dist/adapters/next.js +5 -13
  26. package/dist/adapters/next.mjs +3 -11
  27. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +1 -1
  28. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +1 -1
  29. package/dist/adapters/node-http/content-type/form-data/index.d.ts +2 -2
  30. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
  31. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +1 -1
  32. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +1 -1
  33. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +2 -1
  34. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +1 -1
  35. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +1 -1
  36. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +1 -1
  37. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +2 -2
  38. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
  39. package/dist/adapters/node-http/content-type/json/index.js +4 -5
  40. package/dist/adapters/node-http/content-type/json/index.mjs +2 -3
  41. package/dist/adapters/node-http/index.js +3 -11
  42. package/dist/adapters/node-http/index.mjs +3 -11
  43. package/dist/adapters/node-http/internals/contentType.d.ts +3 -3
  44. package/dist/adapters/node-http/internals/contentType.d.ts.map +1 -1
  45. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +2 -2
  46. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  47. package/dist/adapters/node-http/types.d.ts +4 -5
  48. package/dist/adapters/node-http/types.d.ts.map +1 -1
  49. package/dist/adapters/standalone.d.ts +2 -2
  50. package/dist/adapters/standalone.d.ts.map +1 -1
  51. package/dist/adapters/standalone.js +3 -11
  52. package/dist/adapters/standalone.mjs +3 -11
  53. package/dist/adapters/ws.d.ts +6 -6
  54. package/dist/adapters/ws.d.ts.map +1 -1
  55. package/dist/adapters/ws.js +25 -31
  56. package/dist/adapters/ws.mjs +3 -9
  57. package/dist/bundle-analysis.json +81 -611
  58. package/dist/http.d.ts +2 -0
  59. package/dist/http.d.ts.map +1 -0
  60. package/dist/http.js +14 -0
  61. package/dist/http.mjs +1 -0
  62. package/dist/index.d.ts +15 -7
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +17 -95
  65. package/dist/index.mjs +1 -90
  66. package/dist/{nodeHTTPRequestHandler-a7049e69.js → nodeHTTPRequestHandler-30028c9a.js} +3 -5
  67. package/dist/{nodeHTTPRequestHandler-ca65b7c9.mjs → nodeHTTPRequestHandler-36974696.mjs} +1 -3
  68. package/dist/{nodeHTTPRequestHandler-abbfb330.js → nodeHTTPRequestHandler-640c1f60.js} +1 -3
  69. package/dist/observable.d.ts +2 -0
  70. package/dist/observable.d.ts.map +1 -0
  71. package/dist/observable.js +14 -0
  72. package/dist/observable.mjs +1 -0
  73. package/dist/rpc.d.ts +2 -0
  74. package/dist/rpc.d.ts.map +1 -0
  75. package/dist/rpc.js +14 -0
  76. package/dist/rpc.mjs +1 -0
  77. package/dist/unstableDoNotImportThis.d.ts +9 -0
  78. package/dist/unstableDoNotImportThis.d.ts.map +1 -0
  79. package/package.json +32 -41
  80. package/src/adapters/aws-lambda/index.ts +8 -6
  81. package/src/adapters/aws-lambda/utils.ts +8 -5
  82. package/src/adapters/express.ts +3 -3
  83. package/src/adapters/fastify/fastifyRequestHandler.ts +7 -7
  84. package/src/adapters/fastify/fastifyTRPCPlugin.ts +6 -5
  85. package/src/adapters/fetch/fetchRequestHandler.ts +6 -6
  86. package/src/adapters/fetch/types.ts +2 -2
  87. package/src/adapters/next.ts +5 -9
  88. package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +2 -1
  89. package/src/adapters/node-http/content-type/form-data/index.ts +4 -7
  90. package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +2 -1
  91. package/src/adapters/node-http/content-type/form-data/streamSlice.ts +2 -1
  92. package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +1 -1
  93. package/src/adapters/node-http/content-type/json/getPostBody.ts +3 -3
  94. package/src/adapters/node-http/content-type/json/index.ts +1 -1
  95. package/src/adapters/node-http/internals/contentType.ts +3 -3
  96. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +7 -7
  97. package/src/adapters/node-http/types.ts +4 -5
  98. package/src/adapters/standalone.ts +3 -3
  99. package/src/adapters/ws.ts +16 -12
  100. package/src/http.ts +2 -0
  101. package/src/index.ts +30 -14
  102. package/src/observable.ts +1 -0
  103. package/src/rpc.ts +2 -0
  104. package/src/unstableDoNotImportThis.ts +9 -0
  105. package/dist/TRPCError-0b220715.js +0 -42
  106. package/dist/TRPCError-2bb6a7d0.js +0 -37
  107. package/dist/TRPCError-7e8e2a1b.mjs +0 -39
  108. package/dist/batchStreamFormatter-2c1405a1.js +0 -31
  109. package/dist/batchStreamFormatter-93cdcdd4.js +0 -32
  110. package/dist/batchStreamFormatter-fc1ffb26.mjs +0 -30
  111. package/dist/codes-62b059d2.js +0 -48
  112. package/dist/codes-87f6824b.js +0 -48
  113. package/dist/codes-c924c3db.mjs +0 -44
  114. package/dist/config-50853f49.mjs +0 -6
  115. package/dist/config-774dae36.js +0 -8
  116. package/dist/config-e3dd843b.js +0 -11
  117. package/dist/contentType-0447b256.js +0 -58
  118. package/dist/contentType-31223e57.mjs +0 -52
  119. package/dist/contentType-a229790b.js +0 -54
  120. package/dist/core/index.d.ts +0 -9
  121. package/dist/core/index.d.ts.map +0 -1
  122. package/dist/core/initTRPC.d.ts +0 -84
  123. package/dist/core/initTRPC.d.ts.map +0 -1
  124. package/dist/core/internals/config.d.ts +0 -75
  125. package/dist/core/internals/config.d.ts.map +0 -1
  126. package/dist/core/internals/getParseFn.d.ts +0 -4
  127. package/dist/core/internals/getParseFn.d.ts.map +0 -1
  128. package/dist/core/internals/mergeRouters.d.ts +0 -15
  129. package/dist/core/internals/mergeRouters.d.ts.map +0 -1
  130. package/dist/core/internals/mergeWithoutOverrides.d.ts +0 -5
  131. package/dist/core/internals/mergeWithoutOverrides.d.ts.map +0 -1
  132. package/dist/core/internals/omitPrototype.d.ts +0 -6
  133. package/dist/core/internals/omitPrototype.d.ts.map +0 -1
  134. package/dist/core/internals/procedureBuilder.d.ts +0 -99
  135. package/dist/core/internals/procedureBuilder.d.ts.map +0 -1
  136. package/dist/core/internals/utils.d.ts +0 -50
  137. package/dist/core/internals/utils.d.ts.map +0 -1
  138. package/dist/core/middleware.d.ts +0 -106
  139. package/dist/core/middleware.d.ts.map +0 -1
  140. package/dist/core/parser.d.ts +0 -28
  141. package/dist/core/parser.d.ts.map +0 -1
  142. package/dist/core/procedure.d.ts +0 -54
  143. package/dist/core/procedure.d.ts.map +0 -1
  144. package/dist/core/router.d.ts +0 -64
  145. package/dist/core/router.d.ts.map +0 -1
  146. package/dist/core/types.d.ts +0 -24
  147. package/dist/core/types.d.ts.map +0 -1
  148. package/dist/error/TRPCError.d.ts +0 -12
  149. package/dist/error/TRPCError.d.ts.map +0 -1
  150. package/dist/error/formatter.d.ts +0 -33
  151. package/dist/error/formatter.d.ts.map +0 -1
  152. package/dist/error/utils.d.ts +0 -9
  153. package/dist/error/utils.d.ts.map +0 -1
  154. package/dist/getCauseFromUnknown-2d66414a.mjs +0 -33
  155. package/dist/getCauseFromUnknown-ac5f46a2.js +0 -34
  156. package/dist/getCauseFromUnknown-d535264a.js +0 -35
  157. package/dist/http/batchStreamFormatter.d.ts +0 -24
  158. package/dist/http/batchStreamFormatter.d.ts.map +0 -1
  159. package/dist/http/contentType.d.ts +0 -29
  160. package/dist/http/contentType.d.ts.map +0 -1
  161. package/dist/http/getHTTPStatusCode.d.ts +0 -24
  162. package/dist/http/getHTTPStatusCode.d.ts.map +0 -1
  163. package/dist/http/index.d.ts +0 -5
  164. package/dist/http/index.d.ts.map +0 -1
  165. package/dist/http/index.js +0 -22
  166. package/dist/http/index.mjs +0 -10
  167. package/dist/http/internals/types.d.ts +0 -32
  168. package/dist/http/internals/types.d.ts.map +0 -1
  169. package/dist/http/resolveHTTPResponse.d.ts +0 -52
  170. package/dist/http/resolveHTTPResponse.d.ts.map +0 -1
  171. package/dist/http/types.d.ts +0 -46
  172. package/dist/http/types.d.ts.map +0 -1
  173. package/dist/index-8759c3db.js +0 -48
  174. package/dist/index-a496f86b.js +0 -52
  175. package/dist/index-a9e3e610.mjs +0 -49
  176. package/dist/internals/invert.d.ts +0 -12
  177. package/dist/internals/invert.d.ts.map +0 -1
  178. package/dist/internals/types.d.ts +0 -26
  179. package/dist/internals/types.d.ts.map +0 -1
  180. package/dist/observable/index.d.ts +0 -5
  181. package/dist/observable/index.d.ts.map +0 -1
  182. package/dist/observable/index.js +0 -156
  183. package/dist/observable/index.mjs +0 -147
  184. package/dist/observable/internals/identity.d.ts +0 -2
  185. package/dist/observable/internals/identity.d.ts.map +0 -1
  186. package/dist/observable/internals/observableToPromise.d.ts +0 -10
  187. package/dist/observable/internals/observableToPromise.d.ts.map +0 -1
  188. package/dist/observable/internals/pipe.d.ts +0 -4
  189. package/dist/observable/internals/pipe.d.ts.map +0 -1
  190. package/dist/observable/observable.d.ts +0 -5
  191. package/dist/observable/observable.d.ts.map +0 -1
  192. package/dist/observable/operators/index.d.ts +0 -4
  193. package/dist/observable/operators/index.d.ts.map +0 -1
  194. package/dist/observable/operators/map.d.ts +0 -3
  195. package/dist/observable/operators/map.d.ts.map +0 -1
  196. package/dist/observable/operators/share.d.ts +0 -6
  197. package/dist/observable/operators/share.d.ts.map +0 -1
  198. package/dist/observable/operators/tap.d.ts +0 -3
  199. package/dist/observable/operators/tap.d.ts.map +0 -1
  200. package/dist/observable/types.d.ts +0 -29
  201. package/dist/observable/types.d.ts.map +0 -1
  202. package/dist/observable-15588f28.js +0 -83
  203. package/dist/observable-464116ac.js +0 -84
  204. package/dist/observable-ade1bad8.mjs +0 -81
  205. package/dist/parseTRPCMessage-1377f305.js +0 -56
  206. package/dist/parseTRPCMessage-95955211.js +0 -63
  207. package/dist/parseTRPCMessage-a0f17853.mjs +0 -54
  208. package/dist/procedureBuilder-0446e596.js +0 -327
  209. package/dist/procedureBuilder-4b68e226.js +0 -349
  210. package/dist/procedureBuilder-cf58dadb.mjs +0 -340
  211. package/dist/resolveHTTPResponse-3ebaa4f6.mjs +0 -292
  212. package/dist/resolveHTTPResponse-7c84e387.js +0 -294
  213. package/dist/resolveHTTPResponse-fa37ffd7.js +0 -266
  214. package/dist/router-18d63731.js +0 -163
  215. package/dist/router-8dcebd61.js +0 -153
  216. package/dist/router-f9ce121f.mjs +0 -156
  217. package/dist/rpc/codes.d.ts +0 -51
  218. package/dist/rpc/codes.d.ts.map +0 -1
  219. package/dist/rpc/envelopes.d.ts +0 -95
  220. package/dist/rpc/envelopes.d.ts.map +0 -1
  221. package/dist/rpc/index.d.ts +0 -4
  222. package/dist/rpc/index.d.ts.map +0 -1
  223. package/dist/rpc/index.js +0 -12
  224. package/dist/rpc/index.mjs +0 -2
  225. package/dist/rpc/parseTRPCMessage.d.ts +0 -5
  226. package/dist/rpc/parseTRPCMessage.d.ts.map +0 -1
  227. package/dist/shared/createProxy/index.d.ts +0 -19
  228. package/dist/shared/createProxy/index.d.ts.map +0 -1
  229. package/dist/shared/getCauseFromUnknown.d.ts +0 -2
  230. package/dist/shared/getCauseFromUnknown.d.ts.map +0 -1
  231. package/dist/shared/getErrorShape.d.ts +0 -15
  232. package/dist/shared/getErrorShape.d.ts.map +0 -1
  233. package/dist/shared/index.d.ts +0 -8
  234. package/dist/shared/index.d.ts.map +0 -1
  235. package/dist/shared/index.js +0 -16
  236. package/dist/shared/index.mjs +0 -4
  237. package/dist/shared/internal/serialize.d.ts +0 -62
  238. package/dist/shared/internal/serialize.d.ts.map +0 -1
  239. package/dist/shared/jsonify.d.ts +0 -11
  240. package/dist/shared/jsonify.d.ts.map +0 -1
  241. package/dist/shared/transformTRPCResponse.d.ts +0 -9
  242. package/dist/shared/transformTRPCResponse.d.ts.map +0 -1
  243. package/dist/shared/types.d.ts +0 -6
  244. package/dist/shared/types.d.ts.map +0 -1
  245. package/dist/transformTRPCResponse-31040a38.mjs +0 -98
  246. package/dist/transformTRPCResponse-59486a84.js +0 -97
  247. package/dist/transformTRPCResponse-9f3b865f.js +0 -104
  248. package/dist/transformer.d.ts +0 -67
  249. package/dist/transformer.d.ts.map +0 -1
  250. package/dist/types.d.ts +0 -68
  251. package/dist/types.d.ts.map +0 -1
  252. package/dist/unstableInternalsExport.d.ts +0 -12
  253. package/dist/unstableInternalsExport.d.ts.map +0 -1
  254. package/dist/unstableInternalsExport.js +0 -16
  255. package/dist/unstableInternalsExport.mjs +0 -5
  256. package/src/core/index.ts +0 -28
  257. package/src/core/initTRPC.ts +0 -186
  258. package/src/core/internals/config.ts +0 -90
  259. package/src/core/internals/getParseFn.ts +0 -43
  260. package/src/core/internals/mergeRouters.ts +0 -90
  261. package/src/core/internals/mergeWithoutOverrides.ts +0 -19
  262. package/src/core/internals/omitPrototype.ts +0 -9
  263. package/src/core/internals/procedureBuilder.ts +0 -455
  264. package/src/core/internals/utils.ts +0 -79
  265. package/src/core/middleware.ts +0 -236
  266. package/src/core/parser.ts +0 -52
  267. package/src/core/procedure.ts +0 -68
  268. package/src/core/router.ts +0 -241
  269. package/src/core/types.ts +0 -67
  270. package/src/error/TRPCError.ts +0 -52
  271. package/src/error/formatter.ts +0 -54
  272. package/src/error/utils.ts +0 -23
  273. package/src/http/batchStreamFormatter.ts +0 -29
  274. package/src/http/contentType.ts +0 -99
  275. package/src/http/getHTTPStatusCode.ts +0 -61
  276. package/src/http/index.ts +0 -4
  277. package/src/http/internals/types.ts +0 -40
  278. package/src/http/resolveHTTPResponse.ts +0 -458
  279. package/src/http/types.ts +0 -51
  280. package/src/internals/invert.ts +0 -21
  281. package/src/internals/types.ts +0 -27
  282. package/src/observable/index.ts +0 -4
  283. package/src/observable/internals/identity.ts +0 -3
  284. package/src/observable/internals/observableToPromise.ts +0 -49
  285. package/src/observable/internals/pipe.ts +0 -23
  286. package/src/observable/observable.ts +0 -78
  287. package/src/observable/operators/index.ts +0 -3
  288. package/src/observable/operators/map.ts +0 -25
  289. package/src/observable/operators/share.ts +0 -65
  290. package/src/observable/operators/tap.ts +0 -26
  291. package/src/observable/types.ts +0 -80
  292. package/src/rpc/codes.ts +0 -44
  293. package/src/rpc/envelopes.ts +0 -136
  294. package/src/rpc/index.ts +0 -3
  295. package/src/rpc/parseTRPCMessage.ts +0 -84
  296. package/src/shared/createProxy/index.ts +0 -59
  297. package/src/shared/getCauseFromUnknown.ts +0 -31
  298. package/src/shared/getErrorShape.ts +0 -36
  299. package/src/shared/index.ts +0 -11
  300. package/src/shared/internal/serialize.ts +0 -122
  301. package/src/shared/jsonify.ts +0 -22
  302. package/src/shared/transformTRPCResponse.ts +0 -40
  303. package/src/shared/types.ts +0 -9
  304. package/src/transformer.ts +0 -84
  305. package/src/types.ts +0 -104
  306. package/src/unstableInternalsExport.ts +0 -13
  307. package/subscription/index.d.ts +0 -1
  308. package/subscription/index.js +0 -1
  309. package/unstableInternalsExport/index.d.ts +0 -1
  310. package/unstableInternalsExport/index.js +0 -1
@@ -1,294 +0,0 @@
1
- 'use strict';
2
-
3
- var router = require('./router-18d63731.js');
4
- var TRPCError = require('./TRPCError-0b220715.js');
5
- require('./config-774dae36.js');
6
- var transformTRPCResponse = require('./transformTRPCResponse-9f3b865f.js');
7
- var contentType = require('./contentType-a229790b.js');
8
-
9
- const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
10
- GET: 'query',
11
- POST: 'mutation'
12
- };
13
- const fallbackContentTypeHandler = {
14
- getInputs: contentType.getJsonContentTypeInputs
15
- };
16
- function initResponse(initOpts) {
17
- const { ctx , paths , type , responseMeta , untransformedJSON , errors =[] , } = initOpts;
18
- let status = untransformedJSON ? transformTRPCResponse.getHTTPStatusCode(untransformedJSON) : 200;
19
- const headers = {
20
- 'Content-Type': 'application/json'
21
- };
22
- const eagerGeneration = !untransformedJSON;
23
- const data = eagerGeneration ? [] : Array.isArray(untransformedJSON) ? untransformedJSON : [
24
- untransformedJSON
25
- ];
26
- const meta = responseMeta?.({
27
- ctx,
28
- paths,
29
- type,
30
- data,
31
- errors,
32
- eagerGeneration
33
- }) ?? {};
34
- for (const [key, value] of Object.entries(meta.headers ?? {})){
35
- headers[key] = value;
36
- }
37
- if (meta.status) {
38
- status = meta.status;
39
- }
40
- return {
41
- status,
42
- headers
43
- };
44
- }
45
- async function inputToProcedureCall(procedureOpts) {
46
- const { opts , ctx , type , input , path } = procedureOpts;
47
- try {
48
- const data = await router.callProcedure({
49
- procedures: opts.router._def.procedures,
50
- path,
51
- getRawInput: async ()=>input,
52
- ctx,
53
- type
54
- });
55
- return {
56
- result: {
57
- data
58
- }
59
- };
60
- } catch (cause) {
61
- const error = TRPCError.getTRPCErrorFromUnknown(cause);
62
- opts.onError?.({
63
- error,
64
- path,
65
- input,
66
- ctx,
67
- type: type,
68
- req: opts.req
69
- });
70
- return {
71
- error: transformTRPCResponse.getErrorShape({
72
- config: opts.router._def._config,
73
- error,
74
- type,
75
- path,
76
- input,
77
- ctx
78
- })
79
- };
80
- }
81
- }
82
- function caughtErrorToData(cause, errorOpts) {
83
- const { router , req , onError } = errorOpts.opts;
84
- const error = TRPCError.getTRPCErrorFromUnknown(cause);
85
- onError?.({
86
- error,
87
- path: errorOpts.path,
88
- input: errorOpts.input,
89
- ctx: errorOpts.ctx,
90
- type: errorOpts.type,
91
- req
92
- });
93
- const untransformedJSON = {
94
- error: transformTRPCResponse.getErrorShape({
95
- config: router._def._config,
96
- error,
97
- type: errorOpts.type,
98
- path: errorOpts.path,
99
- input: errorOpts.input,
100
- ctx: errorOpts.ctx
101
- })
102
- };
103
- const transformedJSON = transformTRPCResponse.transformTRPCResponse(router._def._config, untransformedJSON);
104
- const body = JSON.stringify(transformedJSON);
105
- return {
106
- error,
107
- untransformedJSON,
108
- body
109
- };
110
- }
111
- // implementation
112
- async function resolveHTTPResponse(opts) {
113
- const { router , req , unstable_onHead , unstable_onChunk } = opts;
114
- if (req.method === 'HEAD') {
115
- // can be used for lambda warmup
116
- const headResponse = {
117
- status: 204
118
- };
119
- unstable_onHead?.(headResponse, false);
120
- unstable_onChunk?.([
121
- -1,
122
- ''
123
- ]);
124
- return headResponse;
125
- }
126
- const contentTypeHandler = opts.contentTypeHandler ?? fallbackContentTypeHandler;
127
- const batchingEnabled = opts.batching?.enabled ?? true;
128
- const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
129
- let ctx = undefined;
130
- let paths;
131
- const isBatchCall = !!req.query.get('batch');
132
- const isStreamCall = isBatchCall && unstable_onHead && unstable_onChunk && req.headers['trpc-batch-mode'] === 'stream';
133
- try {
134
- if (opts.error) {
135
- throw opts.error;
136
- }
137
- if (isBatchCall && !batchingEnabled) {
138
- throw new Error(`Batching is not enabled on the server`);
139
- }
140
- /* istanbul ignore if -- @preserve */ if (type === 'subscription') {
141
- throw new TRPCError.TRPCError({
142
- message: 'Subscriptions should use wsLink',
143
- code: 'METHOD_NOT_SUPPORTED'
144
- });
145
- }
146
- if (type === 'unknown') {
147
- throw new TRPCError.TRPCError({
148
- message: `Unexpected request method ${req.method}`,
149
- code: 'METHOD_NOT_SUPPORTED'
150
- });
151
- }
152
- const inputs = await contentTypeHandler.getInputs({
153
- isBatchCall,
154
- req,
155
- router,
156
- preprocessedBody: opts.preprocessedBody ?? false
157
- });
158
- paths = isBatchCall ? decodeURIComponent(opts.path).split(',') : [
159
- opts.path
160
- ];
161
- const info = {
162
- isBatchCall,
163
- calls: paths.map((path, idx)=>({
164
- path,
165
- type,
166
- input: inputs[idx] ?? undefined
167
- }))
168
- };
169
- ctx = await opts.createContext({
170
- info
171
- });
172
- const promises = paths.map((path, index)=>inputToProcedureCall({
173
- opts,
174
- ctx,
175
- type,
176
- input: inputs[index],
177
- path
178
- }));
179
- if (!isStreamCall) {
180
- /**
181
- * Non-streaming response:
182
- * - await all responses in parallel, blocking on the slowest one
183
- * - create headers with known response body
184
- * - return a complete HTTPResponse
185
- */ const untransformedJSON = await Promise.all(promises);
186
- const errors = untransformedJSON.flatMap((response)=>'error' in response ? [
187
- response.error
188
- ] : []);
189
- const headResponse1 = initResponse({
190
- ctx,
191
- paths,
192
- type,
193
- responseMeta: opts.responseMeta,
194
- untransformedJSON,
195
- errors
196
- });
197
- unstable_onHead?.(headResponse1, false);
198
- // return body stuff
199
- const result = isBatchCall ? untransformedJSON : untransformedJSON[0]; // eslint-disable-line @typescript-eslint/no-non-null-assertion -- `untransformedJSON` should be the length of `paths` which should be at least 1 otherwise there wouldn't be a request at all
200
- const transformedJSON = transformTRPCResponse.transformTRPCResponse(router._def._config, result);
201
- const body = JSON.stringify(transformedJSON);
202
- unstable_onChunk?.([
203
- -1,
204
- body
205
- ]);
206
- return {
207
- status: headResponse1.status,
208
- headers: headResponse1.headers,
209
- body
210
- };
211
- }
212
- /**
213
- * Streaming response:
214
- * - block on none, call `onChunk` as soon as each response is ready
215
- * - create headers with minimal data (cannot know the response body in advance)
216
- * - return void
217
- */ const headResponse2 = initResponse({
218
- ctx,
219
- paths,
220
- type,
221
- responseMeta: opts.responseMeta
222
- });
223
- unstable_onHead(headResponse2, true);
224
- const indexedPromises = new Map(promises.map((promise, index)=>[
225
- index,
226
- promise.then((r)=>[
227
- index,
228
- r
229
- ])
230
- ]));
231
- for (const _ of paths){
232
- const [index, untransformedJSON1] = await Promise.race(indexedPromises.values());
233
- indexedPromises.delete(index);
234
- try {
235
- const transformedJSON1 = transformTRPCResponse.transformTRPCResponse(router._def._config, untransformedJSON1);
236
- const body1 = JSON.stringify(transformedJSON1);
237
- unstable_onChunk([
238
- index,
239
- body1
240
- ]);
241
- } catch (cause) {
242
- const path = paths[index];
243
- const input = inputs[index];
244
- const { body: body2 } = caughtErrorToData(cause, {
245
- opts,
246
- ctx,
247
- type,
248
- path,
249
- input
250
- });
251
- unstable_onChunk([
252
- index,
253
- body2
254
- ]);
255
- }
256
- }
257
- return;
258
- } catch (cause1) {
259
- // we get here if
260
- // - batching is called when it's not enabled
261
- // - `createContext()` throws
262
- // - `router._def._config.transformer.output.serialize()` throws
263
- // - post body is too large
264
- // - input deserialization fails
265
- // - `errorFormatter` return value is malformed
266
- const { error , untransformedJSON: untransformedJSON2 , body: body3 } = caughtErrorToData(cause1, {
267
- opts,
268
- ctx,
269
- type
270
- });
271
- const headResponse3 = initResponse({
272
- ctx,
273
- paths,
274
- type,
275
- responseMeta: opts.responseMeta,
276
- untransformedJSON: untransformedJSON2,
277
- errors: [
278
- error
279
- ]
280
- });
281
- unstable_onHead?.(headResponse3, false);
282
- unstable_onChunk?.([
283
- -1,
284
- body3
285
- ]);
286
- return {
287
- status: headResponse3.status,
288
- headers: headResponse3.headers,
289
- body: body3
290
- };
291
- }
292
- }
293
-
294
- exports.resolveHTTPResponse = resolveHTTPResponse;
@@ -1,266 +0,0 @@
1
- import { e as callProcedure } from './router-8dcebd61.js';
2
- import { T as TRPCError, g as getTRPCErrorFromUnknown } from './TRPCError-2bb6a7d0.js';
3
- import './config-e3dd843b.js';
4
- import { t as transformTRPCResponse, g as getHTTPStatusCode, a as getErrorShape } from './transformTRPCResponse-59486a84.js';
5
- import { g as getJsonContentTypeInputs } from './contentType-0447b256.js';
6
-
7
- const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
8
- GET: 'query',
9
- POST: 'mutation',
10
- };
11
- const fallbackContentTypeHandler = {
12
- getInputs: getJsonContentTypeInputs,
13
- };
14
- function initResponse(initOpts) {
15
- const { ctx, paths, type, responseMeta, untransformedJSON, errors = [], } = initOpts;
16
- let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
17
- const headers = {
18
- 'Content-Type': 'application/json',
19
- };
20
- const eagerGeneration = !untransformedJSON;
21
- const data = eagerGeneration
22
- ? []
23
- : Array.isArray(untransformedJSON)
24
- ? untransformedJSON
25
- : [untransformedJSON];
26
- const meta = responseMeta?.({
27
- ctx,
28
- paths,
29
- type,
30
- data,
31
- errors,
32
- eagerGeneration,
33
- }) ?? {};
34
- for (const [key, value] of Object.entries(meta.headers ?? {})) {
35
- headers[key] = value;
36
- }
37
- if (meta.status) {
38
- status = meta.status;
39
- }
40
- return {
41
- status,
42
- headers,
43
- };
44
- }
45
- async function inputToProcedureCall(procedureOpts) {
46
- const { opts, ctx, type, input, path } = procedureOpts;
47
- try {
48
- const data = await callProcedure({
49
- procedures: opts.router._def.procedures,
50
- path,
51
- getRawInput: async () => input,
52
- ctx,
53
- type,
54
- });
55
- return {
56
- result: {
57
- data,
58
- },
59
- };
60
- }
61
- catch (cause) {
62
- const error = getTRPCErrorFromUnknown(cause);
63
- opts.onError?.({ error, path, input, ctx, type: type, req: opts.req });
64
- return {
65
- error: getErrorShape({
66
- config: opts.router._def._config,
67
- error,
68
- type,
69
- path,
70
- input,
71
- ctx,
72
- }),
73
- };
74
- }
75
- }
76
- function caughtErrorToData(cause, errorOpts) {
77
- const { router, req, onError } = errorOpts.opts;
78
- const error = getTRPCErrorFromUnknown(cause);
79
- onError?.({
80
- error,
81
- path: errorOpts.path,
82
- input: errorOpts.input,
83
- ctx: errorOpts.ctx,
84
- type: errorOpts.type,
85
- req,
86
- });
87
- const untransformedJSON = {
88
- error: getErrorShape({
89
- config: router._def._config,
90
- error,
91
- type: errorOpts.type,
92
- path: errorOpts.path,
93
- input: errorOpts.input,
94
- ctx: errorOpts.ctx,
95
- }),
96
- };
97
- const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
98
- const body = JSON.stringify(transformedJSON);
99
- return {
100
- error,
101
- untransformedJSON,
102
- body,
103
- };
104
- }
105
- // implementation
106
- async function resolveHTTPResponse(opts) {
107
- const { router, req, unstable_onHead, unstable_onChunk } = opts;
108
- if (req.method === 'HEAD') {
109
- // can be used for lambda warmup
110
- const headResponse = {
111
- status: 204,
112
- };
113
- unstable_onHead?.(headResponse, false);
114
- unstable_onChunk?.([-1, '']);
115
- return headResponse;
116
- }
117
- const contentTypeHandler = opts.contentTypeHandler ?? fallbackContentTypeHandler;
118
- const batchingEnabled = opts.batching?.enabled ?? true;
119
- const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
120
- let ctx = undefined;
121
- let paths;
122
- const isBatchCall = !!req.query.get('batch');
123
- const isStreamCall = isBatchCall &&
124
- unstable_onHead &&
125
- unstable_onChunk &&
126
- req.headers['trpc-batch-mode'] === 'stream';
127
- try {
128
- if (opts.error) {
129
- throw opts.error;
130
- }
131
- if (isBatchCall && !batchingEnabled) {
132
- throw new Error(`Batching is not enabled on the server`);
133
- }
134
- /* istanbul ignore if -- @preserve */
135
- if (type === 'subscription') {
136
- throw new TRPCError({
137
- message: 'Subscriptions should use wsLink',
138
- code: 'METHOD_NOT_SUPPORTED',
139
- });
140
- }
141
- if (type === 'unknown') {
142
- throw new TRPCError({
143
- message: `Unexpected request method ${req.method}`,
144
- code: 'METHOD_NOT_SUPPORTED',
145
- });
146
- }
147
- const inputs = await contentTypeHandler.getInputs({
148
- isBatchCall,
149
- req,
150
- router,
151
- preprocessedBody: opts.preprocessedBody ?? false,
152
- });
153
- paths = isBatchCall
154
- ? decodeURIComponent(opts.path).split(',')
155
- : [opts.path];
156
- const info = {
157
- isBatchCall,
158
- calls: paths.map((path, idx) => ({
159
- path,
160
- type,
161
- input: inputs[idx] ?? undefined,
162
- })),
163
- };
164
- ctx = await opts.createContext({ info });
165
- const promises = paths.map((path, index) => inputToProcedureCall({ opts, ctx, type, input: inputs[index], path }));
166
- if (!isStreamCall) {
167
- /**
168
- * Non-streaming response:
169
- * - await all responses in parallel, blocking on the slowest one
170
- * - create headers with known response body
171
- * - return a complete HTTPResponse
172
- */
173
- const untransformedJSON = await Promise.all(promises);
174
- const errors = untransformedJSON.flatMap((response) => 'error' in response ? [response.error] : []);
175
- const headResponse = initResponse({
176
- ctx,
177
- paths,
178
- type,
179
- responseMeta: opts.responseMeta,
180
- untransformedJSON,
181
- errors,
182
- });
183
- unstable_onHead?.(headResponse, false);
184
- // return body stuff
185
- const result = isBatchCall ? untransformedJSON : untransformedJSON[0]; // eslint-disable-line @typescript-eslint/no-non-null-assertion -- `untransformedJSON` should be the length of `paths` which should be at least 1 otherwise there wouldn't be a request at all
186
- const transformedJSON = transformTRPCResponse(router._def._config, result);
187
- const body = JSON.stringify(transformedJSON);
188
- unstable_onChunk?.([-1, body]);
189
- return {
190
- status: headResponse.status,
191
- headers: headResponse.headers,
192
- body,
193
- };
194
- }
195
- /**
196
- * Streaming response:
197
- * - block on none, call `onChunk` as soon as each response is ready
198
- * - create headers with minimal data (cannot know the response body in advance)
199
- * - return void
200
- */
201
- const headResponse = initResponse({
202
- ctx,
203
- paths,
204
- type,
205
- responseMeta: opts.responseMeta,
206
- });
207
- unstable_onHead(headResponse, true);
208
- const indexedPromises = new Map(promises.map((promise, index) => [
209
- index,
210
- promise.then((r) => [index, r]),
211
- ]));
212
- for (const _ of paths) {
213
- const [index, untransformedJSON] = await Promise.race(indexedPromises.values());
214
- indexedPromises.delete(index);
215
- try {
216
- const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
217
- const body = JSON.stringify(transformedJSON);
218
- unstable_onChunk([index, body]);
219
- }
220
- catch (cause) {
221
- const path = paths[index];
222
- const input = inputs[index];
223
- const { body } = caughtErrorToData(cause, {
224
- opts,
225
- ctx,
226
- type,
227
- path,
228
- input,
229
- });
230
- unstable_onChunk([index, body]);
231
- }
232
- }
233
- return;
234
- }
235
- catch (cause) {
236
- // we get here if
237
- // - batching is called when it's not enabled
238
- // - `createContext()` throws
239
- // - `router._def._config.transformer.output.serialize()` throws
240
- // - post body is too large
241
- // - input deserialization fails
242
- // - `errorFormatter` return value is malformed
243
- const { error, untransformedJSON, body } = caughtErrorToData(cause, {
244
- opts,
245
- ctx,
246
- type,
247
- });
248
- const headResponse = initResponse({
249
- ctx,
250
- paths,
251
- type,
252
- responseMeta: opts.responseMeta,
253
- untransformedJSON,
254
- errors: [error],
255
- });
256
- unstable_onHead?.(headResponse, false);
257
- unstable_onChunk?.([-1, body]);
258
- return {
259
- status: headResponse.status,
260
- headers: headResponse.headers,
261
- body,
262
- };
263
- }
264
- }
265
-
266
- export { resolveHTTPResponse as r };