@trpc/server 11.0.0-rc.359 → 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 (168) hide show
  1. package/dist/@trpc/server/http.d.ts +7 -6
  2. package/dist/@trpc/server/http.d.ts.map +1 -1
  3. package/dist/@trpc/server/index.d.ts +1 -1
  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 +6 -65
  12. package/dist/adapters/aws-lambda/index.mjs +8 -61
  13. package/dist/adapters/express.d.ts.map +1 -1
  14. package/dist/adapters/express.js +1 -1
  15. package/dist/adapters/express.mjs +1 -1
  16. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +10 -1
  17. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  18. package/dist/adapters/fastify/fastifyRequestHandler.js +12 -55
  19. package/dist/adapters/fastify/fastifyRequestHandler.mjs +13 -56
  20. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  21. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  22. package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -5
  23. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  24. package/dist/adapters/fetch/fetchRequestHandler.js +28 -71
  25. package/dist/adapters/fetch/fetchRequestHandler.mjs +29 -72
  26. package/dist/adapters/fetch/types.d.ts +9 -12
  27. package/dist/adapters/fetch/types.d.ts.map +1 -1
  28. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts +2 -7
  29. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
  30. package/dist/adapters/next.d.ts.map +1 -1
  31. package/dist/adapters/next.js +1 -2
  32. package/dist/adapters/next.mjs +1 -2
  33. package/dist/adapters/node-http/incomingMessageToRequest.d.ts +18 -0
  34. package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -0
  35. package/dist/adapters/node-http/incomingMessageToRequest.js +71 -0
  36. package/dist/adapters/node-http/incomingMessageToRequest.mjs +69 -0
  37. package/dist/adapters/node-http/index.d.ts +1 -0
  38. package/dist/adapters/node-http/index.d.ts.map +1 -1
  39. package/dist/adapters/node-http/index.js +2 -0
  40. package/dist/adapters/node-http/index.mjs +1 -0
  41. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
  42. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  43. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +27 -72
  44. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +28 -73
  45. package/dist/adapters/node-http/types.d.ts +2 -14
  46. package/dist/adapters/node-http/types.d.ts.map +1 -1
  47. package/dist/adapters/standalone.d.ts.map +1 -1
  48. package/dist/adapters/standalone.js +0 -1
  49. package/dist/adapters/standalone.mjs +0 -1
  50. package/dist/adapters/ws.d.ts +2 -12
  51. package/dist/adapters/ws.d.ts.map +1 -1
  52. package/dist/adapters/ws.js +1 -1
  53. package/dist/adapters/ws.mjs +1 -1
  54. package/dist/bundle-analysis.json +173 -259
  55. package/dist/http.js +4 -4
  56. package/dist/http.mjs +2 -2
  57. package/dist/index.js +2 -2
  58. package/dist/index.mjs +1 -1
  59. package/dist/unstable-core-do-not-import/http/contentType.d.ts +8 -28
  60. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
  61. package/dist/unstable-core-do-not-import/http/contentType.js +164 -38
  62. package/dist/unstable-core-do-not-import/http/contentType.mjs +164 -38
  63. package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts +14 -0
  64. package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts.map +1 -0
  65. package/dist/unstable-core-do-not-import/http/contentTypeParsers.js +14 -0
  66. package/dist/unstable-core-do-not-import/http/contentTypeParsers.mjs +12 -0
  67. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +1 -1
  68. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
  69. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +7 -43
  70. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
  71. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +115 -124
  72. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +116 -125
  73. package/dist/unstable-core-do-not-import/http/types.d.ts +25 -29
  74. package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
  75. package/dist/unstable-core-do-not-import/initTRPC.d.ts +1 -1
  76. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +1 -3
  77. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
  78. package/dist/unstable-core-do-not-import/procedureBuilder.js +0 -2
  79. package/dist/unstable-core-do-not-import/procedureBuilder.mjs +1 -2
  80. package/dist/unstable-core-do-not-import/rootConfig.d.ts +12 -0
  81. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
  82. package/dist/unstable-core-do-not-import/utils.d.ts +3 -0
  83. package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
  84. package/dist/unstable-core-do-not-import/utils.js +2 -0
  85. package/dist/unstable-core-do-not-import/utils.mjs +2 -1
  86. package/dist/unstable-core-do-not-import.d.ts +9 -3
  87. package/dist/unstable-core-do-not-import.d.ts.map +1 -1
  88. package/dist/unstable-core-do-not-import.js +11 -9
  89. package/dist/unstable-core-do-not-import.mjs +7 -6
  90. package/package.json +3 -14
  91. package/src/@trpc/server/http.ts +7 -13
  92. package/src/@trpc/server/index.ts +1 -0
  93. package/src/adapters/aws-lambda/getPlanner.ts +191 -0
  94. package/src/adapters/aws-lambda/index.ts +43 -92
  95. package/src/adapters/express.ts +1 -6
  96. package/src/adapters/fastify/fastifyRequestHandler.ts +22 -77
  97. package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
  98. package/src/adapters/fetch/fetchRequestHandler.ts +34 -98
  99. package/src/adapters/fetch/types.ts +24 -15
  100. package/src/adapters/next-app-dir/nextAppDirCaller.ts +2 -9
  101. package/src/adapters/next.ts +1 -6
  102. package/src/adapters/node-http/incomingMessageToRequest.ts +94 -0
  103. package/src/adapters/node-http/index.ts +1 -0
  104. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +31 -110
  105. package/src/adapters/node-http/types.ts +9 -19
  106. package/src/adapters/standalone.ts +1 -2
  107. package/src/adapters/ws.ts +9 -14
  108. package/src/unstable-core-do-not-import/http/contentType.ts +199 -82
  109. package/src/unstable-core-do-not-import/http/contentTypeParsers.ts +29 -0
  110. package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +2 -2
  111. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +133 -205
  112. package/src/unstable-core-do-not-import/http/types.ts +25 -32
  113. package/src/unstable-core-do-not-import/procedureBuilder.ts +2 -4
  114. package/src/unstable-core-do-not-import/rootConfig.ts +21 -0
  115. package/src/unstable-core-do-not-import/utils.ts +4 -0
  116. package/src/unstable-core-do-not-import.ts +9 -3
  117. package/adapters/node-http/content-type/form-data/index.d.ts +0 -1
  118. package/adapters/node-http/content-type/form-data/index.js +0 -1
  119. package/adapters/node-http/content-type/json/index.d.ts +0 -1
  120. package/adapters/node-http/content-type/json/index.js +0 -1
  121. package/dist/adapters/aws-lambda/utils.d.ts +0 -40
  122. package/dist/adapters/aws-lambda/utils.d.ts.map +0 -1
  123. package/dist/adapters/aws-lambda/utils.js +0 -100
  124. package/dist/adapters/aws-lambda/utils.mjs +0 -93
  125. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -73
  126. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
  127. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -161
  128. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -157
  129. package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -26
  130. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +0 -1
  131. package/dist/adapters/node-http/content-type/form-data/index.js +0 -135
  132. package/dist/adapters/node-http/content-type/form-data/index.mjs +0 -108
  133. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
  134. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
  135. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
  136. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
  137. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
  138. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
  139. package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
  140. package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
  141. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
  142. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
  143. package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -30
  144. package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -26
  145. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +0 -7
  146. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +0 -1
  147. package/dist/adapters/node-http/content-type/json/getPostBody.js +0 -53
  148. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +0 -51
  149. package/dist/adapters/node-http/content-type/json/index.d.ts +0 -2
  150. package/dist/adapters/node-http/content-type/json/index.d.ts.map +0 -1
  151. package/dist/adapters/node-http/content-type/json/index.js +0 -16
  152. package/dist/adapters/node-http/content-type/json/index.mjs +0 -14
  153. package/dist/adapters/node-http/internals/contentType.d.ts +0 -9
  154. package/dist/adapters/node-http/internals/contentType.d.ts.map +0 -1
  155. package/dist/adapters/node-http/internals/contentType.js +0 -8
  156. package/dist/adapters/node-http/internals/contentType.mjs +0 -6
  157. package/dist/unstable-core-do-not-import/http/index.d.ts +0 -12
  158. package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
  159. package/src/adapters/aws-lambda/utils.ts +0 -164
  160. package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +0 -277
  161. package/src/adapters/node-http/content-type/form-data/index.ts +0 -163
  162. package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +0 -56
  163. package/src/adapters/node-http/content-type/form-data/streamSlice.ts +0 -56
  164. package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +0 -89
  165. package/src/adapters/node-http/content-type/json/getPostBody.ts +0 -58
  166. package/src/adapters/node-http/content-type/json/index.ts +0 -11
  167. package/src/adapters/node-http/internals/contentType.ts +0 -31
  168. package/src/unstable-core-do-not-import/http/index.ts +0 -29
package/dist/http.js CHANGED
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  var getHTTPStatusCode = require('./unstable-core-do-not-import/http/getHTTPStatusCode.js');
4
- var resolveHTTPResponse = require('./unstable-core-do-not-import/http/resolveHTTPResponse.js');
5
4
  var batchStreamFormatter = require('./unstable-core-do-not-import/http/batchStreamFormatter.js');
6
- var contentType = require('./unstable-core-do-not-import/http/contentType.js');
5
+ var contentTypeParsers = require('./unstable-core-do-not-import/http/contentTypeParsers.js');
6
+ var resolveHTTPResponse = require('./unstable-core-do-not-import/http/resolveHTTPResponse.js');
7
7
  var toURL = require('./unstable-core-do-not-import/http/toURL.js');
8
8
  require('./unstable-core-do-not-import/rootConfig.js');
9
9
 
@@ -11,7 +11,7 @@ require('./unstable-core-do-not-import/rootConfig.js');
11
11
 
12
12
  exports.getHTTPStatusCode = getHTTPStatusCode.getHTTPStatusCode;
13
13
  exports.getHTTPStatusCodeFromError = getHTTPStatusCode.getHTTPStatusCodeFromError;
14
- exports.resolveHTTPResponse = resolveHTTPResponse.resolveHTTPResponse;
15
14
  exports.getBatchStreamFormatter = batchStreamFormatter.getBatchStreamFormatter;
16
- exports.getJsonContentTypeInputs = contentType.getJsonContentTypeInputs;
15
+ exports.octetInputParser = contentTypeParsers.octetInputParser;
16
+ exports.resolveResponse = resolveHTTPResponse.resolveResponse;
17
17
  exports.toURL = toURL.toURL;
package/dist/http.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  export { getHTTPStatusCode, getHTTPStatusCodeFromError } from './unstable-core-do-not-import/http/getHTTPStatusCode.mjs';
2
- export { resolveHTTPResponse } from './unstable-core-do-not-import/http/resolveHTTPResponse.mjs';
3
2
  export { getBatchStreamFormatter } from './unstable-core-do-not-import/http/batchStreamFormatter.mjs';
4
- export { getJsonContentTypeInputs } from './unstable-core-do-not-import/http/contentType.mjs';
3
+ export { octetInputParser } from './unstable-core-do-not-import/http/contentTypeParsers.mjs';
4
+ export { resolveResponse } from './unstable-core-do-not-import/http/resolveHTTPResponse.mjs';
5
5
  export { toURL } from './unstable-core-do-not-import/http/toURL.mjs';
6
6
  import './unstable-core-do-not-import/rootConfig.mjs';
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var createProxy = require('./unstable-core-do-not-import/createProxy.js');
4
- var getErrorShape = require('./unstable-core-do-not-import/error/getErrorShape.js');
5
4
  var TRPCError = require('./unstable-core-do-not-import/error/TRPCError.js');
5
+ var getErrorShape = require('./unstable-core-do-not-import/error/getErrorShape.js');
6
6
  var router = require('./unstable-core-do-not-import/router.js');
7
7
  var transformer = require('./unstable-core-do-not-import/transformer.js');
8
8
  var initTRPC = require('./unstable-core-do-not-import/initTRPC.js');
@@ -12,9 +12,9 @@ require('./unstable-core-do-not-import/rootConfig.js');
12
12
 
13
13
 
14
14
  exports.createTRPCFlatProxy = createProxy.createFlatProxy;
15
- exports.getErrorShape = getErrorShape.getErrorShape;
16
15
  exports.TRPCError = TRPCError.TRPCError;
17
16
  exports.getTRPCErrorFromUnknown = TRPCError.getTRPCErrorFromUnknown;
17
+ exports.getErrorShape = getErrorShape.getErrorShape;
18
18
  exports.callProcedure = router.callProcedure;
19
19
  exports.callTRPCProcedure = router.callProcedure;
20
20
  exports.transformTRPCResponse = transformer.transformTRPCResponse;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  export { createFlatProxy as createTRPCFlatProxy } from './unstable-core-do-not-import/createProxy.mjs';
2
- export { getErrorShape } from './unstable-core-do-not-import/error/getErrorShape.mjs';
3
2
  export { TRPCError, getTRPCErrorFromUnknown } from './unstable-core-do-not-import/error/TRPCError.mjs';
3
+ export { getErrorShape } from './unstable-core-do-not-import/error/getErrorShape.mjs';
4
4
  export { callProcedure, callProcedure as callTRPCProcedure } from './unstable-core-do-not-import/router.mjs';
5
5
  export { transformTRPCResponse } from './unstable-core-do-not-import/transformer.mjs';
6
6
  export { initTRPC } from './unstable-core-do-not-import/initTRPC.mjs';
@@ -1,29 +1,9 @@
1
- import { TRPCError } from '../error/TRPCError';
2
- import type { AnyRouter } from '../router';
3
- import type { MaybePromise } from '../types';
4
- import type { HTTPRequest } from './types';
5
- type GetInputs = (opts: {
6
- req: HTTPRequest;
7
- isBatchCall: boolean;
8
- router: AnyRouter;
9
- preprocessedBody: boolean;
10
- }) => MaybePromise<Record<number, unknown>>;
11
- export type BodyResult = {
12
- ok: true;
13
- data: unknown;
14
- /**
15
- * If the HTTP handler has already parsed the body
16
- */
17
- preprocessed: boolean;
18
- } | {
19
- ok: false;
20
- error: TRPCError;
21
- };
22
- export type BaseContentTypeHandler<TOptions> = {
23
- isMatch(opts: TOptions): boolean;
24
- getBody: (opts: TOptions) => MaybePromise<BodyResult>;
25
- getInputs: GetInputs;
26
- };
27
- export declare const getJsonContentTypeInputs: GetInputs;
28
- export {};
1
+ import type { RootConfig } from '../rootConfig';
2
+ import type { TRPCRequestInfo } from './types';
3
+ export declare function getRequestInfo(opts: {
4
+ path: string;
5
+ req: Request;
6
+ searchParams: URLSearchParams;
7
+ config: RootConfig<any>;
8
+ }): TRPCRequestInfo;
29
9
  //# sourceMappingURL=contentType.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"contentType.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/contentType.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,KAAK,SAAS,GAAG,CAAC,IAAI,EAAE;IACtB,GAAG,EAAE,WAAW,CAAC;IACjB,WAAW,EAAE,OAAO,CAAC;IACrB,MAAM,EAAE,SAAS,CAAC;IAClB,gBAAgB,EAAE,OAAO,CAAC;CAC3B,KAAK,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAE5C,MAAM,MAAM,UAAU,GAClB;IACE,EAAE,EAAE,IAAI,CAAC;IACT,IAAI,EAAE,OAAO,CAAC;IACd;;OAEG;IACH,YAAY,EAAE,OAAO,CAAC;CACvB,GACD;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,SAAS,CAAA;CAAE,CAAC;AAEpC,MAAM,MAAM,sBAAsB,CAAC,QAAQ,IAAI;IAC7C,OAAO,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC;IACjC,OAAO,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,YAAY,CAAC,UAAU,CAAC,CAAC;IACtD,SAAS,EAAE,SAAS,CAAC;CACtB,CAAC;AAqCF,eAAO,MAAM,wBAAwB,EAAE,SAgCtC,CAAC"}
1
+ {"version":3,"file":"contentType.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/contentType.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEhD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AA4M/C,wBAAgB,cAAc,CAAC,IAAI,EAAE;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,OAAO,CAAC;IACb,YAAY,EAAE,eAAe,CAAC;IAC9B,MAAM,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC;CACzB,GAAG,eAAe,CAGlB"}
@@ -1,54 +1,180 @@
1
1
  'use strict';
2
2
 
3
3
  var TRPCError = require('../error/TRPCError.js');
4
+ var utils = require('../utils.js');
4
5
 
5
- function getRawProcedureInputOrThrow(opts) {
6
- const { req } = opts;
7
- try {
8
- if (req.method === 'GET') {
9
- if (!req.query.has('input')) {
10
- return undefined;
6
+ /**
7
+ * Memoize a function that takes no arguments
8
+ * @internal
9
+ */ function memo(fn) {
10
+ let promise = null;
11
+ let value = utils.unsetMarker;
12
+ return {
13
+ /**
14
+ * Lazily read the value
15
+ */ read: async ()=>{
16
+ if (value !== utils.unsetMarker) {
17
+ return value;
11
18
  }
12
- const raw = req.query.get('input');
13
- return JSON.parse(raw);
14
- }
15
- if (!opts.preprocessedBody && typeof req.body === 'string') {
16
- // A mutation with no inputs will have req.body === ''
17
- return req.body.length === 0 ? undefined : JSON.parse(req.body);
19
+ if (promise === null) {
20
+ // dedupes promises and catches errors
21
+ promise = fn().catch((cause)=>{
22
+ if (cause instanceof TRPCError.TRPCError) {
23
+ throw cause;
24
+ }
25
+ throw new TRPCError.TRPCError({
26
+ code: 'BAD_REQUEST',
27
+ message: cause instanceof Error ? cause.message : 'Invalid input',
28
+ cause
29
+ });
30
+ });
31
+ }
32
+ value = await promise;
33
+ promise = null;
34
+ return value;
35
+ },
36
+ /**
37
+ * Get an already stored result
38
+ */ result: ()=>{
39
+ return value !== utils.unsetMarker ? value : undefined;
18
40
  }
19
- return req.body;
20
- } catch (cause) {
21
- throw new TRPCError.TRPCError({
22
- code: 'PARSE_ERROR',
23
- cause
41
+ };
42
+ }
43
+ const jsonContentTypeHandler = {
44
+ isMatch (req) {
45
+ return !!req.headers.get('content-type')?.startsWith('application/json');
46
+ },
47
+ parse (opts) {
48
+ const { req } = opts;
49
+ const isBatchCall = opts.searchParams.get('batch') === '1';
50
+ const paths = isBatchCall ? opts.path.split(',') : [
51
+ opts.path
52
+ ];
53
+ const getInputs = memo(async ()=>{
54
+ let inputs = undefined;
55
+ if (req.method === 'GET') {
56
+ const queryInput = opts.searchParams.get('input');
57
+ if (queryInput) {
58
+ inputs = JSON.parse(queryInput);
59
+ }
60
+ } else {
61
+ inputs = await req.json();
62
+ }
63
+ if (inputs === undefined) {
64
+ return {};
65
+ }
66
+ if (!isBatchCall) {
67
+ return {
68
+ 0: opts.config.transformer.input.deserialize(inputs)
69
+ };
70
+ }
71
+ if (!utils.isObject(inputs)) {
72
+ throw new TRPCError.TRPCError({
73
+ code: 'BAD_REQUEST',
74
+ message: '"input" needs to be an object when doing a batch call'
75
+ });
76
+ }
77
+ const acc = {};
78
+ for (const index of paths.keys()){
79
+ const input = inputs[index];
80
+ if (input !== undefined) {
81
+ acc[index] = opts.config.transformer.input.deserialize(input);
82
+ }
83
+ }
84
+ return acc;
24
85
  });
86
+ return {
87
+ isBatchCall,
88
+ calls: paths.map((path, index)=>({
89
+ path,
90
+ getRawInput: async ()=>{
91
+ const inputs = await getInputs.read();
92
+ return inputs[index];
93
+ },
94
+ result: ()=>{
95
+ return getInputs.result()?.[index];
96
+ }
97
+ }))
98
+ };
25
99
  }
26
- }
27
- const deserializeInputValue = (rawValue, transformer)=>{
28
- return typeof rawValue !== 'undefined' ? transformer.input.deserialize(rawValue) : rawValue;
29
100
  };
30
- const getJsonContentTypeInputs = (opts)=>{
31
- const rawInput = getRawProcedureInputOrThrow(opts);
32
- const transformer = opts.router._def._config.transformer;
33
- if (!opts.isBatchCall) {
101
+ const formDataContentTypeHandler = {
102
+ isMatch (req) {
103
+ return !!req.headers.get('content-type')?.startsWith('multipart/form-data');
104
+ },
105
+ parse (opts) {
106
+ const { req } = opts;
107
+ if (req.method !== 'POST') {
108
+ throw new TRPCError.TRPCError({
109
+ code: 'METHOD_NOT_SUPPORTED',
110
+ message: 'Only POST requests are supported for multipart/form-data requests'
111
+ });
112
+ }
113
+ const getInputs = memo(async ()=>{
114
+ const fd = await req.formData();
115
+ return fd;
116
+ });
34
117
  return {
35
- 0: deserializeInputValue(rawInput, transformer)
118
+ calls: [
119
+ {
120
+ path: opts.path,
121
+ getRawInput: getInputs.read,
122
+ result: getInputs.result
123
+ }
124
+ ],
125
+ isBatchCall: false
36
126
  };
37
127
  }
38
- /* istanbul ignore if */ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
39
- throw new TRPCError.TRPCError({
40
- code: 'BAD_REQUEST',
41
- message: '"input" needs to be an object when doing a batch call'
128
+ };
129
+ const octetStreamContentTypeHandler = {
130
+ isMatch (req) {
131
+ return !!req.headers.get('content-type')?.startsWith('application/octet-stream');
132
+ },
133
+ parse (opts) {
134
+ const { req } = opts;
135
+ if (req.method !== 'POST') {
136
+ throw new TRPCError.TRPCError({
137
+ code: 'METHOD_NOT_SUPPORTED',
138
+ message: 'Only POST requests are supported for application/octet-stream requests'
139
+ });
140
+ }
141
+ const getInputs = memo(async ()=>{
142
+ return req.body;
42
143
  });
144
+ return {
145
+ calls: [
146
+ {
147
+ path: opts.path,
148
+ getRawInput: getInputs.read,
149
+ result: getInputs.result
150
+ }
151
+ ],
152
+ isBatchCall: false
153
+ };
43
154
  }
44
- const input = {};
45
- for(const key in rawInput){
46
- const k = key;
47
- const rawValue = rawInput[k];
48
- const value = deserializeInputValue(rawValue, transformer);
49
- input[k] = value;
50
- }
51
- return input;
52
155
  };
156
+ const handlers = [
157
+ jsonContentTypeHandler,
158
+ formDataContentTypeHandler,
159
+ octetStreamContentTypeHandler
160
+ ];
161
+ function getContentTypeHandler(req) {
162
+ const handler = handlers.find((handler)=>handler.isMatch(req));
163
+ if (handler) {
164
+ return handler;
165
+ }
166
+ if (!handler && req.method === 'GET') {
167
+ // fallback to JSON for get requests so GET-requests can be opened in browser easily
168
+ return jsonContentTypeHandler;
169
+ }
170
+ throw new TRPCError.TRPCError({
171
+ code: 'UNSUPPORTED_MEDIA_TYPE',
172
+ message: req.headers.has('content-type') ? `Unsupported content-type "${req.headers.get('content-type')}` : 'Missing content-type header'
173
+ });
174
+ }
175
+ function getRequestInfo(opts) {
176
+ const handler = getContentTypeHandler(opts.req);
177
+ return handler.parse(opts);
178
+ }
53
179
 
54
- exports.getJsonContentTypeInputs = getJsonContentTypeInputs;
180
+ exports.getRequestInfo = getRequestInfo;
@@ -1,52 +1,178 @@
1
1
  import { TRPCError } from '../error/TRPCError.mjs';
2
+ import { unsetMarker, isObject } from '../utils.mjs';
2
3
 
3
- function getRawProcedureInputOrThrow(opts) {
4
- const { req } = opts;
5
- try {
6
- if (req.method === 'GET') {
7
- if (!req.query.has('input')) {
8
- return undefined;
4
+ /**
5
+ * Memoize a function that takes no arguments
6
+ * @internal
7
+ */ function memo(fn) {
8
+ let promise = null;
9
+ let value = unsetMarker;
10
+ return {
11
+ /**
12
+ * Lazily read the value
13
+ */ read: async ()=>{
14
+ if (value !== unsetMarker) {
15
+ return value;
9
16
  }
10
- const raw = req.query.get('input');
11
- return JSON.parse(raw);
12
- }
13
- if (!opts.preprocessedBody && typeof req.body === 'string') {
14
- // A mutation with no inputs will have req.body === ''
15
- return req.body.length === 0 ? undefined : JSON.parse(req.body);
17
+ if (promise === null) {
18
+ // dedupes promises and catches errors
19
+ promise = fn().catch((cause)=>{
20
+ if (cause instanceof TRPCError) {
21
+ throw cause;
22
+ }
23
+ throw new TRPCError({
24
+ code: 'BAD_REQUEST',
25
+ message: cause instanceof Error ? cause.message : 'Invalid input',
26
+ cause
27
+ });
28
+ });
29
+ }
30
+ value = await promise;
31
+ promise = null;
32
+ return value;
33
+ },
34
+ /**
35
+ * Get an already stored result
36
+ */ result: ()=>{
37
+ return value !== unsetMarker ? value : undefined;
16
38
  }
17
- return req.body;
18
- } catch (cause) {
19
- throw new TRPCError({
20
- code: 'PARSE_ERROR',
21
- cause
39
+ };
40
+ }
41
+ const jsonContentTypeHandler = {
42
+ isMatch (req) {
43
+ return !!req.headers.get('content-type')?.startsWith('application/json');
44
+ },
45
+ parse (opts) {
46
+ const { req } = opts;
47
+ const isBatchCall = opts.searchParams.get('batch') === '1';
48
+ const paths = isBatchCall ? opts.path.split(',') : [
49
+ opts.path
50
+ ];
51
+ const getInputs = memo(async ()=>{
52
+ let inputs = undefined;
53
+ if (req.method === 'GET') {
54
+ const queryInput = opts.searchParams.get('input');
55
+ if (queryInput) {
56
+ inputs = JSON.parse(queryInput);
57
+ }
58
+ } else {
59
+ inputs = await req.json();
60
+ }
61
+ if (inputs === undefined) {
62
+ return {};
63
+ }
64
+ if (!isBatchCall) {
65
+ return {
66
+ 0: opts.config.transformer.input.deserialize(inputs)
67
+ };
68
+ }
69
+ if (!isObject(inputs)) {
70
+ throw new TRPCError({
71
+ code: 'BAD_REQUEST',
72
+ message: '"input" needs to be an object when doing a batch call'
73
+ });
74
+ }
75
+ const acc = {};
76
+ for (const index of paths.keys()){
77
+ const input = inputs[index];
78
+ if (input !== undefined) {
79
+ acc[index] = opts.config.transformer.input.deserialize(input);
80
+ }
81
+ }
82
+ return acc;
22
83
  });
84
+ return {
85
+ isBatchCall,
86
+ calls: paths.map((path, index)=>({
87
+ path,
88
+ getRawInput: async ()=>{
89
+ const inputs = await getInputs.read();
90
+ return inputs[index];
91
+ },
92
+ result: ()=>{
93
+ return getInputs.result()?.[index];
94
+ }
95
+ }))
96
+ };
23
97
  }
24
- }
25
- const deserializeInputValue = (rawValue, transformer)=>{
26
- return typeof rawValue !== 'undefined' ? transformer.input.deserialize(rawValue) : rawValue;
27
98
  };
28
- const getJsonContentTypeInputs = (opts)=>{
29
- const rawInput = getRawProcedureInputOrThrow(opts);
30
- const transformer = opts.router._def._config.transformer;
31
- if (!opts.isBatchCall) {
99
+ const formDataContentTypeHandler = {
100
+ isMatch (req) {
101
+ return !!req.headers.get('content-type')?.startsWith('multipart/form-data');
102
+ },
103
+ parse (opts) {
104
+ const { req } = opts;
105
+ if (req.method !== 'POST') {
106
+ throw new TRPCError({
107
+ code: 'METHOD_NOT_SUPPORTED',
108
+ message: 'Only POST requests are supported for multipart/form-data requests'
109
+ });
110
+ }
111
+ const getInputs = memo(async ()=>{
112
+ const fd = await req.formData();
113
+ return fd;
114
+ });
32
115
  return {
33
- 0: deserializeInputValue(rawInput, transformer)
116
+ calls: [
117
+ {
118
+ path: opts.path,
119
+ getRawInput: getInputs.read,
120
+ result: getInputs.result
121
+ }
122
+ ],
123
+ isBatchCall: false
34
124
  };
35
125
  }
36
- /* istanbul ignore if */ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
37
- throw new TRPCError({
38
- code: 'BAD_REQUEST',
39
- message: '"input" needs to be an object when doing a batch call'
126
+ };
127
+ const octetStreamContentTypeHandler = {
128
+ isMatch (req) {
129
+ return !!req.headers.get('content-type')?.startsWith('application/octet-stream');
130
+ },
131
+ parse (opts) {
132
+ const { req } = opts;
133
+ if (req.method !== 'POST') {
134
+ throw new TRPCError({
135
+ code: 'METHOD_NOT_SUPPORTED',
136
+ message: 'Only POST requests are supported for application/octet-stream requests'
137
+ });
138
+ }
139
+ const getInputs = memo(async ()=>{
140
+ return req.body;
40
141
  });
142
+ return {
143
+ calls: [
144
+ {
145
+ path: opts.path,
146
+ getRawInput: getInputs.read,
147
+ result: getInputs.result
148
+ }
149
+ ],
150
+ isBatchCall: false
151
+ };
41
152
  }
42
- const input = {};
43
- for(const key in rawInput){
44
- const k = key;
45
- const rawValue = rawInput[k];
46
- const value = deserializeInputValue(rawValue, transformer);
47
- input[k] = value;
48
- }
49
- return input;
50
153
  };
154
+ const handlers = [
155
+ jsonContentTypeHandler,
156
+ formDataContentTypeHandler,
157
+ octetStreamContentTypeHandler
158
+ ];
159
+ function getContentTypeHandler(req) {
160
+ const handler = handlers.find((handler)=>handler.isMatch(req));
161
+ if (handler) {
162
+ return handler;
163
+ }
164
+ if (!handler && req.method === 'GET') {
165
+ // fallback to JSON for get requests so GET-requests can be opened in browser easily
166
+ return jsonContentTypeHandler;
167
+ }
168
+ throw new TRPCError({
169
+ code: 'UNSUPPORTED_MEDIA_TYPE',
170
+ message: req.headers.has('content-type') ? `Unsupported content-type "${req.headers.get('content-type')}` : 'Missing content-type header'
171
+ });
172
+ }
173
+ function getRequestInfo(opts) {
174
+ const handler = getContentTypeHandler(opts.req);
175
+ return handler.parse(opts);
176
+ }
51
177
 
52
- export { getJsonContentTypeInputs };
178
+ export { getRequestInfo };
@@ -0,0 +1,14 @@
1
+ import type { ParserZodEsque } from '../parser';
2
+ type UtilityParser<TInput, TOutput> = ParserZodEsque<TInput, TOutput> & {
3
+ parse: (input: unknown) => TOutput;
4
+ };
5
+ /**
6
+ * File is only available from Node19+ but it always extends Blob so we can use that as a type until we eventually drop Node18
7
+ */
8
+ interface FileLike extends Blob {
9
+ readonly name: string;
10
+ }
11
+ type OctetInput = Blob | Uint8Array | FileLike;
12
+ export declare const octetInputParser: UtilityParser<OctetInput, ReadableStream>;
13
+ export {};
14
+ //# sourceMappingURL=contentTypeParsers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contentTypeParsers.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/contentTypeParsers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD,KAAK,aAAa,CAAC,MAAM,EAAE,OAAO,IAAI,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IACtE,KAAK,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;CACpC,CAAC;AAIF;;GAEG;AACH,UAAU,QAAS,SAAQ,IAAI;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,KAAK,UAAU,GAAG,IAAI,GAAG,UAAU,GAAG,QAAQ,CAAC;AAC/C,eAAO,MAAM,gBAAgB,EAAE,aAAa,CAAC,UAAU,EAAE,cAAc,CAYtE,CAAC"}
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ const octetInputParser = {
4
+ _input: null,
5
+ _output: null,
6
+ parse (input) {
7
+ if (input instanceof ReadableStream) {
8
+ return input;
9
+ }
10
+ throw new Error(`Parsed input was expected to be a ReadableStream but was: ${typeof input}`);
11
+ }
12
+ };
13
+
14
+ exports.octetInputParser = octetInputParser;
@@ -0,0 +1,12 @@
1
+ const octetInputParser = {
2
+ _input: null,
3
+ _output: null,
4
+ parse (input) {
5
+ if (input instanceof ReadableStream) {
6
+ return input;
7
+ }
8
+ throw new Error(`Parsed input was expected to be a ReadableStream but was: ${typeof input}`);
9
+ }
10
+ };
11
+
12
+ export { octetInputParser };
@@ -1,5 +1,5 @@
1
1
  import type { TRPCError } from '../error/TRPCError';
2
2
  import type { TRPCResponse } from '../rpc';
3
- export declare function getHTTPStatusCode(json: TRPCResponse | TRPCResponse[]): any;
3
+ export declare function getHTTPStatusCode(json: TRPCResponse | TRPCResponse[]): number;
4
4
  export declare function getHTTPStatusCodeFromError(error: TRPCError): number;
5
5
  //# sourceMappingURL=getHTTPStatusCode.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getHTTPStatusCode.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/getHTTPStatusCode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAA2B,YAAY,EAAE,MAAM,QAAQ,CAAC;AA6BpE,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,YAAY,GAAG,YAAY,EAAE,OAyBpE;AAED,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,SAAS,UAE1D"}
1
+ {"version":3,"file":"getHTTPStatusCode.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/getHTTPStatusCode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAA2B,YAAY,EAAE,MAAM,QAAQ,CAAC;AA6BpE,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,YAAY,GAAG,YAAY,EAAE,GAAG,MAAM,CAyB7E;AAED,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,SAAS,UAE1D"}