@trpc/server 11.0.0-alpha-tmp-export-from-main-nuke-core.215 → 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 (207) 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 -12
  9. package/dist/adapters/aws-lambda/index.mjs +3 -8
  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 -8
  14. package/dist/adapters/express.mjs +2 -8
  15. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  16. package/dist/adapters/fastify/index.js +4 -12
  17. package/dist/adapters/fastify/index.mjs +2 -10
  18. package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -1
  19. package/dist/adapters/fetch/index.js +4 -10
  20. package/dist/adapters/fetch/index.mjs +2 -8
  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 -11
  24. package/dist/adapters/next.mjs +3 -9
  25. package/dist/adapters/node-http/content-type/json/index.js +3 -5
  26. package/dist/adapters/node-http/content-type/json/index.mjs +1 -3
  27. package/dist/adapters/node-http/index.js +2 -8
  28. package/dist/adapters/node-http/index.mjs +2 -8
  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 -8
  34. package/dist/adapters/standalone.mjs +2 -8
  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 -31
  38. package/dist/adapters/ws.mjs +1 -6
  39. package/dist/http.d.ts +6 -6
  40. package/dist/http.d.ts.map +1 -1
  41. package/dist/http.js +21 -12
  42. package/dist/http.mjs +1 -7
  43. package/dist/index.js +41 -15
  44. package/dist/index.mjs +1 -5
  45. package/dist/{nodeHTTPRequestHandler-257a3b5f.mjs → nodeHTTPRequestHandler-2d5c8791.mjs} +2 -4
  46. package/dist/{nodeHTTPRequestHandler-f4b7e374.js → nodeHTTPRequestHandler-55f05150.js} +4 -6
  47. package/dist/{nodeHTTPRequestHandler-29178135.js → nodeHTTPRequestHandler-7691fc79.js} +1 -3
  48. package/dist/observable.d.ts +2 -2
  49. package/dist/observable.d.ts.map +1 -1
  50. package/dist/observable.js +25 -9
  51. package/dist/observable.mjs +1 -3
  52. package/dist/rpc.d.ts +2 -2
  53. package/dist/rpc.d.ts.map +1 -1
  54. package/dist/rpc.js +13 -5
  55. package/dist/rpc.mjs +1 -2
  56. package/dist/shared.js +9 -4
  57. package/dist/shared.mjs +1 -2
  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 +2 -2
  76. package/src/rpc.ts +2 -2
  77. package/dist/TRPCError-10da405a.js +0 -61
  78. package/dist/TRPCError-14edcf88.mjs +0 -63
  79. package/dist/TRPCError-e2c8eb77.js +0 -67
  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 -767
  84. package/dist/contentType-2ed02bef.mjs +0 -52
  85. package/dist/contentType-3a829011.js +0 -54
  86. package/dist/contentType-e4a2373e.js +0 -59
  87. package/dist/createProxy-0306dcaa.mjs +0 -121
  88. package/dist/createProxy-7e413449.js +0 -127
  89. package/dist/createProxy-dce0c414.js +0 -116
  90. package/dist/initTRPC-2cef03ab.js +0 -384
  91. package/dist/initTRPC-38faa18f.js +0 -371
  92. package/dist/initTRPC-fa1b3d57.mjs +0 -376
  93. package/dist/observable-2a5a96d5.js +0 -125
  94. package/dist/observable-62674916.js +0 -125
  95. package/dist/observable-6d025a37.mjs +0 -121
  96. package/dist/operators-0cc4956a.js +0 -99
  97. package/dist/operators-b08617cb.js +0 -103
  98. package/dist/operators-d0c3e794.mjs +0 -99
  99. package/dist/parseTRPCMessage-6e6c204e.js +0 -65
  100. package/dist/parseTRPCMessage-8eb0bbe9.mjs +0 -62
  101. package/dist/parseTRPCMessage-ebdfe3ca.js +0 -67
  102. package/dist/resolveHTTPResponse-833ca2b8.js +0 -265
  103. package/dist/resolveHTTPResponse-9322334c.mjs +0 -291
  104. package/dist/resolveHTTPResponse-d64c48e1.js +0 -293
  105. package/dist/rootConfig-3371c473.js +0 -89
  106. package/dist/rootConfig-571c9267.js +0 -94
  107. package/dist/rootConfig-6b04a6b8.mjs +0 -81
  108. package/dist/router-3344b52a.js +0 -262
  109. package/dist/router-919c37eb.js +0 -259
  110. package/dist/router-d6789e4b.mjs +0 -252
  111. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts +0 -6
  112. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts.map +0 -1
  113. package/dist/unstable-core-do-not-import/createProxy.d.ts +0 -19
  114. package/dist/unstable-core-do-not-import/createProxy.d.ts.map +0 -1
  115. package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +0 -13
  116. package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +0 -1
  117. package/dist/unstable-core-do-not-import/error/formatter.d.ts +0 -33
  118. package/dist/unstable-core-do-not-import/error/formatter.d.ts.map +0 -1
  119. package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts +0 -15
  120. package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts.map +0 -1
  121. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +0 -24
  122. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +0 -1
  123. package/dist/unstable-core-do-not-import/http/contentType.d.ts +0 -29
  124. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +0 -1
  125. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +0 -5
  126. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +0 -1
  127. package/dist/unstable-core-do-not-import/http/index.d.ts +0 -7
  128. package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
  129. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +0 -51
  130. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +0 -1
  131. package/dist/unstable-core-do-not-import/http/types.d.ts +0 -96
  132. package/dist/unstable-core-do-not-import/http/types.d.ts.map +0 -1
  133. package/dist/unstable-core-do-not-import/index.d.ts +0 -35
  134. package/dist/unstable-core-do-not-import/index.d.ts.map +0 -1
  135. package/dist/unstable-core-do-not-import/index.js +0 -51
  136. package/dist/unstable-core-do-not-import/index.mjs +0 -11
  137. package/dist/unstable-core-do-not-import/initTRPC.d.ts +0 -96
  138. package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +0 -1
  139. package/dist/unstable-core-do-not-import/middleware.d.ts +0 -105
  140. package/dist/unstable-core-do-not-import/middleware.d.ts.map +0 -1
  141. package/dist/unstable-core-do-not-import/observable/index.d.ts +0 -5
  142. package/dist/unstable-core-do-not-import/observable/index.d.ts.map +0 -1
  143. package/dist/unstable-core-do-not-import/observable/observable.d.ts +0 -13
  144. package/dist/unstable-core-do-not-import/observable/observable.d.ts.map +0 -1
  145. package/dist/unstable-core-do-not-import/observable/operators.d.ts +0 -8
  146. package/dist/unstable-core-do-not-import/observable/operators.d.ts.map +0 -1
  147. package/dist/unstable-core-do-not-import/observable/types.d.ts +0 -26
  148. package/dist/unstable-core-do-not-import/observable/types.d.ts.map +0 -1
  149. package/dist/unstable-core-do-not-import/parser.d.ts +0 -30
  150. package/dist/unstable-core-do-not-import/parser.d.ts.map +0 -1
  151. package/dist/unstable-core-do-not-import/procedure.d.ts +0 -72
  152. package/dist/unstable-core-do-not-import/procedure.d.ts.map +0 -1
  153. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +0 -101
  154. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +0 -1
  155. package/dist/unstable-core-do-not-import/rootConfig.d.ts +0 -75
  156. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +0 -1
  157. package/dist/unstable-core-do-not-import/router.d.ts +0 -85
  158. package/dist/unstable-core-do-not-import/router.d.ts.map +0 -1
  159. package/dist/unstable-core-do-not-import/rpc/codes.d.ts +0 -51
  160. package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +0 -1
  161. package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +0 -97
  162. package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +0 -1
  163. package/dist/unstable-core-do-not-import/rpc/index.d.ts +0 -5
  164. package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +0 -1
  165. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts +0 -5
  166. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts.map +0 -1
  167. package/dist/unstable-core-do-not-import/serialize.d.ts +0 -62
  168. package/dist/unstable-core-do-not-import/serialize.d.ts.map +0 -1
  169. package/dist/unstable-core-do-not-import/transformer.d.ts +0 -107
  170. package/dist/unstable-core-do-not-import/transformer.d.ts.map +0 -1
  171. package/dist/unstable-core-do-not-import/types.d.ts +0 -98
  172. package/dist/unstable-core-do-not-import/types.d.ts.map +0 -1
  173. package/dist/unstable-core-do-not-import/utils.d.ts +0 -31
  174. package/dist/unstable-core-do-not-import/utils.d.ts.map +0 -1
  175. package/src/unstable-core-do-not-import/TRPCInferrable.ts +0 -9
  176. package/src/unstable-core-do-not-import/createProxy.ts +0 -59
  177. package/src/unstable-core-do-not-import/error/TRPCError.ts +0 -82
  178. package/src/unstable-core-do-not-import/error/formatter.ts +0 -51
  179. package/src/unstable-core-do-not-import/error/getErrorShape.ts +0 -36
  180. package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +0 -29
  181. package/src/unstable-core-do-not-import/http/contentType.ts +0 -99
  182. package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +0 -57
  183. package/src/unstable-core-do-not-import/http/index.ts +0 -23
  184. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +0 -458
  185. package/src/unstable-core-do-not-import/http/types.ts +0 -111
  186. package/src/unstable-core-do-not-import/index.ts +0 -108
  187. package/src/unstable-core-do-not-import/initTRPC.ts +0 -206
  188. package/src/unstable-core-do-not-import/middleware.ts +0 -233
  189. package/src/unstable-core-do-not-import/observable/index.ts +0 -10
  190. package/src/unstable-core-do-not-import/observable/observable.ts +0 -155
  191. package/src/unstable-core-do-not-import/observable/operators.ts +0 -119
  192. package/src/unstable-core-do-not-import/observable/types.ts +0 -76
  193. package/src/unstable-core-do-not-import/parser.ts +0 -94
  194. package/src/unstable-core-do-not-import/procedure.ts +0 -108
  195. package/src/unstable-core-do-not-import/procedureBuilder.ts +0 -458
  196. package/src/unstable-core-do-not-import/rootConfig.ts +0 -90
  197. package/src/unstable-core-do-not-import/router.ts +0 -370
  198. package/src/unstable-core-do-not-import/rpc/codes.ts +0 -44
  199. package/src/unstable-core-do-not-import/rpc/envelopes.ts +0 -136
  200. package/src/unstable-core-do-not-import/rpc/index.ts +0 -21
  201. package/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts +0 -85
  202. package/src/unstable-core-do-not-import/serialize.ts +0 -122
  203. package/src/unstable-core-do-not-import/transformer.ts +0 -202
  204. package/src/unstable-core-do-not-import/types.ts +0 -151
  205. package/src/unstable-core-do-not-import/utils.ts +0 -66
  206. package/unstable-core-do-not-import/index.d.ts +0 -1
  207. package/unstable-core-do-not-import/index.js +0 -1
@@ -1,458 +0,0 @@
1
- import { getErrorShape } from '../error/getErrorShape';
2
- import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError';
3
- import type { ProcedureType } from '../procedure';
4
- import type {
5
- AnyRouter,
6
- inferRouterContext,
7
- inferRouterError,
8
- } from '../router';
9
- import { callProcedure } from '../router';
10
- import type { TRPCResponse } from '../rpc';
11
- import { transformTRPCResponse } from '../transformer';
12
- import type { Maybe } from '../types';
13
- import type { BaseContentTypeHandler } from './contentType';
14
- import { getJsonContentTypeInputs } from './contentType';
15
- import { getHTTPStatusCode } from './getHTTPStatusCode';
16
- import type {
17
- HTTPBaseHandlerOptions,
18
- HTTPHeaders,
19
- HTTPRequest,
20
- HTTPResponse,
21
- ResolveHTTPRequestOptionsContextFn,
22
- ResponseChunk,
23
- TRPCRequestInfo,
24
- } from './types';
25
-
26
- const HTTP_METHOD_PROCEDURE_TYPE_MAP: Record<
27
- string,
28
- ProcedureType | undefined
29
- > = {
30
- GET: 'query',
31
- POST: 'mutation',
32
- };
33
-
34
- const fallbackContentTypeHandler = {
35
- getInputs: getJsonContentTypeInputs,
36
- };
37
-
38
- type PartialBy<TBaseType, TKey extends keyof TBaseType> = Omit<
39
- TBaseType,
40
- TKey
41
- > &
42
- Partial<Pick<TBaseType, TKey>>;
43
-
44
- interface ResolveHTTPRequestOptions<
45
- TRouter extends AnyRouter,
46
- TRequest extends HTTPRequest,
47
- > extends HTTPBaseHandlerOptions<TRouter, TRequest> {
48
- createContext: ResolveHTTPRequestOptionsContextFn<TRouter>;
49
- req: TRequest;
50
- path: string;
51
- error?: Maybe<TRPCError>;
52
- contentTypeHandler?: BaseContentTypeHandler<any>;
53
- preprocessedBody?: boolean;
54
- /**
55
- * Called as soon as the response head is known.
56
- * When streaming, headers will have been generated
57
- * **without** knowing the response body.
58
- *
59
- * Without this callback, streaming is disabled.
60
- */
61
- unstable_onHead: (
62
- headResponse: Omit<HTTPResponse, 'body'>,
63
- isStreaming: boolean,
64
- ) => void;
65
- /**
66
- * Called for every procedure with `[index, result]`.
67
- *
68
- * Will be called a single time with `index = -1` if
69
- * - response is an error
70
- * - response is empty (HEAD request)
71
- *
72
- * Without this callback, streaming is disabled.
73
- */
74
- unstable_onChunk: (chunk: ResponseChunk) => void;
75
- }
76
-
77
- function initResponse<
78
- TRouter extends AnyRouter,
79
- TRequest extends HTTPRequest,
80
- >(initOpts: {
81
- ctx: inferRouterContext<TRouter> | undefined;
82
- paths: string[] | undefined;
83
- type: ProcedureType | 'unknown';
84
- responseMeta?: HTTPBaseHandlerOptions<TRouter, TRequest>['responseMeta'];
85
- untransformedJSON?:
86
- | TRPCResponse<unknown, inferRouterError<TRouter>>
87
- | TRPCResponse<unknown, inferRouterError<TRouter>>[]
88
- | undefined;
89
- errors?: TRPCError[];
90
- }): HTTPResponse {
91
- const {
92
- ctx,
93
- paths,
94
- type,
95
- responseMeta,
96
- untransformedJSON,
97
- errors = [],
98
- } = initOpts;
99
-
100
- let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
101
- const headers: HTTPHeaders = {
102
- 'Content-Type': 'application/json',
103
- };
104
-
105
- const eagerGeneration = !untransformedJSON;
106
- const data = eagerGeneration
107
- ? []
108
- : Array.isArray(untransformedJSON)
109
- ? untransformedJSON
110
- : [untransformedJSON];
111
-
112
- const meta =
113
- responseMeta?.({
114
- ctx,
115
- paths,
116
- type,
117
- data,
118
- errors,
119
- eagerGeneration,
120
- }) ?? {};
121
-
122
- for (const [key, value] of Object.entries(meta.headers ?? {})) {
123
- headers[key] = value;
124
- }
125
- if (meta.status) {
126
- status = meta.status;
127
- }
128
-
129
- return {
130
- status,
131
- headers,
132
- };
133
- }
134
-
135
- async function inputToProcedureCall<
136
- TRouter extends AnyRouter,
137
- TRequest extends HTTPRequest,
138
- >(procedureOpts: {
139
- opts: Pick<
140
- ResolveHTTPRequestOptions<TRouter, TRequest>,
141
- 'onError' | 'req' | 'router'
142
- >;
143
- ctx: inferRouterContext<TRouter> | undefined;
144
- type: 'mutation' | 'query';
145
- input: unknown;
146
- path: string;
147
- }): Promise<TRPCResponse<unknown, inferRouterError<TRouter>>> {
148
- const { opts, ctx, type, input, path } = procedureOpts;
149
- try {
150
- const data = await callProcedure({
151
- procedures: opts.router._def.procedures,
152
- path,
153
- getRawInput: async () => input,
154
- ctx,
155
- type,
156
- });
157
- return {
158
- result: {
159
- data,
160
- },
161
- };
162
- } catch (cause) {
163
- const error = getTRPCErrorFromUnknown(cause);
164
-
165
- opts.onError?.({ error, path, input, ctx, type: type, req: opts.req });
166
-
167
- return {
168
- error: getErrorShape({
169
- config: opts.router._def._config,
170
- error,
171
- type,
172
- path,
173
- input,
174
- ctx,
175
- }),
176
- };
177
- }
178
- }
179
-
180
- function caughtErrorToData<
181
- TRouter extends AnyRouter,
182
- TRequest extends HTTPRequest,
183
- >(
184
- cause: unknown,
185
- errorOpts: {
186
- opts: Pick<
187
- ResolveHTTPRequestOptions<TRouter, TRequest>,
188
- 'onError' | 'req' | 'router'
189
- >;
190
- ctx: inferRouterContext<TRouter> | undefined;
191
- type: ProcedureType | 'unknown';
192
- path?: string;
193
- input?: unknown;
194
- },
195
- ) {
196
- const { router, req, onError } = errorOpts.opts;
197
- const error = getTRPCErrorFromUnknown(cause);
198
- onError?.({
199
- error,
200
- path: errorOpts.path,
201
- input: errorOpts.input,
202
- ctx: errorOpts.ctx,
203
- type: errorOpts.type,
204
- req,
205
- });
206
- const untransformedJSON = {
207
- error: getErrorShape({
208
- config: router._def._config,
209
- error,
210
- type: errorOpts.type,
211
- path: errorOpts.path,
212
- input: errorOpts.input,
213
- ctx: errorOpts.ctx,
214
- }),
215
- };
216
- const transformedJSON = transformTRPCResponse(
217
- router._def._config,
218
- untransformedJSON,
219
- );
220
- const body = JSON.stringify(transformedJSON);
221
- return {
222
- error,
223
- untransformedJSON,
224
- body,
225
- };
226
- }
227
-
228
- /**
229
- * Since `resolveHTTPResponse` is a public API (community adapters),
230
- * let's give it a strong type signature to increase discoverability.
231
- */
232
-
233
- /**
234
- * Non-streaming signature for `resolveHTTPResponse`:
235
- * @param opts.unstable_onHead `undefined`
236
- * @param opts.unstable_onChunk `undefined`
237
- * @returns `Promise<HTTPResponse>`
238
- */
239
- export async function resolveHTTPResponse<
240
- TRouter extends AnyRouter,
241
- TRequest extends HTTPRequest,
242
- >(
243
- opts: Omit<
244
- ResolveHTTPRequestOptions<TRouter, TRequest>,
245
- 'unstable_onChunk' | 'unstable_onHead'
246
- >,
247
- ): Promise<HTTPResponse>;
248
- /**
249
- * Streaming signature for `resolveHTTPResponse`:
250
- * @param opts.unstable_onHead called as soon as the response head is known
251
- * @param opts.unstable_onChunk called for every procedure with `[index, result]`
252
- * @returns `Promise<void>` since the response is streamed
253
- */
254
- export async function resolveHTTPResponse<
255
- TRouter extends AnyRouter,
256
- TRequest extends HTTPRequest,
257
- >(opts: ResolveHTTPRequestOptions<TRouter, TRequest>): Promise<void>;
258
- // implementation
259
- export async function resolveHTTPResponse<
260
- TRouter extends AnyRouter,
261
- TRequest extends HTTPRequest,
262
- >(
263
- opts: PartialBy<
264
- ResolveHTTPRequestOptions<TRouter, TRequest>,
265
- 'unstable_onChunk' | 'unstable_onHead'
266
- >,
267
- ): Promise<HTTPResponse | void> {
268
- const { router, req, unstable_onHead, unstable_onChunk } = opts;
269
-
270
- if (req.method === 'HEAD') {
271
- // can be used for lambda warmup
272
- const headResponse: HTTPResponse = {
273
- status: 204,
274
- };
275
- unstable_onHead?.(headResponse, false);
276
- unstable_onChunk?.([-1, '']);
277
- return headResponse;
278
- }
279
- const contentTypeHandler =
280
- opts.contentTypeHandler ?? fallbackContentTypeHandler;
281
- const batchingEnabled = opts.batching?.enabled ?? true;
282
- const type =
283
- HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? ('unknown' as const);
284
- let ctx: inferRouterContext<TRouter> | undefined = undefined;
285
- let paths: string[] | undefined;
286
-
287
- const isBatchCall = !!req.query.get('batch');
288
- const isStreamCall =
289
- isBatchCall &&
290
- unstable_onHead &&
291
- unstable_onChunk &&
292
- req.headers['trpc-batch-mode'] === 'stream';
293
-
294
- try {
295
- if (opts.error) {
296
- throw opts.error;
297
- }
298
- if (isBatchCall && !batchingEnabled) {
299
- throw new Error(`Batching is not enabled on the server`);
300
- }
301
- /* istanbul ignore if -- @preserve */
302
- if (type === 'subscription') {
303
- throw new TRPCError({
304
- message: 'Subscriptions should use wsLink',
305
- code: 'METHOD_NOT_SUPPORTED',
306
- });
307
- }
308
- if (type === 'unknown') {
309
- throw new TRPCError({
310
- message: `Unexpected request method ${req.method}`,
311
- code: 'METHOD_NOT_SUPPORTED',
312
- });
313
- }
314
-
315
- const inputs = await contentTypeHandler.getInputs({
316
- isBatchCall,
317
- req,
318
- router,
319
- preprocessedBody: opts.preprocessedBody ?? false,
320
- });
321
-
322
- paths = isBatchCall
323
- ? decodeURIComponent(opts.path).split(',')
324
- : [opts.path];
325
- const info: TRPCRequestInfo = {
326
- isBatchCall,
327
- calls: paths.map((path, idx) => ({
328
- path,
329
- type,
330
- input: inputs[idx] ?? undefined,
331
- })),
332
- };
333
- ctx = await opts.createContext({ info });
334
- const promises = paths.map((path, index) =>
335
- inputToProcedureCall({ opts, ctx, type, input: inputs[index], path }),
336
- );
337
-
338
- if (!isStreamCall) {
339
- /**
340
- * Non-streaming response:
341
- * - await all responses in parallel, blocking on the slowest one
342
- * - create headers with known response body
343
- * - return a complete HTTPResponse
344
- */
345
-
346
- const untransformedJSON = await Promise.all(promises);
347
- const errors = untransformedJSON.flatMap((response) =>
348
- 'error' in response ? [response.error] : [],
349
- );
350
-
351
- const headResponse = initResponse({
352
- ctx,
353
- paths,
354
- type,
355
- responseMeta: opts.responseMeta,
356
- untransformedJSON,
357
- errors,
358
- });
359
- unstable_onHead?.(headResponse, false);
360
-
361
- // 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
363
- const transformedJSON = transformTRPCResponse(
364
- router._def._config,
365
- result,
366
- );
367
- const body = JSON.stringify(transformedJSON);
368
- unstable_onChunk?.([-1, body]);
369
-
370
- return {
371
- status: headResponse.status,
372
- headers: headResponse.headers,
373
- body,
374
- };
375
- }
376
-
377
- /**
378
- * Streaming response:
379
- * - block on none, call `onChunk` as soon as each response is ready
380
- * - create headers with minimal data (cannot know the response body in advance)
381
- * - return void
382
- */
383
- const headResponse = initResponse({
384
- ctx,
385
- paths,
386
- type,
387
- responseMeta: opts.responseMeta,
388
- });
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(),
400
- );
401
- indexedPromises.delete(index);
402
-
403
- try {
404
- const transformedJSON = transformTRPCResponse(
405
- router._def._config,
406
- untransformedJSON,
407
- );
408
- const body = JSON.stringify(transformedJSON);
409
-
410
- unstable_onChunk([index, body]);
411
- } catch (cause) {
412
- const path = paths[index];
413
- const input = inputs[index];
414
- const { body } = caughtErrorToData(cause, {
415
- opts,
416
- ctx,
417
- type,
418
- path,
419
- input,
420
- });
421
-
422
- unstable_onChunk([index, body]);
423
- }
424
- }
425
- return;
426
- } catch (cause) {
427
- // we get here if
428
- // - batching is called when it's not enabled
429
- // - `createContext()` throws
430
- // - `router._def._config.transformer.output.serialize()` throws
431
- // - post body is too large
432
- // - input deserialization fails
433
- // - `errorFormatter` return value is malformed
434
- const { error, untransformedJSON, body } = caughtErrorToData(cause, {
435
- opts,
436
- ctx,
437
- type,
438
- });
439
-
440
- const headResponse = initResponse({
441
- ctx,
442
- paths,
443
- type,
444
- responseMeta: opts.responseMeta,
445
- untransformedJSON,
446
- errors: [error],
447
- });
448
- unstable_onHead?.(headResponse, false);
449
-
450
- unstable_onChunk?.([-1, body]);
451
-
452
- return {
453
- status: headResponse.status,
454
- headers: headResponse.headers,
455
- body,
456
- };
457
- }
458
- }
@@ -1,111 +0,0 @@
1
- import type { TRPCError } from '../error/TRPCError';
2
- import type { ProcedureType } from '../procedure';
3
- import type {
4
- AnyRouter,
5
- inferRouterContext,
6
- inferRouterError,
7
- } from '../router';
8
- import type { TRPCResponse } from '../rpc';
9
- import type { Dict } from '../types';
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];
20
-
21
- export interface ResponseMeta {
22
- status?: number;
23
- headers?: HTTPHeaders;
24
- }
25
-
26
- /**
27
- * @internal
28
- */
29
- export type ResponseMetaFn<TRouter extends AnyRouter> = (opts: {
30
- data: TRPCResponse<unknown, inferRouterError<TRouter>>[];
31
- ctx?: inferRouterContext<TRouter>;
32
- /**
33
- * The different tRPC paths requested
34
- **/
35
- paths?: string[];
36
- type: ProcedureType | 'unknown';
37
- errors: TRPCError[];
38
- /**
39
- * `true` if the `ResponseMeta` are being
40
- * generated without knowing the response data
41
- * (e.g. for streaming requests).
42
- */
43
- eagerGeneration?: boolean;
44
- }) => ResponseMeta;
45
-
46
- export interface HTTPRequest {
47
- method: string;
48
- query: URLSearchParams;
49
- headers: HTTPHeaders;
50
- body: unknown;
51
- }
52
-
53
- /**
54
- * Base interface for anything using HTTP
55
- */
56
- export interface HTTPBaseHandlerOptions<TRouter extends AnyRouter, TRequest>
57
- extends BaseHandlerOptions<TRouter, TRequest> {
58
- /**
59
- * Add handler to be called before response is sent to the user
60
- * Useful for setting cache headers
61
- * @link https://trpc.io/docs/v11/caching
62
- */
63
- responseMeta?: ResponseMetaFn<TRouter>;
64
- }
65
-
66
- /** @internal */
67
- export type ProcedureCall = {
68
- type: ProcedureType;
69
- input?: unknown;
70
- path: string;
71
- };
72
-
73
- /**
74
- * Information about the incoming request
75
- * @internal
76
- */
77
- export type TRPCRequestInfo = {
78
- isBatchCall: boolean;
79
- calls: ProcedureCall[];
80
- };
81
-
82
- /**
83
- * Inner createContext function for `resolveHTTPResponse` used to forward `TRPCRequestInfo` to `createContext`
84
- * @internal
85
- */
86
- export type ResolveHTTPRequestOptionsContextFn<TRouter extends AnyRouter> =
87
- (opts: { info: TRPCRequestInfo }) => Promise<inferRouterContext<TRouter>>;
88
-
89
- /**
90
- * @internal
91
- */
92
- export type OnErrorFunction<TRouter extends AnyRouter, TRequest> = (opts: {
93
- error: TRPCError;
94
- type: ProcedureType | 'unknown';
95
- path: string | undefined;
96
- req: TRequest;
97
- input: unknown;
98
- ctx: inferRouterContext<TRouter> | undefined;
99
- }) => void;
100
-
101
- /**
102
- * Base interface for any response handler
103
- * @internal
104
- */
105
- export interface BaseHandlerOptions<TRouter extends AnyRouter, TRequest> {
106
- onError?: OnErrorFunction<TRouter, TRequest>;
107
- batching?: {
108
- enabled: boolean;
109
- };
110
- router: TRouter;
111
- }
@@ -1,108 +0,0 @@
1
- /**
2
- * This file is here to make TypeScript happy and prevent _"The inferred type of 'createContext' cannot be named without a reference to [...]"_.
3
- *
4
- * We're basically just re-exporting everything from core.
5
- *
6
- * If you need to import anything from here, please open an issue at https://github.com/trpc/trpc/issues
7
- */
8
- export type { DefaultErrorShape, DefaultErrorData } from './error/formatter';
9
- export { getErrorShape } from './error/getErrorShape';
10
- export {
11
- TRPCError,
12
- getCauseFromUnknown,
13
- getTRPCErrorFromUnknown,
14
- } from './error/TRPCError';
15
-
16
- export { initTRPC } from './initTRPC';
17
- export type {
18
- AnyMiddlewareFunction,
19
- MiddlewareFunction,
20
- MiddlewareBuilder,
21
- } from './middleware';
22
- export {
23
- createInputMiddleware,
24
- createOutputMiddleware,
25
- experimental_standaloneMiddleware,
26
- middlewareMarker,
27
- } from './middleware';
28
- export type { inferParser } from './parser';
29
- export type {
30
- Procedure,
31
- ProcedureType,
32
- AnyProcedure,
33
- AnyQueryProcedure,
34
- AnyMutationProcedure,
35
- AnySubscriptionProcedure,
36
- ProcedureArgs,
37
- ProcedureOptions,
38
- MutationProcedure,
39
- inferHandlerInput,
40
- inferProcedureInput,
41
- inferProcedureOutput,
42
- inferTransformedProcedureOutput,
43
- inferTransformedSubscriptionOutput,
44
- inferProcedureParams,
45
- QueryProcedure,
46
- SubscriptionProcedure,
47
- } from './procedure';
48
- export { procedureTypes } from './procedure';
49
- export type { ProcedureBuilder } from './procedureBuilder';
50
- export { unsetMarker, createBuilder } from './procedureBuilder';
51
- export type { AnyRootConfig, RootConfig } from './rootConfig';
52
- export type {
53
- AnyRouter,
54
- ProcedureRecord,
55
- ProcedureRouterRecord,
56
- CreateRouterInner,
57
- Router,
58
- RouterCaller,
59
- AnyRouterDef,
60
- inferRouterContext,
61
- inferRouterError,
62
- inferRouterInputs,
63
- inferRouterMeta,
64
- inferRouterOutputs,
65
- createRouterFactory,
66
- } from './router';
67
-
68
- export type { TRPCInferrable, inferErrorShape } from './TRPCInferrable';
69
- export { callProcedure, mergeRouters } from './router';
70
- export type {
71
- CombinedDataTransformer,
72
- CombinedDataTransformerClient,
73
- DataTransformer,
74
- DataTransformerOptions,
75
- DefaultDataTransformer,
76
- } from './transformer';
77
- export { transformResult, transformTRPCResponse } from './transformer';
78
-
79
- export { createFlatProxy, createRecursiveProxy } from './createProxy';
80
-
81
- // For `.d.ts` files https://github.com/trpc/trpc/issues/3943
82
- export type { SerializeObject, Serialize } from './serialize';
83
-
84
- export type {
85
- DeepPartial,
86
- Dict,
87
- DistributiveOmit,
88
- Filter,
89
- FilterKeys,
90
- Maybe,
91
- MaybePromise,
92
- Simplify,
93
- Unwrap,
94
- WithoutIndexSignature,
95
- Overwrite,
96
- PickFirstDefined,
97
- ValidateShape,
98
- IntersectionError,
99
- ProtectedIntersection,
100
- } from './types';
101
- export { isObject } from './utils';
102
-
103
- /**
104
- * These should be re-exported from separate entrypoints in server package
105
- */
106
- export * from './observable';
107
- export * from './rpc';
108
- export * from './http';