@trpc/server 9.24.0 → 9.25.0

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.
@@ -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 CreateLambdaContextOptions<T extends APIGatewayEvent> = {
17
+ event: T;
18
+ context: APIGWContext;
19
+ };
20
+ export type LambdaCreateContextFn<
21
+ TRouter extends AnyRouter,
22
+ TEvent extends APIGatewayEvent,
23
+ > = ({
24
+ event,
25
+ context,
26
+ }: CreateLambdaContextOptions<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: LambdaCreateContextFn<TRouter, TEvent>;
47
+ }
48
+ | {
49
+ /**
50
+ * @link https://trpc.io/docs/context
51
+ **/
52
+ createContext?: LambdaCreateContextFn<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';
@@ -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
  /**