@trpc/server 11.0.0-alpha-tmp-export-from-main.213 → 11.0.0-alpha-tmp-export-from-main.217
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/adapters/aws-lambda/index.js +14 -105
- package/dist/adapters/aws-lambda/index.mjs +2 -91
- package/dist/adapters/aws-lambda/utils.js +100 -0
- package/dist/adapters/aws-lambda/utils.mjs +93 -0
- package/dist/adapters/express.js +1 -6
- package/dist/adapters/express.mjs +1 -4
- package/dist/adapters/fastify/fastifyRequestHandler.js +81 -0
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +79 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +51 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +49 -0
- package/dist/adapters/fastify/index.js +4 -125
- package/dist/adapters/fastify/index.mjs +2 -125
- package/dist/adapters/fetch/fetchRequestHandler.js +118 -0
- package/dist/adapters/fetch/fetchRequestHandler.mjs +116 -0
- package/dist/adapters/fetch/index.js +2 -115
- package/dist/adapters/fetch/index.mjs +1 -116
- package/dist/adapters/next.js +1 -5
- package/dist/adapters/next.mjs +1 -3
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +161 -0
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +157 -0
- package/dist/adapters/node-http/content-type/form-data/index.js +20 -646
- package/dist/adapters/node-http/content-type/form-data/index.mjs +9 -631
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +29 -0
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +27 -0
- package/dist/adapters/node-http/content-type/form-data/streamSlice.js +46 -0
- package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +44 -0
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +30 -0
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +26 -0
- package/dist/adapters/node-http/content-type/json/getPostBody.js +42 -0
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +40 -0
- package/dist/adapters/node-http/content-type/json/index.js +3 -41
- package/dist/adapters/node-http/content-type/json/index.mjs +3 -39
- package/dist/adapters/node-http/index.js +1 -6
- package/dist/adapters/node-http/index.mjs +1 -4
- package/dist/{contentType-72ed9df5.mjs → adapters/node-http/internals/contentType.mjs} +1 -1
- package/dist/{nodeHTTPRequestHandler-55f05150.js → adapters/node-http/nodeHTTPRequestHandler.js} +2 -2
- package/dist/{nodeHTTPRequestHandler-2d5c8791.mjs → adapters/node-http/nodeHTTPRequestHandler.mjs} +2 -2
- package/dist/adapters/standalone.js +2 -11
- package/dist/adapters/standalone.mjs +2 -5
- package/dist/adapters/ws.js +0 -2
- package/dist/http.js +5 -7
- package/dist/index.js +10 -12
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.js +203 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.mjs +201 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.js +167 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.mjs +163 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.js +35 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.mjs +30 -0
- package/dist/observable.js +6 -8
- package/dist/rpc.js +3 -5
- package/dist/shared.js +2 -4
- package/package.json +4 -4
- package/src/adapters/standalone.ts +1 -1
- package/dist/contentType-24c44bba.js +0 -5
- package/dist/nodeHTTPRequestHandler-7691fc79.js +0 -105
- /package/dist/{contentType-d9d22104.js → adapters/node-http/internals/contentType.js} +0 -0
|
@@ -1,98 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var core = require('@trpc/core');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 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`
|
|
9
|
-
*
|
|
10
|
-
* Do **not** import from `@trpc/core`
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
14
|
-
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
15
|
-
* ```
|
|
16
|
-
*/ // @trpc/server
|
|
17
|
-
function isPayloadV1(event) {
|
|
18
|
-
return determinePayloadFormat(event) == '1.0';
|
|
19
|
-
}
|
|
20
|
-
function isPayloadV2(event) {
|
|
21
|
-
return determinePayloadFormat(event) == '2.0';
|
|
22
|
-
}
|
|
23
|
-
function determinePayloadFormat(event) {
|
|
24
|
-
// https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
|
|
25
|
-
// According to AWS support, version is is extracted from the version property in the event.
|
|
26
|
-
// If there is no version property, then the version is implied as 1.0
|
|
27
|
-
const unknownEvent = event;
|
|
28
|
-
if (typeof unknownEvent.version === 'undefined') {
|
|
29
|
-
return '1.0';
|
|
30
|
-
} else {
|
|
31
|
-
if ([
|
|
32
|
-
'1.0',
|
|
33
|
-
'2.0'
|
|
34
|
-
].includes(unknownEvent.version)) {
|
|
35
|
-
return unknownEvent.version;
|
|
36
|
-
} else {
|
|
37
|
-
return 'custom';
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
function getHTTPMethod(event) {
|
|
42
|
-
if (isPayloadV1(event)) {
|
|
43
|
-
return event.httpMethod;
|
|
44
|
-
}
|
|
45
|
-
if (isPayloadV2(event)) {
|
|
46
|
-
return event.requestContext.http.method;
|
|
47
|
-
}
|
|
48
|
-
throw new core.TRPCError({
|
|
49
|
-
code: 'INTERNAL_SERVER_ERROR',
|
|
50
|
-
message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
function getPath(event) {
|
|
54
|
-
if (isPayloadV1(event)) {
|
|
55
|
-
if (!event.pathParameters) {
|
|
56
|
-
// Then this event was not triggered by a resource denoted with {proxy+}
|
|
57
|
-
return event.path.split('/').pop() ?? '';
|
|
58
|
-
}
|
|
59
|
-
const matches = event.resource.matchAll(/\{(.*?)\}/g);
|
|
60
|
-
for (const match of matches){
|
|
61
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
62
|
-
const group = match[1];
|
|
63
|
-
if (group.includes('+') && event.pathParameters) {
|
|
64
|
-
return event.pathParameters[group.replace('+', '')] ?? '';
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return event.path.slice(1);
|
|
68
|
-
}
|
|
69
|
-
if (isPayloadV2(event)) {
|
|
70
|
-
const matches1 = event.routeKey.matchAll(/\{(.*?)\}/g);
|
|
71
|
-
for (const match1 of matches1){
|
|
72
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
73
|
-
const group1 = match1[1];
|
|
74
|
-
if (group1.includes('+') && event.pathParameters) {
|
|
75
|
-
return event.pathParameters[group1.replace('+', '')] ?? '';
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return event.rawPath.slice(1);
|
|
79
|
-
}
|
|
80
|
-
throw new core.TRPCError({
|
|
81
|
-
code: 'INTERNAL_SERVER_ERROR',
|
|
82
|
-
message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
function transformHeaders(headers) {
|
|
86
|
-
const obj = {};
|
|
87
|
-
for (const [key, value] of Object.entries(headers)){
|
|
88
|
-
if (typeof value === 'undefined') {
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
obj[key] = Array.isArray(value) ? value.join(',') : value;
|
|
92
|
-
}
|
|
93
|
-
return obj;
|
|
94
|
-
}
|
|
95
|
-
const UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = 'Custom payload format version not handled by this adapter. Please use either 1.0 or 2.0. More information here' + 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html';
|
|
4
|
+
var utils = require('./utils.js');
|
|
96
5
|
|
|
97
6
|
/**
|
|
98
7
|
* 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`
|
|
@@ -118,38 +27,38 @@ function lambdaEventToHTTPRequest(event) {
|
|
|
118
27
|
body = event.body;
|
|
119
28
|
}
|
|
120
29
|
return {
|
|
121
|
-
method: getHTTPMethod(event),
|
|
30
|
+
method: utils.getHTTPMethod(event),
|
|
122
31
|
query: query,
|
|
123
32
|
headers: event.headers,
|
|
124
33
|
body: body
|
|
125
34
|
};
|
|
126
35
|
}
|
|
127
36
|
function tRPCOutputToAPIGatewayOutput(event, response) {
|
|
128
|
-
if (isPayloadV1(event)) {
|
|
37
|
+
if (utils.isPayloadV1(event)) {
|
|
129
38
|
const resp = {
|
|
130
39
|
statusCode: response.status,
|
|
131
40
|
body: response.body ?? '',
|
|
132
|
-
headers: transformHeaders(response.headers ?? {})
|
|
41
|
+
headers: utils.transformHeaders(response.headers ?? {})
|
|
133
42
|
};
|
|
134
43
|
return resp;
|
|
135
|
-
} else if (isPayloadV2(event)) {
|
|
44
|
+
} else if (utils.isPayloadV2(event)) {
|
|
136
45
|
const resp1 = {
|
|
137
46
|
statusCode: response.status,
|
|
138
47
|
body: response.body ?? undefined,
|
|
139
|
-
headers: transformHeaders(response.headers ?? {})
|
|
48
|
+
headers: utils.transformHeaders(response.headers ?? {})
|
|
140
49
|
};
|
|
141
50
|
return resp1;
|
|
142
51
|
} else {
|
|
143
52
|
throw new core.TRPCError({
|
|
144
53
|
code: 'INTERNAL_SERVER_ERROR',
|
|
145
|
-
message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
54
|
+
message: utils.UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
146
55
|
});
|
|
147
56
|
}
|
|
148
57
|
}
|
|
149
58
|
function awsLambdaRequestHandler(opts) {
|
|
150
59
|
return async (event, context)=>{
|
|
151
60
|
const req = lambdaEventToHTTPRequest(event);
|
|
152
|
-
const path = getPath(event);
|
|
61
|
+
const path = utils.getPath(event);
|
|
153
62
|
const createContext = async (innerOpts)=>{
|
|
154
63
|
return await opts.createContext?.({
|
|
155
64
|
event,
|
|
@@ -176,10 +85,10 @@ function awsLambdaRequestHandler(opts) {
|
|
|
176
85
|
};
|
|
177
86
|
}
|
|
178
87
|
|
|
179
|
-
exports.UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE;
|
|
88
|
+
exports.UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = utils.UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE;
|
|
89
|
+
exports.getHTTPMethod = utils.getHTTPMethod;
|
|
90
|
+
exports.getPath = utils.getPath;
|
|
91
|
+
exports.isPayloadV1 = utils.isPayloadV1;
|
|
92
|
+
exports.isPayloadV2 = utils.isPayloadV2;
|
|
93
|
+
exports.transformHeaders = utils.transformHeaders;
|
|
180
94
|
exports.awsLambdaRequestHandler = awsLambdaRequestHandler;
|
|
181
|
-
exports.getHTTPMethod = getHTTPMethod;
|
|
182
|
-
exports.getPath = getPath;
|
|
183
|
-
exports.isPayloadV1 = isPayloadV1;
|
|
184
|
-
exports.isPayloadV2 = isPayloadV2;
|
|
185
|
-
exports.transformHeaders = transformHeaders;
|
|
@@ -1,94 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 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`
|
|
5
|
-
*
|
|
6
|
-
* Do **not** import from `@trpc/core`
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
10
|
-
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
11
|
-
* ```
|
|
12
|
-
*/ // @trpc/server
|
|
13
|
-
function isPayloadV1(event) {
|
|
14
|
-
return determinePayloadFormat(event) == '1.0';
|
|
15
|
-
}
|
|
16
|
-
function isPayloadV2(event) {
|
|
17
|
-
return determinePayloadFormat(event) == '2.0';
|
|
18
|
-
}
|
|
19
|
-
function determinePayloadFormat(event) {
|
|
20
|
-
// https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
|
|
21
|
-
// According to AWS support, version is is extracted from the version property in the event.
|
|
22
|
-
// If there is no version property, then the version is implied as 1.0
|
|
23
|
-
const unknownEvent = event;
|
|
24
|
-
if (typeof unknownEvent.version === 'undefined') {
|
|
25
|
-
return '1.0';
|
|
26
|
-
} else {
|
|
27
|
-
if ([
|
|
28
|
-
'1.0',
|
|
29
|
-
'2.0'
|
|
30
|
-
].includes(unknownEvent.version)) {
|
|
31
|
-
return unknownEvent.version;
|
|
32
|
-
} else {
|
|
33
|
-
return 'custom';
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
function getHTTPMethod(event) {
|
|
38
|
-
if (isPayloadV1(event)) {
|
|
39
|
-
return event.httpMethod;
|
|
40
|
-
}
|
|
41
|
-
if (isPayloadV2(event)) {
|
|
42
|
-
return event.requestContext.http.method;
|
|
43
|
-
}
|
|
44
|
-
throw new TRPCError({
|
|
45
|
-
code: 'INTERNAL_SERVER_ERROR',
|
|
46
|
-
message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
function getPath(event) {
|
|
50
|
-
if (isPayloadV1(event)) {
|
|
51
|
-
if (!event.pathParameters) {
|
|
52
|
-
// Then this event was not triggered by a resource denoted with {proxy+}
|
|
53
|
-
return event.path.split('/').pop() ?? '';
|
|
54
|
-
}
|
|
55
|
-
const matches = event.resource.matchAll(/\{(.*?)\}/g);
|
|
56
|
-
for (const match of matches){
|
|
57
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
58
|
-
const group = match[1];
|
|
59
|
-
if (group.includes('+') && event.pathParameters) {
|
|
60
|
-
return event.pathParameters[group.replace('+', '')] ?? '';
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return event.path.slice(1);
|
|
64
|
-
}
|
|
65
|
-
if (isPayloadV2(event)) {
|
|
66
|
-
const matches1 = event.routeKey.matchAll(/\{(.*?)\}/g);
|
|
67
|
-
for (const match1 of matches1){
|
|
68
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
69
|
-
const group1 = match1[1];
|
|
70
|
-
if (group1.includes('+') && event.pathParameters) {
|
|
71
|
-
return event.pathParameters[group1.replace('+', '')] ?? '';
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return event.rawPath.slice(1);
|
|
75
|
-
}
|
|
76
|
-
throw new TRPCError({
|
|
77
|
-
code: 'INTERNAL_SERVER_ERROR',
|
|
78
|
-
message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
function transformHeaders(headers) {
|
|
82
|
-
const obj = {};
|
|
83
|
-
for (const [key, value] of Object.entries(headers)){
|
|
84
|
-
if (typeof value === 'undefined') {
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
87
|
-
obj[key] = Array.isArray(value) ? value.join(',') : value;
|
|
88
|
-
}
|
|
89
|
-
return obj;
|
|
90
|
-
}
|
|
91
|
-
const UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = 'Custom payload format version not handled by this adapter. Please use either 1.0 or 2.0. More information here' + 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html';
|
|
1
|
+
import { resolveHTTPResponse, TRPCError } from '@trpc/core';
|
|
2
|
+
import { getPath, getHTTPMethod, isPayloadV1, transformHeaders, isPayloadV2, UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE } from './utils.mjs';
|
|
92
3
|
|
|
93
4
|
/**
|
|
94
5
|
* 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`
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@trpc/core');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 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`
|
|
7
|
+
*
|
|
8
|
+
* Do **not** import from `@trpc/core`
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
12
|
+
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
13
|
+
* ```
|
|
14
|
+
*/ // @trpc/server
|
|
15
|
+
function isPayloadV1(event) {
|
|
16
|
+
return determinePayloadFormat(event) == '1.0';
|
|
17
|
+
}
|
|
18
|
+
function isPayloadV2(event) {
|
|
19
|
+
return determinePayloadFormat(event) == '2.0';
|
|
20
|
+
}
|
|
21
|
+
function determinePayloadFormat(event) {
|
|
22
|
+
// https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
|
|
23
|
+
// According to AWS support, version is is extracted from the version property in the event.
|
|
24
|
+
// If there is no version property, then the version is implied as 1.0
|
|
25
|
+
const unknownEvent = event;
|
|
26
|
+
if (typeof unknownEvent.version === 'undefined') {
|
|
27
|
+
return '1.0';
|
|
28
|
+
} else {
|
|
29
|
+
if ([
|
|
30
|
+
'1.0',
|
|
31
|
+
'2.0'
|
|
32
|
+
].includes(unknownEvent.version)) {
|
|
33
|
+
return unknownEvent.version;
|
|
34
|
+
} else {
|
|
35
|
+
return 'custom';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function getHTTPMethod(event) {
|
|
40
|
+
if (isPayloadV1(event)) {
|
|
41
|
+
return event.httpMethod;
|
|
42
|
+
}
|
|
43
|
+
if (isPayloadV2(event)) {
|
|
44
|
+
return event.requestContext.http.method;
|
|
45
|
+
}
|
|
46
|
+
throw new core.TRPCError({
|
|
47
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
48
|
+
message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function getPath(event) {
|
|
52
|
+
if (isPayloadV1(event)) {
|
|
53
|
+
if (!event.pathParameters) {
|
|
54
|
+
// Then this event was not triggered by a resource denoted with {proxy+}
|
|
55
|
+
return event.path.split('/').pop() ?? '';
|
|
56
|
+
}
|
|
57
|
+
const matches = event.resource.matchAll(/\{(.*?)\}/g);
|
|
58
|
+
for (const match of matches){
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
60
|
+
const group = match[1];
|
|
61
|
+
if (group.includes('+') && event.pathParameters) {
|
|
62
|
+
return event.pathParameters[group.replace('+', '')] ?? '';
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return event.path.slice(1);
|
|
66
|
+
}
|
|
67
|
+
if (isPayloadV2(event)) {
|
|
68
|
+
const matches1 = event.routeKey.matchAll(/\{(.*?)\}/g);
|
|
69
|
+
for (const match1 of matches1){
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
71
|
+
const group1 = match1[1];
|
|
72
|
+
if (group1.includes('+') && event.pathParameters) {
|
|
73
|
+
return event.pathParameters[group1.replace('+', '')] ?? '';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return event.rawPath.slice(1);
|
|
77
|
+
}
|
|
78
|
+
throw new core.TRPCError({
|
|
79
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
80
|
+
message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
function transformHeaders(headers) {
|
|
84
|
+
const obj = {};
|
|
85
|
+
for (const [key, value] of Object.entries(headers)){
|
|
86
|
+
if (typeof value === 'undefined') {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
obj[key] = Array.isArray(value) ? value.join(',') : value;
|
|
90
|
+
}
|
|
91
|
+
return obj;
|
|
92
|
+
}
|
|
93
|
+
const UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = 'Custom payload format version not handled by this adapter. Please use either 1.0 or 2.0. More information here' + 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html';
|
|
94
|
+
|
|
95
|
+
exports.UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE;
|
|
96
|
+
exports.getHTTPMethod = getHTTPMethod;
|
|
97
|
+
exports.getPath = getPath;
|
|
98
|
+
exports.isPayloadV1 = isPayloadV1;
|
|
99
|
+
exports.isPayloadV2 = isPayloadV2;
|
|
100
|
+
exports.transformHeaders = transformHeaders;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { TRPCError } from '@trpc/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 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`
|
|
5
|
+
*
|
|
6
|
+
* Do **not** import from `@trpc/core`
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
10
|
+
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
11
|
+
* ```
|
|
12
|
+
*/ // @trpc/server
|
|
13
|
+
function isPayloadV1(event) {
|
|
14
|
+
return determinePayloadFormat(event) == '1.0';
|
|
15
|
+
}
|
|
16
|
+
function isPayloadV2(event) {
|
|
17
|
+
return determinePayloadFormat(event) == '2.0';
|
|
18
|
+
}
|
|
19
|
+
function determinePayloadFormat(event) {
|
|
20
|
+
// https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
|
|
21
|
+
// According to AWS support, version is is extracted from the version property in the event.
|
|
22
|
+
// If there is no version property, then the version is implied as 1.0
|
|
23
|
+
const unknownEvent = event;
|
|
24
|
+
if (typeof unknownEvent.version === 'undefined') {
|
|
25
|
+
return '1.0';
|
|
26
|
+
} else {
|
|
27
|
+
if ([
|
|
28
|
+
'1.0',
|
|
29
|
+
'2.0'
|
|
30
|
+
].includes(unknownEvent.version)) {
|
|
31
|
+
return unknownEvent.version;
|
|
32
|
+
} else {
|
|
33
|
+
return 'custom';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function getHTTPMethod(event) {
|
|
38
|
+
if (isPayloadV1(event)) {
|
|
39
|
+
return event.httpMethod;
|
|
40
|
+
}
|
|
41
|
+
if (isPayloadV2(event)) {
|
|
42
|
+
return event.requestContext.http.method;
|
|
43
|
+
}
|
|
44
|
+
throw new TRPCError({
|
|
45
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
46
|
+
message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function getPath(event) {
|
|
50
|
+
if (isPayloadV1(event)) {
|
|
51
|
+
if (!event.pathParameters) {
|
|
52
|
+
// Then this event was not triggered by a resource denoted with {proxy+}
|
|
53
|
+
return event.path.split('/').pop() ?? '';
|
|
54
|
+
}
|
|
55
|
+
const matches = event.resource.matchAll(/\{(.*?)\}/g);
|
|
56
|
+
for (const match of matches){
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
58
|
+
const group = match[1];
|
|
59
|
+
if (group.includes('+') && event.pathParameters) {
|
|
60
|
+
return event.pathParameters[group.replace('+', '')] ?? '';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return event.path.slice(1);
|
|
64
|
+
}
|
|
65
|
+
if (isPayloadV2(event)) {
|
|
66
|
+
const matches1 = event.routeKey.matchAll(/\{(.*?)\}/g);
|
|
67
|
+
for (const match1 of matches1){
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
69
|
+
const group1 = match1[1];
|
|
70
|
+
if (group1.includes('+') && event.pathParameters) {
|
|
71
|
+
return event.pathParameters[group1.replace('+', '')] ?? '';
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return event.rawPath.slice(1);
|
|
75
|
+
}
|
|
76
|
+
throw new TRPCError({
|
|
77
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
78
|
+
message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function transformHeaders(headers) {
|
|
82
|
+
const obj = {};
|
|
83
|
+
for (const [key, value] of Object.entries(headers)){
|
|
84
|
+
if (typeof value === 'undefined') {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
obj[key] = Array.isArray(value) ? value.join(',') : value;
|
|
88
|
+
}
|
|
89
|
+
return obj;
|
|
90
|
+
}
|
|
91
|
+
const UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = 'Custom payload format version not handled by this adapter. Please use either 1.0 or 2.0. More information here' + 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html';
|
|
92
|
+
|
|
93
|
+
export { UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE, getHTTPMethod, getPath, isPayloadV1, isPayloadV2, transformHeaders };
|
package/dist/adapters/express.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var nodeHTTPRequestHandler = require('../nodeHTTPRequestHandler-55f05150.js');
|
|
6
|
-
require('@trpc/core');
|
|
7
|
-
require('./node-http/content-type/json/index.js');
|
|
8
|
-
require('../contentType-d9d22104.js');
|
|
3
|
+
var nodeHTTPRequestHandler = require('./node-http/nodeHTTPRequestHandler.js');
|
|
9
4
|
|
|
10
5
|
function createExpressMiddleware(opts) {
|
|
11
6
|
return async (req, res)=>{
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import '@trpc/core';
|
|
3
|
-
import './node-http/content-type/json/index.mjs';
|
|
4
|
-
import '../contentType-72ed9df5.mjs';
|
|
1
|
+
import { nodeHTTPRequestHandler } from './node-http/nodeHTTPRequestHandler.mjs';
|
|
5
2
|
|
|
6
3
|
function createExpressMiddleware(opts) {
|
|
7
4
|
return async (req, res)=>{
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_stream = require('node:stream');
|
|
4
|
+
var core = require('@trpc/core');
|
|
5
|
+
|
|
6
|
+
async function fastifyRequestHandler(opts) {
|
|
7
|
+
const createContext = async (innerOpts)=>{
|
|
8
|
+
return await opts.createContext?.({
|
|
9
|
+
...opts,
|
|
10
|
+
...innerOpts
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
|
|
14
|
+
const req = {
|
|
15
|
+
query,
|
|
16
|
+
method: opts.req.method,
|
|
17
|
+
headers: opts.req.headers,
|
|
18
|
+
body: opts.req.body ?? 'null'
|
|
19
|
+
};
|
|
20
|
+
let resolve;
|
|
21
|
+
const promise = new Promise((r)=>resolve = r);
|
|
22
|
+
let isStream = false;
|
|
23
|
+
let stream;
|
|
24
|
+
let formatter;
|
|
25
|
+
const unstable_onHead = (head, isStreaming)=>{
|
|
26
|
+
if (!opts.res.statusCode || opts.res.statusCode === 200) {
|
|
27
|
+
opts.res.statusCode = head.status;
|
|
28
|
+
}
|
|
29
|
+
for (const [key, value] of Object.entries(head.headers ?? {})){
|
|
30
|
+
/* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
void opts.res.header(key, value);
|
|
34
|
+
}
|
|
35
|
+
if (isStreaming) {
|
|
36
|
+
void opts.res.header('Transfer-Encoding', 'chunked');
|
|
37
|
+
void opts.res.header('Vary', opts.res.hasHeader('Vary') ? 'trpc-batch-mode, ' + opts.res.getHeader('Vary') : 'trpc-batch-mode');
|
|
38
|
+
stream = new node_stream.Readable();
|
|
39
|
+
stream._read = ()=>{}; // eslint-disable-line @typescript-eslint/no-empty-function -- https://github.com/fastify/fastify/issues/805#issuecomment-369172154
|
|
40
|
+
resolve(opts.res.send(stream));
|
|
41
|
+
isStream = true;
|
|
42
|
+
formatter = core.getBatchStreamFormatter();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const unstable_onChunk = ([index, string])=>{
|
|
46
|
+
if (index === -1) {
|
|
47
|
+
// full response, no streaming
|
|
48
|
+
resolve(opts.res.send(string));
|
|
49
|
+
} else {
|
|
50
|
+
stream.push(formatter(index, string));
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
core.resolveHTTPResponse({
|
|
54
|
+
req,
|
|
55
|
+
createContext,
|
|
56
|
+
path: opts.path,
|
|
57
|
+
router: opts.router,
|
|
58
|
+
batching: opts.batching,
|
|
59
|
+
responseMeta: opts.responseMeta,
|
|
60
|
+
onError (o) {
|
|
61
|
+
opts?.onError?.({
|
|
62
|
+
...o,
|
|
63
|
+
req: opts.req
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
unstable_onHead,
|
|
67
|
+
unstable_onChunk
|
|
68
|
+
}).then(()=>{
|
|
69
|
+
if (isStream) {
|
|
70
|
+
stream.push(formatter.end());
|
|
71
|
+
stream.push(null); // https://github.com/fastify/fastify/issues/805#issuecomment-369172154
|
|
72
|
+
}
|
|
73
|
+
}).catch(()=>{
|
|
74
|
+
if (isStream) {
|
|
75
|
+
stream.push(null);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
return promise;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
exports.fastifyRequestHandler = fastifyRequestHandler;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { resolveHTTPResponse, getBatchStreamFormatter } from '@trpc/core';
|
|
3
|
+
|
|
4
|
+
async function fastifyRequestHandler(opts) {
|
|
5
|
+
const createContext = async (innerOpts)=>{
|
|
6
|
+
return await opts.createContext?.({
|
|
7
|
+
...opts,
|
|
8
|
+
...innerOpts
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
|
|
12
|
+
const req = {
|
|
13
|
+
query,
|
|
14
|
+
method: opts.req.method,
|
|
15
|
+
headers: opts.req.headers,
|
|
16
|
+
body: opts.req.body ?? 'null'
|
|
17
|
+
};
|
|
18
|
+
let resolve;
|
|
19
|
+
const promise = new Promise((r)=>resolve = r);
|
|
20
|
+
let isStream = false;
|
|
21
|
+
let stream;
|
|
22
|
+
let formatter;
|
|
23
|
+
const unstable_onHead = (head, isStreaming)=>{
|
|
24
|
+
if (!opts.res.statusCode || opts.res.statusCode === 200) {
|
|
25
|
+
opts.res.statusCode = head.status;
|
|
26
|
+
}
|
|
27
|
+
for (const [key, value] of Object.entries(head.headers ?? {})){
|
|
28
|
+
/* istanbul ignore if -- @preserve */ if (typeof value === 'undefined') {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
void opts.res.header(key, value);
|
|
32
|
+
}
|
|
33
|
+
if (isStreaming) {
|
|
34
|
+
void opts.res.header('Transfer-Encoding', 'chunked');
|
|
35
|
+
void opts.res.header('Vary', opts.res.hasHeader('Vary') ? 'trpc-batch-mode, ' + opts.res.getHeader('Vary') : 'trpc-batch-mode');
|
|
36
|
+
stream = new Readable();
|
|
37
|
+
stream._read = ()=>{}; // eslint-disable-line @typescript-eslint/no-empty-function -- https://github.com/fastify/fastify/issues/805#issuecomment-369172154
|
|
38
|
+
resolve(opts.res.send(stream));
|
|
39
|
+
isStream = true;
|
|
40
|
+
formatter = getBatchStreamFormatter();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const unstable_onChunk = ([index, string])=>{
|
|
44
|
+
if (index === -1) {
|
|
45
|
+
// full response, no streaming
|
|
46
|
+
resolve(opts.res.send(string));
|
|
47
|
+
} else {
|
|
48
|
+
stream.push(formatter(index, string));
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
resolveHTTPResponse({
|
|
52
|
+
req,
|
|
53
|
+
createContext,
|
|
54
|
+
path: opts.path,
|
|
55
|
+
router: opts.router,
|
|
56
|
+
batching: opts.batching,
|
|
57
|
+
responseMeta: opts.responseMeta,
|
|
58
|
+
onError (o) {
|
|
59
|
+
opts?.onError?.({
|
|
60
|
+
...o,
|
|
61
|
+
req: opts.req
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
unstable_onHead,
|
|
65
|
+
unstable_onChunk
|
|
66
|
+
}).then(()=>{
|
|
67
|
+
if (isStream) {
|
|
68
|
+
stream.push(formatter.end());
|
|
69
|
+
stream.push(null); // https://github.com/fastify/fastify/issues/805#issuecomment-369172154
|
|
70
|
+
}
|
|
71
|
+
}).catch(()=>{
|
|
72
|
+
if (isStream) {
|
|
73
|
+
stream.push(null);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return promise;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { fastifyRequestHandler };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ws = require('../ws.js');
|
|
4
|
+
var fastifyRequestHandler = require('./fastifyRequestHandler.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 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`
|
|
8
|
+
*
|
|
9
|
+
* Do **not** import from `@trpc/core`
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import type { AnyTRPCRouter } from '@trpc/server'
|
|
13
|
+
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
|
|
14
|
+
* ```
|
|
15
|
+
*/ /// <reference types="@fastify/websocket" />
|
|
16
|
+
function fastifyTRPCPlugin(fastify, opts, done) {
|
|
17
|
+
fastify.removeContentTypeParser('application/json');
|
|
18
|
+
fastify.addContentTypeParser('application/json', {
|
|
19
|
+
parseAs: 'string'
|
|
20
|
+
}, function(_, body, _done) {
|
|
21
|
+
_done(null, body);
|
|
22
|
+
});
|
|
23
|
+
let prefix = opts.prefix ?? '';
|
|
24
|
+
// https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
|
|
25
|
+
// @ts-expect-error property 'default' does not exists on type ...
|
|
26
|
+
if (typeof fastifyTRPCPlugin.default !== 'function') {
|
|
27
|
+
prefix = ''; // handled by fastify internally
|
|
28
|
+
}
|
|
29
|
+
fastify.all(`${prefix}/:path`, async (req, res)=>{
|
|
30
|
+
const path = req.params.path;
|
|
31
|
+
await fastifyRequestHandler.fastifyRequestHandler({
|
|
32
|
+
...opts.trpcOptions,
|
|
33
|
+
req,
|
|
34
|
+
res,
|
|
35
|
+
path
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
if (opts.useWSS) {
|
|
39
|
+
ws.applyWSSHandler({
|
|
40
|
+
...opts.trpcOptions,
|
|
41
|
+
wss: fastify.websocketServer
|
|
42
|
+
});
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
44
|
+
fastify.get(prefix ?? '/', {
|
|
45
|
+
websocket: true
|
|
46
|
+
}, ()=>{});
|
|
47
|
+
}
|
|
48
|
+
done();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
exports.fastifyTRPCPlugin = fastifyTRPCPlugin;
|