@trpc/server 11.0.0-rc.359 → 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 -6
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/@trpc/server/index.d.ts +1 -1
- 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 +6 -65
- package/dist/adapters/aws-lambda/index.mjs +8 -61
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +1 -1
- package/dist/adapters/express.mjs +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +10 -1
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +12 -55
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +13 -56
- 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 +1 -5
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +28 -71
- package/dist/adapters/fetch/fetchRequestHandler.mjs +29 -72
- package/dist/adapters/fetch/types.d.ts +9 -12
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts +2 -7
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +1 -2
- package/dist/adapters/next.mjs +1 -2
- 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 -72
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +28 -73
- package/dist/adapters/node-http/types.d.ts +2 -14
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +0 -1
- package/dist/adapters/standalone.mjs +0 -1
- package/dist/adapters/ws.d.ts +2 -12
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +1 -1
- package/dist/adapters/ws.mjs +1 -1
- package/dist/bundle-analysis.json +173 -259
- package/dist/http.js +4 -4
- package/dist/http.mjs +2 -2
- package/dist/index.js +2 -2
- package/dist/index.mjs +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +8 -28
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +164 -38
- package/dist/unstable-core-do-not-import/http/contentType.mjs +164 -38
- 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 +115 -124
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +116 -125
- package/dist/unstable-core-do-not-import/http/types.d.ts +25 -29
- 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 +12 -0
- 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 -9
- package/dist/unstable-core-do-not-import.mjs +7 -6
- package/package.json +3 -14
- package/src/@trpc/server/http.ts +7 -13
- package/src/@trpc/server/index.ts +1 -0
- package/src/adapters/aws-lambda/getPlanner.ts +191 -0
- package/src/adapters/aws-lambda/index.ts +43 -92
- package/src/adapters/express.ts +1 -6
- package/src/adapters/fastify/fastifyRequestHandler.ts +22 -77
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
- package/src/adapters/fetch/fetchRequestHandler.ts +34 -98
- package/src/adapters/fetch/types.ts +24 -15
- package/src/adapters/next-app-dir/nextAppDirCaller.ts +2 -9
- package/src/adapters/next.ts +1 -6
- 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 -110
- package/src/adapters/node-http/types.ts +9 -19
- package/src/adapters/standalone.ts +1 -2
- package/src/adapters/ws.ts +9 -14
- package/src/unstable-core-do-not-import/http/contentType.ts +199 -82
- 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 +133 -205
- package/src/unstable-core-do-not-import/http/types.ts +25 -32
- package/src/unstable-core-do-not-import/procedureBuilder.ts +2 -4
- package/src/unstable-core-do-not-import/rootConfig.ts +21 -0
- 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/utils.d.ts +0 -40
- package/dist/adapters/aws-lambda/utils.d.ts.map +0 -1
- package/dist/adapters/aws-lambda/utils.js +0 -100
- package/dist/adapters/aws-lambda/utils.mjs +0 -93
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -73
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -161
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -157
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -26
- 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 -135
- package/dist/adapters/node-http/content-type/form-data/index.mjs +0 -108
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
- package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -30
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -26
- 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 -53
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +0 -51
- package/dist/adapters/node-http/content-type/json/index.d.ts +0 -2
- 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 -16
- package/dist/adapters/node-http/content-type/json/index.mjs +0 -14
- package/dist/adapters/node-http/internals/contentType.d.ts +0 -9
- package/dist/adapters/node-http/internals/contentType.d.ts.map +0 -1
- package/dist/adapters/node-http/internals/contentType.js +0 -8
- package/dist/adapters/node-http/internals/contentType.mjs +0 -6
- package/dist/unstable-core-do-not-import/http/index.d.ts +0 -12
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
- package/src/adapters/aws-lambda/utils.ts +0 -164
- package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +0 -277
- package/src/adapters/node-http/content-type/form-data/index.ts +0 -163
- package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +0 -56
- package/src/adapters/node-http/content-type/form-data/streamSlice.ts +0 -56
- package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +0 -89
- package/src/adapters/node-http/content-type/json/getPostBody.ts +0 -58
- package/src/adapters/node-http/content-type/json/index.ts +0 -11
- package/src/adapters/node-http/internals/contentType.ts +0 -31
- package/src/unstable-core-do-not-import/http/index.ts +0 -29
|
@@ -1,51 +1,15 @@
|
|
|
1
1
|
import { TRPCError } from '../error/TRPCError';
|
|
2
|
-
import type
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
import type { HTTPBaseHandlerOptions, HTTPRequest, HTTPResponse, ResolveHTTPRequestOptionsContextFn, ResponseChunk } from './types';
|
|
6
|
-
interface ResolveHTTPRequestOptions<TRouter extends AnyRouter, TRequest extends HTTPRequest> extends HTTPBaseHandlerOptions<TRouter, TRequest> {
|
|
2
|
+
import { type AnyRouter } from '../router';
|
|
3
|
+
import type { HTTPBaseHandlerOptions, ResolveHTTPRequestOptionsContextFn } from './types';
|
|
4
|
+
interface ResolveHTTPRequestOptions<TRouter extends AnyRouter> extends HTTPBaseHandlerOptions<TRouter, Request> {
|
|
7
5
|
createContext: ResolveHTTPRequestOptionsContextFn<TRouter>;
|
|
8
|
-
req:
|
|
6
|
+
req: Request;
|
|
9
7
|
path: string;
|
|
10
|
-
error?: Maybe<TRPCError>;
|
|
11
|
-
contentTypeHandler?: BaseContentTypeHandler<any>;
|
|
12
|
-
preprocessedBody?: boolean;
|
|
13
8
|
/**
|
|
14
|
-
*
|
|
15
|
-
* When streaming, headers will have been generated
|
|
16
|
-
* **without** knowing the response body.
|
|
17
|
-
*
|
|
18
|
-
* Without this callback, streaming is disabled.
|
|
9
|
+
* If the request had an issue before reaching the handler
|
|
19
10
|
*/
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Called for every procedure with `[index, result]`.
|
|
23
|
-
*
|
|
24
|
-
* Will be called a single time with `index = -1` if
|
|
25
|
-
* - response is an error
|
|
26
|
-
* - response is empty (HEAD request)
|
|
27
|
-
*
|
|
28
|
-
* Without this callback, streaming is disabled.
|
|
29
|
-
*/
|
|
30
|
-
unstable_onChunk: (chunk: ResponseChunk) => void;
|
|
11
|
+
error: TRPCError | null;
|
|
31
12
|
}
|
|
32
|
-
|
|
33
|
-
* Since `resolveHTTPResponse` is a public API (community adapters),
|
|
34
|
-
* let's give it a strong type signature to increase discoverability.
|
|
35
|
-
*/
|
|
36
|
-
/**
|
|
37
|
-
* Non-streaming signature for `resolveHTTPResponse`:
|
|
38
|
-
* @param opts.unstable_onHead `undefined`
|
|
39
|
-
* @param opts.unstable_onChunk `undefined`
|
|
40
|
-
* @returns `Promise<HTTPResponse>`
|
|
41
|
-
*/
|
|
42
|
-
export declare function resolveHTTPResponse<TRouter extends AnyRouter, TRequest extends HTTPRequest>(opts: Omit<ResolveHTTPRequestOptions<TRouter, TRequest>, 'unstable_onChunk' | 'unstable_onHead'>): Promise<HTTPResponse>;
|
|
43
|
-
/**
|
|
44
|
-
* Streaming signature for `resolveHTTPResponse`:
|
|
45
|
-
* @param opts.unstable_onHead called as soon as the response head is known
|
|
46
|
-
* @param opts.unstable_onChunk called for every procedure with `[index, result]`
|
|
47
|
-
* @returns `Promise<void>` since the response is streamed
|
|
48
|
-
*/
|
|
49
|
-
export declare function resolveHTTPResponse<TRouter extends AnyRouter, TRequest extends HTTPRequest>(opts: ResolveHTTPRequestOptions<TRouter, TRequest>): Promise<void>;
|
|
13
|
+
export declare function resolveResponse<TRouter extends AnyRouter>(opts: ResolveHTTPRequestOptions<TRouter>): Promise<Response>;
|
|
50
14
|
export {};
|
|
51
15
|
//# sourceMappingURL=resolveHTTPResponse.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveHTTPResponse.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/resolveHTTPResponse.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"resolveHTTPResponse.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/resolveHTTPResponse.ts"],"names":[],"mappings":"AAEA,OAAO,EAA2B,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAExE,OAAO,EAEL,KAAK,SAAS,EAGf,MAAM,WAAW,CAAC;AAMnB,OAAO,KAAK,EACV,sBAAsB,EACtB,kCAAkC,EAEnC,MAAM,SAAS,CAAC;AAUjB,UAAU,yBAAyB,CAAC,OAAO,SAAS,SAAS,CAC3D,SAAQ,sBAAsB,CAAC,OAAO,EAAE,OAAO,CAAC;IAChD,aAAa,EAAE,kCAAkC,CAAC,OAAO,CAAC,CAAC;IAC3D,GAAG,EAAE,OAAO,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;CACzB;AAuHD,wBAAsB,eAAe,CAAC,OAAO,SAAS,SAAS,EAC7D,IAAI,EAAE,yBAAyB,CAAC,OAAO,CAAC,GACvC,OAAO,CAAC,QAAQ,CAAC,CAkOnB"}
|
|
@@ -4,6 +4,7 @@ var getErrorShape = require('../error/getErrorShape.js');
|
|
|
4
4
|
var TRPCError = require('../error/TRPCError.js');
|
|
5
5
|
var router = require('../router.js');
|
|
6
6
|
var transformer = require('../transformer.js');
|
|
7
|
+
var batchStreamFormatter = require('./batchStreamFormatter.js');
|
|
7
8
|
var contentType = require('./contentType.js');
|
|
8
9
|
var getHTTPStatusCode = require('./getHTTPStatusCode.js');
|
|
9
10
|
|
|
@@ -11,29 +12,46 @@ const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
|
|
|
11
12
|
GET: 'query',
|
|
12
13
|
POST: 'mutation'
|
|
13
14
|
};
|
|
14
|
-
const fallbackContentTypeHandler = {
|
|
15
|
-
getInputs: contentType.getJsonContentTypeInputs
|
|
16
|
-
};
|
|
17
15
|
function initResponse(initOpts) {
|
|
18
|
-
const { ctx ,
|
|
16
|
+
const { ctx , info , type , responseMeta , untransformedJSON , errors =[] , } = initOpts;
|
|
19
17
|
let status = untransformedJSON ? getHTTPStatusCode.getHTTPStatusCode(untransformedJSON) : 200;
|
|
20
|
-
const headers =
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
const headers = new Headers([
|
|
19
|
+
[
|
|
20
|
+
'Content-Type',
|
|
21
|
+
'application/json'
|
|
22
|
+
]
|
|
23
|
+
]);
|
|
23
24
|
const eagerGeneration = !untransformedJSON;
|
|
24
25
|
const data = eagerGeneration ? [] : Array.isArray(untransformedJSON) ? untransformedJSON : [
|
|
25
26
|
untransformedJSON
|
|
26
27
|
];
|
|
27
28
|
const meta = responseMeta?.({
|
|
28
29
|
ctx,
|
|
29
|
-
|
|
30
|
+
info,
|
|
31
|
+
paths: info?.calls.map((call)=>call.path),
|
|
30
32
|
type,
|
|
31
33
|
data,
|
|
32
34
|
errors,
|
|
33
35
|
eagerGeneration
|
|
34
36
|
}) ?? {};
|
|
35
|
-
|
|
36
|
-
headers
|
|
37
|
+
if (meta.headers) {
|
|
38
|
+
if (meta.headers instanceof Headers) {
|
|
39
|
+
for (const [key, value] of meta.headers.entries()){
|
|
40
|
+
headers.append(key, value);
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
/**
|
|
44
|
+
* @deprecated, delete in v12
|
|
45
|
+
*/ for (const [key1, value1] of Object.entries(meta.headers)){
|
|
46
|
+
if (Array.isArray(value1)) {
|
|
47
|
+
for (const v of value1){
|
|
48
|
+
headers.append(key1, v);
|
|
49
|
+
}
|
|
50
|
+
} else if (typeof value1 === 'string') {
|
|
51
|
+
headers.set(key1, value1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
37
55
|
}
|
|
38
56
|
if (meta.status) {
|
|
39
57
|
status = meta.status;
|
|
@@ -72,79 +90,54 @@ function caughtErrorToData(cause, errorOpts) {
|
|
|
72
90
|
body
|
|
73
91
|
};
|
|
74
92
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const
|
|
93
|
+
async function resolveResponse(opts) {
|
|
94
|
+
const { router: router$1 , req } = opts;
|
|
95
|
+
const url = new URL(req.url);
|
|
78
96
|
if (req.method === 'HEAD') {
|
|
79
97
|
// can be used for lambda warmup
|
|
80
|
-
|
|
98
|
+
return new Response(null, {
|
|
81
99
|
status: 204
|
|
82
|
-
};
|
|
83
|
-
unstable_onHead?.(headResponse, false);
|
|
84
|
-
unstable_onChunk?.([
|
|
85
|
-
-1,
|
|
86
|
-
''
|
|
87
|
-
]);
|
|
88
|
-
return headResponse;
|
|
100
|
+
});
|
|
89
101
|
}
|
|
90
|
-
const contentTypeHandler = opts.contentTypeHandler ?? fallbackContentTypeHandler;
|
|
91
102
|
const allowBatching = opts.allowBatching ?? opts.batching?.enabled ?? true;
|
|
92
|
-
const allowMethodOverride = opts.allowMethodOverride ?? false;
|
|
103
|
+
const allowMethodOverride = (opts.allowMethodOverride ?? false) && req.method === 'POST';
|
|
93
104
|
const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
|
|
94
105
|
let ctx = undefined;
|
|
95
|
-
let
|
|
96
|
-
const
|
|
97
|
-
const isStreamCall = isBatchCall && unstable_onHead && unstable_onChunk && req.headers['trpc-batch-mode'] === 'stream';
|
|
106
|
+
let info = undefined;
|
|
107
|
+
const isStreamCall = req.headers.get('trpc-batch-mode') === 'stream';
|
|
98
108
|
try {
|
|
109
|
+
info = contentType.getRequestInfo({
|
|
110
|
+
req,
|
|
111
|
+
path: decodeURIComponent(opts.path),
|
|
112
|
+
config: router$1._def._config,
|
|
113
|
+
searchParams: url.searchParams
|
|
114
|
+
});
|
|
115
|
+
// we create context early so that error handlers may access context information
|
|
116
|
+
ctx = await opts.createContext({
|
|
117
|
+
info
|
|
118
|
+
});
|
|
99
119
|
if (opts.error) {
|
|
100
120
|
throw opts.error;
|
|
101
121
|
}
|
|
102
|
-
if (isBatchCall && !allowBatching) {
|
|
122
|
+
if (info.isBatchCall && !allowBatching) {
|
|
103
123
|
throw new TRPCError.TRPCError({
|
|
104
124
|
code: 'BAD_REQUEST',
|
|
105
125
|
message: `Batching is not enabled on the server`
|
|
106
126
|
});
|
|
107
127
|
}
|
|
108
|
-
/* istanbul ignore if -- @preserve */ if (type === 'subscription') {
|
|
109
|
-
throw new TRPCError.TRPCError({
|
|
110
|
-
message: 'Subscriptions should use wsLink',
|
|
111
|
-
code: 'METHOD_NOT_SUPPORTED'
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
128
|
if (type === 'unknown') {
|
|
115
129
|
throw new TRPCError.TRPCError({
|
|
116
130
|
message: `Unexpected request method ${req.method}`,
|
|
117
131
|
code: 'METHOD_NOT_SUPPORTED'
|
|
118
132
|
});
|
|
119
133
|
}
|
|
120
|
-
const inputs = await contentTypeHandler.getInputs({
|
|
121
|
-
isBatchCall,
|
|
122
|
-
req,
|
|
123
|
-
router: router$1,
|
|
124
|
-
preprocessedBody: opts.preprocessedBody ?? false
|
|
125
|
-
});
|
|
126
|
-
paths = isBatchCall ? decodeURIComponent(opts.path).split(',') : [
|
|
127
|
-
opts.path
|
|
128
|
-
];
|
|
129
|
-
const info = {
|
|
130
|
-
isBatchCall,
|
|
131
|
-
calls: paths.map((path, idx)=>({
|
|
132
|
-
path,
|
|
133
|
-
type,
|
|
134
|
-
input: inputs[idx] ?? undefined
|
|
135
|
-
}))
|
|
136
|
-
};
|
|
137
|
-
ctx = await opts.createContext({
|
|
138
|
-
info
|
|
139
|
-
});
|
|
140
134
|
const errors = [];
|
|
141
|
-
const promises =
|
|
142
|
-
const input = inputs[index];
|
|
135
|
+
const promises = info.calls.map(async (call)=>{
|
|
143
136
|
try {
|
|
144
137
|
const data = await router.callProcedure({
|
|
145
138
|
procedures: opts.router._def.procedures,
|
|
146
|
-
path,
|
|
147
|
-
getRawInput:
|
|
139
|
+
path: call.path,
|
|
140
|
+
getRawInput: call.getRawInput,
|
|
148
141
|
ctx,
|
|
149
142
|
type,
|
|
150
143
|
allowMethodOverride
|
|
@@ -157,9 +150,10 @@ async function resolveHTTPResponse(opts) {
|
|
|
157
150
|
} catch (cause) {
|
|
158
151
|
const error = TRPCError.getTRPCErrorFromUnknown(cause);
|
|
159
152
|
errors.push(error);
|
|
153
|
+
const input = call.result();
|
|
160
154
|
opts.onError?.({
|
|
161
155
|
error,
|
|
162
|
-
path,
|
|
156
|
+
path: call.path,
|
|
163
157
|
input,
|
|
164
158
|
ctx,
|
|
165
159
|
type: type,
|
|
@@ -170,7 +164,7 @@ async function resolveHTTPResponse(opts) {
|
|
|
170
164
|
config: opts.router._def._config,
|
|
171
165
|
error,
|
|
172
166
|
type,
|
|
173
|
-
path,
|
|
167
|
+
path: call.path,
|
|
174
168
|
input,
|
|
175
169
|
ctx
|
|
176
170
|
})
|
|
@@ -184,76 +178,79 @@ async function resolveHTTPResponse(opts) {
|
|
|
184
178
|
* - create headers with known response body
|
|
185
179
|
* - return a complete HTTPResponse
|
|
186
180
|
*/ const untransformedJSON = await Promise.all(promises);
|
|
187
|
-
const
|
|
181
|
+
const errors1 = untransformedJSON.flatMap((response)=>'error' in response ? [
|
|
182
|
+
response.error
|
|
183
|
+
] : []);
|
|
184
|
+
const headResponse = initResponse({
|
|
188
185
|
ctx,
|
|
189
|
-
|
|
186
|
+
info,
|
|
190
187
|
type,
|
|
191
188
|
responseMeta: opts.responseMeta,
|
|
192
189
|
untransformedJSON,
|
|
193
|
-
errors
|
|
190
|
+
errors: errors1
|
|
194
191
|
});
|
|
195
|
-
unstable_onHead?.(headResponse1, false);
|
|
196
192
|
// return body stuff
|
|
197
|
-
const result = isBatchCall ? untransformedJSON : untransformedJSON[0];
|
|
193
|
+
const result = info.isBatchCall ? untransformedJSON : untransformedJSON[0];
|
|
198
194
|
const transformedJSON = transformer.transformTRPCResponse(router$1._def._config, result);
|
|
199
195
|
const body = JSON.stringify(transformedJSON);
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
return {
|
|
205
|
-
status: headResponse1.status,
|
|
206
|
-
headers: headResponse1.headers,
|
|
207
|
-
body
|
|
208
|
-
};
|
|
196
|
+
return new Response(body, {
|
|
197
|
+
status: headResponse.status,
|
|
198
|
+
headers: headResponse.headers
|
|
199
|
+
});
|
|
209
200
|
}
|
|
210
201
|
/**
|
|
211
202
|
* Streaming response:
|
|
212
203
|
* - block on none, call `onChunk` as soon as each response is ready
|
|
213
204
|
* - create headers with minimal data (cannot know the response body in advance)
|
|
214
205
|
* - return void
|
|
215
|
-
*/ const
|
|
206
|
+
*/ const headResponse1 = initResponse({
|
|
216
207
|
ctx,
|
|
217
|
-
|
|
208
|
+
info,
|
|
218
209
|
type,
|
|
219
|
-
responseMeta: opts.responseMeta
|
|
210
|
+
responseMeta: opts.responseMeta,
|
|
211
|
+
errors: []
|
|
220
212
|
});
|
|
221
|
-
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
r
|
|
227
|
-
])
|
|
228
|
-
]));
|
|
229
|
-
for (const _ of paths){
|
|
230
|
-
const [index, untransformedJSON1] = await Promise.race(indexedPromises.values());
|
|
231
|
-
indexedPromises.delete(index);
|
|
232
|
-
try {
|
|
233
|
-
const transformedJSON1 = transformer.transformTRPCResponse(router$1._def._config, untransformedJSON1);
|
|
234
|
-
const body1 = JSON.stringify(transformedJSON1);
|
|
235
|
-
unstable_onChunk([
|
|
213
|
+
const encoder = new TextEncoderStream();
|
|
214
|
+
const stream = encoder.readable;
|
|
215
|
+
const controller = encoder.writable.getWriter();
|
|
216
|
+
async function exec() {
|
|
217
|
+
const indexedPromises = new Map(promises.map((promise, index)=>[
|
|
236
218
|
index,
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
219
|
+
promise.then((r)=>[
|
|
220
|
+
index,
|
|
221
|
+
r
|
|
222
|
+
])
|
|
223
|
+
]));
|
|
224
|
+
const formatter = batchStreamFormatter.getBatchStreamFormatter();
|
|
225
|
+
while(indexedPromises.size > 0){
|
|
226
|
+
const [index, untransformedJSON] = await Promise.race(indexedPromises.values());
|
|
227
|
+
indexedPromises.delete(index);
|
|
228
|
+
try {
|
|
229
|
+
const transformedJSON = transformer.transformTRPCResponse(router$1._def._config, untransformedJSON);
|
|
230
|
+
const body = JSON.stringify(transformedJSON);
|
|
231
|
+
await controller.write(formatter(index, body));
|
|
232
|
+
} catch (cause) {
|
|
233
|
+
const call = info.calls[index];
|
|
234
|
+
const input = call.result();
|
|
235
|
+
const { body: body1 } = caughtErrorToData(cause, {
|
|
236
|
+
opts,
|
|
237
|
+
ctx,
|
|
238
|
+
type,
|
|
239
|
+
path: call.path,
|
|
240
|
+
input
|
|
241
|
+
});
|
|
242
|
+
await controller.write(formatter(index, body1));
|
|
243
|
+
}
|
|
253
244
|
}
|
|
245
|
+
await controller.write(formatter.end());
|
|
246
|
+
await controller.close();
|
|
254
247
|
}
|
|
255
|
-
|
|
256
|
-
|
|
248
|
+
exec().catch((err)=>controller.abort(err));
|
|
249
|
+
return new Response(stream, {
|
|
250
|
+
headers: headResponse1.headers,
|
|
251
|
+
status: headResponse1.status
|
|
252
|
+
});
|
|
253
|
+
} catch (cause) {
|
|
257
254
|
// we get here if
|
|
258
255
|
// - batching is called when it's not enabled
|
|
259
256
|
// - `createContext()` throws
|
|
@@ -261,32 +258,26 @@ async function resolveHTTPResponse(opts) {
|
|
|
261
258
|
// - post body is too large
|
|
262
259
|
// - input deserialization fails
|
|
263
260
|
// - `errorFormatter` return value is malformed
|
|
264
|
-
const { error , untransformedJSON:
|
|
261
|
+
const { error , untransformedJSON: untransformedJSON1 , body: body1 } = caughtErrorToData(cause, {
|
|
265
262
|
opts,
|
|
266
263
|
ctx,
|
|
267
264
|
type
|
|
268
265
|
});
|
|
269
|
-
const
|
|
266
|
+
const headResponse2 = initResponse({
|
|
270
267
|
ctx,
|
|
271
|
-
|
|
268
|
+
info,
|
|
272
269
|
type,
|
|
273
270
|
responseMeta: opts.responseMeta,
|
|
274
|
-
untransformedJSON:
|
|
271
|
+
untransformedJSON: untransformedJSON1,
|
|
275
272
|
errors: [
|
|
276
273
|
error
|
|
277
274
|
]
|
|
278
275
|
});
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
]);
|
|
284
|
-
return {
|
|
285
|
-
status: headResponse3.status,
|
|
286
|
-
headers: headResponse3.headers,
|
|
287
|
-
body: body3
|
|
288
|
-
};
|
|
276
|
+
return new Response(body1, {
|
|
277
|
+
status: headResponse2.status,
|
|
278
|
+
headers: headResponse2.headers
|
|
279
|
+
});
|
|
289
280
|
}
|
|
290
281
|
}
|
|
291
282
|
|
|
292
|
-
exports.
|
|
283
|
+
exports.resolveResponse = resolveResponse;
|