@trpc/server 11.0.0-alpha-tmp-export-from-main.211 → 11.0.0-alpha-tmp-export-from-main-nuke-core.229

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 (234) hide show
  1. package/dist/@trpc/server/http.d.ts +7 -0
  2. package/dist/@trpc/server/http.d.ts.map +1 -0
  3. package/dist/{@trpc-server.d.ts → @trpc/server/index.d.ts} +4 -4
  4. package/dist/@trpc/server/index.d.ts.map +1 -0
  5. package/dist/@trpc/server/rpc.d.ts +3 -0
  6. package/dist/@trpc/server/rpc.d.ts.map +1 -0
  7. package/dist/TRPCError-54f51dec.mjs +63 -0
  8. package/dist/TRPCError-6f02e18d.js +67 -0
  9. package/dist/TRPCError-792b0901.js +61 -0
  10. package/dist/adapters/aws-lambda/index.d.ts +1 -2
  11. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  12. package/dist/adapters/aws-lambda/index.js +10 -7
  13. package/dist/adapters/aws-lambda/index.mjs +6 -3
  14. package/dist/adapters/aws-lambda/utils.d.ts +2 -3
  15. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
  16. package/dist/adapters/express.d.ts +1 -2
  17. package/dist/adapters/express.d.ts.map +1 -1
  18. package/dist/adapters/express.js +8 -2
  19. package/dist/adapters/express.mjs +8 -2
  20. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +2 -2
  21. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  22. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -2
  23. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  24. package/dist/adapters/fastify/index.js +11 -4
  25. package/dist/adapters/fastify/index.mjs +9 -2
  26. package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -2
  27. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  28. package/dist/adapters/fetch/index.js +9 -4
  29. package/dist/adapters/fetch/index.mjs +7 -2
  30. package/dist/adapters/fetch/types.d.ts +2 -3
  31. package/dist/adapters/fetch/types.d.ts.map +1 -1
  32. package/dist/adapters/next.d.ts +1 -2
  33. package/dist/adapters/next.d.ts.map +1 -1
  34. package/dist/adapters/next.js +10 -5
  35. package/dist/adapters/next.mjs +8 -3
  36. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +1 -1
  37. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
  38. package/dist/adapters/node-http/content-type/json/index.js +5 -3
  39. package/dist/adapters/node-http/content-type/json/index.mjs +3 -1
  40. package/dist/adapters/node-http/index.js +8 -2
  41. package/dist/adapters/node-http/index.mjs +8 -2
  42. package/dist/adapters/node-http/internals/contentType.d.ts +2 -2
  43. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -2
  44. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  45. package/dist/adapters/node-http/types.d.ts +3 -4
  46. package/dist/adapters/node-http/types.d.ts.map +1 -1
  47. package/dist/adapters/standalone.d.ts +1 -2
  48. package/dist/adapters/standalone.d.ts.map +1 -1
  49. package/dist/adapters/standalone.js +9 -2
  50. package/dist/adapters/standalone.mjs +9 -2
  51. package/dist/adapters/ws.d.ts +3 -2
  52. package/dist/adapters/ws.d.ts.map +1 -1
  53. package/dist/adapters/ws.js +31 -26
  54. package/dist/adapters/ws.mjs +6 -1
  55. package/dist/batchStreamFormatter-1971db27.js +32 -0
  56. package/dist/batchStreamFormatter-378277dc.mjs +30 -0
  57. package/dist/batchStreamFormatter-4375dc8b.js +31 -0
  58. package/dist/bundle-analysis.json +757 -0
  59. package/dist/contentType-19471015.js +59 -0
  60. package/dist/contentType-2e62862d.mjs +52 -0
  61. package/dist/contentType-4cd0be82.js +54 -0
  62. package/dist/createProxy-562377d3.js +127 -0
  63. package/dist/createProxy-72161f42.js +116 -0
  64. package/dist/createProxy-f7ba8b4d.mjs +121 -0
  65. package/dist/http.d.ts +1 -6
  66. package/dist/http.d.ts.map +1 -1
  67. package/dist/http.js +12 -21
  68. package/dist/http.mjs +7 -1
  69. package/dist/index.d.ts +1 -1
  70. package/dist/index.js +15 -41
  71. package/dist/index.mjs +5 -1
  72. package/dist/initTRPC-12d9099b.js +371 -0
  73. package/dist/initTRPC-69a60dfb.js +384 -0
  74. package/dist/initTRPC-dcdfb896.mjs +376 -0
  75. package/dist/{nodeHTTPRequestHandler-55f05150.js → nodeHTTPRequestHandler-71a3f758.js} +5 -4
  76. package/dist/{nodeHTTPRequestHandler-2d5c8791.mjs → nodeHTTPRequestHandler-c5d1d163.mjs} +3 -2
  77. package/dist/{nodeHTTPRequestHandler-7691fc79.js → nodeHTTPRequestHandler-f60f0b99.js} +3 -1
  78. package/dist/observable/index.d.ts +5 -0
  79. package/dist/observable/index.d.ts.map +1 -0
  80. package/dist/observable/index.js +110 -0
  81. package/dist/observable/index.mjs +101 -0
  82. package/dist/observable/observable.d.ts +13 -0
  83. package/dist/observable/observable.d.ts.map +1 -0
  84. package/dist/observable/operators.d.ts +8 -0
  85. package/dist/observable/operators.d.ts.map +1 -0
  86. package/dist/observable/types.d.ts +26 -0
  87. package/dist/observable/types.d.ts.map +1 -0
  88. package/dist/observable-25f4098f.js +126 -0
  89. package/dist/observable-bf75028f.mjs +122 -0
  90. package/dist/observable-e837fe43.js +126 -0
  91. package/dist/parseTRPCMessage-aeb00dbd.js +67 -0
  92. package/dist/parseTRPCMessage-ddbcc719.mjs +62 -0
  93. package/dist/parseTRPCMessage-ee50b281.js +65 -0
  94. package/dist/resolveHTTPResponse-104460bf.js +293 -0
  95. package/dist/resolveHTTPResponse-6a85f0a7.js +265 -0
  96. package/dist/resolveHTTPResponse-eb29e5cf.mjs +291 -0
  97. package/dist/rootConfig-52aed7d7.js +88 -0
  98. package/dist/rootConfig-7d3e1fc8.mjs +76 -0
  99. package/dist/rootConfig-ccd626d6.js +83 -0
  100. package/dist/router-5a3e1181.mjs +252 -0
  101. package/dist/router-5e93cd14.js +259 -0
  102. package/dist/router-d9e22315.js +262 -0
  103. package/dist/rpc.d.ts +1 -2
  104. package/dist/rpc.d.ts.map +1 -1
  105. package/dist/rpc.js +5 -13
  106. package/dist/rpc.mjs +2 -1
  107. package/dist/shared.d.ts +1 -1
  108. package/dist/shared.js +4 -9
  109. package/dist/shared.mjs +2 -1
  110. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts +6 -0
  111. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts.map +1 -0
  112. package/dist/unstable-core-do-not-import/createProxy.d.ts +19 -0
  113. package/dist/unstable-core-do-not-import/createProxy.d.ts.map +1 -0
  114. package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +13 -0
  115. package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +1 -0
  116. package/dist/unstable-core-do-not-import/error/formatter.d.ts +33 -0
  117. package/dist/unstable-core-do-not-import/error/formatter.d.ts.map +1 -0
  118. package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts +15 -0
  119. package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts.map +1 -0
  120. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +24 -0
  121. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +1 -0
  122. package/dist/unstable-core-do-not-import/http/contentType.d.ts +29 -0
  123. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -0
  124. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +5 -0
  125. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -0
  126. package/dist/unstable-core-do-not-import/http/index.d.ts +7 -0
  127. package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -0
  128. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +51 -0
  129. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -0
  130. package/dist/unstable-core-do-not-import/http/types.d.ts +96 -0
  131. package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -0
  132. package/dist/unstable-core-do-not-import/index.d.ts +37 -0
  133. package/dist/unstable-core-do-not-import/index.d.ts.map +1 -0
  134. package/dist/unstable-core-do-not-import/index.js +43 -0
  135. package/dist/unstable-core-do-not-import/index.mjs +9 -0
  136. package/dist/unstable-core-do-not-import/initTRPC.d.ts +96 -0
  137. package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -0
  138. package/dist/unstable-core-do-not-import/middleware.d.ts +105 -0
  139. package/dist/unstable-core-do-not-import/middleware.d.ts.map +1 -0
  140. package/dist/unstable-core-do-not-import/parser.d.ts +30 -0
  141. package/dist/unstable-core-do-not-import/parser.d.ts.map +1 -0
  142. package/dist/unstable-core-do-not-import/procedure.d.ts +72 -0
  143. package/dist/unstable-core-do-not-import/procedure.d.ts.map +1 -0
  144. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +101 -0
  145. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -0
  146. package/dist/unstable-core-do-not-import/rootConfig.d.ts +75 -0
  147. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -0
  148. package/dist/unstable-core-do-not-import/router.d.ts +85 -0
  149. package/dist/unstable-core-do-not-import/router.d.ts.map +1 -0
  150. package/dist/unstable-core-do-not-import/rpc/codes.d.ts +51 -0
  151. package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +1 -0
  152. package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +97 -0
  153. package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +1 -0
  154. package/dist/unstable-core-do-not-import/rpc/index.d.ts +5 -0
  155. package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +1 -0
  156. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts +5 -0
  157. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts.map +1 -0
  158. package/dist/unstable-core-do-not-import/serialize.d.ts +62 -0
  159. package/dist/unstable-core-do-not-import/serialize.d.ts.map +1 -0
  160. package/dist/unstable-core-do-not-import/transformer.d.ts +107 -0
  161. package/dist/unstable-core-do-not-import/transformer.d.ts.map +1 -0
  162. package/dist/unstable-core-do-not-import/types.d.ts +98 -0
  163. package/dist/unstable-core-do-not-import/types.d.ts.map +1 -0
  164. package/dist/unstable-core-do-not-import/utils.d.ts +27 -0
  165. package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -0
  166. package/package.json +53 -125
  167. package/src/@trpc/server/http.ts +26 -0
  168. package/src/{@trpc-server.ts → @trpc/server/index.ts} +3 -3
  169. package/src/@trpc/server/rpc.ts +26 -0
  170. package/src/adapters/aws-lambda/index.ts +4 -5
  171. package/src/adapters/aws-lambda/utils.ts +3 -4
  172. package/src/adapters/express.ts +1 -3
  173. package/src/adapters/fastify/fastifyRequestHandler.ts +6 -4
  174. package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -2
  175. package/src/adapters/fetch/fetchRequestHandler.ts +6 -4
  176. package/src/adapters/fetch/types.ts +2 -3
  177. package/src/adapters/next.ts +2 -3
  178. package/src/adapters/node-http/content-type/form-data/index.ts +1 -1
  179. package/src/adapters/node-http/content-type/json/getPostBody.ts +2 -2
  180. package/src/adapters/node-http/content-type/json/index.ts +1 -1
  181. package/src/adapters/node-http/internals/contentType.ts +2 -2
  182. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +6 -4
  183. package/src/adapters/node-http/types.ts +4 -5
  184. package/src/adapters/standalone.ts +2 -2
  185. package/src/adapters/ws.ts +14 -13
  186. package/src/http.ts +1 -26
  187. package/src/index.ts +1 -1
  188. package/src/observable/index.ts +10 -0
  189. package/src/observable/observable.ts +158 -0
  190. package/src/observable/operators.ts +119 -0
  191. package/src/observable/types.ts +76 -0
  192. package/src/rpc.ts +1 -26
  193. package/src/shared.ts +1 -1
  194. package/src/unstable-core-do-not-import/TRPCInferrable.ts +9 -0
  195. package/src/unstable-core-do-not-import/createProxy.ts +59 -0
  196. package/src/unstable-core-do-not-import/error/TRPCError.ts +82 -0
  197. package/src/unstable-core-do-not-import/error/formatter.ts +51 -0
  198. package/src/unstable-core-do-not-import/error/getErrorShape.ts +36 -0
  199. package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +29 -0
  200. package/src/unstable-core-do-not-import/http/contentType.ts +99 -0
  201. package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +57 -0
  202. package/src/unstable-core-do-not-import/http/index.ts +23 -0
  203. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +458 -0
  204. package/src/unstable-core-do-not-import/http/types.ts +111 -0
  205. package/src/unstable-core-do-not-import/index.ts +110 -0
  206. package/src/unstable-core-do-not-import/initTRPC.ts +206 -0
  207. package/src/unstable-core-do-not-import/middleware.ts +233 -0
  208. package/src/unstable-core-do-not-import/parser.ts +94 -0
  209. package/src/unstable-core-do-not-import/procedure.ts +108 -0
  210. package/src/unstable-core-do-not-import/procedureBuilder.ts +458 -0
  211. package/src/unstable-core-do-not-import/rootConfig.ts +90 -0
  212. package/src/unstable-core-do-not-import/router.ts +370 -0
  213. package/src/unstable-core-do-not-import/rpc/codes.ts +44 -0
  214. package/src/unstable-core-do-not-import/rpc/envelopes.ts +136 -0
  215. package/src/unstable-core-do-not-import/rpc/index.ts +21 -0
  216. package/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts +85 -0
  217. package/src/unstable-core-do-not-import/serialize.ts +122 -0
  218. package/src/unstable-core-do-not-import/transformer.ts +202 -0
  219. package/src/unstable-core-do-not-import/types.ts +151 -0
  220. package/src/unstable-core-do-not-import/utils.ts +59 -0
  221. package/unstable-core-do-not-import/index.d.ts +1 -0
  222. package/unstable-core-do-not-import/index.js +1 -0
  223. package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts +0 -9
  224. package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts.map +0 -1
  225. package/dist/@trpc-server/http.d.ts +0 -2
  226. package/dist/@trpc-server/http.d.ts.map +0 -1
  227. package/dist/@trpc-server.d.ts.map +0 -1
  228. package/dist/observable.d.ts +0 -3
  229. package/dist/observable.d.ts.map +0 -1
  230. package/dist/observable.js +0 -32
  231. package/dist/observable.mjs +0 -1
  232. package/src/@trpc-core-unstable-do-not-import-this-please.ts +0 -9
  233. package/src/@trpc-server/http.ts +0 -2
  234. package/src/observable.ts +0 -16
@@ -0,0 +1,293 @@
1
+ 'use strict';
2
+
3
+ var createProxy = require('./createProxy-562377d3.js');
4
+ var TRPCError = require('./TRPCError-6f02e18d.js');
5
+ var router = require('./router-d9e22315.js');
6
+ var contentType = require('./contentType-4cd0be82.js');
7
+
8
+ const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
9
+ GET: 'query',
10
+ POST: 'mutation'
11
+ };
12
+ const fallbackContentTypeHandler = {
13
+ getInputs: contentType.getJsonContentTypeInputs
14
+ };
15
+ function initResponse(initOpts) {
16
+ const { ctx , paths , type , responseMeta , untransformedJSON , errors =[] , } = initOpts;
17
+ let status = untransformedJSON ? createProxy.getHTTPStatusCode(untransformedJSON) : 200;
18
+ const headers = {
19
+ 'Content-Type': 'application/json'
20
+ };
21
+ const eagerGeneration = !untransformedJSON;
22
+ const data = eagerGeneration ? [] : Array.isArray(untransformedJSON) ? untransformedJSON : [
23
+ untransformedJSON
24
+ ];
25
+ const meta = responseMeta?.({
26
+ ctx,
27
+ paths,
28
+ type,
29
+ data,
30
+ errors,
31
+ eagerGeneration
32
+ }) ?? {};
33
+ for (const [key, value] of Object.entries(meta.headers ?? {})){
34
+ headers[key] = value;
35
+ }
36
+ if (meta.status) {
37
+ status = meta.status;
38
+ }
39
+ return {
40
+ status,
41
+ headers
42
+ };
43
+ }
44
+ async function inputToProcedureCall(procedureOpts) {
45
+ const { opts , ctx , type , input , path } = procedureOpts;
46
+ try {
47
+ const data = await router.callProcedure({
48
+ procedures: opts.router._def.procedures,
49
+ path,
50
+ getRawInput: async ()=>input,
51
+ ctx,
52
+ type
53
+ });
54
+ return {
55
+ result: {
56
+ data
57
+ }
58
+ };
59
+ } catch (cause) {
60
+ const error = TRPCError.getTRPCErrorFromUnknown(cause);
61
+ opts.onError?.({
62
+ error,
63
+ path,
64
+ input,
65
+ ctx,
66
+ type: type,
67
+ req: opts.req
68
+ });
69
+ return {
70
+ error: createProxy.getErrorShape({
71
+ config: opts.router._def._config,
72
+ error,
73
+ type,
74
+ path,
75
+ input,
76
+ ctx
77
+ })
78
+ };
79
+ }
80
+ }
81
+ function caughtErrorToData(cause, errorOpts) {
82
+ const { router: router$1 , req , onError } = errorOpts.opts;
83
+ const error = TRPCError.getTRPCErrorFromUnknown(cause);
84
+ onError?.({
85
+ error,
86
+ path: errorOpts.path,
87
+ input: errorOpts.input,
88
+ ctx: errorOpts.ctx,
89
+ type: errorOpts.type,
90
+ req
91
+ });
92
+ const untransformedJSON = {
93
+ error: createProxy.getErrorShape({
94
+ config: router$1._def._config,
95
+ error,
96
+ type: errorOpts.type,
97
+ path: errorOpts.path,
98
+ input: errorOpts.input,
99
+ ctx: errorOpts.ctx
100
+ })
101
+ };
102
+ const transformedJSON = router.transformTRPCResponse(router$1._def._config, untransformedJSON);
103
+ const body = JSON.stringify(transformedJSON);
104
+ return {
105
+ error,
106
+ untransformedJSON,
107
+ body
108
+ };
109
+ }
110
+ // implementation
111
+ async function resolveHTTPResponse(opts) {
112
+ const { router: router$1 , req , unstable_onHead , unstable_onChunk } = opts;
113
+ if (req.method === 'HEAD') {
114
+ // can be used for lambda warmup
115
+ const headResponse = {
116
+ status: 204
117
+ };
118
+ unstable_onHead?.(headResponse, false);
119
+ unstable_onChunk?.([
120
+ -1,
121
+ ''
122
+ ]);
123
+ return headResponse;
124
+ }
125
+ const contentTypeHandler = opts.contentTypeHandler ?? fallbackContentTypeHandler;
126
+ const batchingEnabled = opts.batching?.enabled ?? true;
127
+ const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
128
+ let ctx = undefined;
129
+ let paths;
130
+ const isBatchCall = !!req.query.get('batch');
131
+ const isStreamCall = isBatchCall && unstable_onHead && unstable_onChunk && req.headers['trpc-batch-mode'] === 'stream';
132
+ try {
133
+ if (opts.error) {
134
+ throw opts.error;
135
+ }
136
+ if (isBatchCall && !batchingEnabled) {
137
+ throw new Error(`Batching is not enabled on the server`);
138
+ }
139
+ /* istanbul ignore if -- @preserve */ if (type === 'subscription') {
140
+ throw new TRPCError.TRPCError({
141
+ message: 'Subscriptions should use wsLink',
142
+ code: 'METHOD_NOT_SUPPORTED'
143
+ });
144
+ }
145
+ if (type === 'unknown') {
146
+ throw new TRPCError.TRPCError({
147
+ message: `Unexpected request method ${req.method}`,
148
+ code: 'METHOD_NOT_SUPPORTED'
149
+ });
150
+ }
151
+ const inputs = await contentTypeHandler.getInputs({
152
+ isBatchCall,
153
+ req,
154
+ router: router$1,
155
+ preprocessedBody: opts.preprocessedBody ?? false
156
+ });
157
+ paths = isBatchCall ? decodeURIComponent(opts.path).split(',') : [
158
+ opts.path
159
+ ];
160
+ const info = {
161
+ isBatchCall,
162
+ calls: paths.map((path, idx)=>({
163
+ path,
164
+ type,
165
+ input: inputs[idx] ?? undefined
166
+ }))
167
+ };
168
+ ctx = await opts.createContext({
169
+ info
170
+ });
171
+ const promises = paths.map((path, index)=>inputToProcedureCall({
172
+ opts,
173
+ ctx,
174
+ type,
175
+ input: inputs[index],
176
+ path
177
+ }));
178
+ if (!isStreamCall) {
179
+ /**
180
+ * Non-streaming response:
181
+ * - await all responses in parallel, blocking on the slowest one
182
+ * - create headers with known response body
183
+ * - return a complete HTTPResponse
184
+ */ const untransformedJSON = await Promise.all(promises);
185
+ const errors = untransformedJSON.flatMap((response)=>'error' in response ? [
186
+ response.error
187
+ ] : []);
188
+ const headResponse1 = initResponse({
189
+ ctx,
190
+ paths,
191
+ type,
192
+ responseMeta: opts.responseMeta,
193
+ untransformedJSON,
194
+ errors
195
+ });
196
+ unstable_onHead?.(headResponse1, false);
197
+ // return body stuff
198
+ 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
199
+ const transformedJSON = router.transformTRPCResponse(router$1._def._config, result);
200
+ const body = JSON.stringify(transformedJSON);
201
+ unstable_onChunk?.([
202
+ -1,
203
+ body
204
+ ]);
205
+ return {
206
+ status: headResponse1.status,
207
+ headers: headResponse1.headers,
208
+ body
209
+ };
210
+ }
211
+ /**
212
+ * Streaming response:
213
+ * - block on none, call `onChunk` as soon as each response is ready
214
+ * - create headers with minimal data (cannot know the response body in advance)
215
+ * - return void
216
+ */ const headResponse2 = initResponse({
217
+ ctx,
218
+ paths,
219
+ type,
220
+ responseMeta: opts.responseMeta
221
+ });
222
+ unstable_onHead(headResponse2, true);
223
+ const indexedPromises = new Map(promises.map((promise, index)=>[
224
+ index,
225
+ promise.then((r)=>[
226
+ index,
227
+ r
228
+ ])
229
+ ]));
230
+ for (const _ of paths){
231
+ const [index, untransformedJSON1] = await Promise.race(indexedPromises.values());
232
+ indexedPromises.delete(index);
233
+ try {
234
+ const transformedJSON1 = router.transformTRPCResponse(router$1._def._config, untransformedJSON1);
235
+ const body1 = JSON.stringify(transformedJSON1);
236
+ unstable_onChunk([
237
+ index,
238
+ body1
239
+ ]);
240
+ } catch (cause) {
241
+ const path = paths[index];
242
+ const input = inputs[index];
243
+ const { body: body2 } = caughtErrorToData(cause, {
244
+ opts,
245
+ ctx,
246
+ type,
247
+ path,
248
+ input
249
+ });
250
+ unstable_onChunk([
251
+ index,
252
+ body2
253
+ ]);
254
+ }
255
+ }
256
+ return;
257
+ } catch (cause1) {
258
+ // we get here if
259
+ // - batching is called when it's not enabled
260
+ // - `createContext()` throws
261
+ // - `router._def._config.transformer.output.serialize()` throws
262
+ // - post body is too large
263
+ // - input deserialization fails
264
+ // - `errorFormatter` return value is malformed
265
+ const { error , untransformedJSON: untransformedJSON2 , body: body3 } = caughtErrorToData(cause1, {
266
+ opts,
267
+ ctx,
268
+ type
269
+ });
270
+ const headResponse3 = initResponse({
271
+ ctx,
272
+ paths,
273
+ type,
274
+ responseMeta: opts.responseMeta,
275
+ untransformedJSON: untransformedJSON2,
276
+ errors: [
277
+ error
278
+ ]
279
+ });
280
+ unstable_onHead?.(headResponse3, false);
281
+ unstable_onChunk?.([
282
+ -1,
283
+ body3
284
+ ]);
285
+ return {
286
+ status: headResponse3.status,
287
+ headers: headResponse3.headers,
288
+ body: body3
289
+ };
290
+ }
291
+ }
292
+
293
+ exports.resolveHTTPResponse = resolveHTTPResponse;
@@ -0,0 +1,265 @@
1
+ import { g as getHTTPStatusCode, a as getErrorShape } from './createProxy-72161f42.js';
2
+ import { T as TRPCError, g as getTRPCErrorFromUnknown } from './TRPCError-792b0901.js';
3
+ import { t as transformTRPCResponse, c as callProcedure } from './router-5e93cd14.js';
4
+ import { g as getJsonContentTypeInputs } from './contentType-19471015.js';
5
+
6
+ const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
7
+ GET: 'query',
8
+ POST: 'mutation',
9
+ };
10
+ const fallbackContentTypeHandler = {
11
+ getInputs: getJsonContentTypeInputs,
12
+ };
13
+ function initResponse(initOpts) {
14
+ const { ctx, paths, type, responseMeta, untransformedJSON, errors = [], } = initOpts;
15
+ let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
16
+ const headers = {
17
+ 'Content-Type': 'application/json',
18
+ };
19
+ const eagerGeneration = !untransformedJSON;
20
+ const data = eagerGeneration
21
+ ? []
22
+ : Array.isArray(untransformedJSON)
23
+ ? untransformedJSON
24
+ : [untransformedJSON];
25
+ const meta = responseMeta?.({
26
+ ctx,
27
+ paths,
28
+ type,
29
+ data,
30
+ errors,
31
+ eagerGeneration,
32
+ }) ?? {};
33
+ for (const [key, value] of Object.entries(meta.headers ?? {})) {
34
+ headers[key] = value;
35
+ }
36
+ if (meta.status) {
37
+ status = meta.status;
38
+ }
39
+ return {
40
+ status,
41
+ headers,
42
+ };
43
+ }
44
+ async function inputToProcedureCall(procedureOpts) {
45
+ const { opts, ctx, type, input, path } = procedureOpts;
46
+ try {
47
+ const data = await callProcedure({
48
+ procedures: opts.router._def.procedures,
49
+ path,
50
+ getRawInput: async () => input,
51
+ ctx,
52
+ type,
53
+ });
54
+ return {
55
+ result: {
56
+ data,
57
+ },
58
+ };
59
+ }
60
+ catch (cause) {
61
+ const error = getTRPCErrorFromUnknown(cause);
62
+ opts.onError?.({ error, path, input, ctx, type: type, req: opts.req });
63
+ return {
64
+ error: getErrorShape({
65
+ config: opts.router._def._config,
66
+ error,
67
+ type,
68
+ path,
69
+ input,
70
+ ctx,
71
+ }),
72
+ };
73
+ }
74
+ }
75
+ function caughtErrorToData(cause, errorOpts) {
76
+ const { router, req, onError } = errorOpts.opts;
77
+ const error = getTRPCErrorFromUnknown(cause);
78
+ onError?.({
79
+ error,
80
+ path: errorOpts.path,
81
+ input: errorOpts.input,
82
+ ctx: errorOpts.ctx,
83
+ type: errorOpts.type,
84
+ req,
85
+ });
86
+ const untransformedJSON = {
87
+ error: getErrorShape({
88
+ config: router._def._config,
89
+ error,
90
+ type: errorOpts.type,
91
+ path: errorOpts.path,
92
+ input: errorOpts.input,
93
+ ctx: errorOpts.ctx,
94
+ }),
95
+ };
96
+ const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
97
+ const body = JSON.stringify(transformedJSON);
98
+ return {
99
+ error,
100
+ untransformedJSON,
101
+ body,
102
+ };
103
+ }
104
+ // implementation
105
+ async function resolveHTTPResponse(opts) {
106
+ const { router, req, unstable_onHead, unstable_onChunk } = opts;
107
+ if (req.method === 'HEAD') {
108
+ // can be used for lambda warmup
109
+ const headResponse = {
110
+ status: 204,
111
+ };
112
+ unstable_onHead?.(headResponse, false);
113
+ unstable_onChunk?.([-1, '']);
114
+ return headResponse;
115
+ }
116
+ const contentTypeHandler = opts.contentTypeHandler ?? fallbackContentTypeHandler;
117
+ const batchingEnabled = opts.batching?.enabled ?? true;
118
+ const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
119
+ let ctx = undefined;
120
+ let paths;
121
+ const isBatchCall = !!req.query.get('batch');
122
+ const isStreamCall = isBatchCall &&
123
+ unstable_onHead &&
124
+ unstable_onChunk &&
125
+ req.headers['trpc-batch-mode'] === 'stream';
126
+ try {
127
+ if (opts.error) {
128
+ throw opts.error;
129
+ }
130
+ if (isBatchCall && !batchingEnabled) {
131
+ throw new Error(`Batching is not enabled on the server`);
132
+ }
133
+ /* istanbul ignore if -- @preserve */
134
+ if (type === 'subscription') {
135
+ throw new TRPCError({
136
+ message: 'Subscriptions should use wsLink',
137
+ code: 'METHOD_NOT_SUPPORTED',
138
+ });
139
+ }
140
+ if (type === 'unknown') {
141
+ throw new TRPCError({
142
+ message: `Unexpected request method ${req.method}`,
143
+ code: 'METHOD_NOT_SUPPORTED',
144
+ });
145
+ }
146
+ const inputs = await contentTypeHandler.getInputs({
147
+ isBatchCall,
148
+ req,
149
+ router,
150
+ preprocessedBody: opts.preprocessedBody ?? false,
151
+ });
152
+ paths = isBatchCall
153
+ ? decodeURIComponent(opts.path).split(',')
154
+ : [opts.path];
155
+ const info = {
156
+ isBatchCall,
157
+ calls: paths.map((path, idx) => ({
158
+ path,
159
+ type,
160
+ input: inputs[idx] ?? undefined,
161
+ })),
162
+ };
163
+ ctx = await opts.createContext({ info });
164
+ const promises = paths.map((path, index) => inputToProcedureCall({ opts, ctx, type, input: inputs[index], path }));
165
+ if (!isStreamCall) {
166
+ /**
167
+ * Non-streaming response:
168
+ * - await all responses in parallel, blocking on the slowest one
169
+ * - create headers with known response body
170
+ * - return a complete HTTPResponse
171
+ */
172
+ const untransformedJSON = await Promise.all(promises);
173
+ const errors = untransformedJSON.flatMap((response) => 'error' in response ? [response.error] : []);
174
+ const headResponse = initResponse({
175
+ ctx,
176
+ paths,
177
+ type,
178
+ responseMeta: opts.responseMeta,
179
+ untransformedJSON,
180
+ errors,
181
+ });
182
+ unstable_onHead?.(headResponse, false);
183
+ // return body stuff
184
+ 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
185
+ const transformedJSON = transformTRPCResponse(router._def._config, result);
186
+ const body = JSON.stringify(transformedJSON);
187
+ unstable_onChunk?.([-1, body]);
188
+ return {
189
+ status: headResponse.status,
190
+ headers: headResponse.headers,
191
+ body,
192
+ };
193
+ }
194
+ /**
195
+ * Streaming response:
196
+ * - block on none, call `onChunk` as soon as each response is ready
197
+ * - create headers with minimal data (cannot know the response body in advance)
198
+ * - return void
199
+ */
200
+ const headResponse = initResponse({
201
+ ctx,
202
+ paths,
203
+ type,
204
+ responseMeta: opts.responseMeta,
205
+ });
206
+ unstable_onHead(headResponse, true);
207
+ const indexedPromises = new Map(promises.map((promise, index) => [
208
+ index,
209
+ promise.then((r) => [index, r]),
210
+ ]));
211
+ for (const _ of paths) {
212
+ const [index, untransformedJSON] = await Promise.race(indexedPromises.values());
213
+ indexedPromises.delete(index);
214
+ try {
215
+ const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
216
+ const body = JSON.stringify(transformedJSON);
217
+ unstable_onChunk([index, body]);
218
+ }
219
+ catch (cause) {
220
+ const path = paths[index];
221
+ const input = inputs[index];
222
+ const { body } = caughtErrorToData(cause, {
223
+ opts,
224
+ ctx,
225
+ type,
226
+ path,
227
+ input,
228
+ });
229
+ unstable_onChunk([index, body]);
230
+ }
231
+ }
232
+ return;
233
+ }
234
+ catch (cause) {
235
+ // we get here if
236
+ // - batching is called when it's not enabled
237
+ // - `createContext()` throws
238
+ // - `router._def._config.transformer.output.serialize()` throws
239
+ // - post body is too large
240
+ // - input deserialization fails
241
+ // - `errorFormatter` return value is malformed
242
+ const { error, untransformedJSON, body } = caughtErrorToData(cause, {
243
+ opts,
244
+ ctx,
245
+ type,
246
+ });
247
+ const headResponse = initResponse({
248
+ ctx,
249
+ paths,
250
+ type,
251
+ responseMeta: opts.responseMeta,
252
+ untransformedJSON,
253
+ errors: [error],
254
+ });
255
+ unstable_onHead?.(headResponse, false);
256
+ unstable_onChunk?.([-1, body]);
257
+ return {
258
+ status: headResponse.status,
259
+ headers: headResponse.headers,
260
+ body,
261
+ };
262
+ }
263
+ }
264
+
265
+ export { resolveHTTPResponse as r };