@trpc/server 11.0.0-rc.361 → 11.0.0-rc.362
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 +7 -76
- 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 +12 -63
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +13 -64
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- 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 +28 -85
- 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 +27 -70
- 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 +180 -277
- 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/resolveHTTPResponse.d.ts +7 -43
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +119 -134
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.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 +11 -7
- 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 +40 -89
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
- 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/resolveHTTPResponse.ts +135 -210
- 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/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
|
@@ -1,22 +1,21 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
1
2
|
import { getErrorShape } from '../error/getErrorShape';
|
|
2
3
|
import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError';
|
|
3
4
|
import type { ProcedureType } from '../procedure';
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import {
|
|
6
|
+
callProcedure,
|
|
7
|
+
type AnyRouter,
|
|
8
|
+
type inferRouterContext,
|
|
9
|
+
type inferRouterError,
|
|
8
10
|
} from '../router';
|
|
9
|
-
import { callProcedure } from '../router';
|
|
10
11
|
import type { TRPCResponse } from '../rpc';
|
|
11
12
|
import { transformTRPCResponse } from '../transformer';
|
|
13
|
+
import { getBatchStreamFormatter } from './batchStreamFormatter';
|
|
14
|
+
import { getRequestInfo } from './contentType';
|
|
12
15
|
import { getHTTPStatusCode } from './getHTTPStatusCode';
|
|
13
16
|
import type {
|
|
14
17
|
HTTPBaseHandlerOptions,
|
|
15
|
-
HTTPHeaders,
|
|
16
|
-
HTTPRequest,
|
|
17
|
-
HTTPResponse,
|
|
18
18
|
ResolveHTTPRequestOptionsContextFn,
|
|
19
|
-
ResponseChunk,
|
|
20
19
|
TRPCRequestInfo,
|
|
21
20
|
} from './types';
|
|
22
21
|
|
|
@@ -28,61 +27,31 @@ const HTTP_METHOD_PROCEDURE_TYPE_MAP: Record<
|
|
|
28
27
|
POST: 'mutation',
|
|
29
28
|
};
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
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> {
|
|
30
|
+
interface ResolveHTTPRequestOptions<TRouter extends AnyRouter>
|
|
31
|
+
extends HTTPBaseHandlerOptions<TRouter, Request> {
|
|
41
32
|
createContext: ResolveHTTPRequestOptionsContextFn<TRouter>;
|
|
42
|
-
req:
|
|
33
|
+
req: Request;
|
|
43
34
|
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
35
|
/**
|
|
58
|
-
*
|
|
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.
|
|
36
|
+
* If the request had an issue before reaching the handler
|
|
65
37
|
*/
|
|
66
|
-
|
|
38
|
+
error: TRPCError | null;
|
|
67
39
|
}
|
|
68
40
|
|
|
69
|
-
function initResponse<
|
|
70
|
-
TRouter extends AnyRouter,
|
|
71
|
-
TRequest extends HTTPRequest,
|
|
72
|
-
>(initOpts: {
|
|
41
|
+
function initResponse<TRouter extends AnyRouter, TRequest>(initOpts: {
|
|
73
42
|
ctx: inferRouterContext<TRouter> | undefined;
|
|
74
|
-
|
|
43
|
+
info: TRPCRequestInfo | undefined;
|
|
75
44
|
type: ProcedureType | 'unknown';
|
|
76
45
|
responseMeta?: HTTPBaseHandlerOptions<TRouter, TRequest>['responseMeta'];
|
|
77
46
|
untransformedJSON?:
|
|
78
47
|
| TRPCResponse<unknown, inferRouterError<TRouter>>
|
|
79
48
|
| TRPCResponse<unknown, inferRouterError<TRouter>>[]
|
|
80
49
|
| undefined;
|
|
81
|
-
errors
|
|
82
|
-
})
|
|
50
|
+
errors: TRPCError[];
|
|
51
|
+
}) {
|
|
83
52
|
const {
|
|
84
53
|
ctx,
|
|
85
|
-
|
|
54
|
+
info,
|
|
86
55
|
type,
|
|
87
56
|
responseMeta,
|
|
88
57
|
untransformedJSON,
|
|
@@ -90,9 +59,8 @@ function initResponse<
|
|
|
90
59
|
} = initOpts;
|
|
91
60
|
|
|
92
61
|
let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
};
|
|
62
|
+
|
|
63
|
+
const headers = new Headers([['Content-Type', 'application/json']]);
|
|
96
64
|
|
|
97
65
|
const eagerGeneration = !untransformedJSON;
|
|
98
66
|
const data = eagerGeneration
|
|
@@ -104,15 +72,33 @@ function initResponse<
|
|
|
104
72
|
const meta =
|
|
105
73
|
responseMeta?.({
|
|
106
74
|
ctx,
|
|
107
|
-
|
|
75
|
+
info,
|
|
76
|
+
paths: info?.calls.map((call) => call.path),
|
|
108
77
|
type,
|
|
109
78
|
data,
|
|
110
79
|
errors,
|
|
111
80
|
eagerGeneration,
|
|
112
81
|
}) ?? {};
|
|
113
82
|
|
|
114
|
-
|
|
115
|
-
headers
|
|
83
|
+
if (meta.headers) {
|
|
84
|
+
if (meta.headers instanceof Headers) {
|
|
85
|
+
for (const [key, value] of meta.headers.entries()) {
|
|
86
|
+
headers.append(key, value);
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
/**
|
|
90
|
+
* @deprecated, delete in v12
|
|
91
|
+
*/
|
|
92
|
+
for (const [key, value] of Object.entries(meta.headers)) {
|
|
93
|
+
if (Array.isArray(value)) {
|
|
94
|
+
for (const v of value) {
|
|
95
|
+
headers.append(key, v);
|
|
96
|
+
}
|
|
97
|
+
} else if (typeof value === 'string') {
|
|
98
|
+
headers.set(key, value);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
116
102
|
}
|
|
117
103
|
if (meta.status) {
|
|
118
104
|
status = meta.status;
|
|
@@ -124,14 +110,11 @@ function initResponse<
|
|
|
124
110
|
};
|
|
125
111
|
}
|
|
126
112
|
|
|
127
|
-
function caughtErrorToData<
|
|
128
|
-
TRouter extends AnyRouter,
|
|
129
|
-
TRequest extends HTTPRequest,
|
|
130
|
-
>(
|
|
113
|
+
function caughtErrorToData<TRouter extends AnyRouter>(
|
|
131
114
|
cause: unknown,
|
|
132
115
|
errorOpts: {
|
|
133
116
|
opts: Pick<
|
|
134
|
-
ResolveHTTPRequestOptions<TRouter
|
|
117
|
+
ResolveHTTPRequestOptions<TRouter>,
|
|
135
118
|
'onError' | 'req' | 'router'
|
|
136
119
|
>;
|
|
137
120
|
ctx: inferRouterContext<TRouter> | undefined;
|
|
@@ -172,86 +155,52 @@ function caughtErrorToData<
|
|
|
172
155
|
};
|
|
173
156
|
}
|
|
174
157
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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;
|
|
158
|
+
export async function resolveResponse<TRouter extends AnyRouter>(
|
|
159
|
+
opts: ResolveHTTPRequestOptions<TRouter>,
|
|
160
|
+
): Promise<Response> {
|
|
161
|
+
const { router, req } = opts;
|
|
162
|
+
|
|
163
|
+
const url = new URL(req.url);
|
|
216
164
|
|
|
217
165
|
if (req.method === 'HEAD') {
|
|
218
166
|
// can be used for lambda warmup
|
|
219
|
-
|
|
167
|
+
return new Response(null, {
|
|
220
168
|
status: 204,
|
|
221
|
-
};
|
|
222
|
-
unstable_onHead?.(headResponse, false);
|
|
223
|
-
unstable_onChunk?.([-1, '']);
|
|
224
|
-
return headResponse;
|
|
169
|
+
});
|
|
225
170
|
}
|
|
226
171
|
const allowBatching = opts.allowBatching ?? opts.batching?.enabled ?? true;
|
|
227
|
-
const allowMethodOverride =
|
|
172
|
+
const allowMethodOverride =
|
|
173
|
+
(opts.allowMethodOverride ?? false) && req.method === 'POST';
|
|
228
174
|
|
|
229
175
|
const type =
|
|
230
176
|
HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? ('unknown' as const);
|
|
231
177
|
let ctx: inferRouterContext<TRouter> | undefined = undefined;
|
|
232
|
-
let
|
|
178
|
+
let info: TRPCRequestInfo | undefined = undefined;
|
|
233
179
|
|
|
234
|
-
const
|
|
235
|
-
const isStreamCall =
|
|
236
|
-
isBatchCall &&
|
|
237
|
-
unstable_onHead &&
|
|
238
|
-
unstable_onChunk &&
|
|
239
|
-
req.headers['trpc-batch-mode'] === 'stream';
|
|
180
|
+
const isStreamCall = req.headers.get('trpc-batch-mode') === 'stream';
|
|
240
181
|
|
|
241
182
|
try {
|
|
242
|
-
|
|
183
|
+
info = getRequestInfo({
|
|
184
|
+
req,
|
|
185
|
+
path: decodeURIComponent(opts.path),
|
|
186
|
+
config: router._def._config,
|
|
187
|
+
searchParams: url.searchParams,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// we create context early so that error handlers may access context information
|
|
191
|
+
ctx = await opts.createContext({
|
|
192
|
+
info,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
if (opts.error) {
|
|
196
|
+
throw opts.error;
|
|
197
|
+
}
|
|
198
|
+
if (info.isBatchCall && !allowBatching) {
|
|
243
199
|
throw new TRPCError({
|
|
244
200
|
code: 'BAD_REQUEST',
|
|
245
201
|
message: `Batching is not enabled on the server`,
|
|
246
202
|
});
|
|
247
203
|
}
|
|
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
204
|
if (type === 'unknown') {
|
|
256
205
|
throw new TRPCError({
|
|
257
206
|
message: `Unexpected request method ${req.method}`,
|
|
@@ -259,42 +208,16 @@ export async function resolveHTTPResponse<
|
|
|
259
208
|
});
|
|
260
209
|
}
|
|
261
210
|
|
|
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
211
|
const errors: TRPCError[] = [];
|
|
275
212
|
|
|
276
213
|
const promises: Promise<
|
|
277
214
|
TRPCResponse<unknown, inferRouterError<TRouter>>
|
|
278
|
-
>[] =
|
|
279
|
-
async function getRawInput() {
|
|
280
|
-
return await opts.getInput({
|
|
281
|
-
isBatchCall,
|
|
282
|
-
batch: index,
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
|
|
215
|
+
>[] = info.calls.map(async (call) => {
|
|
286
216
|
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
217
|
const data = await callProcedure({
|
|
295
218
|
procedures: opts.router._def.procedures,
|
|
296
|
-
path,
|
|
297
|
-
getRawInput: getRawInput,
|
|
219
|
+
path: call.path,
|
|
220
|
+
getRawInput: call.getRawInput,
|
|
298
221
|
ctx,
|
|
299
222
|
type,
|
|
300
223
|
allowMethodOverride,
|
|
@@ -307,18 +230,11 @@ export async function resolveHTTPResponse<
|
|
|
307
230
|
} catch (cause) {
|
|
308
231
|
const error = getTRPCErrorFromUnknown(cause);
|
|
309
232
|
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
|
-
}
|
|
233
|
+
const input = call.result();
|
|
318
234
|
|
|
319
235
|
opts.onError?.({
|
|
320
236
|
error,
|
|
321
|
-
path,
|
|
237
|
+
path: call.path,
|
|
322
238
|
input,
|
|
323
239
|
ctx,
|
|
324
240
|
type: type,
|
|
@@ -330,14 +246,13 @@ export async function resolveHTTPResponse<
|
|
|
330
246
|
config: opts.router._def._config,
|
|
331
247
|
error,
|
|
332
248
|
type,
|
|
333
|
-
path,
|
|
249
|
+
path: call.path,
|
|
334
250
|
input,
|
|
335
251
|
ctx,
|
|
336
252
|
}),
|
|
337
253
|
};
|
|
338
254
|
}
|
|
339
255
|
});
|
|
340
|
-
|
|
341
256
|
if (!isStreamCall) {
|
|
342
257
|
/**
|
|
343
258
|
* Non-streaming response:
|
|
@@ -347,31 +262,33 @@ export async function resolveHTTPResponse<
|
|
|
347
262
|
*/
|
|
348
263
|
|
|
349
264
|
const untransformedJSON = await Promise.all(promises);
|
|
265
|
+
const errors = untransformedJSON.flatMap((response) =>
|
|
266
|
+
'error' in response ? [response.error] : [],
|
|
267
|
+
);
|
|
350
268
|
|
|
351
269
|
const headResponse = initResponse({
|
|
352
270
|
ctx,
|
|
353
|
-
|
|
271
|
+
info,
|
|
354
272
|
type,
|
|
355
273
|
responseMeta: opts.responseMeta,
|
|
356
274
|
untransformedJSON,
|
|
357
275
|
errors,
|
|
358
276
|
});
|
|
359
|
-
unstable_onHead?.(headResponse, false);
|
|
360
277
|
|
|
361
278
|
// return body stuff
|
|
362
|
-
const result = isBatchCall
|
|
279
|
+
const result = info.isBatchCall
|
|
280
|
+
? untransformedJSON
|
|
281
|
+
: untransformedJSON[0]!;
|
|
363
282
|
const transformedJSON = transformTRPCResponse(
|
|
364
283
|
router._def._config,
|
|
365
284
|
result,
|
|
366
285
|
);
|
|
367
286
|
const body = JSON.stringify(transformedJSON);
|
|
368
|
-
unstable_onChunk?.([-1, body]);
|
|
369
287
|
|
|
370
|
-
return {
|
|
288
|
+
return new Response(body, {
|
|
371
289
|
status: headResponse.status,
|
|
372
290
|
headers: headResponse.headers,
|
|
373
|
-
|
|
374
|
-
};
|
|
291
|
+
});
|
|
375
292
|
}
|
|
376
293
|
|
|
377
294
|
/**
|
|
@@ -382,50 +299,62 @@ export async function resolveHTTPResponse<
|
|
|
382
299
|
*/
|
|
383
300
|
const headResponse = initResponse({
|
|
384
301
|
ctx,
|
|
385
|
-
|
|
302
|
+
info,
|
|
386
303
|
type,
|
|
387
304
|
responseMeta: opts.responseMeta,
|
|
305
|
+
errors: [],
|
|
388
306
|
});
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
indexedPromises.values(),
|
|
307
|
+
|
|
308
|
+
const encoder = new TextEncoderStream();
|
|
309
|
+
const stream = encoder.readable;
|
|
310
|
+
const controller = encoder.writable.getWriter();
|
|
311
|
+
async function exec() {
|
|
312
|
+
const indexedPromises = new Map(
|
|
313
|
+
promises.map((promise, index) => [
|
|
314
|
+
index,
|
|
315
|
+
promise.then((r) => [index, r] as const),
|
|
316
|
+
]),
|
|
400
317
|
);
|
|
401
|
-
|
|
318
|
+
const formatter = getBatchStreamFormatter();
|
|
402
319
|
|
|
403
|
-
|
|
404
|
-
const
|
|
405
|
-
|
|
406
|
-
untransformedJSON,
|
|
320
|
+
while (indexedPromises.size > 0) {
|
|
321
|
+
const [index, untransformedJSON] = await Promise.race(
|
|
322
|
+
indexedPromises.values(),
|
|
407
323
|
);
|
|
408
|
-
|
|
324
|
+
indexedPromises.delete(index);
|
|
409
325
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
326
|
+
try {
|
|
327
|
+
const transformedJSON = transformTRPCResponse(
|
|
328
|
+
router._def._config,
|
|
329
|
+
untransformedJSON,
|
|
330
|
+
);
|
|
331
|
+
const body = JSON.stringify(transformedJSON);
|
|
332
|
+
|
|
333
|
+
await controller.write(formatter(index, body));
|
|
334
|
+
} catch (cause) {
|
|
335
|
+
const call = info!.calls[index]!;
|
|
336
|
+
const input = call.result();
|
|
337
|
+
const { body } = caughtErrorToData(cause, {
|
|
338
|
+
opts,
|
|
339
|
+
ctx,
|
|
340
|
+
type,
|
|
341
|
+
path: call.path,
|
|
342
|
+
input,
|
|
343
|
+
});
|
|
424
344
|
|
|
425
|
-
|
|
345
|
+
await controller.write(formatter(index, body));
|
|
346
|
+
}
|
|
426
347
|
}
|
|
348
|
+
|
|
349
|
+
await controller.write(formatter.end());
|
|
350
|
+
await controller.close();
|
|
427
351
|
}
|
|
428
|
-
|
|
352
|
+
exec().catch((err) => controller.abort(err));
|
|
353
|
+
|
|
354
|
+
return new Response(stream, {
|
|
355
|
+
headers: headResponse.headers,
|
|
356
|
+
status: headResponse.status,
|
|
357
|
+
});
|
|
429
358
|
} catch (cause) {
|
|
430
359
|
// we get here if
|
|
431
360
|
// - batching is called when it's not enabled
|
|
@@ -442,20 +371,16 @@ export async function resolveHTTPResponse<
|
|
|
442
371
|
|
|
443
372
|
const headResponse = initResponse({
|
|
444
373
|
ctx,
|
|
445
|
-
|
|
374
|
+
info,
|
|
446
375
|
type,
|
|
447
376
|
responseMeta: opts.responseMeta,
|
|
448
377
|
untransformedJSON,
|
|
449
378
|
errors: [error],
|
|
450
379
|
});
|
|
451
|
-
unstable_onHead?.(headResponse, false);
|
|
452
|
-
|
|
453
|
-
unstable_onChunk?.([-1, body]);
|
|
454
380
|
|
|
455
|
-
return {
|
|
381
|
+
return new Response(body, {
|
|
456
382
|
status: headResponse.status,
|
|
457
383
|
headers: headResponse.headers,
|
|
458
|
-
|
|
459
|
-
};
|
|
384
|
+
});
|
|
460
385
|
}
|
|
461
386
|
}
|
|
@@ -8,19 +8,14 @@ import type {
|
|
|
8
8
|
import type { TRPCResponse } from '../rpc';
|
|
9
9
|
import type { Dict } from '../types';
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
headers?: HTTPHeaders;
|
|
16
|
-
body?: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export type ResponseChunk = [procedureIndex: number, responseBody: string];
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated use `Headers` instead, this will be removed in v12
|
|
13
|
+
*/
|
|
14
|
+
type HTTPHeaders = Dict<string[] | string>;
|
|
20
15
|
|
|
21
16
|
export interface ResponseMeta {
|
|
22
17
|
status?: number;
|
|
23
|
-
headers?: HTTPHeaders;
|
|
18
|
+
headers?: Headers | HTTPHeaders;
|
|
24
19
|
}
|
|
25
20
|
|
|
26
21
|
/**
|
|
@@ -31,24 +26,18 @@ export type ResponseMetaFn<TRouter extends AnyRouter> = (opts: {
|
|
|
31
26
|
ctx?: inferRouterContext<TRouter>;
|
|
32
27
|
/**
|
|
33
28
|
* The different tRPC paths requested
|
|
29
|
+
* @deprecated use `info` instead, this will be removed in v12
|
|
34
30
|
**/
|
|
35
|
-
paths
|
|
31
|
+
paths: string[] | undefined;
|
|
32
|
+
info: TRPCRequestInfo | undefined;
|
|
36
33
|
type: ProcedureType | 'unknown';
|
|
37
34
|
errors: TRPCError[];
|
|
38
35
|
/**
|
|
39
|
-
* `true` if the `ResponseMeta`
|
|
40
|
-
* generated without knowing the response data
|
|
41
|
-
* (e.g. for streaming requests).
|
|
36
|
+
* `true` if the `ResponseMeta` is being generated without knowing the response data (e.g. for streamed requests).
|
|
42
37
|
*/
|
|
43
|
-
eagerGeneration
|
|
38
|
+
eagerGeneration: boolean;
|
|
44
39
|
}) => ResponseMeta;
|
|
45
40
|
|
|
46
|
-
export interface HTTPRequest {
|
|
47
|
-
method: string;
|
|
48
|
-
query: URLSearchParams;
|
|
49
|
-
headers: HTTPHeaders;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
41
|
/**
|
|
53
42
|
* Base interface for anything using HTTP
|
|
54
43
|
*/
|
|
@@ -62,23 +51,29 @@ export interface HTTPBaseHandlerOptions<TRouter extends AnyRouter, TRequest>
|
|
|
62
51
|
responseMeta?: ResponseMetaFn<TRouter>;
|
|
63
52
|
}
|
|
64
53
|
|
|
65
|
-
|
|
66
|
-
export type ProcedureCall = {
|
|
67
|
-
type: ProcedureType;
|
|
54
|
+
interface TRPCRequestInfoProcedureCall {
|
|
68
55
|
path: string;
|
|
69
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Read the raw input (deduped and memoized)
|
|
58
|
+
*/
|
|
59
|
+
getRawInput: () => Promise<unknown>;
|
|
60
|
+
/**
|
|
61
|
+
* Get already parsed inputs - won't trigger reading the body or parsing the inputs
|
|
62
|
+
*/
|
|
63
|
+
result: () => unknown;
|
|
64
|
+
}
|
|
70
65
|
|
|
71
66
|
/**
|
|
72
67
|
* Information about the incoming request
|
|
73
|
-
* @
|
|
68
|
+
* @public
|
|
74
69
|
*/
|
|
75
|
-
export
|
|
70
|
+
export interface TRPCRequestInfo {
|
|
76
71
|
isBatchCall: boolean;
|
|
77
|
-
calls:
|
|
78
|
-
}
|
|
72
|
+
calls: TRPCRequestInfoProcedureCall[];
|
|
73
|
+
}
|
|
79
74
|
|
|
80
75
|
/**
|
|
81
|
-
* Inner createContext function for `
|
|
76
|
+
* Inner createContext function for `resolveResponse` used to forward `TRPCRequestInfo` to `createContext`
|
|
82
77
|
* @internal
|
|
83
78
|
*/
|
|
84
79
|
export type ResolveHTTPRequestOptionsContextFn<TRouter extends AnyRouter> =
|
|
@@ -30,6 +30,7 @@ import type {
|
|
|
30
30
|
Simplify,
|
|
31
31
|
TypeError,
|
|
32
32
|
} from './types';
|
|
33
|
+
import type { UnsetMarker } from './utils';
|
|
33
34
|
import { mergeWithoutOverrides } from './utils';
|
|
34
35
|
|
|
35
36
|
type IntersectIfDefined<TType, TWith> = TType extends UnsetMarker
|
|
@@ -37,10 +38,7 @@ type IntersectIfDefined<TType, TWith> = TType extends UnsetMarker
|
|
|
37
38
|
: TWith extends UnsetMarker
|
|
38
39
|
? TType
|
|
39
40
|
: Simplify<TType & TWith>;
|
|
40
|
-
|
|
41
|
-
/** @internal */
|
|
42
|
-
export const unsetMarker = Symbol('unsetMarker');
|
|
43
|
-
type UnsetMarker = typeof unsetMarker;
|
|
41
|
+
``;
|
|
44
42
|
type DefaultValue<TValue, TFallback> = TValue extends UnsetMarker
|
|
45
43
|
? TFallback
|
|
46
44
|
: TValue;
|
|
@@ -97,13 +97,3 @@ export type CreateContextCallback<
|
|
|
97
97
|
createContext: TFunction;
|
|
98
98
|
}
|
|
99
99
|
>;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* @internal
|
|
103
|
-
*/
|
|
104
|
-
export type WrapCreateContext<TFunction extends (...args: any[]) => any> = {
|
|
105
|
-
/**
|
|
106
|
-
* @link https://trpc.io/docs/v11/context
|
|
107
|
-
**/
|
|
108
|
-
createContext?: TFunction;
|
|
109
|
-
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* **DO NOT IMPORT FROM
|
|
2
|
+
* **DO NOT IMPORT FROM THIS FILE**
|
|
3
3
|
*
|
|
4
4
|
* This file is here to:
|
|
5
5
|
* - make TypeScript happy and prevent _"The inferred type of 'createContext' cannot be named without a reference to [...]"_.
|
|
@@ -12,10 +12,16 @@ export * from './unstable-core-do-not-import/clientish/inference';
|
|
|
12
12
|
export * from './unstable-core-do-not-import/clientish/inferrable';
|
|
13
13
|
export * from './unstable-core-do-not-import/clientish/serialize';
|
|
14
14
|
export * from './unstable-core-do-not-import/createProxy';
|
|
15
|
+
export * from './unstable-core-do-not-import/error/TRPCError';
|
|
15
16
|
export * from './unstable-core-do-not-import/error/formatter';
|
|
16
17
|
export * from './unstable-core-do-not-import/error/getErrorShape';
|
|
17
|
-
export * from './unstable-core-do-not-import/
|
|
18
|
-
export * from './unstable-core-do-not-import/http';
|
|
18
|
+
export * from './unstable-core-do-not-import/http/batchStreamFormatter';
|
|
19
|
+
export * from './unstable-core-do-not-import/http/contentType';
|
|
20
|
+
export * from './unstable-core-do-not-import/http/contentTypeParsers';
|
|
21
|
+
export * from './unstable-core-do-not-import/http/getHTTPStatusCode';
|
|
22
|
+
export * from './unstable-core-do-not-import/http/resolveHTTPResponse';
|
|
23
|
+
export * from './unstable-core-do-not-import/http/toURL';
|
|
24
|
+
export * from './unstable-core-do-not-import/http/types';
|
|
19
25
|
export * from './unstable-core-do-not-import/initTRPC';
|
|
20
26
|
export * from './unstable-core-do-not-import/middleware';
|
|
21
27
|
export * from './unstable-core-do-not-import/parser';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '../../../../dist/adapters/node-http/content-type/form-data';
|