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

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