@trpc/server 11.0.0-rc.361 → 11.0.0-rc.362

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 (173) hide show
  1. package/dist/@trpc/server/http.d.ts +7 -5
  2. package/dist/@trpc/server/http.d.ts.map +1 -1
  3. package/dist/@trpc/server/index.d.ts +1 -2
  4. package/dist/@trpc/server/index.d.ts.map +1 -1
  5. package/dist/adapters/aws-lambda/getPlanner.d.ts +13 -0
  6. package/dist/adapters/aws-lambda/getPlanner.d.ts.map +1 -0
  7. package/dist/adapters/aws-lambda/getPlanner.js +143 -0
  8. package/dist/adapters/aws-lambda/getPlanner.mjs +141 -0
  9. package/dist/adapters/aws-lambda/index.d.ts +12 -9
  10. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  11. package/dist/adapters/aws-lambda/index.js +7 -76
  12. package/dist/adapters/aws-lambda/index.mjs +9 -72
  13. package/dist/adapters/express.js +1 -0
  14. package/dist/adapters/express.mjs +1 -0
  15. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +19 -2
  16. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  17. package/dist/adapters/fastify/fastifyRequestHandler.js +12 -63
  18. package/dist/adapters/fastify/fastifyRequestHandler.mjs +13 -64
  19. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  20. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  21. package/dist/adapters/fetch/fetchRequestHandler.d.ts +2 -2
  22. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  23. package/dist/adapters/fetch/fetchRequestHandler.js +28 -85
  24. package/dist/adapters/fetch/fetchRequestHandler.mjs +29 -86
  25. package/dist/adapters/fetch/types.d.ts +2 -2
  26. package/dist/adapters/fetch/types.d.ts.map +1 -1
  27. package/dist/adapters/next.js +1 -1
  28. package/dist/adapters/next.mjs +1 -1
  29. package/dist/adapters/node-http/incomingMessageToRequest.d.ts +18 -0
  30. package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -0
  31. package/dist/adapters/node-http/incomingMessageToRequest.js +71 -0
  32. package/dist/adapters/node-http/incomingMessageToRequest.mjs +69 -0
  33. package/dist/adapters/node-http/index.d.ts +1 -0
  34. package/dist/adapters/node-http/index.d.ts.map +1 -1
  35. package/dist/adapters/node-http/index.js +2 -0
  36. package/dist/adapters/node-http/index.mjs +1 -0
  37. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
  38. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  39. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +27 -70
  40. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +28 -71
  41. package/dist/adapters/node-http/types.d.ts +7 -8
  42. package/dist/adapters/node-http/types.d.ts.map +1 -1
  43. package/dist/adapters/ws.js +1 -1
  44. package/dist/adapters/ws.mjs +1 -1
  45. package/dist/bundle-analysis.json +180 -277
  46. package/dist/http.js +4 -2
  47. package/dist/http.mjs +2 -1
  48. package/dist/index.js +2 -4
  49. package/dist/index.mjs +1 -2
  50. package/dist/unstable-core-do-not-import/http/contentType.d.ts +8 -20
  51. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
  52. package/dist/unstable-core-do-not-import/http/contentType.js +180 -0
  53. package/dist/unstable-core-do-not-import/http/contentType.mjs +178 -0
  54. package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts +14 -0
  55. package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts.map +1 -0
  56. package/dist/unstable-core-do-not-import/http/contentTypeParsers.js +14 -0
  57. package/dist/unstable-core-do-not-import/http/contentTypeParsers.mjs +12 -0
  58. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +1 -1
  59. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
  60. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +7 -43
  61. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
  62. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +119 -134
  63. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +119 -134
  64. package/dist/unstable-core-do-not-import/http/types.d.ts +25 -27
  65. package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
  66. package/dist/unstable-core-do-not-import/initTRPC.d.ts +1 -1
  67. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +1 -3
  68. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
  69. package/dist/unstable-core-do-not-import/procedureBuilder.js +0 -2
  70. package/dist/unstable-core-do-not-import/procedureBuilder.mjs +1 -2
  71. package/dist/unstable-core-do-not-import/rootConfig.d.ts +0 -9
  72. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
  73. package/dist/unstable-core-do-not-import/utils.d.ts +3 -0
  74. package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
  75. package/dist/unstable-core-do-not-import/utils.js +2 -0
  76. package/dist/unstable-core-do-not-import/utils.mjs +2 -1
  77. package/dist/unstable-core-do-not-import.d.ts +9 -3
  78. package/dist/unstable-core-do-not-import.d.ts.map +1 -1
  79. package/dist/unstable-core-do-not-import.js +11 -7
  80. package/dist/unstable-core-do-not-import.mjs +7 -5
  81. package/package.json +3 -13
  82. package/src/@trpc/server/http.ts +7 -12
  83. package/src/@trpc/server/index.ts +0 -3
  84. package/src/adapters/aws-lambda/getPlanner.ts +191 -0
  85. package/src/adapters/aws-lambda/index.ts +43 -107
  86. package/src/adapters/express.ts +1 -1
  87. package/src/adapters/fastify/fastifyRequestHandler.ts +40 -89
  88. package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
  89. package/src/adapters/fetch/fetchRequestHandler.ts +35 -111
  90. package/src/adapters/fetch/types.ts +4 -2
  91. package/src/adapters/next.ts +1 -1
  92. package/src/adapters/node-http/incomingMessageToRequest.ts +94 -0
  93. package/src/adapters/node-http/index.ts +1 -0
  94. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +31 -97
  95. package/src/adapters/node-http/types.ts +27 -37
  96. package/src/adapters/standalone.ts +1 -1
  97. package/src/unstable-core-do-not-import/http/contentType.ts +214 -22
  98. package/src/unstable-core-do-not-import/http/contentTypeParsers.ts +29 -0
  99. package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +2 -2
  100. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +135 -210
  101. package/src/unstable-core-do-not-import/http/types.ts +25 -30
  102. package/src/unstable-core-do-not-import/procedureBuilder.ts +2 -4
  103. package/src/unstable-core-do-not-import/rootConfig.ts +0 -10
  104. package/src/unstable-core-do-not-import/utils.ts +4 -0
  105. package/src/unstable-core-do-not-import.ts +9 -3
  106. package/adapters/node-http/content-type/form-data/index.d.ts +0 -1
  107. package/adapters/node-http/content-type/form-data/index.js +0 -1
  108. package/adapters/node-http/content-type/json/index.d.ts +0 -1
  109. package/adapters/node-http/content-type/json/index.js +0 -1
  110. package/dist/adapters/aws-lambda/content-type/json/index.d.ts +0 -10
  111. package/dist/adapters/aws-lambda/content-type/json/index.d.ts.map +0 -1
  112. package/dist/adapters/aws-lambda/content-type/json/index.js +0 -62
  113. package/dist/adapters/aws-lambda/content-type/json/index.mjs +0 -60
  114. package/dist/adapters/aws-lambda/utils.d.ts +0 -31
  115. package/dist/adapters/aws-lambda/utils.d.ts.map +0 -1
  116. package/dist/adapters/aws-lambda/utils.js +0 -111
  117. package/dist/adapters/aws-lambda/utils.mjs +0 -103
  118. package/dist/adapters/content-handlers/concurrentCache.d.ts +0 -7
  119. package/dist/adapters/content-handlers/concurrentCache.d.ts.map +0 -1
  120. package/dist/adapters/content-handlers/concurrentCache.js +0 -17
  121. package/dist/adapters/content-handlers/concurrentCache.mjs +0 -15
  122. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +0 -10
  123. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +0 -1
  124. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +0 -33
  125. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +0 -31
  126. package/dist/adapters/fastify/content-type/json/index.d.ts +0 -8
  127. package/dist/adapters/fastify/content-type/json/index.d.ts.map +0 -1
  128. package/dist/adapters/fastify/content-type/json/index.js +0 -62
  129. package/dist/adapters/fastify/content-type/json/index.mjs +0 -60
  130. package/dist/adapters/fastify/types.d.ts +0 -11
  131. package/dist/adapters/fastify/types.d.ts.map +0 -1
  132. package/dist/adapters/fetch/content-type/json/index.d.ts +0 -9
  133. package/dist/adapters/fetch/content-type/json/index.d.ts.map +0 -1
  134. package/dist/adapters/fetch/content-type/json/index.js +0 -55
  135. package/dist/adapters/fetch/content-type/json/index.mjs +0 -53
  136. package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -5
  137. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +0 -1
  138. package/dist/adapters/node-http/content-type/form-data/index.js +0 -32
  139. package/dist/adapters/node-http/content-type/form-data/index.mjs +0 -30
  140. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +0 -7
  141. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +0 -1
  142. package/dist/adapters/node-http/content-type/json/getPostBody.js +0 -45
  143. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +0 -43
  144. package/dist/adapters/node-http/content-type/json/index.d.ts +0 -5
  145. package/dist/adapters/node-http/content-type/json/index.d.ts.map +0 -1
  146. package/dist/adapters/node-http/content-type/json/index.js +0 -68
  147. package/dist/adapters/node-http/content-type/json/index.mjs +0 -66
  148. package/dist/adapters/node-http/content-type/octet/index.d.ts +0 -5
  149. package/dist/adapters/node-http/content-type/octet/index.d.ts.map +0 -1
  150. package/dist/adapters/node-http/content-type/octet/index.js +0 -19
  151. package/dist/adapters/node-http/content-type/octet/index.mjs +0 -17
  152. package/dist/adapters/node-http/content-type/types.d.ts +0 -8
  153. package/dist/adapters/node-http/content-type/types.d.ts.map +0 -1
  154. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts +0 -16
  155. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts.map +0 -1
  156. package/dist/unstable-core-do-not-import/contentTypeParsers.js +0 -23
  157. package/dist/unstable-core-do-not-import/contentTypeParsers.mjs +0 -21
  158. package/dist/unstable-core-do-not-import/http/index.d.ts +0 -11
  159. package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
  160. package/src/adapters/aws-lambda/content-type/json/index.ts +0 -108
  161. package/src/adapters/aws-lambda/utils.ts +0 -170
  162. package/src/adapters/content-handlers/concurrentCache.ts +0 -16
  163. package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +0 -45
  164. package/src/adapters/fastify/content-type/json/index.ts +0 -106
  165. package/src/adapters/fastify/types.ts +0 -22
  166. package/src/adapters/fetch/content-type/json/index.ts +0 -90
  167. package/src/adapters/node-http/content-type/form-data/index.ts +0 -37
  168. package/src/adapters/node-http/content-type/json/getPostBody.ts +0 -49
  169. package/src/adapters/node-http/content-type/json/index.ts +0 -100
  170. package/src/adapters/node-http/content-type/octet/index.ts +0 -27
  171. package/src/adapters/node-http/content-type/types.ts +0 -19
  172. package/src/unstable-core-do-not-import/contentTypeParsers.ts +0 -37
  173. package/src/unstable-core-do-not-import/http/index.ts +0 -28
@@ -1,22 +1,21 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
1
2
  import { getErrorShape } from '../error/getErrorShape';
2
3
  import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError';
3
4
  import type { ProcedureType } from '../procedure';
4
- import type {
5
- AnyRouter,
6
- inferRouterContext,
7
- inferRouterError,
5
+ import {
6
+ callProcedure,
7
+ type AnyRouter,
8
+ type inferRouterContext,
9
+ type inferRouterError,
8
10
  } from '../router';
9
- import { callProcedure } from '../router';
10
11
  import type { TRPCResponse } from '../rpc';
11
12
  import { transformTRPCResponse } from '../transformer';
13
+ import { getBatchStreamFormatter } from './batchStreamFormatter';
14
+ import { getRequestInfo } from './contentType';
12
15
  import { getHTTPStatusCode } from './getHTTPStatusCode';
13
16
  import type {
14
17
  HTTPBaseHandlerOptions,
15
- HTTPHeaders,
16
- HTTPRequest,
17
- HTTPResponse,
18
18
  ResolveHTTPRequestOptionsContextFn,
19
- ResponseChunk,
20
19
  TRPCRequestInfo,
21
20
  } from './types';
22
21
 
@@ -28,61 +27,31 @@ const HTTP_METHOD_PROCEDURE_TYPE_MAP: Record<
28
27
  POST: 'mutation',
29
28
  };
30
29
 
31
- type PartialBy<TBaseType, TKey extends keyof TBaseType> = Omit<
32
- TBaseType,
33
- TKey
34
- > &
35
- Partial<Pick<TBaseType, TKey>>;
36
-
37
- interface ResolveHTTPRequestOptions<
38
- TRouter extends AnyRouter,
39
- TRequest extends HTTPRequest,
40
- > extends HTTPBaseHandlerOptions<TRouter, TRequest> {
30
+ interface ResolveHTTPRequestOptions<TRouter extends AnyRouter>
31
+ extends HTTPBaseHandlerOptions<TRouter, Request> {
41
32
  createContext: ResolveHTTPRequestOptionsContextFn<TRouter>;
42
- req: TRequest;
33
+ req: Request;
43
34
  path: string;
44
- getInput: (opts: { isBatchCall: boolean; batch: number }) => Promise<unknown>;
45
- error?: TRPCError;
46
- /**
47
- * Called as soon as the response head is known.
48
- * When streaming, headers will have been generated
49
- * **without** knowing the response body.
50
- *
51
- * Without this callback, streaming is disabled.
52
- */
53
- unstable_onHead: (
54
- headResponse: Omit<HTTPResponse, 'body'>,
55
- isStreaming: boolean,
56
- ) => void;
57
35
  /**
58
- * Called for every procedure with `[index, result]`.
59
- *
60
- * Will be called a single time with `index = -1` if
61
- * - response is an error
62
- * - response is empty (HEAD request)
63
- *
64
- * Without this callback, streaming is disabled.
36
+ * If the request had an issue before reaching the handler
65
37
  */
66
- unstable_onChunk: (chunk: ResponseChunk) => void;
38
+ error: TRPCError | null;
67
39
  }
68
40
 
69
- function initResponse<
70
- TRouter extends AnyRouter,
71
- TRequest extends HTTPRequest,
72
- >(initOpts: {
41
+ function initResponse<TRouter extends AnyRouter, TRequest>(initOpts: {
73
42
  ctx: inferRouterContext<TRouter> | undefined;
74
- paths: string[] | undefined;
43
+ info: TRPCRequestInfo | undefined;
75
44
  type: ProcedureType | 'unknown';
76
45
  responseMeta?: HTTPBaseHandlerOptions<TRouter, TRequest>['responseMeta'];
77
46
  untransformedJSON?:
78
47
  | TRPCResponse<unknown, inferRouterError<TRouter>>
79
48
  | TRPCResponse<unknown, inferRouterError<TRouter>>[]
80
49
  | undefined;
81
- errors?: TRPCError[];
82
- }): HTTPResponse {
50
+ errors: TRPCError[];
51
+ }) {
83
52
  const {
84
53
  ctx,
85
- paths,
54
+ info,
86
55
  type,
87
56
  responseMeta,
88
57
  untransformedJSON,
@@ -90,9 +59,8 @@ function initResponse<
90
59
  } = initOpts;
91
60
 
92
61
  let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
93
- const headers: HTTPHeaders = {
94
- 'Content-Type': 'application/json',
95
- };
62
+
63
+ const headers = new Headers([['Content-Type', 'application/json']]);
96
64
 
97
65
  const eagerGeneration = !untransformedJSON;
98
66
  const data = eagerGeneration
@@ -104,15 +72,33 @@ function initResponse<
104
72
  const meta =
105
73
  responseMeta?.({
106
74
  ctx,
107
- paths,
75
+ info,
76
+ paths: info?.calls.map((call) => call.path),
108
77
  type,
109
78
  data,
110
79
  errors,
111
80
  eagerGeneration,
112
81
  }) ?? {};
113
82
 
114
- for (const [key, value] of Object.entries(meta.headers ?? {})) {
115
- headers[key] = value;
83
+ if (meta.headers) {
84
+ if (meta.headers instanceof Headers) {
85
+ for (const [key, value] of meta.headers.entries()) {
86
+ headers.append(key, value);
87
+ }
88
+ } else {
89
+ /**
90
+ * @deprecated, delete in v12
91
+ */
92
+ for (const [key, value] of Object.entries(meta.headers)) {
93
+ if (Array.isArray(value)) {
94
+ for (const v of value) {
95
+ headers.append(key, v);
96
+ }
97
+ } else if (typeof value === 'string') {
98
+ headers.set(key, value);
99
+ }
100
+ }
101
+ }
116
102
  }
117
103
  if (meta.status) {
118
104
  status = meta.status;
@@ -124,14 +110,11 @@ function initResponse<
124
110
  };
125
111
  }
126
112
 
127
- function caughtErrorToData<
128
- TRouter extends AnyRouter,
129
- TRequest extends HTTPRequest,
130
- >(
113
+ function caughtErrorToData<TRouter extends AnyRouter>(
131
114
  cause: unknown,
132
115
  errorOpts: {
133
116
  opts: Pick<
134
- ResolveHTTPRequestOptions<TRouter, TRequest>,
117
+ ResolveHTTPRequestOptions<TRouter>,
135
118
  'onError' | 'req' | 'router'
136
119
  >;
137
120
  ctx: inferRouterContext<TRouter> | undefined;
@@ -172,86 +155,52 @@ function caughtErrorToData<
172
155
  };
173
156
  }
174
157
 
175
- /**
176
- * Since `resolveHTTPResponse` is a public API (community adapters),
177
- * let's give it a strong type signature to increase discoverability.
178
- */
179
-
180
- /**
181
- * Non-streaming signature for `resolveHTTPResponse`:
182
- * @param opts.unstable_onHead `undefined`
183
- * @param opts.unstable_onChunk `undefined`
184
- * @returns `Promise<HTTPResponse>`
185
- */
186
- export async function resolveHTTPResponse<
187
- TRouter extends AnyRouter,
188
- TRequest extends HTTPRequest,
189
- >(
190
- opts: Omit<
191
- ResolveHTTPRequestOptions<TRouter, TRequest>,
192
- 'unstable_onChunk' | 'unstable_onHead'
193
- >,
194
- ): Promise<HTTPResponse>;
195
- /**
196
- * Streaming signature for `resolveHTTPResponse`:
197
- * @param opts.unstable_onHead called as soon as the response head is known
198
- * @param opts.unstable_onChunk called for every procedure with `[index, result]`
199
- * @returns `Promise<void>` since the response is streamed
200
- */
201
- export async function resolveHTTPResponse<
202
- TRouter extends AnyRouter,
203
- TRequest extends HTTPRequest,
204
- >(opts: ResolveHTTPRequestOptions<TRouter, TRequest>): Promise<void>;
205
- // implementation
206
- export async function resolveHTTPResponse<
207
- TRouter extends AnyRouter,
208
- TRequest extends HTTPRequest,
209
- >(
210
- opts: PartialBy<
211
- ResolveHTTPRequestOptions<TRouter, TRequest>,
212
- 'unstable_onChunk' | 'unstable_onHead'
213
- >,
214
- ): Promise<HTTPResponse | void> {
215
- const { router, req, unstable_onHead, unstable_onChunk } = opts;
158
+ export async function resolveResponse<TRouter extends AnyRouter>(
159
+ opts: ResolveHTTPRequestOptions<TRouter>,
160
+ ): Promise<Response> {
161
+ const { router, req } = opts;
162
+
163
+ const url = new URL(req.url);
216
164
 
217
165
  if (req.method === 'HEAD') {
218
166
  // can be used for lambda warmup
219
- const headResponse: HTTPResponse = {
167
+ return new Response(null, {
220
168
  status: 204,
221
- };
222
- unstable_onHead?.(headResponse, false);
223
- unstable_onChunk?.([-1, '']);
224
- return headResponse;
169
+ });
225
170
  }
226
171
  const allowBatching = opts.allowBatching ?? opts.batching?.enabled ?? true;
227
- const allowMethodOverride = opts.allowMethodOverride ?? false;
172
+ const allowMethodOverride =
173
+ (opts.allowMethodOverride ?? false) && req.method === 'POST';
228
174
 
229
175
  const type =
230
176
  HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? ('unknown' as const);
231
177
  let ctx: inferRouterContext<TRouter> | undefined = undefined;
232
- let paths: string[] | undefined;
178
+ let info: TRPCRequestInfo | undefined = undefined;
233
179
 
234
- const isBatchCall = !!req.query.get('batch');
235
- const isStreamCall =
236
- isBatchCall &&
237
- unstable_onHead &&
238
- unstable_onChunk &&
239
- req.headers['trpc-batch-mode'] === 'stream';
180
+ const isStreamCall = req.headers.get('trpc-batch-mode') === 'stream';
240
181
 
241
182
  try {
242
- if (isBatchCall && !allowBatching) {
183
+ info = getRequestInfo({
184
+ req,
185
+ path: decodeURIComponent(opts.path),
186
+ config: router._def._config,
187
+ searchParams: url.searchParams,
188
+ });
189
+
190
+ // we create context early so that error handlers may access context information
191
+ ctx = await opts.createContext({
192
+ info,
193
+ });
194
+
195
+ if (opts.error) {
196
+ throw opts.error;
197
+ }
198
+ if (info.isBatchCall && !allowBatching) {
243
199
  throw new TRPCError({
244
200
  code: 'BAD_REQUEST',
245
201
  message: `Batching is not enabled on the server`,
246
202
  });
247
203
  }
248
- /* istanbul ignore if -- @preserve */
249
- if (type === 'subscription') {
250
- throw new TRPCError({
251
- message: 'Subscriptions should use wsLink',
252
- code: 'METHOD_NOT_SUPPORTED',
253
- });
254
- }
255
204
  if (type === 'unknown') {
256
205
  throw new TRPCError({
257
206
  message: `Unexpected request method ${req.method}`,
@@ -259,42 +208,16 @@ export async function resolveHTTPResponse<
259
208
  });
260
209
  }
261
210
 
262
- paths = isBatchCall
263
- ? decodeURIComponent(opts.path).split(',')
264
- : [opts.path];
265
- const info: TRPCRequestInfo = {
266
- isBatchCall,
267
- calls: paths.map((path) => ({
268
- path,
269
- type,
270
- })),
271
- };
272
- ctx = await opts.createContext({ info });
273
-
274
211
  const errors: TRPCError[] = [];
275
212
 
276
213
  const promises: Promise<
277
214
  TRPCResponse<unknown, inferRouterError<TRouter>>
278
- >[] = paths.map(async (path, index) => {
279
- async function getRawInput() {
280
- return await opts.getInput({
281
- isBatchCall,
282
- batch: index,
283
- });
284
- }
285
-
215
+ >[] = info.calls.map(async (call) => {
286
216
  try {
287
- if (opts.error) {
288
- // sometimes an error may be generated above this function in the stack
289
- // for instance a 405 error if the method is not supported
290
- // But we need to handle it here to ensure the error is formatted correctly
291
- throw opts.error;
292
- }
293
-
294
217
  const data = await callProcedure({
295
218
  procedures: opts.router._def.procedures,
296
- path,
297
- getRawInput: getRawInput,
219
+ path: call.path,
220
+ getRawInput: call.getRawInput,
298
221
  ctx,
299
222
  type,
300
223
  allowMethodOverride,
@@ -307,18 +230,11 @@ export async function resolveHTTPResponse<
307
230
  } catch (cause) {
308
231
  const error = getTRPCErrorFromUnknown(cause);
309
232
  errors.push(error);
310
-
311
- let input: unknown;
312
- try {
313
- input = await getRawInput();
314
- } catch (e) {
315
- // if `getRawInput` fails, we can't include the input in the error
316
- input = undefined;
317
- }
233
+ const input = call.result();
318
234
 
319
235
  opts.onError?.({
320
236
  error,
321
- path,
237
+ path: call.path,
322
238
  input,
323
239
  ctx,
324
240
  type: type,
@@ -330,14 +246,13 @@ export async function resolveHTTPResponse<
330
246
  config: opts.router._def._config,
331
247
  error,
332
248
  type,
333
- path,
249
+ path: call.path,
334
250
  input,
335
251
  ctx,
336
252
  }),
337
253
  };
338
254
  }
339
255
  });
340
-
341
256
  if (!isStreamCall) {
342
257
  /**
343
258
  * Non-streaming response:
@@ -347,31 +262,33 @@ export async function resolveHTTPResponse<
347
262
  */
348
263
 
349
264
  const untransformedJSON = await Promise.all(promises);
265
+ const errors = untransformedJSON.flatMap((response) =>
266
+ 'error' in response ? [response.error] : [],
267
+ );
350
268
 
351
269
  const headResponse = initResponse({
352
270
  ctx,
353
- paths,
271
+ info,
354
272
  type,
355
273
  responseMeta: opts.responseMeta,
356
274
  untransformedJSON,
357
275
  errors,
358
276
  });
359
- unstable_onHead?.(headResponse, false);
360
277
 
361
278
  // return body stuff
362
- 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
279
+ const result = info.isBatchCall
280
+ ? untransformedJSON
281
+ : untransformedJSON[0]!;
363
282
  const transformedJSON = transformTRPCResponse(
364
283
  router._def._config,
365
284
  result,
366
285
  );
367
286
  const body = JSON.stringify(transformedJSON);
368
- unstable_onChunk?.([-1, body]);
369
287
 
370
- return {
288
+ return new Response(body, {
371
289
  status: headResponse.status,
372
290
  headers: headResponse.headers,
373
- body,
374
- };
291
+ });
375
292
  }
376
293
 
377
294
  /**
@@ -382,50 +299,62 @@ export async function resolveHTTPResponse<
382
299
  */
383
300
  const headResponse = initResponse({
384
301
  ctx,
385
- paths,
302
+ info,
386
303
  type,
387
304
  responseMeta: opts.responseMeta,
305
+ errors: [],
388
306
  });
389
- unstable_onHead(headResponse, true);
390
-
391
- const indexedPromises = new Map(
392
- promises.map((promise, index) => [
393
- index,
394
- promise.then((r) => [index, r] as const),
395
- ]),
396
- );
397
- for (const _ of paths) {
398
- const [index, untransformedJSON] = await Promise.race(
399
- indexedPromises.values(),
307
+
308
+ const encoder = new TextEncoderStream();
309
+ const stream = encoder.readable;
310
+ const controller = encoder.writable.getWriter();
311
+ async function exec() {
312
+ const indexedPromises = new Map(
313
+ promises.map((promise, index) => [
314
+ index,
315
+ promise.then((r) => [index, r] as const),
316
+ ]),
400
317
  );
401
- indexedPromises.delete(index);
318
+ const formatter = getBatchStreamFormatter();
402
319
 
403
- try {
404
- const transformedJSON = transformTRPCResponse(
405
- router._def._config,
406
- untransformedJSON,
320
+ while (indexedPromises.size > 0) {
321
+ const [index, untransformedJSON] = await Promise.race(
322
+ indexedPromises.values(),
407
323
  );
408
- const body = JSON.stringify(transformedJSON);
324
+ indexedPromises.delete(index);
409
325
 
410
- unstable_onChunk([index, body]);
411
- } catch (cause) {
412
- const path = paths[index];
413
- const input = await opts.getInput({
414
- isBatchCall,
415
- batch: index,
416
- });
417
- const { body } = caughtErrorToData(cause, {
418
- opts,
419
- ctx,
420
- type,
421
- path,
422
- input,
423
- });
326
+ try {
327
+ const transformedJSON = transformTRPCResponse(
328
+ router._def._config,
329
+ untransformedJSON,
330
+ );
331
+ const body = JSON.stringify(transformedJSON);
332
+
333
+ await controller.write(formatter(index, body));
334
+ } catch (cause) {
335
+ const call = info!.calls[index]!;
336
+ const input = call.result();
337
+ const { body } = caughtErrorToData(cause, {
338
+ opts,
339
+ ctx,
340
+ type,
341
+ path: call.path,
342
+ input,
343
+ });
424
344
 
425
- unstable_onChunk([index, body]);
345
+ await controller.write(formatter(index, body));
346
+ }
426
347
  }
348
+
349
+ await controller.write(formatter.end());
350
+ await controller.close();
427
351
  }
428
- return;
352
+ exec().catch((err) => controller.abort(err));
353
+
354
+ return new Response(stream, {
355
+ headers: headResponse.headers,
356
+ status: headResponse.status,
357
+ });
429
358
  } catch (cause) {
430
359
  // we get here if
431
360
  // - batching is called when it's not enabled
@@ -442,20 +371,16 @@ export async function resolveHTTPResponse<
442
371
 
443
372
  const headResponse = initResponse({
444
373
  ctx,
445
- paths,
374
+ info,
446
375
  type,
447
376
  responseMeta: opts.responseMeta,
448
377
  untransformedJSON,
449
378
  errors: [error],
450
379
  });
451
- unstable_onHead?.(headResponse, false);
452
-
453
- unstable_onChunk?.([-1, body]);
454
380
 
455
- return {
381
+ return new Response(body, {
456
382
  status: headResponse.status,
457
383
  headers: headResponse.headers,
458
- body,
459
- };
384
+ });
460
385
  }
461
386
  }
@@ -8,19 +8,14 @@ import type {
8
8
  import type { TRPCResponse } from '../rpc';
9
9
  import type { Dict } from '../types';
10
10
 
11
- export type HTTPHeaders = Dict<string[] | string>;
12
-
13
- export interface HTTPResponse {
14
- status: number;
15
- headers?: HTTPHeaders;
16
- body?: string;
17
- }
18
-
19
- export type ResponseChunk = [procedureIndex: number, responseBody: string];
11
+ /**
12
+ * @deprecated use `Headers` instead, this will be removed in v12
13
+ */
14
+ type HTTPHeaders = Dict<string[] | string>;
20
15
 
21
16
  export interface ResponseMeta {
22
17
  status?: number;
23
- headers?: HTTPHeaders;
18
+ headers?: Headers | HTTPHeaders;
24
19
  }
25
20
 
26
21
  /**
@@ -31,24 +26,18 @@ export type ResponseMetaFn<TRouter extends AnyRouter> = (opts: {
31
26
  ctx?: inferRouterContext<TRouter>;
32
27
  /**
33
28
  * The different tRPC paths requested
29
+ * @deprecated use `info` instead, this will be removed in v12
34
30
  **/
35
- paths?: string[];
31
+ paths: string[] | undefined;
32
+ info: TRPCRequestInfo | undefined;
36
33
  type: ProcedureType | 'unknown';
37
34
  errors: TRPCError[];
38
35
  /**
39
- * `true` if the `ResponseMeta` are being
40
- * generated without knowing the response data
41
- * (e.g. for streaming requests).
36
+ * `true` if the `ResponseMeta` is being generated without knowing the response data (e.g. for streamed requests).
42
37
  */
43
- eagerGeneration?: boolean;
38
+ eagerGeneration: boolean;
44
39
  }) => ResponseMeta;
45
40
 
46
- export interface HTTPRequest {
47
- method: string;
48
- query: URLSearchParams;
49
- headers: HTTPHeaders;
50
- }
51
-
52
41
  /**
53
42
  * Base interface for anything using HTTP
54
43
  */
@@ -62,23 +51,29 @@ export interface HTTPBaseHandlerOptions<TRouter extends AnyRouter, TRequest>
62
51
  responseMeta?: ResponseMetaFn<TRouter>;
63
52
  }
64
53
 
65
- /** @internal */
66
- export type ProcedureCall = {
67
- type: ProcedureType;
54
+ interface TRPCRequestInfoProcedureCall {
68
55
  path: string;
69
- };
56
+ /**
57
+ * Read the raw input (deduped and memoized)
58
+ */
59
+ getRawInput: () => Promise<unknown>;
60
+ /**
61
+ * Get already parsed inputs - won't trigger reading the body or parsing the inputs
62
+ */
63
+ result: () => unknown;
64
+ }
70
65
 
71
66
  /**
72
67
  * Information about the incoming request
73
- * @internal
68
+ * @public
74
69
  */
75
- export type TRPCRequestInfo = {
70
+ export interface TRPCRequestInfo {
76
71
  isBatchCall: boolean;
77
- calls: ProcedureCall[];
78
- };
72
+ calls: TRPCRequestInfoProcedureCall[];
73
+ }
79
74
 
80
75
  /**
81
- * Inner createContext function for `resolveHTTPResponse` used to forward `TRPCRequestInfo` to `createContext`
76
+ * Inner createContext function for `resolveResponse` used to forward `TRPCRequestInfo` to `createContext`
82
77
  * @internal
83
78
  */
84
79
  export type ResolveHTTPRequestOptionsContextFn<TRouter extends AnyRouter> =
@@ -30,6 +30,7 @@ import type {
30
30
  Simplify,
31
31
  TypeError,
32
32
  } from './types';
33
+ import type { UnsetMarker } from './utils';
33
34
  import { mergeWithoutOverrides } from './utils';
34
35
 
35
36
  type IntersectIfDefined<TType, TWith> = TType extends UnsetMarker
@@ -37,10 +38,7 @@ type IntersectIfDefined<TType, TWith> = TType extends UnsetMarker
37
38
  : TWith extends UnsetMarker
38
39
  ? TType
39
40
  : Simplify<TType & TWith>;
40
-
41
- /** @internal */
42
- export const unsetMarker = Symbol('unsetMarker');
43
- type UnsetMarker = typeof unsetMarker;
41
+ ``;
44
42
  type DefaultValue<TValue, TFallback> = TValue extends UnsetMarker
45
43
  ? TFallback
46
44
  : TValue;
@@ -97,13 +97,3 @@ export type CreateContextCallback<
97
97
  createContext: TFunction;
98
98
  }
99
99
  >;
100
-
101
- /**
102
- * @internal
103
- */
104
- export type WrapCreateContext<TFunction extends (...args: any[]) => any> = {
105
- /**
106
- * @link https://trpc.io/docs/v11/context
107
- **/
108
- createContext?: TFunction;
109
- };
@@ -1,3 +1,7 @@
1
+ /** @internal */
2
+ export const unsetMarker = Symbol('unsetMarker');
3
+ export type UnsetMarker = typeof unsetMarker;
4
+
1
5
  /**
2
6
  * Ensures there are no duplicate keys when building a procedure.
3
7
  * @internal
@@ -1,5 +1,5 @@
1
1
  /**
2
- * **DO NOT IMPORT FROM HERE FILE**
2
+ * **DO NOT IMPORT FROM THIS FILE**
3
3
  *
4
4
  * This file is here to:
5
5
  * - make TypeScript happy and prevent _"The inferred type of 'createContext' cannot be named without a reference to [...]"_.
@@ -12,10 +12,16 @@ export * from './unstable-core-do-not-import/clientish/inference';
12
12
  export * from './unstable-core-do-not-import/clientish/inferrable';
13
13
  export * from './unstable-core-do-not-import/clientish/serialize';
14
14
  export * from './unstable-core-do-not-import/createProxy';
15
+ export * from './unstable-core-do-not-import/error/TRPCError';
15
16
  export * from './unstable-core-do-not-import/error/formatter';
16
17
  export * from './unstable-core-do-not-import/error/getErrorShape';
17
- export * from './unstable-core-do-not-import/error/TRPCError';
18
- export * from './unstable-core-do-not-import/http';
18
+ export * from './unstable-core-do-not-import/http/batchStreamFormatter';
19
+ export * from './unstable-core-do-not-import/http/contentType';
20
+ export * from './unstable-core-do-not-import/http/contentTypeParsers';
21
+ export * from './unstable-core-do-not-import/http/getHTTPStatusCode';
22
+ export * from './unstable-core-do-not-import/http/resolveHTTPResponse';
23
+ export * from './unstable-core-do-not-import/http/toURL';
24
+ export * from './unstable-core-do-not-import/http/types';
19
25
  export * from './unstable-core-do-not-import/initTRPC';
20
26
  export * from './unstable-core-do-not-import/middleware';
21
27
  export * from './unstable-core-do-not-import/parser';
@@ -1 +0,0 @@
1
- export * from '../../../../dist/adapters/node-http/content-type/form-data';