@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,100 +0,0 @@
|
|
|
1
|
-
// @trpc/server
|
|
2
|
-
import { TRPCError } from '../../../../@trpc/server';
|
|
3
|
-
import type {
|
|
4
|
-
AnyRouter,
|
|
5
|
-
CombinedDataTransformer,
|
|
6
|
-
} from '../../../../@trpc/server';
|
|
7
|
-
import { createConcurrentCache } from '../../../content-handlers/concurrentCache';
|
|
8
|
-
import type { NodeHTTPRequest, NodeHTTPResponse } from '../../types';
|
|
9
|
-
import type { NodeHTTPContentTypeHandler } from '../types';
|
|
10
|
-
import { getPostBody } from './getPostBody';
|
|
11
|
-
|
|
12
|
-
export const getNodeHTTPJSONContentTypeHandler: <
|
|
13
|
-
TRouter extends AnyRouter,
|
|
14
|
-
TRequest extends NodeHTTPRequest,
|
|
15
|
-
TResponse extends NodeHTTPResponse,
|
|
16
|
-
>() => NodeHTTPContentTypeHandler<TRouter, TRequest, TResponse> = () => {
|
|
17
|
-
const cache = createConcurrentCache();
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
name: 'node-http-json',
|
|
21
|
-
isMatch: (headers) => {
|
|
22
|
-
return !!headers.get('content-type')?.startsWith('application/json');
|
|
23
|
-
},
|
|
24
|
-
getInputs: async (opts, info) => {
|
|
25
|
-
async function getRawProcedureInputOrThrow() {
|
|
26
|
-
const bodyResult = await getPostBody(opts);
|
|
27
|
-
if (!bodyResult.ok) {
|
|
28
|
-
throw bodyResult.error;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const preprocessedBody = bodyResult.preprocessed;
|
|
32
|
-
const body = bodyResult.data;
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
if (opts.req.method === 'GET') {
|
|
36
|
-
const input = opts.query.get('input');
|
|
37
|
-
if (!input) {
|
|
38
|
-
return undefined;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return JSON.parse(input);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (preprocessedBody || typeof body !== 'string') {
|
|
45
|
-
// Some tools like nextjs may parse json
|
|
46
|
-
// requests before they reach us. So we just use them as is
|
|
47
|
-
return body;
|
|
48
|
-
} else {
|
|
49
|
-
return JSON.parse(body);
|
|
50
|
-
}
|
|
51
|
-
} catch (cause) {
|
|
52
|
-
throw new TRPCError({
|
|
53
|
-
code: 'PARSE_ERROR',
|
|
54
|
-
cause,
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const deserializeInputValue = (
|
|
60
|
-
rawValue: unknown,
|
|
61
|
-
transformer: CombinedDataTransformer,
|
|
62
|
-
) => {
|
|
63
|
-
return typeof rawValue !== 'undefined'
|
|
64
|
-
? transformer.input.deserialize(rawValue)
|
|
65
|
-
: rawValue;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const rawInput = await cache.concurrentSafeGet('rawInput', () =>
|
|
69
|
-
getRawProcedureInputOrThrow(),
|
|
70
|
-
);
|
|
71
|
-
if (rawInput === undefined) {
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const transformer = opts.router._def._config.transformer;
|
|
76
|
-
|
|
77
|
-
if (!info.isBatchCall) {
|
|
78
|
-
return cache.concurrentSafeGet('input', () =>
|
|
79
|
-
deserializeInputValue(rawInput, transformer),
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/* istanbul ignore if */
|
|
84
|
-
if (
|
|
85
|
-
rawInput == null ||
|
|
86
|
-
typeof rawInput !== 'object' ||
|
|
87
|
-
Array.isArray(rawInput)
|
|
88
|
-
) {
|
|
89
|
-
throw new TRPCError({
|
|
90
|
-
code: 'BAD_REQUEST',
|
|
91
|
-
message: '"input" needs to be an object when doing a batch call',
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return cache.concurrentSafeGet(String(info.batch), () =>
|
|
96
|
-
deserializeInputValue(rawInput[info.batch], transformer),
|
|
97
|
-
);
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Stream } 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 getOctetContentTypeHandler: <
|
|
8
|
-
TRouter extends AnyRouter,
|
|
9
|
-
TRequest extends NodeHTTPRequest,
|
|
10
|
-
TResponse extends NodeHTTPResponse,
|
|
11
|
-
>() => NodeHTTPContentTypeHandler<TRouter, TRequest, TResponse> = () => ({
|
|
12
|
-
name: 'node-http-octet',
|
|
13
|
-
isMatch: (headers) => {
|
|
14
|
-
return !!headers
|
|
15
|
-
.get('content-type')
|
|
16
|
-
?.startsWith('application/octet-stream');
|
|
17
|
-
},
|
|
18
|
-
async getInputs(opts, inputOpts) {
|
|
19
|
-
if (inputOpts.isBatchCall) {
|
|
20
|
-
throw new Error('Batch calls not supported for octet-stream');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const stream = Stream.Readable.toWeb(opts.req);
|
|
24
|
-
|
|
25
|
-
return stream;
|
|
26
|
-
},
|
|
27
|
-
});
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// @trpc/server
|
|
2
|
-
import type { AnyRouter } from '../../../@trpc/server';
|
|
3
|
-
// @trpc/server/http
|
|
4
|
-
import type { BaseContentTypeHandler } from '../../../@trpc/server/http';
|
|
5
|
-
import type {
|
|
6
|
-
NodeHTTPRequest,
|
|
7
|
-
NodeHTTPRequestHandlerOptions,
|
|
8
|
-
NodeHTTPResponse,
|
|
9
|
-
} from '../types';
|
|
10
|
-
|
|
11
|
-
export interface NodeHTTPContentTypeHandler<
|
|
12
|
-
TRouter extends AnyRouter,
|
|
13
|
-
TRequest extends NodeHTTPRequest,
|
|
14
|
-
TResponse extends NodeHTTPResponse,
|
|
15
|
-
> extends BaseContentTypeHandler<
|
|
16
|
-
NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse> & {
|
|
17
|
-
query: URLSearchParams;
|
|
18
|
-
}
|
|
19
|
-
> {}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { TRPCError } from './error/TRPCError';
|
|
2
|
-
import type { ParserZodEsque } from './parser';
|
|
3
|
-
|
|
4
|
-
export type UtilityParser<TInput, TOutput> = ParserZodEsque<TInput, TOutput> & {
|
|
5
|
-
parse: (input: unknown) => TOutput;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
// Should be the same possible types as packages/client/src/links/internals/contentTypes.ts isOctetType
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* File is only available from Node19+ but it always extends Blob so we can use that as a type until we eventually drop Node18
|
|
12
|
-
*/
|
|
13
|
-
interface FileLike extends Blob {
|
|
14
|
-
readonly name: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* When expecting a supported octet type to be passed from the frontend, this parser may be used to validate the type for your procedure. Note: the output is always a `Readable` stream.
|
|
19
|
-
*/
|
|
20
|
-
export function parseOctetInput<
|
|
21
|
-
TInput extends Blob | Uint8Array | FileLike,
|
|
22
|
-
>(): UtilityParser<TInput, ReadableStream> {
|
|
23
|
-
return {
|
|
24
|
-
_input: null as any as TInput,
|
|
25
|
-
_output: null as any as ReadableStream,
|
|
26
|
-
parse(input: unknown) {
|
|
27
|
-
if (input instanceof ReadableStream) {
|
|
28
|
-
return input;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
throw new TRPCError({
|
|
32
|
-
code: 'INTERNAL_SERVER_ERROR',
|
|
33
|
-
cause: `Parsed input was expected to be a ReadableStream but was: ${typeof input}`,
|
|
34
|
-
});
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
getHTTPStatusCode,
|
|
3
|
-
getHTTPStatusCodeFromError,
|
|
4
|
-
} from './getHTTPStatusCode';
|
|
5
|
-
export { resolveHTTPResponse } from './resolveHTTPResponse';
|
|
6
|
-
export type {
|
|
7
|
-
BaseHandlerOptions,
|
|
8
|
-
HTTPBaseHandlerOptions,
|
|
9
|
-
HTTPHeaders,
|
|
10
|
-
HTTPRequest,
|
|
11
|
-
HTTPResponse,
|
|
12
|
-
/**
|
|
13
|
-
* @deprecated Use `HTTPErrorHandler` instead
|
|
14
|
-
*/
|
|
15
|
-
HTTPErrorHandler as OnErrorFunction,
|
|
16
|
-
HTTPErrorHandler,
|
|
17
|
-
ProcedureCall,
|
|
18
|
-
ResolveHTTPRequestOptionsContextFn,
|
|
19
|
-
ResponseChunk,
|
|
20
|
-
ResponseMeta,
|
|
21
|
-
ResponseMetaFn,
|
|
22
|
-
TRPCRequestInfo,
|
|
23
|
-
} from './types';
|
|
24
|
-
|
|
25
|
-
export { getBatchStreamFormatter } from './batchStreamFormatter';
|
|
26
|
-
export type { BaseContentTypeHandler, BodyResult } from './contentType';
|
|
27
|
-
|
|
28
|
-
export { toURL } from './toURL';
|
|
@@ -1,461 +0,0 @@
|
|
|
1
|
-
import { getErrorShape } from '../error/getErrorShape';
|
|
2
|
-
import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError';
|
|
3
|
-
import type { ProcedureType } from '../procedure';
|
|
4
|
-
import type {
|
|
5
|
-
AnyRouter,
|
|
6
|
-
inferRouterContext,
|
|
7
|
-
inferRouterError,
|
|
8
|
-
} from '../router';
|
|
9
|
-
import { callProcedure } from '../router';
|
|
10
|
-
import type { TRPCResponse } from '../rpc';
|
|
11
|
-
import { transformTRPCResponse } from '../transformer';
|
|
12
|
-
import { getHTTPStatusCode } from './getHTTPStatusCode';
|
|
13
|
-
import type {
|
|
14
|
-
HTTPBaseHandlerOptions,
|
|
15
|
-
HTTPHeaders,
|
|
16
|
-
HTTPRequest,
|
|
17
|
-
HTTPResponse,
|
|
18
|
-
ResolveHTTPRequestOptionsContextFn,
|
|
19
|
-
ResponseChunk,
|
|
20
|
-
TRPCRequestInfo,
|
|
21
|
-
} from './types';
|
|
22
|
-
|
|
23
|
-
const HTTP_METHOD_PROCEDURE_TYPE_MAP: Record<
|
|
24
|
-
string,
|
|
25
|
-
ProcedureType | undefined
|
|
26
|
-
> = {
|
|
27
|
-
GET: 'query',
|
|
28
|
-
POST: 'mutation',
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
type PartialBy<TBaseType, TKey extends keyof TBaseType> = Omit<
|
|
32
|
-
TBaseType,
|
|
33
|
-
TKey
|
|
34
|
-
> &
|
|
35
|
-
Partial<Pick<TBaseType, TKey>>;
|
|
36
|
-
|
|
37
|
-
interface ResolveHTTPRequestOptions<
|
|
38
|
-
TRouter extends AnyRouter,
|
|
39
|
-
TRequest extends HTTPRequest,
|
|
40
|
-
> extends HTTPBaseHandlerOptions<TRouter, TRequest> {
|
|
41
|
-
createContext: ResolveHTTPRequestOptionsContextFn<TRouter>;
|
|
42
|
-
req: TRequest;
|
|
43
|
-
path: string;
|
|
44
|
-
getInput: (opts: { isBatchCall: boolean; batch: number }) => Promise<unknown>;
|
|
45
|
-
error?: TRPCError;
|
|
46
|
-
/**
|
|
47
|
-
* Called as soon as the response head is known.
|
|
48
|
-
* When streaming, headers will have been generated
|
|
49
|
-
* **without** knowing the response body.
|
|
50
|
-
*
|
|
51
|
-
* Without this callback, streaming is disabled.
|
|
52
|
-
*/
|
|
53
|
-
unstable_onHead: (
|
|
54
|
-
headResponse: Omit<HTTPResponse, 'body'>,
|
|
55
|
-
isStreaming: boolean,
|
|
56
|
-
) => void;
|
|
57
|
-
/**
|
|
58
|
-
* Called for every procedure with `[index, result]`.
|
|
59
|
-
*
|
|
60
|
-
* Will be called a single time with `index = -1` if
|
|
61
|
-
* - response is an error
|
|
62
|
-
* - response is empty (HEAD request)
|
|
63
|
-
*
|
|
64
|
-
* Without this callback, streaming is disabled.
|
|
65
|
-
*/
|
|
66
|
-
unstable_onChunk: (chunk: ResponseChunk) => void;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function initResponse<
|
|
70
|
-
TRouter extends AnyRouter,
|
|
71
|
-
TRequest extends HTTPRequest,
|
|
72
|
-
>(initOpts: {
|
|
73
|
-
ctx: inferRouterContext<TRouter> | undefined;
|
|
74
|
-
paths: string[] | undefined;
|
|
75
|
-
type: ProcedureType | 'unknown';
|
|
76
|
-
responseMeta?: HTTPBaseHandlerOptions<TRouter, TRequest>['responseMeta'];
|
|
77
|
-
untransformedJSON?:
|
|
78
|
-
| TRPCResponse<unknown, inferRouterError<TRouter>>
|
|
79
|
-
| TRPCResponse<unknown, inferRouterError<TRouter>>[]
|
|
80
|
-
| undefined;
|
|
81
|
-
errors?: TRPCError[];
|
|
82
|
-
}): HTTPResponse {
|
|
83
|
-
const {
|
|
84
|
-
ctx,
|
|
85
|
-
paths,
|
|
86
|
-
type,
|
|
87
|
-
responseMeta,
|
|
88
|
-
untransformedJSON,
|
|
89
|
-
errors = [],
|
|
90
|
-
} = initOpts;
|
|
91
|
-
|
|
92
|
-
let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
|
|
93
|
-
const headers: HTTPHeaders = {
|
|
94
|
-
'Content-Type': 'application/json',
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const eagerGeneration = !untransformedJSON;
|
|
98
|
-
const data = eagerGeneration
|
|
99
|
-
? []
|
|
100
|
-
: Array.isArray(untransformedJSON)
|
|
101
|
-
? untransformedJSON
|
|
102
|
-
: [untransformedJSON];
|
|
103
|
-
|
|
104
|
-
const meta =
|
|
105
|
-
responseMeta?.({
|
|
106
|
-
ctx,
|
|
107
|
-
paths,
|
|
108
|
-
type,
|
|
109
|
-
data,
|
|
110
|
-
errors,
|
|
111
|
-
eagerGeneration,
|
|
112
|
-
}) ?? {};
|
|
113
|
-
|
|
114
|
-
for (const [key, value] of Object.entries(meta.headers ?? {})) {
|
|
115
|
-
headers[key] = value;
|
|
116
|
-
}
|
|
117
|
-
if (meta.status) {
|
|
118
|
-
status = meta.status;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return {
|
|
122
|
-
status,
|
|
123
|
-
headers,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function caughtErrorToData<
|
|
128
|
-
TRouter extends AnyRouter,
|
|
129
|
-
TRequest extends HTTPRequest,
|
|
130
|
-
>(
|
|
131
|
-
cause: unknown,
|
|
132
|
-
errorOpts: {
|
|
133
|
-
opts: Pick<
|
|
134
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
135
|
-
'onError' | 'req' | 'router'
|
|
136
|
-
>;
|
|
137
|
-
ctx: inferRouterContext<TRouter> | undefined;
|
|
138
|
-
type: ProcedureType | 'unknown';
|
|
139
|
-
path?: string;
|
|
140
|
-
input?: unknown;
|
|
141
|
-
},
|
|
142
|
-
) {
|
|
143
|
-
const { router, req, onError } = errorOpts.opts;
|
|
144
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
145
|
-
onError?.({
|
|
146
|
-
error,
|
|
147
|
-
path: errorOpts.path,
|
|
148
|
-
input: errorOpts.input,
|
|
149
|
-
ctx: errorOpts.ctx,
|
|
150
|
-
type: errorOpts.type,
|
|
151
|
-
req,
|
|
152
|
-
});
|
|
153
|
-
const untransformedJSON = {
|
|
154
|
-
error: getErrorShape({
|
|
155
|
-
config: router._def._config,
|
|
156
|
-
error,
|
|
157
|
-
type: errorOpts.type,
|
|
158
|
-
path: errorOpts.path,
|
|
159
|
-
input: errorOpts.input,
|
|
160
|
-
ctx: errorOpts.ctx,
|
|
161
|
-
}),
|
|
162
|
-
};
|
|
163
|
-
const transformedJSON = transformTRPCResponse(
|
|
164
|
-
router._def._config,
|
|
165
|
-
untransformedJSON,
|
|
166
|
-
);
|
|
167
|
-
const body = JSON.stringify(transformedJSON);
|
|
168
|
-
return {
|
|
169
|
-
error,
|
|
170
|
-
untransformedJSON,
|
|
171
|
-
body,
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Since `resolveHTTPResponse` is a public API (community adapters),
|
|
177
|
-
* let's give it a strong type signature to increase discoverability.
|
|
178
|
-
*/
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Non-streaming signature for `resolveHTTPResponse`:
|
|
182
|
-
* @param opts.unstable_onHead `undefined`
|
|
183
|
-
* @param opts.unstable_onChunk `undefined`
|
|
184
|
-
* @returns `Promise<HTTPResponse>`
|
|
185
|
-
*/
|
|
186
|
-
export async function resolveHTTPResponse<
|
|
187
|
-
TRouter extends AnyRouter,
|
|
188
|
-
TRequest extends HTTPRequest,
|
|
189
|
-
>(
|
|
190
|
-
opts: Omit<
|
|
191
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
192
|
-
'unstable_onChunk' | 'unstable_onHead'
|
|
193
|
-
>,
|
|
194
|
-
): Promise<HTTPResponse>;
|
|
195
|
-
/**
|
|
196
|
-
* Streaming signature for `resolveHTTPResponse`:
|
|
197
|
-
* @param opts.unstable_onHead called as soon as the response head is known
|
|
198
|
-
* @param opts.unstable_onChunk called for every procedure with `[index, result]`
|
|
199
|
-
* @returns `Promise<void>` since the response is streamed
|
|
200
|
-
*/
|
|
201
|
-
export async function resolveHTTPResponse<
|
|
202
|
-
TRouter extends AnyRouter,
|
|
203
|
-
TRequest extends HTTPRequest,
|
|
204
|
-
>(opts: ResolveHTTPRequestOptions<TRouter, TRequest>): Promise<void>;
|
|
205
|
-
// implementation
|
|
206
|
-
export async function resolveHTTPResponse<
|
|
207
|
-
TRouter extends AnyRouter,
|
|
208
|
-
TRequest extends HTTPRequest,
|
|
209
|
-
>(
|
|
210
|
-
opts: PartialBy<
|
|
211
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
212
|
-
'unstable_onChunk' | 'unstable_onHead'
|
|
213
|
-
>,
|
|
214
|
-
): Promise<HTTPResponse | void> {
|
|
215
|
-
const { router, req, unstable_onHead, unstable_onChunk } = opts;
|
|
216
|
-
|
|
217
|
-
if (req.method === 'HEAD') {
|
|
218
|
-
// can be used for lambda warmup
|
|
219
|
-
const headResponse: HTTPResponse = {
|
|
220
|
-
status: 204,
|
|
221
|
-
};
|
|
222
|
-
unstable_onHead?.(headResponse, false);
|
|
223
|
-
unstable_onChunk?.([-1, '']);
|
|
224
|
-
return headResponse;
|
|
225
|
-
}
|
|
226
|
-
const allowBatching = opts.allowBatching ?? opts.batching?.enabled ?? true;
|
|
227
|
-
const allowMethodOverride = opts.allowMethodOverride ?? false;
|
|
228
|
-
|
|
229
|
-
const type =
|
|
230
|
-
HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? ('unknown' as const);
|
|
231
|
-
let ctx: inferRouterContext<TRouter> | undefined = undefined;
|
|
232
|
-
let paths: string[] | undefined;
|
|
233
|
-
|
|
234
|
-
const isBatchCall = !!req.query.get('batch');
|
|
235
|
-
const isStreamCall =
|
|
236
|
-
isBatchCall &&
|
|
237
|
-
unstable_onHead &&
|
|
238
|
-
unstable_onChunk &&
|
|
239
|
-
req.headers['trpc-batch-mode'] === 'stream';
|
|
240
|
-
|
|
241
|
-
try {
|
|
242
|
-
if (isBatchCall && !allowBatching) {
|
|
243
|
-
throw new TRPCError({
|
|
244
|
-
code: 'BAD_REQUEST',
|
|
245
|
-
message: `Batching is not enabled on the server`,
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
/* istanbul ignore if -- @preserve */
|
|
249
|
-
if (type === 'subscription') {
|
|
250
|
-
throw new TRPCError({
|
|
251
|
-
message: 'Subscriptions should use wsLink',
|
|
252
|
-
code: 'METHOD_NOT_SUPPORTED',
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
if (type === 'unknown') {
|
|
256
|
-
throw new TRPCError({
|
|
257
|
-
message: `Unexpected request method ${req.method}`,
|
|
258
|
-
code: 'METHOD_NOT_SUPPORTED',
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
paths = isBatchCall
|
|
263
|
-
? decodeURIComponent(opts.path).split(',')
|
|
264
|
-
: [opts.path];
|
|
265
|
-
const info: TRPCRequestInfo = {
|
|
266
|
-
isBatchCall,
|
|
267
|
-
calls: paths.map((path) => ({
|
|
268
|
-
path,
|
|
269
|
-
type,
|
|
270
|
-
})),
|
|
271
|
-
};
|
|
272
|
-
ctx = await opts.createContext({ info });
|
|
273
|
-
|
|
274
|
-
const errors: TRPCError[] = [];
|
|
275
|
-
|
|
276
|
-
const promises: Promise<
|
|
277
|
-
TRPCResponse<unknown, inferRouterError<TRouter>>
|
|
278
|
-
>[] = paths.map(async (path, index) => {
|
|
279
|
-
async function getRawInput() {
|
|
280
|
-
return await opts.getInput({
|
|
281
|
-
isBatchCall,
|
|
282
|
-
batch: index,
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
try {
|
|
287
|
-
if (opts.error) {
|
|
288
|
-
// sometimes an error may be generated above this function in the stack
|
|
289
|
-
// for instance a 405 error if the method is not supported
|
|
290
|
-
// But we need to handle it here to ensure the error is formatted correctly
|
|
291
|
-
throw opts.error;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const data = await callProcedure({
|
|
295
|
-
procedures: opts.router._def.procedures,
|
|
296
|
-
path,
|
|
297
|
-
getRawInput: getRawInput,
|
|
298
|
-
ctx,
|
|
299
|
-
type,
|
|
300
|
-
allowMethodOverride,
|
|
301
|
-
});
|
|
302
|
-
return {
|
|
303
|
-
result: {
|
|
304
|
-
data,
|
|
305
|
-
},
|
|
306
|
-
};
|
|
307
|
-
} catch (cause) {
|
|
308
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
309
|
-
errors.push(error);
|
|
310
|
-
|
|
311
|
-
let input: unknown;
|
|
312
|
-
try {
|
|
313
|
-
input = await getRawInput();
|
|
314
|
-
} catch (e) {
|
|
315
|
-
// if `getRawInput` fails, we can't include the input in the error
|
|
316
|
-
input = undefined;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
opts.onError?.({
|
|
320
|
-
error,
|
|
321
|
-
path,
|
|
322
|
-
input,
|
|
323
|
-
ctx,
|
|
324
|
-
type: type,
|
|
325
|
-
req: opts.req,
|
|
326
|
-
});
|
|
327
|
-
|
|
328
|
-
return {
|
|
329
|
-
error: getErrorShape({
|
|
330
|
-
config: opts.router._def._config,
|
|
331
|
-
error,
|
|
332
|
-
type,
|
|
333
|
-
path,
|
|
334
|
-
input,
|
|
335
|
-
ctx,
|
|
336
|
-
}),
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
if (!isStreamCall) {
|
|
342
|
-
/**
|
|
343
|
-
* Non-streaming response:
|
|
344
|
-
* - await all responses in parallel, blocking on the slowest one
|
|
345
|
-
* - create headers with known response body
|
|
346
|
-
* - return a complete HTTPResponse
|
|
347
|
-
*/
|
|
348
|
-
|
|
349
|
-
const untransformedJSON = await Promise.all(promises);
|
|
350
|
-
|
|
351
|
-
const headResponse = initResponse({
|
|
352
|
-
ctx,
|
|
353
|
-
paths,
|
|
354
|
-
type,
|
|
355
|
-
responseMeta: opts.responseMeta,
|
|
356
|
-
untransformedJSON,
|
|
357
|
-
errors,
|
|
358
|
-
});
|
|
359
|
-
unstable_onHead?.(headResponse, false);
|
|
360
|
-
|
|
361
|
-
// return body stuff
|
|
362
|
-
const result = isBatchCall ? untransformedJSON : untransformedJSON[0]!; // eslint-disable-line @typescript-eslint/no-non-null-assertion -- `untransformedJSON` should be the length of `paths` which should be at least 1 otherwise there wouldn't be a request at all
|
|
363
|
-
const transformedJSON = transformTRPCResponse(
|
|
364
|
-
router._def._config,
|
|
365
|
-
result,
|
|
366
|
-
);
|
|
367
|
-
const body = JSON.stringify(transformedJSON);
|
|
368
|
-
unstable_onChunk?.([-1, body]);
|
|
369
|
-
|
|
370
|
-
return {
|
|
371
|
-
status: headResponse.status,
|
|
372
|
-
headers: headResponse.headers,
|
|
373
|
-
body,
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Streaming response:
|
|
379
|
-
* - block on none, call `onChunk` as soon as each response is ready
|
|
380
|
-
* - create headers with minimal data (cannot know the response body in advance)
|
|
381
|
-
* - return void
|
|
382
|
-
*/
|
|
383
|
-
const headResponse = initResponse({
|
|
384
|
-
ctx,
|
|
385
|
-
paths,
|
|
386
|
-
type,
|
|
387
|
-
responseMeta: opts.responseMeta,
|
|
388
|
-
});
|
|
389
|
-
unstable_onHead(headResponse, true);
|
|
390
|
-
|
|
391
|
-
const indexedPromises = new Map(
|
|
392
|
-
promises.map((promise, index) => [
|
|
393
|
-
index,
|
|
394
|
-
promise.then((r) => [index, r] as const),
|
|
395
|
-
]),
|
|
396
|
-
);
|
|
397
|
-
for (const _ of paths) {
|
|
398
|
-
const [index, untransformedJSON] = await Promise.race(
|
|
399
|
-
indexedPromises.values(),
|
|
400
|
-
);
|
|
401
|
-
indexedPromises.delete(index);
|
|
402
|
-
|
|
403
|
-
try {
|
|
404
|
-
const transformedJSON = transformTRPCResponse(
|
|
405
|
-
router._def._config,
|
|
406
|
-
untransformedJSON,
|
|
407
|
-
);
|
|
408
|
-
const body = JSON.stringify(transformedJSON);
|
|
409
|
-
|
|
410
|
-
unstable_onChunk([index, body]);
|
|
411
|
-
} catch (cause) {
|
|
412
|
-
const path = paths[index];
|
|
413
|
-
const input = await opts.getInput({
|
|
414
|
-
isBatchCall,
|
|
415
|
-
batch: index,
|
|
416
|
-
});
|
|
417
|
-
const { body } = caughtErrorToData(cause, {
|
|
418
|
-
opts,
|
|
419
|
-
ctx,
|
|
420
|
-
type,
|
|
421
|
-
path,
|
|
422
|
-
input,
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
unstable_onChunk([index, body]);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
return;
|
|
429
|
-
} catch (cause) {
|
|
430
|
-
// we get here if
|
|
431
|
-
// - batching is called when it's not enabled
|
|
432
|
-
// - `createContext()` throws
|
|
433
|
-
// - `router._def._config.transformer.output.serialize()` throws
|
|
434
|
-
// - post body is too large
|
|
435
|
-
// - input deserialization fails
|
|
436
|
-
// - `errorFormatter` return value is malformed
|
|
437
|
-
const { error, untransformedJSON, body } = caughtErrorToData(cause, {
|
|
438
|
-
opts,
|
|
439
|
-
ctx,
|
|
440
|
-
type,
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
const headResponse = initResponse({
|
|
444
|
-
ctx,
|
|
445
|
-
paths,
|
|
446
|
-
type,
|
|
447
|
-
responseMeta: opts.responseMeta,
|
|
448
|
-
untransformedJSON,
|
|
449
|
-
errors: [error],
|
|
450
|
-
});
|
|
451
|
-
unstable_onHead?.(headResponse, false);
|
|
452
|
-
|
|
453
|
-
unstable_onChunk?.([-1, body]);
|
|
454
|
-
|
|
455
|
-
return {
|
|
456
|
-
status: headResponse.status,
|
|
457
|
-
headers: headResponse.headers,
|
|
458
|
-
body,
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
}
|