@trpc/server 11.0.0-alpha-tmp-export-from-main.218 → 11.0.0-alpha-tmp-export-from-main-nuke-core.239

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