@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.
- package/dist/@trpc/server/http.d.ts +7 -5
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/@trpc/server/index.d.ts +1 -2
- package/dist/@trpc/server/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/getPlanner.d.ts +13 -0
- package/dist/adapters/aws-lambda/getPlanner.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/getPlanner.js +143 -0
- package/dist/adapters/aws-lambda/getPlanner.mjs +141 -0
- package/dist/adapters/aws-lambda/index.d.ts +12 -9
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +8 -77
- package/dist/adapters/aws-lambda/index.mjs +9 -72
- package/dist/adapters/express.js +1 -0
- package/dist/adapters/express.mjs +1 -0
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +19 -2
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +22 -64
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +22 -64
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +1 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +1 -0
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +2 -2
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +29 -86
- package/dist/adapters/fetch/fetchRequestHandler.mjs +29 -86
- package/dist/adapters/fetch/types.d.ts +2 -2
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next.js +1 -1
- package/dist/adapters/next.mjs +1 -1
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts +18 -0
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -0
- package/dist/adapters/node-http/incomingMessageToRequest.js +71 -0
- package/dist/adapters/node-http/incomingMessageToRequest.mjs +69 -0
- package/dist/adapters/node-http/index.d.ts +1 -0
- package/dist/adapters/node-http/index.d.ts.map +1 -1
- package/dist/adapters/node-http/index.js +2 -0
- package/dist/adapters/node-http/index.mjs +1 -0
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +28 -71
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +28 -71
- package/dist/adapters/node-http/types.d.ts +7 -8
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/ws.js +1 -1
- package/dist/adapters/ws.mjs +1 -1
- package/dist/bundle-analysis.json +174 -271
- package/dist/http.js +4 -2
- package/dist/http.mjs +2 -1
- package/dist/index.js +2 -4
- package/dist/index.mjs +1 -2
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +8 -20
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +180 -0
- package/dist/unstable-core-do-not-import/http/contentType.mjs +178 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts +14 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.js +14 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.mjs +12 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +1 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts +15 -0
- package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/{resolveHTTPResponse.js → resolveResponse.js} +119 -134
- package/dist/unstable-core-do-not-import/http/{resolveHTTPResponse.mjs → resolveResponse.mjs} +119 -134
- package/dist/unstable-core-do-not-import/http/types.d.ts +25 -27
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +1 -3
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.js +0 -2
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +1 -2
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +0 -9
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.d.ts +3 -0
- package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.js +2 -0
- package/dist/unstable-core-do-not-import/utils.mjs +2 -1
- package/dist/unstable-core-do-not-import.d.ts +9 -3
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +12 -8
- package/dist/unstable-core-do-not-import.mjs +7 -5
- package/package.json +3 -13
- package/src/@trpc/server/http.ts +7 -12
- package/src/@trpc/server/index.ts +0 -3
- package/src/adapters/aws-lambda/getPlanner.ts +191 -0
- package/src/adapters/aws-lambda/index.ts +43 -107
- package/src/adapters/express.ts +1 -1
- package/src/adapters/fastify/fastifyRequestHandler.ts +43 -90
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +4 -3
- package/src/adapters/fetch/fetchRequestHandler.ts +35 -111
- package/src/adapters/fetch/types.ts +4 -2
- package/src/adapters/next.ts +1 -1
- package/src/adapters/node-http/incomingMessageToRequest.ts +94 -0
- package/src/adapters/node-http/index.ts +1 -0
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +31 -97
- package/src/adapters/node-http/types.ts +27 -37
- package/src/adapters/standalone.ts +1 -1
- package/src/unstable-core-do-not-import/http/contentType.ts +214 -22
- package/src/unstable-core-do-not-import/http/contentTypeParsers.ts +29 -0
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +2 -2
- package/src/unstable-core-do-not-import/http/resolveResponse.ts +386 -0
- package/src/unstable-core-do-not-import/http/types.ts +25 -30
- package/src/unstable-core-do-not-import/procedureBuilder.ts +2 -4
- package/src/unstable-core-do-not-import/rootConfig.ts +0 -10
- package/src/unstable-core-do-not-import/utils.ts +4 -0
- package/src/unstable-core-do-not-import.ts +9 -3
- package/adapters/node-http/content-type/form-data/index.d.ts +0 -1
- package/adapters/node-http/content-type/form-data/index.js +0 -1
- package/adapters/node-http/content-type/json/index.d.ts +0 -1
- package/adapters/node-http/content-type/json/index.js +0 -1
- package/dist/adapters/aws-lambda/content-type/json/index.d.ts +0 -10
- package/dist/adapters/aws-lambda/content-type/json/index.d.ts.map +0 -1
- package/dist/adapters/aws-lambda/content-type/json/index.js +0 -62
- package/dist/adapters/aws-lambda/content-type/json/index.mjs +0 -60
- package/dist/adapters/aws-lambda/utils.d.ts +0 -31
- package/dist/adapters/aws-lambda/utils.d.ts.map +0 -1
- package/dist/adapters/aws-lambda/utils.js +0 -111
- package/dist/adapters/aws-lambda/utils.mjs +0 -103
- package/dist/adapters/content-handlers/concurrentCache.d.ts +0 -7
- package/dist/adapters/content-handlers/concurrentCache.d.ts.map +0 -1
- package/dist/adapters/content-handlers/concurrentCache.js +0 -17
- package/dist/adapters/content-handlers/concurrentCache.mjs +0 -15
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +0 -10
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +0 -1
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +0 -33
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +0 -31
- package/dist/adapters/fastify/content-type/json/index.d.ts +0 -8
- package/dist/adapters/fastify/content-type/json/index.d.ts.map +0 -1
- package/dist/adapters/fastify/content-type/json/index.js +0 -62
- package/dist/adapters/fastify/content-type/json/index.mjs +0 -60
- package/dist/adapters/fastify/types.d.ts +0 -11
- package/dist/adapters/fastify/types.d.ts.map +0 -1
- package/dist/adapters/fetch/content-type/json/index.d.ts +0 -9
- package/dist/adapters/fetch/content-type/json/index.d.ts.map +0 -1
- package/dist/adapters/fetch/content-type/json/index.js +0 -55
- package/dist/adapters/fetch/content-type/json/index.mjs +0 -53
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -5
- package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/index.js +0 -32
- package/dist/adapters/node-http/content-type/form-data/index.mjs +0 -30
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +0 -7
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.js +0 -45
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +0 -43
- package/dist/adapters/node-http/content-type/json/index.d.ts +0 -5
- package/dist/adapters/node-http/content-type/json/index.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/json/index.js +0 -68
- package/dist/adapters/node-http/content-type/json/index.mjs +0 -66
- package/dist/adapters/node-http/content-type/octet/index.d.ts +0 -5
- package/dist/adapters/node-http/content-type/octet/index.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/octet/index.js +0 -19
- package/dist/adapters/node-http/content-type/octet/index.mjs +0 -17
- package/dist/adapters/node-http/content-type/types.d.ts +0 -8
- package/dist/adapters/node-http/content-type/types.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts +0 -16
- package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/contentTypeParsers.js +0 -23
- package/dist/unstable-core-do-not-import/contentTypeParsers.mjs +0 -21
- package/dist/unstable-core-do-not-import/http/index.d.ts +0 -11
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +0 -51
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +0 -1
- package/src/adapters/aws-lambda/content-type/json/index.ts +0 -108
- package/src/adapters/aws-lambda/utils.ts +0 -170
- package/src/adapters/content-handlers/concurrentCache.ts +0 -16
- package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +0 -45
- package/src/adapters/fastify/content-type/json/index.ts +0 -106
- package/src/adapters/fastify/types.ts +0 -22
- package/src/adapters/fetch/content-type/json/index.ts +0 -90
- package/src/adapters/node-http/content-type/form-data/index.ts +0 -37
- package/src/adapters/node-http/content-type/json/getPostBody.ts +0 -49
- package/src/adapters/node-http/content-type/json/index.ts +0 -100
- package/src/adapters/node-http/content-type/octet/index.ts +0 -27
- package/src/adapters/node-http/content-type/types.ts +0 -19
- package/src/unstable-core-do-not-import/contentTypeParsers.ts +0 -37
- package/src/unstable-core-do-not-import/http/index.ts +0 -28
- 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
|
-
}
|