@trpc/server 9.24.0 → 9.25.2

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 (32) hide show
  1. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.d.ts +1 -1
  2. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +100 -20
  3. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +100 -20
  4. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +99 -19
  5. package/adapters/aws-lambda/package.json +4 -0
  6. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.d.ts +1 -0
  7. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.dev.js +25 -0
  8. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.js +7 -0
  9. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.prod.js +25 -0
  10. package/adapters/lambda/dist/trpc-server-adapters-lambda.esm.js +21 -0
  11. package/adapters/lambda/package.json +4 -0
  12. package/dist/declarations/src/TRPCError.d.ts.map +1 -1
  13. package/dist/declarations/src/adapters/aws-lambda/index.d.ts +14 -0
  14. package/dist/declarations/src/adapters/aws-lambda/index.d.ts.map +1 -0
  15. package/dist/declarations/src/adapters/aws-lambda/utils.d.ts +34 -0
  16. package/dist/declarations/src/adapters/aws-lambda/utils.d.ts.map +1 -0
  17. package/dist/declarations/src/adapters/lambda/index.d.ts +12 -0
  18. package/dist/declarations/src/adapters/lambda/index.d.ts.map +1 -0
  19. package/dist/declarations/src/http/internals/types.d.ts +1 -2
  20. package/dist/declarations/src/http/internals/types.d.ts.map +1 -1
  21. package/dist/router-2f54c292.cjs.dev.js +507 -0
  22. package/dist/router-459409c8.cjs.prod.js +503 -0
  23. package/dist/router-93491dae.esm.js +504 -0
  24. package/dist/trpc-server.cjs.dev.js +5 -503
  25. package/dist/trpc-server.cjs.prod.js +5 -499
  26. package/dist/trpc-server.esm.js +3 -503
  27. package/package.json +5 -2
  28. package/src/TRPCError.ts +3 -1
  29. package/src/adapters/aws-lambda/index.ts +156 -0
  30. package/src/adapters/aws-lambda/utils.ts +91 -0
  31. package/src/adapters/lambda/index.ts +18 -0
  32. package/src/http/internals/types.ts +1 -1
@@ -0,0 +1,156 @@
1
+ import type {
2
+ Context as APIGWContext,
3
+ APIGatewayProxyEvent,
4
+ APIGatewayProxyEventV2,
5
+ APIGatewayProxyResult,
6
+ APIGatewayProxyStructuredResultV2,
7
+ } from 'aws-lambda';
8
+ import { TRPCError, resolveHTTPResponse } from '../..';
9
+ import { HTTPHeaders, HTTPRequest } from '../../http/internals/types';
10
+ import type { HTTPResponse } from '../../http/internals/types';
11
+ import { AnyRouter, inferRouterContext } from '../../router';
12
+ import {
13
+ APIGatewayEvent,
14
+ APIGatewayResult,
15
+ AWSLambdaOptions,
16
+ UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE,
17
+ isPayloadV1,
18
+ isPayloadV2,
19
+ } from './utils';
20
+
21
+ export type { CreateAWSLambdaContextOptions, AWSLambdaOptions } from './utils';
22
+
23
+ function lambdaEventToHTTPRequest(event: APIGatewayEvent): HTTPRequest {
24
+ const query = new URLSearchParams();
25
+ for (const [key, value] of Object.entries(
26
+ event.queryStringParameters ?? {},
27
+ )) {
28
+ if (typeof value !== 'undefined') {
29
+ query.append(key, value);
30
+ }
31
+ }
32
+
33
+ return {
34
+ method: getHTTPMethod(event),
35
+ query: query,
36
+ headers: event.headers,
37
+ body: event.body,
38
+ };
39
+ }
40
+
41
+ function getHTTPMethod(event: APIGatewayEvent) {
42
+ if (isPayloadV1(event)) {
43
+ return event.httpMethod;
44
+ }
45
+ if (isPayloadV2(event)) {
46
+ return event.requestContext.http.method;
47
+ }
48
+ throw new TRPCError({
49
+ code: 'INTERNAL_SERVER_ERROR',
50
+ message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE,
51
+ });
52
+ }
53
+ function getPath(event: APIGatewayEvent) {
54
+ if (isPayloadV1(event)) {
55
+ return event.path.slice(1);
56
+ }
57
+ if (isPayloadV2(event)) {
58
+ return event.rawPath.slice(1);
59
+ }
60
+ throw new TRPCError({
61
+ code: 'INTERNAL_SERVER_ERROR',
62
+ message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE,
63
+ });
64
+ }
65
+ function transformHeaders(headers: HTTPHeaders): APIGatewayResult['headers'] {
66
+ const obj: APIGatewayResult['headers'] = {};
67
+
68
+ for (const [key, value] of Object.entries(headers)) {
69
+ if (typeof value === 'undefined') {
70
+ continue;
71
+ }
72
+ obj[key] = Array.isArray(value) ? value.join(',') : value;
73
+ }
74
+ return obj;
75
+ }
76
+ function tRPCOutputToAPIGatewayOutput<
77
+ TEvent extends APIGatewayEvent,
78
+ TResult extends APIGatewayResult,
79
+ >(event: TEvent, response: HTTPResponse): TResult {
80
+ if (isPayloadV1(event)) {
81
+ const resp: APIGatewayProxyResult = {
82
+ statusCode: response.status,
83
+ body: response.body ?? '',
84
+ headers: transformHeaders(response.headers ?? {}),
85
+ };
86
+ return resp as TResult;
87
+ } else if (isPayloadV2(event)) {
88
+ const resp: APIGatewayProxyStructuredResultV2 = {
89
+ statusCode: response.status,
90
+ body: response.body ?? undefined,
91
+ headers: transformHeaders(response.headers ?? {}),
92
+ };
93
+ return resp as TResult;
94
+ } else {
95
+ throw new TRPCError({
96
+ code: 'INTERNAL_SERVER_ERROR',
97
+ message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE,
98
+ });
99
+ }
100
+ }
101
+
102
+ /** Will check the createContext of the TRouter and get the parameter of event.
103
+ * @internal
104
+ **/
105
+ type inferAPIGWEvent<
106
+ TRouter extends AnyRouter,
107
+ TEvent extends APIGatewayEvent,
108
+ > = AWSLambdaOptions<TRouter, TEvent>['createContext'] extends NonNullable<
109
+ AWSLambdaOptions<TRouter, TEvent>['createContext']
110
+ >
111
+ ? Parameters<AWSLambdaOptions<TRouter, TEvent>['createContext']>[0]['event']
112
+ : APIGatewayEvent;
113
+
114
+ /** 1:1 mapping of v1 or v2 input events, deduces which is which.
115
+ * @internal
116
+ **/
117
+ type inferAPIGWReturn<T> = T extends APIGatewayProxyEvent
118
+ ? APIGatewayProxyResult
119
+ : T extends APIGatewayProxyEventV2
120
+ ? APIGatewayProxyStructuredResultV2
121
+ : never;
122
+ export function awsLambdaRequestHandler<
123
+ TRouter extends AnyRouter,
124
+ TEvent extends inferAPIGWEvent<TRouter, TEvent>,
125
+ TResult extends inferAPIGWReturn<TEvent>,
126
+ >(
127
+ opts: AWSLambdaOptions<TRouter, TEvent>,
128
+ ): (event: TEvent, context: APIGWContext) => Promise<TResult> {
129
+ return async (event, context) => {
130
+ const req = lambdaEventToHTTPRequest(event);
131
+ const path = getPath(event);
132
+ const createContext = async function _createContext(): Promise<
133
+ inferRouterContext<TRouter>
134
+ > {
135
+ return await opts.createContext?.({ event, context });
136
+ };
137
+
138
+ const response = await resolveHTTPResponse({
139
+ router: opts.router,
140
+ batching: opts.batching,
141
+ responseMeta: opts?.responseMeta,
142
+ createContext,
143
+ req,
144
+ path,
145
+ error: null,
146
+ onError(o) {
147
+ opts?.onError?.({
148
+ ...o,
149
+ req: event,
150
+ });
151
+ },
152
+ });
153
+
154
+ return tRPCOutputToAPIGatewayOutput<TEvent, TResult>(event, response);
155
+ };
156
+ }
@@ -0,0 +1,91 @@
1
+ import type {
2
+ Context as APIGWContext,
3
+ APIGatewayProxyEvent,
4
+ APIGatewayProxyEventV2,
5
+ APIGatewayProxyResult,
6
+ APIGatewayProxyStructuredResultV2,
7
+ } from 'aws-lambda';
8
+ import type { ResponseMetaFn } from '../../http/internals/types';
9
+ import type { AnyRouter, inferRouterContext } from '../../router';
10
+
11
+ export type APIGatewayEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2;
12
+ export type APIGatewayResult =
13
+ | APIGatewayProxyResult
14
+ | APIGatewayProxyStructuredResultV2;
15
+
16
+ export type CreateAWSLambdaContextOptions<T extends APIGatewayEvent> = {
17
+ event: T;
18
+ context: APIGWContext;
19
+ };
20
+ export type AWSLambdaCreateContextFn<
21
+ TRouter extends AnyRouter,
22
+ TEvent extends APIGatewayEvent,
23
+ > = ({
24
+ event,
25
+ context,
26
+ }: CreateAWSLambdaContextOptions<TEvent>) =>
27
+ | inferRouterContext<TRouter>
28
+ | Promise<inferRouterContext<TRouter>>;
29
+
30
+ export type AWSLambdaOptions<
31
+ TRouter extends AnyRouter,
32
+ TEvent extends APIGatewayEvent,
33
+ > =
34
+ | {
35
+ router: TRouter;
36
+ batching?: {
37
+ enabled: boolean;
38
+ };
39
+ onError?: (options: Record<string, unknown>) => void;
40
+ responseMeta?: ResponseMetaFn<TRouter>;
41
+ } & (
42
+ | {
43
+ /**
44
+ * @link https://trpc.io/docs/context
45
+ **/
46
+ createContext: AWSLambdaCreateContextFn<TRouter, TEvent>;
47
+ }
48
+ | {
49
+ /**
50
+ * @link https://trpc.io/docs/context
51
+ **/
52
+ createContext?: AWSLambdaCreateContextFn<TRouter, TEvent>;
53
+ }
54
+ );
55
+
56
+ export function isPayloadV1(
57
+ event: APIGatewayEvent,
58
+ ): event is APIGatewayProxyEvent {
59
+ return determinePayloadFormat(event) == '1.0';
60
+ }
61
+ export function isPayloadV2(
62
+ event: APIGatewayEvent,
63
+ ): event is APIGatewayProxyEventV2 {
64
+ return determinePayloadFormat(event) == '2.0';
65
+ }
66
+
67
+ function determinePayloadFormat(
68
+ event: APIGatewayEvent,
69
+ ): APIGatewayPayloadFormatVersion {
70
+ // https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
71
+ // According to AWS support, version is is extracted from the version property in the event.
72
+ // If there is no version property, then the version is implied as 1.0
73
+ const unknownEvent = event as { version?: string };
74
+ if (typeof unknownEvent.version === 'undefined') {
75
+ return '1.0';
76
+ } else {
77
+ if (['1.0', '2.0'].includes(unknownEvent.version)) {
78
+ return unknownEvent.version as APIGatewayPayloadFormatVersion;
79
+ } else {
80
+ return 'custom';
81
+ }
82
+ }
83
+ }
84
+ export type DefinedAPIGatewayPayloadFormats = '1.0' | '2.0';
85
+ export type APIGatewayPayloadFormatVersion =
86
+ | DefinedAPIGatewayPayloadFormats
87
+ | 'custom';
88
+
89
+ export const UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE =
90
+ 'Custom payload format version not handled by this adapter. Please use either 1.0 or 2.0. More information here' +
91
+ 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html';
@@ -0,0 +1,18 @@
1
+ import {
2
+ CreateAWSLambdaContextOptions,
3
+ awsLambdaRequestHandler,
4
+ } from '../aws-lambda';
5
+ import { APIGatewayEvent } from '../aws-lambda/utils';
6
+
7
+ export * from '../aws-lambda';
8
+
9
+ /**
10
+ * @deprecated use `aws-lambda` instead
11
+ */
12
+ export type CreateLambdaContextOptions<T extends APIGatewayEvent> =
13
+ CreateAWSLambdaContextOptions<T>;
14
+
15
+ /**
16
+ * @deprecated use `aws-lambda` instead
17
+ */
18
+ export const lambdaRequestHandler = awsLambdaRequestHandler;
@@ -26,7 +26,7 @@ export interface HTTPRequest {
26
26
  body: unknown;
27
27
  }
28
28
 
29
- type ResponseMetaFn<TRouter extends AnyRouter> = (opts: {
29
+ export type ResponseMetaFn<TRouter extends AnyRouter> = (opts: {
30
30
  data: TRPCResponse<unknown, inferRouterError<TRouter>>[];
31
31
  ctx?: inferRouterContext<TRouter>;
32
32
  /**