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

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 (178) hide show
  1. package/dist/@trpc/server/http.d.ts +7 -5
  2. package/dist/@trpc/server/http.d.ts.map +1 -1
  3. package/dist/@trpc/server/index.d.ts +1 -2
  4. package/dist/@trpc/server/index.d.ts.map +1 -1
  5. package/dist/adapters/aws-lambda/getPlanner.d.ts +13 -0
  6. package/dist/adapters/aws-lambda/getPlanner.d.ts.map +1 -0
  7. package/dist/adapters/aws-lambda/getPlanner.js +143 -0
  8. package/dist/adapters/aws-lambda/getPlanner.mjs +141 -0
  9. package/dist/adapters/aws-lambda/index.d.ts +12 -9
  10. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  11. package/dist/adapters/aws-lambda/index.js +8 -77
  12. package/dist/adapters/aws-lambda/index.mjs +9 -72
  13. package/dist/adapters/express.js +1 -0
  14. package/dist/adapters/express.mjs +1 -0
  15. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +19 -2
  16. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  17. package/dist/adapters/fastify/fastifyRequestHandler.js +22 -64
  18. package/dist/adapters/fastify/fastifyRequestHandler.mjs +22 -64
  19. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  20. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  21. package/dist/adapters/fastify/fastifyTRPCPlugin.js +1 -0
  22. package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +1 -0
  23. package/dist/adapters/fetch/fetchRequestHandler.d.ts +2 -2
  24. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  25. package/dist/adapters/fetch/fetchRequestHandler.js +29 -86
  26. package/dist/adapters/fetch/fetchRequestHandler.mjs +29 -86
  27. package/dist/adapters/fetch/types.d.ts +2 -2
  28. package/dist/adapters/fetch/types.d.ts.map +1 -1
  29. package/dist/adapters/next.js +1 -1
  30. package/dist/adapters/next.mjs +1 -1
  31. package/dist/adapters/node-http/incomingMessageToRequest.d.ts +18 -0
  32. package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -0
  33. package/dist/adapters/node-http/incomingMessageToRequest.js +71 -0
  34. package/dist/adapters/node-http/incomingMessageToRequest.mjs +69 -0
  35. package/dist/adapters/node-http/index.d.ts +1 -0
  36. package/dist/adapters/node-http/index.d.ts.map +1 -1
  37. package/dist/adapters/node-http/index.js +2 -0
  38. package/dist/adapters/node-http/index.mjs +1 -0
  39. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
  40. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  41. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +28 -71
  42. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +28 -71
  43. package/dist/adapters/node-http/types.d.ts +7 -8
  44. package/dist/adapters/node-http/types.d.ts.map +1 -1
  45. package/dist/adapters/ws.js +1 -1
  46. package/dist/adapters/ws.mjs +1 -1
  47. package/dist/bundle-analysis.json +174 -271
  48. package/dist/http.js +4 -2
  49. package/dist/http.mjs +2 -1
  50. package/dist/index.js +2 -4
  51. package/dist/index.mjs +1 -2
  52. package/dist/unstable-core-do-not-import/http/contentType.d.ts +8 -20
  53. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
  54. package/dist/unstable-core-do-not-import/http/contentType.js +180 -0
  55. package/dist/unstable-core-do-not-import/http/contentType.mjs +178 -0
  56. package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts +14 -0
  57. package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts.map +1 -0
  58. package/dist/unstable-core-do-not-import/http/contentTypeParsers.js +14 -0
  59. package/dist/unstable-core-do-not-import/http/contentTypeParsers.mjs +12 -0
  60. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +1 -1
  61. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
  62. package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts +15 -0
  63. package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts.map +1 -0
  64. package/dist/unstable-core-do-not-import/http/{resolveHTTPResponse.js → resolveResponse.js} +119 -134
  65. package/dist/unstable-core-do-not-import/http/{resolveHTTPResponse.mjs → resolveResponse.mjs} +119 -134
  66. package/dist/unstable-core-do-not-import/http/types.d.ts +25 -27
  67. package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
  68. package/dist/unstable-core-do-not-import/initTRPC.d.ts +1 -1
  69. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +1 -3
  70. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
  71. package/dist/unstable-core-do-not-import/procedureBuilder.js +0 -2
  72. package/dist/unstable-core-do-not-import/procedureBuilder.mjs +1 -2
  73. package/dist/unstable-core-do-not-import/rootConfig.d.ts +0 -9
  74. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
  75. package/dist/unstable-core-do-not-import/utils.d.ts +3 -0
  76. package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
  77. package/dist/unstable-core-do-not-import/utils.js +2 -0
  78. package/dist/unstable-core-do-not-import/utils.mjs +2 -1
  79. package/dist/unstable-core-do-not-import.d.ts +9 -3
  80. package/dist/unstable-core-do-not-import.d.ts.map +1 -1
  81. package/dist/unstable-core-do-not-import.js +12 -8
  82. package/dist/unstable-core-do-not-import.mjs +7 -5
  83. package/package.json +3 -13
  84. package/src/@trpc/server/http.ts +7 -12
  85. package/src/@trpc/server/index.ts +0 -3
  86. package/src/adapters/aws-lambda/getPlanner.ts +191 -0
  87. package/src/adapters/aws-lambda/index.ts +43 -107
  88. package/src/adapters/express.ts +1 -1
  89. package/src/adapters/fastify/fastifyRequestHandler.ts +43 -90
  90. package/src/adapters/fastify/fastifyTRPCPlugin.ts +4 -3
  91. package/src/adapters/fetch/fetchRequestHandler.ts +35 -111
  92. package/src/adapters/fetch/types.ts +4 -2
  93. package/src/adapters/next.ts +1 -1
  94. package/src/adapters/node-http/incomingMessageToRequest.ts +94 -0
  95. package/src/adapters/node-http/index.ts +1 -0
  96. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +31 -97
  97. package/src/adapters/node-http/types.ts +27 -37
  98. package/src/adapters/standalone.ts +1 -1
  99. package/src/unstable-core-do-not-import/http/contentType.ts +214 -22
  100. package/src/unstable-core-do-not-import/http/contentTypeParsers.ts +29 -0
  101. package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +2 -2
  102. package/src/unstable-core-do-not-import/http/resolveResponse.ts +386 -0
  103. package/src/unstable-core-do-not-import/http/types.ts +25 -30
  104. package/src/unstable-core-do-not-import/procedureBuilder.ts +2 -4
  105. package/src/unstable-core-do-not-import/rootConfig.ts +0 -10
  106. package/src/unstable-core-do-not-import/utils.ts +4 -0
  107. package/src/unstable-core-do-not-import.ts +9 -3
  108. package/adapters/node-http/content-type/form-data/index.d.ts +0 -1
  109. package/adapters/node-http/content-type/form-data/index.js +0 -1
  110. package/adapters/node-http/content-type/json/index.d.ts +0 -1
  111. package/adapters/node-http/content-type/json/index.js +0 -1
  112. package/dist/adapters/aws-lambda/content-type/json/index.d.ts +0 -10
  113. package/dist/adapters/aws-lambda/content-type/json/index.d.ts.map +0 -1
  114. package/dist/adapters/aws-lambda/content-type/json/index.js +0 -62
  115. package/dist/adapters/aws-lambda/content-type/json/index.mjs +0 -60
  116. package/dist/adapters/aws-lambda/utils.d.ts +0 -31
  117. package/dist/adapters/aws-lambda/utils.d.ts.map +0 -1
  118. package/dist/adapters/aws-lambda/utils.js +0 -111
  119. package/dist/adapters/aws-lambda/utils.mjs +0 -103
  120. package/dist/adapters/content-handlers/concurrentCache.d.ts +0 -7
  121. package/dist/adapters/content-handlers/concurrentCache.d.ts.map +0 -1
  122. package/dist/adapters/content-handlers/concurrentCache.js +0 -17
  123. package/dist/adapters/content-handlers/concurrentCache.mjs +0 -15
  124. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +0 -10
  125. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +0 -1
  126. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +0 -33
  127. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +0 -31
  128. package/dist/adapters/fastify/content-type/json/index.d.ts +0 -8
  129. package/dist/adapters/fastify/content-type/json/index.d.ts.map +0 -1
  130. package/dist/adapters/fastify/content-type/json/index.js +0 -62
  131. package/dist/adapters/fastify/content-type/json/index.mjs +0 -60
  132. package/dist/adapters/fastify/types.d.ts +0 -11
  133. package/dist/adapters/fastify/types.d.ts.map +0 -1
  134. package/dist/adapters/fetch/content-type/json/index.d.ts +0 -9
  135. package/dist/adapters/fetch/content-type/json/index.d.ts.map +0 -1
  136. package/dist/adapters/fetch/content-type/json/index.js +0 -55
  137. package/dist/adapters/fetch/content-type/json/index.mjs +0 -53
  138. package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -5
  139. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +0 -1
  140. package/dist/adapters/node-http/content-type/form-data/index.js +0 -32
  141. package/dist/adapters/node-http/content-type/form-data/index.mjs +0 -30
  142. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +0 -7
  143. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +0 -1
  144. package/dist/adapters/node-http/content-type/json/getPostBody.js +0 -45
  145. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +0 -43
  146. package/dist/adapters/node-http/content-type/json/index.d.ts +0 -5
  147. package/dist/adapters/node-http/content-type/json/index.d.ts.map +0 -1
  148. package/dist/adapters/node-http/content-type/json/index.js +0 -68
  149. package/dist/adapters/node-http/content-type/json/index.mjs +0 -66
  150. package/dist/adapters/node-http/content-type/octet/index.d.ts +0 -5
  151. package/dist/adapters/node-http/content-type/octet/index.d.ts.map +0 -1
  152. package/dist/adapters/node-http/content-type/octet/index.js +0 -19
  153. package/dist/adapters/node-http/content-type/octet/index.mjs +0 -17
  154. package/dist/adapters/node-http/content-type/types.d.ts +0 -8
  155. package/dist/adapters/node-http/content-type/types.d.ts.map +0 -1
  156. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts +0 -16
  157. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts.map +0 -1
  158. package/dist/unstable-core-do-not-import/contentTypeParsers.js +0 -23
  159. package/dist/unstable-core-do-not-import/contentTypeParsers.mjs +0 -21
  160. package/dist/unstable-core-do-not-import/http/index.d.ts +0 -11
  161. package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
  162. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +0 -51
  163. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +0 -1
  164. package/src/adapters/aws-lambda/content-type/json/index.ts +0 -108
  165. package/src/adapters/aws-lambda/utils.ts +0 -170
  166. package/src/adapters/content-handlers/concurrentCache.ts +0 -16
  167. package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +0 -45
  168. package/src/adapters/fastify/content-type/json/index.ts +0 -106
  169. package/src/adapters/fastify/types.ts +0 -22
  170. package/src/adapters/fetch/content-type/json/index.ts +0 -90
  171. package/src/adapters/node-http/content-type/form-data/index.ts +0 -37
  172. package/src/adapters/node-http/content-type/json/getPostBody.ts +0 -49
  173. package/src/adapters/node-http/content-type/json/index.ts +0 -100
  174. package/src/adapters/node-http/content-type/octet/index.ts +0 -27
  175. package/src/adapters/node-http/content-type/types.ts +0 -19
  176. package/src/unstable-core-do-not-import/contentTypeParsers.ts +0 -37
  177. package/src/unstable-core-do-not-import/http/index.ts +0 -28
  178. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +0 -461
@@ -1,108 +0,0 @@
1
- // @trpc/server
2
- import { TRPCError } from '../../../../@trpc/server';
3
- import type {
4
- AnyRouter,
5
- CombinedDataTransformer,
6
- } from '../../../../@trpc/server';
7
- import type {
8
- BaseContentTypeHandler,
9
- HTTPRequest,
10
- } from '../../../../@trpc/server/http';
11
- import { createConcurrentCache } from '../../../content-handlers/concurrentCache';
12
- import {
13
- lambdaEventToHTTPBody,
14
- type APIGatewayEvent,
15
- type AWSLambdaOptions,
16
- } from '../../utils';
17
-
18
- export interface LambdaHTTPContentTypeHandler<
19
- TRouter extends AnyRouter,
20
- TEvent extends APIGatewayEvent,
21
- > extends BaseContentTypeHandler<
22
- AWSLambdaOptions<TRouter, TEvent> & {
23
- event: TEvent;
24
- req: HTTPRequest;
25
- }
26
- > {}
27
-
28
- export const getLambdaHTTPJSONContentTypeHandler: <
29
- TRouter extends AnyRouter,
30
- TEvent extends APIGatewayEvent,
31
- >() => LambdaHTTPContentTypeHandler<TRouter, TEvent> = () => {
32
- const cache = createConcurrentCache();
33
-
34
- return {
35
- name: 'lambda-json',
36
- isMatch: (headers) => {
37
- return !!headers.get('content-type')?.startsWith('application/json');
38
- },
39
- getInputs: async (opts, info) => {
40
- function getRawProcedureInputOrThrow() {
41
- const { event, req } = opts;
42
-
43
- try {
44
- if (req.query.has('input')) {
45
- const input = req.query.get('input');
46
- if (!input) {
47
- return undefined;
48
- }
49
-
50
- return JSON.parse(input);
51
- }
52
-
53
- const body = lambdaEventToHTTPBody(opts.event);
54
- if (typeof body === 'string') {
55
- // A mutation with no inputs will have req.body === ''
56
- return body.length === 0 ? undefined : JSON.parse(body);
57
- }
58
- return event.body;
59
- } catch (cause) {
60
- throw new TRPCError({
61
- code: 'PARSE_ERROR',
62
- cause,
63
- });
64
- }
65
- }
66
-
67
- const deserializeInputValue = (
68
- rawValue: unknown,
69
- transformer: CombinedDataTransformer,
70
- ) => {
71
- return typeof rawValue !== 'undefined'
72
- ? transformer.input.deserialize(rawValue)
73
- : rawValue;
74
- };
75
-
76
- const rawInput = await cache.concurrentSafeGet('rawInput', () =>
77
- getRawProcedureInputOrThrow(),
78
- );
79
- if (rawInput === undefined) {
80
- return undefined;
81
- }
82
-
83
- const transformer = opts.router._def._config.transformer;
84
-
85
- if (!info.isBatchCall) {
86
- return cache.concurrentSafeGet('input', () =>
87
- deserializeInputValue(rawInput, transformer),
88
- );
89
- }
90
-
91
- /* istanbul ignore if */
92
- if (
93
- rawInput == null ||
94
- typeof rawInput !== 'object' ||
95
- Array.isArray(rawInput)
96
- ) {
97
- throw new TRPCError({
98
- code: 'BAD_REQUEST',
99
- message: '"input" needs to be an object when doing a batch call',
100
- });
101
- }
102
-
103
- return cache.concurrentSafeGet(String(info.batch), () =>
104
- deserializeInputValue(rawInput[info.batch], transformer),
105
- );
106
- },
107
- };
108
- };
@@ -1,170 +0,0 @@
1
- /**
2
- * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
3
- *
4
- * @example
5
- * ```ts
6
- * import type { AnyTRPCRouter } from '@trpc/server'
7
- * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
8
- * ```
9
- */
10
- import type {
11
- APIGatewayProxyEvent,
12
- APIGatewayProxyEventV2,
13
- APIGatewayProxyResult,
14
- APIGatewayProxyStructuredResultV2,
15
- Context as APIGWContext,
16
- } from 'aws-lambda';
17
- import type {
18
- AnyRouter,
19
- CreateContextCallback,
20
- inferRouterContext,
21
- } from '../../@trpc/server';
22
- // import @trpc/server
23
-
24
- // @trpc/server
25
- import { TRPCError } from '../../@trpc/server';
26
- import type {
27
- HTTPBaseHandlerOptions,
28
- HTTPHeaders,
29
- TRPCRequestInfo,
30
- } from '../../@trpc/server/http';
31
-
32
- export type APIGatewayEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2;
33
- export type APIGatewayResult =
34
- | APIGatewayProxyResult
35
- | APIGatewayProxyStructuredResultV2;
36
-
37
- export type CreateAWSLambdaContextOptions<TEvent extends APIGatewayEvent> = {
38
- event: TEvent;
39
- context: APIGWContext;
40
- info: TRPCRequestInfo;
41
- };
42
- export type AWSLambdaCreateContextFn<
43
- TRouter extends AnyRouter,
44
- TEvent extends APIGatewayEvent,
45
- > = ({
46
- event,
47
- context,
48
- info,
49
- }: CreateAWSLambdaContextOptions<TEvent>) =>
50
- | inferRouterContext<TRouter>
51
- | Promise<inferRouterContext<TRouter>>;
52
-
53
- export type AWSLambdaOptions<
54
- TRouter extends AnyRouter,
55
- TEvent extends APIGatewayEvent,
56
- > =
57
- | HTTPBaseHandlerOptions<TRouter, TEvent> &
58
- CreateContextCallback<
59
- inferRouterContext<AnyRouter>,
60
- AWSLambdaCreateContextFn<TRouter, TEvent>
61
- >;
62
-
63
- export function isPayloadV1(
64
- event: APIGatewayEvent,
65
- ): event is APIGatewayProxyEvent {
66
- return determinePayloadFormat(event) == '1.0';
67
- }
68
- export function isPayloadV2(
69
- event: APIGatewayEvent,
70
- ): event is APIGatewayProxyEventV2 {
71
- return determinePayloadFormat(event) == '2.0';
72
- }
73
-
74
- function determinePayloadFormat(
75
- event: APIGatewayEvent,
76
- ): APIGatewayPayloadFormatVersion {
77
- // https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
78
- // According to AWS support, version is is extracted from the version property in the event.
79
- // If there is no version property, then the version is implied as 1.0
80
- const unknownEvent = event as { version?: string };
81
- if (typeof unknownEvent.version === 'undefined') {
82
- return '1.0';
83
- } else {
84
- if (['1.0', '2.0'].includes(unknownEvent.version)) {
85
- return unknownEvent.version as APIGatewayPayloadFormatVersion;
86
- } else {
87
- return 'custom';
88
- }
89
- }
90
- }
91
-
92
- export function getHTTPMethod(event: APIGatewayEvent) {
93
- if (isPayloadV1(event)) {
94
- return event.httpMethod;
95
- }
96
- if (isPayloadV2(event)) {
97
- return event.requestContext.http.method;
98
- }
99
- throw new TRPCError({
100
- code: 'INTERNAL_SERVER_ERROR',
101
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE,
102
- });
103
- }
104
-
105
- export function getPath(event: APIGatewayEvent) {
106
- if (isPayloadV1(event)) {
107
- if (!event.pathParameters) {
108
- // Then this event was not triggered by a resource denoted with {proxy+}
109
- return event.path.split('/').pop() ?? '';
110
- }
111
- const matches = event.resource.matchAll(/\{(.*?)\}/g);
112
- for (const match of matches) {
113
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
114
- const group = match[1]!;
115
- if (group.includes('+') && event.pathParameters) {
116
- return event.pathParameters[group.replace('+', '')] ?? '';
117
- }
118
- }
119
- return event.path.slice(1);
120
- }
121
- if (isPayloadV2(event)) {
122
- const matches = event.routeKey.matchAll(/\{(.*?)\}/g);
123
- for (const match of matches) {
124
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
125
- const group = match[1]!;
126
- if (group.includes('+') && event.pathParameters) {
127
- return event.pathParameters[group.replace('+', '')] ?? '';
128
- }
129
- }
130
- return event.rawPath.slice(1);
131
- }
132
- throw new TRPCError({
133
- code: 'INTERNAL_SERVER_ERROR',
134
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE,
135
- });
136
- }
137
-
138
- export function transformHeaders(
139
- headers: HTTPHeaders,
140
- ): APIGatewayResult['headers'] {
141
- const obj: APIGatewayResult['headers'] = {};
142
-
143
- for (const [key, value] of Object.entries(headers)) {
144
- if (typeof value === 'undefined') {
145
- continue;
146
- }
147
- obj[key] = Array.isArray(value) ? value.join(',') : value;
148
- }
149
- return obj;
150
- }
151
-
152
- export type DefinedAPIGatewayPayloadFormats = '1.0' | '2.0';
153
- export type APIGatewayPayloadFormatVersion =
154
- | DefinedAPIGatewayPayloadFormats
155
- | 'custom';
156
-
157
- export const UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE =
158
- 'Custom payload format version not handled by this adapter. Please use either 1.0 or 2.0. More information here' +
159
- 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html';
160
-
161
- export function lambdaEventToHTTPBody(event: APIGatewayEvent) {
162
- let body: string | null | undefined;
163
- if (event.body && event.isBase64Encoded) {
164
- body = Buffer.from(event.body, 'base64').toString('utf8');
165
- } else {
166
- body = event.body;
167
- }
168
-
169
- return body;
170
- }
@@ -1,16 +0,0 @@
1
- /**
2
- * A simple concurrent cache that ensures that only one promise is created for a given key.
3
- */
4
- export function createConcurrentCache() {
5
- const cache = new Map<string, any>();
6
-
7
- return {
8
- concurrentSafeGet<TValue>(key: string, setter: () => TValue): TValue {
9
- if (!cache.has(key)) {
10
- cache.set(key, setter());
11
- }
12
-
13
- return cache.get(key) as TValue;
14
- },
15
- };
16
- }
@@ -1,45 +0,0 @@
1
- import { TRPCError } from '../../@trpc/server';
2
- import type {
3
- BaseContentTypeHandler,
4
- HTTPHeaders,
5
- } from '../../@trpc/server/http';
6
-
7
- interface MinimalHandlerOpts {
8
- req: {
9
- // TODO: This could probably only take Headers and do the conversion higher up,
10
- // but that's a bigger refactor for another day
11
- headers: HTTPHeaders | Headers;
12
- };
13
- }
14
-
15
- export function selectContentHandlerOrUnsupportedMediaType<
16
- THandlerOpts extends MinimalHandlerOpts,
17
- THandler extends BaseContentTypeHandler<THandlerOpts>,
18
- >(handlers: THandler[], opts: THandlerOpts) {
19
- const headers = new Headers(opts.req.headers as HeadersInit);
20
- const contentType = headers.get('content-type');
21
-
22
- if (contentType === null) {
23
- return [
24
- undefined,
25
- new TRPCError({
26
- code: 'UNSUPPORTED_MEDIA_TYPE',
27
- message:
28
- 'No Content-Type header detected on the incoming request. This request may not be supported by your tRPC Adapter, or possibly by tRPC at all',
29
- }),
30
- ] as const;
31
- }
32
-
33
- const handler = handlers.find((handler) => handler.isMatch(headers));
34
- if (!handler) {
35
- return [
36
- undefined,
37
- new TRPCError({
38
- code: 'UNSUPPORTED_MEDIA_TYPE',
39
- message: `Invalid Content-Type header '${contentType}'. This request may not be supported by your tRPC Adapter, or possibly by tRPC at all`,
40
- }),
41
- ] as const;
42
- }
43
-
44
- return [handler] as const;
45
- }
@@ -1,106 +0,0 @@
1
- // @trpc/server
2
- import type { FastifyReply, FastifyRequest } from 'fastify';
3
- import { TRPCError } from '../../../../@trpc/server';
4
- import type {
5
- AnyRouter,
6
- CombinedDataTransformer,
7
- } from '../../../../@trpc/server';
8
- import type { BaseContentTypeHandler } from '../../../../@trpc/server/http';
9
- import { createConcurrentCache } from '../../../content-handlers/concurrentCache';
10
- import type { FastifyRequestHandlerOptions } from '../../types';
11
-
12
- export interface FastifyHTTPContentTypeHandler<
13
- TRouter extends AnyRouter,
14
- TRequest extends FastifyRequest,
15
- TResponse extends FastifyReply,
16
- > extends BaseContentTypeHandler<
17
- FastifyRequestHandlerOptions<TRouter, TRequest, TResponse>
18
- > {}
19
-
20
- export const getFastifyHTTPJSONContentTypeHandler: <
21
- TRouter extends AnyRouter,
22
- TRequest extends FastifyRequest,
23
- TResponse extends FastifyReply,
24
- >() => FastifyHTTPContentTypeHandler<TRouter, TRequest, TResponse> = () => {
25
- const cache = createConcurrentCache();
26
-
27
- return {
28
- name: 'fastify-json',
29
- isMatch: (headers) => {
30
- return !!headers.get('content-type')?.startsWith('application/json');
31
- },
32
- getInputs: async (opts, info) => {
33
- async function getRawProcedureInputOrThrow() {
34
- const { req } = opts;
35
-
36
- try {
37
- if (req.method === 'GET') {
38
- const query = opts.req.query
39
- ? new URLSearchParams(opts.req.query as any)
40
- : new URLSearchParams(opts.req.url.split('?')[1]);
41
-
42
- const input = query.get('input');
43
- if (!input) {
44
- return undefined;
45
- }
46
-
47
- return JSON.parse(input);
48
- }
49
-
50
- const body = opts.req.body ?? 'null';
51
- if (typeof body === 'string') {
52
- // A mutation with no inputs will have req.body === ''
53
- return body.length === 0 ? undefined : JSON.parse(body);
54
- }
55
-
56
- return body;
57
- } catch (cause) {
58
- throw new TRPCError({
59
- code: 'PARSE_ERROR',
60
- cause,
61
- });
62
- }
63
- }
64
-
65
- const deserializeInputValue = (
66
- rawValue: unknown,
67
- transformer: CombinedDataTransformer,
68
- ) => {
69
- return typeof rawValue !== 'undefined'
70
- ? transformer.input.deserialize(rawValue)
71
- : rawValue;
72
- };
73
-
74
- const rawInput = await cache.concurrentSafeGet('rawInput', () =>
75
- getRawProcedureInputOrThrow(),
76
- );
77
- if (rawInput === undefined) {
78
- return undefined;
79
- }
80
-
81
- const transformer = opts.router._def._config.transformer;
82
-
83
- if (!info.isBatchCall) {
84
- return cache.concurrentSafeGet('input', () =>
85
- deserializeInputValue(rawInput, transformer),
86
- );
87
- }
88
-
89
- /* istanbul ignore if */
90
- if (
91
- rawInput == null ||
92
- typeof rawInput !== 'object' ||
93
- Array.isArray(rawInput)
94
- ) {
95
- throw new TRPCError({
96
- code: 'BAD_REQUEST',
97
- message: '"input" needs to be an object when doing a batch call',
98
- });
99
- }
100
-
101
- return cache.concurrentSafeGet(String(info.batch), () =>
102
- deserializeInputValue(rawInput[info.batch], transformer),
103
- );
104
- },
105
- };
106
- };
@@ -1,22 +0,0 @@
1
- import type { FastifyReply, FastifyRequest } from 'fastify';
2
- // @trpc/server
3
- import type { AnyRouter } from '../../@trpc/server';
4
- import type { HTTPBaseHandlerOptions } from '../../@trpc/server/http';
5
- import type { NodeHTTPConditionCreateContextOption } from '../node-http';
6
-
7
- export type FastifyHandlerOptions<
8
- TRouter extends AnyRouter,
9
- TRequest extends FastifyRequest,
10
- TResponse extends FastifyReply,
11
- > = HTTPBaseHandlerOptions<TRouter, TRequest> &
12
- NodeHTTPConditionCreateContextOption<TRouter, TRequest, TResponse>;
13
-
14
- export type FastifyRequestHandlerOptions<
15
- TRouter extends AnyRouter,
16
- TRequest extends FastifyRequest,
17
- TResponse extends FastifyReply,
18
- > = FastifyHandlerOptions<TRouter, TRequest, TResponse> & {
19
- req: TRequest;
20
- res: TResponse;
21
- path: string;
22
- };
@@ -1,90 +0,0 @@
1
- // @trpc/server
2
- import { TRPCError } from '../../../../@trpc/server';
3
- import type {
4
- AnyRouter,
5
- CombinedDataTransformer,
6
- } from '../../../../@trpc/server';
7
- import type { BaseContentTypeHandler } from '../../../../@trpc/server/http';
8
- import { createConcurrentCache } from '../../../content-handlers/concurrentCache';
9
- import type { FetchHandlerRequestOptions } from '../../types';
10
-
11
- export interface FetchHTTPContentTypeHandler<TRouter extends AnyRouter>
12
- extends BaseContentTypeHandler<
13
- FetchHandlerRequestOptions<TRouter> & {
14
- url: URL;
15
- }
16
- > {}
17
-
18
- export const getFetchHTTPJSONContentTypeHandler: <
19
- TRouter extends AnyRouter,
20
- >() => FetchHTTPContentTypeHandler<TRouter> = () => {
21
- const cache = createConcurrentCache();
22
-
23
- return {
24
- name: 'fetch-json',
25
- isMatch: (headers) => {
26
- return !!headers.get('content-type')?.startsWith('application/json');
27
- },
28
- getInputs: async (opts, info) => {
29
- async function getRawProcedureInputOrThrow() {
30
- try {
31
- if (opts.url.searchParams.has('input')) {
32
- const input = opts.url.searchParams.get('input');
33
- if (!input) {
34
- return undefined;
35
- }
36
-
37
- return JSON.parse(input);
38
- }
39
-
40
- return await opts.req.json();
41
- } catch (cause) {
42
- throw new TRPCError({
43
- code: 'PARSE_ERROR',
44
- cause,
45
- });
46
- }
47
- }
48
-
49
- const deserializeInputValue = (
50
- rawValue: unknown,
51
- transformer: CombinedDataTransformer,
52
- ) => {
53
- return typeof rawValue !== 'undefined'
54
- ? transformer.input.deserialize(rawValue)
55
- : rawValue;
56
- };
57
-
58
- const rawInput = await cache.concurrentSafeGet('rawInput', () =>
59
- getRawProcedureInputOrThrow(),
60
- );
61
- if (rawInput === undefined) {
62
- return undefined;
63
- }
64
-
65
- const transformer = opts.router._def._config.transformer;
66
-
67
- if (!info.isBatchCall) {
68
- return cache.concurrentSafeGet('input', () =>
69
- deserializeInputValue(rawInput, transformer),
70
- );
71
- }
72
-
73
- /* istanbul ignore if */
74
- if (
75
- rawInput == null ||
76
- typeof rawInput !== 'object' ||
77
- Array.isArray(rawInput)
78
- ) {
79
- throw new TRPCError({
80
- code: 'BAD_REQUEST',
81
- message: '"input" needs to be an object when doing a batch call',
82
- });
83
- }
84
-
85
- return cache.concurrentSafeGet(String(info.batch), () =>
86
- deserializeInputValue(rawInput[info.batch], transformer),
87
- );
88
- },
89
- };
90
- };
@@ -1,37 +0,0 @@
1
- import { Readable } from 'stream';
2
- // @trpc/server
3
- import type { AnyRouter } from '../../../../@trpc/server';
4
- import type { NodeHTTPRequest, NodeHTTPResponse } from '../../types';
5
- import type { NodeHTTPContentTypeHandler } from '../types';
6
-
7
- export const getFormDataContentTypeHandler: <
8
- TRouter extends AnyRouter,
9
- TRequest extends NodeHTTPRequest,
10
- TResponse extends NodeHTTPResponse,
11
- >() => NodeHTTPContentTypeHandler<TRouter, TRequest, TResponse> = () => ({
12
- name: 'node-http-formdata',
13
- isMatch: (headers) => {
14
- return !!headers.get('content-type')?.startsWith('multipart/form-data');
15
- },
16
- async getInputs(opts, inputOpts) {
17
- if (inputOpts.isBatchCall) {
18
- throw new Error('Batch calls not supported for form-data');
19
- }
20
-
21
- const contentType = opts.req.headers['content-type'];
22
- if (!contentType) {
23
- // Should be unreachable given the isMatch check
24
- throw new Error('No content-type header found');
25
- }
26
-
27
- const form = await new Request('https://unused.com', {
28
- method: 'POST',
29
- headers: { 'content-type': contentType },
30
- body: Readable.toWeb(opts.req) as ReadableStream,
31
- // @ts-expect-error - outdated types? this exists
32
- duplex: 'half',
33
- }).formData();
34
-
35
- return form;
36
- },
37
- });
@@ -1,49 +0,0 @@
1
- // @trpc/server
2
- import { TRPCError } from '../../../../@trpc/server';
3
- import type { BodyResult } from '../../../../@trpc/server/http';
4
- import type { NodeHTTPRequest } from '../../types';
5
-
6
- export async function getPostBody(opts: {
7
- req: NodeHTTPRequest;
8
- maxBodySize?: number;
9
- }): Promise<BodyResult> {
10
- const { req, maxBodySize = Infinity } = opts;
11
- return new Promise((resolve) => {
12
- if ('body' in req) {
13
- resolve({
14
- ok: true,
15
- data: req.body,
16
- // If the request headers specifies a content-type, we assume that the body has been preprocessed
17
- preprocessed:
18
- !!req.headers['content-type']?.startsWith('application/json'),
19
- });
20
- return;
21
- }
22
-
23
- let body = '';
24
- let hasBody = false;
25
-
26
- function onData(data: any) {
27
- body += data;
28
- hasBody = true;
29
- if (body.length > maxBodySize) {
30
- req.off('data', onData);
31
-
32
- resolve({
33
- ok: false,
34
- error: new TRPCError({ code: 'PAYLOAD_TOO_LARGE' }),
35
- });
36
- }
37
- }
38
-
39
- req.on('data', onData);
40
-
41
- req.on('end', () => {
42
- resolve({
43
- ok: true,
44
- data: hasBody ? body : undefined,
45
- preprocessed: false,
46
- });
47
- });
48
- });
49
- }