@trpc/server 11.0.0-next-beta.216 → 11.0.0-next-beta.221
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 -0
- package/dist/@trpc/server/http.d.ts.map +1 -0
- package/dist/{@trpc-server.d.ts → @trpc/server/index.d.ts} +4 -4
- package/dist/@trpc/server/index.d.ts.map +1 -0
- package/dist/@trpc/server/rpc.d.ts +3 -0
- package/dist/@trpc/server/rpc.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/index.d.ts +1 -2
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +6 -5
- package/dist/adapters/aws-lambda/index.mjs +4 -3
- package/dist/adapters/aws-lambda/utils.d.ts +2 -3
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/utils.js +5 -4
- package/dist/adapters/aws-lambda/utils.mjs +3 -2
- package/dist/adapters/express.d.ts +1 -2
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +2 -2
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +6 -3
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +4 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -2
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +0 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +0 -1
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -2
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +6 -4
- package/dist/adapters/fetch/fetchRequestHandler.mjs +4 -2
- package/dist/adapters/fetch/types.d.ts +2 -3
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next.d.ts +4 -2
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +6 -4
- package/dist/adapters/next.mjs +4 -2
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.js +4 -2
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +3 -1
- package/dist/adapters/node-http/content-type/json/index.js +4 -2
- package/dist/adapters/node-http/content-type/json/index.mjs +3 -1
- package/dist/adapters/node-http/internals/contentType.d.ts +2 -2
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -2
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +6 -4
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +4 -2
- package/dist/adapters/node-http/types.d.ts +3 -4
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts +1 -2
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +1 -0
- package/dist/adapters/standalone.mjs +1 -0
- package/dist/adapters/ws.d.ts +3 -3
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +30 -25
- package/dist/adapters/ws.mjs +8 -3
- package/dist/bundle-analysis.json +419 -78
- package/dist/http.d.ts +1 -1
- package/dist/http.js +11 -7
- package/dist/http.mjs +6 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +18 -41
- package/dist/index.mjs +8 -1
- package/dist/observable/index.d.ts +5 -0
- package/dist/observable/index.d.ts.map +1 -0
- package/dist/observable/index.js +13 -0
- package/dist/observable/index.mjs +2 -0
- package/dist/observable/observable.d.ts +13 -0
- package/dist/observable/observable.d.ts.map +1 -0
- package/dist/observable/observable.js +126 -0
- package/dist/observable/observable.mjs +122 -0
- package/dist/observable/operators.d.ts +8 -0
- package/dist/observable/operators.d.ts.map +1 -0
- package/dist/observable/operators.js +103 -0
- package/dist/observable/operators.mjs +99 -0
- package/dist/observable/types.d.ts +26 -0
- package/dist/observable/types.d.ts.map +1 -0
- package/dist/rpc.d.ts +1 -1
- package/dist/rpc.d.ts.map +1 -1
- package/dist/rpc.js +6 -7
- package/dist/rpc.mjs +3 -1
- package/dist/shared.d.ts +1 -1
- package/dist/shared.js +6 -9
- package/dist/shared.mjs +4 -1
- package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts +6 -0
- package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/createProxy.d.ts +19 -0
- package/dist/unstable-core-do-not-import/createProxy.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/createProxy.js +52 -0
- package/dist/unstable-core-do-not-import/createProxy.mjs +49 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +13 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.js +67 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.mjs +63 -0
- package/dist/unstable-core-do-not-import/error/formatter.d.ts +33 -0
- package/dist/unstable-core-do-not-import/error/formatter.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/error/formatter.js +7 -0
- package/dist/unstable-core-do-not-import/error/formatter.mjs +5 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts +15 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.js +31 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.mjs +29 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +24 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.js +32 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.mjs +30 -0
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +29 -0
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/contentType.js +54 -0
- package/dist/unstable-core-do-not-import/http/contentType.mjs +52 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +5 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +51 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +48 -0
- package/dist/unstable-core-do-not-import/http/index.d.ts +7 -0
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +51 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +295 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +293 -0
- package/dist/unstable-core-do-not-import/http/types.d.ts +96 -0
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/index.d.ts +37 -0
- package/dist/unstable-core-do-not-import/index.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/index.js +48 -0
- package/dist/unstable-core-do-not-import/index.mjs +16 -0
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +96 -0
- package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/initTRPC.js +95 -0
- package/dist/unstable-core-do-not-import/initTRPC.mjs +93 -0
- package/dist/unstable-core-do-not-import/middleware.d.ts +105 -0
- package/dist/unstable-core-do-not-import/middleware.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/middleware.js +95 -0
- package/dist/unstable-core-do-not-import/middleware.mjs +89 -0
- package/dist/unstable-core-do-not-import/parser.d.ts +30 -0
- package/dist/unstable-core-do-not-import/parser.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/parser.js +37 -0
- package/dist/unstable-core-do-not-import/parser.mjs +35 -0
- package/dist/unstable-core-do-not-import/procedure.d.ts +72 -0
- package/dist/unstable-core-do-not-import/procedure.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/procedure.js +9 -0
- package/dist/unstable-core-do-not-import/procedure.mjs +7 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +101 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.js +176 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +173 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +75 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rootConfig.js +8 -0
- package/dist/unstable-core-do-not-import/rootConfig.mjs +6 -0
- package/dist/unstable-core-do-not-import/router.d.ts +85 -0
- package/dist/unstable-core-do-not-import/router.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/router.js +156 -0
- package/dist/unstable-core-do-not-import/router.mjs +151 -0
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts +51 -0
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/codes.js +38 -0
- package/dist/unstable-core-do-not-import/rpc/codes.mjs +35 -0
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +97 -0
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/index.d.ts +5 -0
- package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts +5 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.js +59 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.mjs +57 -0
- package/dist/unstable-core-do-not-import/serialize.d.ts +62 -0
- package/dist/unstable-core-do-not-import/serialize.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/transformer.d.ts +107 -0
- package/dist/unstable-core-do-not-import/transformer.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/transformer.js +107 -0
- package/dist/unstable-core-do-not-import/transformer.mjs +102 -0
- package/dist/unstable-core-do-not-import/types.d.ts +98 -0
- package/dist/unstable-core-do-not-import/types.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/utils.d.ts +27 -0
- package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/utils.js +44 -0
- package/dist/unstable-core-do-not-import/utils.mjs +39 -0
- package/package.json +11 -8
- package/src/@trpc/server/http.ts +26 -0
- package/src/{@trpc-server.ts → @trpc/server/index.ts} +3 -3
- package/src/@trpc/server/rpc.ts +26 -0
- package/src/adapters/aws-lambda/index.ts +4 -5
- package/src/adapters/aws-lambda/utils.ts +3 -4
- package/src/adapters/express.ts +1 -3
- package/src/adapters/fastify/fastifyRequestHandler.ts +6 -4
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -2
- package/src/adapters/fetch/fetchRequestHandler.ts +6 -4
- package/src/adapters/fetch/types.ts +2 -3
- package/src/adapters/next.ts +6 -3
- package/src/adapters/node-http/content-type/form-data/index.ts +1 -1
- package/src/adapters/node-http/content-type/json/getPostBody.ts +2 -2
- package/src/adapters/node-http/content-type/json/index.ts +1 -1
- package/src/adapters/node-http/internals/contentType.ts +2 -2
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +6 -4
- package/src/adapters/node-http/types.ts +4 -5
- package/src/adapters/standalone.ts +2 -2
- package/src/adapters/ws.ts +15 -14
- package/src/http.ts +1 -1
- package/src/index.ts +1 -1
- package/src/observable/index.ts +10 -0
- package/src/observable/observable.ts +158 -0
- package/src/observable/operators.ts +119 -0
- package/src/observable/types.ts +76 -0
- package/src/rpc.ts +1 -2
- package/src/shared.ts +1 -1
- package/src/unstable-core-do-not-import/TRPCInferrable.ts +9 -0
- package/src/unstable-core-do-not-import/createProxy.ts +59 -0
- package/src/unstable-core-do-not-import/error/TRPCError.ts +82 -0
- package/src/unstable-core-do-not-import/error/formatter.ts +51 -0
- package/src/unstable-core-do-not-import/error/getErrorShape.ts +36 -0
- package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +29 -0
- package/src/unstable-core-do-not-import/http/contentType.ts +99 -0
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +57 -0
- package/src/unstable-core-do-not-import/http/index.ts +23 -0
- package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +458 -0
- package/src/unstable-core-do-not-import/http/types.ts +111 -0
- package/src/unstable-core-do-not-import/index.ts +110 -0
- package/src/unstable-core-do-not-import/initTRPC.ts +206 -0
- package/src/unstable-core-do-not-import/middleware.ts +237 -0
- package/src/unstable-core-do-not-import/parser.ts +94 -0
- package/src/unstable-core-do-not-import/procedure.ts +108 -0
- package/src/unstable-core-do-not-import/procedureBuilder.ts +458 -0
- package/src/unstable-core-do-not-import/rootConfig.ts +90 -0
- package/src/unstable-core-do-not-import/router.ts +370 -0
- package/src/unstable-core-do-not-import/rpc/codes.ts +44 -0
- package/src/unstable-core-do-not-import/rpc/envelopes.ts +136 -0
- package/src/unstable-core-do-not-import/rpc/index.ts +21 -0
- package/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts +85 -0
- package/src/unstable-core-do-not-import/serialize.ts +122 -0
- package/src/unstable-core-do-not-import/transformer.ts +202 -0
- package/src/unstable-core-do-not-import/types.ts +151 -0
- package/src/unstable-core-do-not-import/utils.ts +59 -0
- package/unstable-core-do-not-import/index.d.ts +1 -0
- package/unstable-core-do-not-import/index.js +1 -0
- package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts +0 -9
- package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts.map +0 -1
- package/dist/@trpc-server/http.d.ts +0 -2
- package/dist/@trpc-server/http.d.ts.map +0 -1
- package/dist/@trpc-server.d.ts.map +0 -1
- package/dist/observable.d.ts +0 -2
- package/dist/observable.d.ts.map +0 -1
- package/dist/observable.js +0 -12
- package/dist/observable.mjs +0 -1
- package/src/@trpc-core-unstable-do-not-import-this-please.ts +0 -9
- package/src/@trpc-server/http.ts +0 -2
- package/src/observable.ts +0 -1
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var TRPCError = require('../error/TRPCError.js');
|
|
4
|
+
|
|
5
|
+
function getRawProcedureInputOrThrow(opts) {
|
|
6
|
+
const { req } = opts;
|
|
7
|
+
try {
|
|
8
|
+
if (req.method === 'GET') {
|
|
9
|
+
if (!req.query.has('input')) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
const raw = req.query.get('input');
|
|
13
|
+
return JSON.parse(raw);
|
|
14
|
+
}
|
|
15
|
+
if (!opts.preprocessedBody && typeof req.body === 'string') {
|
|
16
|
+
// A mutation with no inputs will have req.body === ''
|
|
17
|
+
return req.body.length === 0 ? undefined : JSON.parse(req.body);
|
|
18
|
+
}
|
|
19
|
+
return req.body;
|
|
20
|
+
} catch (cause) {
|
|
21
|
+
throw new TRPCError.TRPCError({
|
|
22
|
+
code: 'PARSE_ERROR',
|
|
23
|
+
cause
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const deserializeInputValue = (rawValue, transformer)=>{
|
|
28
|
+
return typeof rawValue !== 'undefined' ? transformer.input.deserialize(rawValue) : rawValue;
|
|
29
|
+
};
|
|
30
|
+
const getJsonContentTypeInputs = (opts)=>{
|
|
31
|
+
const rawInput = getRawProcedureInputOrThrow(opts);
|
|
32
|
+
const transformer = opts.router._def._config.transformer;
|
|
33
|
+
if (!opts.isBatchCall) {
|
|
34
|
+
return {
|
|
35
|
+
0: deserializeInputValue(rawInput, transformer)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/* istanbul ignore if */ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
|
|
39
|
+
throw new TRPCError.TRPCError({
|
|
40
|
+
code: 'BAD_REQUEST',
|
|
41
|
+
message: '"input" needs to be an object when doing a batch call'
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
const input = {};
|
|
45
|
+
for(const key in rawInput){
|
|
46
|
+
const k = key;
|
|
47
|
+
const rawValue = rawInput[k];
|
|
48
|
+
const value = deserializeInputValue(rawValue, transformer);
|
|
49
|
+
input[k] = value;
|
|
50
|
+
}
|
|
51
|
+
return input;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
exports.getJsonContentTypeInputs = getJsonContentTypeInputs;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { TRPCError } from '../error/TRPCError.mjs';
|
|
2
|
+
|
|
3
|
+
function getRawProcedureInputOrThrow(opts) {
|
|
4
|
+
const { req } = opts;
|
|
5
|
+
try {
|
|
6
|
+
if (req.method === 'GET') {
|
|
7
|
+
if (!req.query.has('input')) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
const raw = req.query.get('input');
|
|
11
|
+
return JSON.parse(raw);
|
|
12
|
+
}
|
|
13
|
+
if (!opts.preprocessedBody && typeof req.body === 'string') {
|
|
14
|
+
// A mutation with no inputs will have req.body === ''
|
|
15
|
+
return req.body.length === 0 ? undefined : JSON.parse(req.body);
|
|
16
|
+
}
|
|
17
|
+
return req.body;
|
|
18
|
+
} catch (cause) {
|
|
19
|
+
throw new TRPCError({
|
|
20
|
+
code: 'PARSE_ERROR',
|
|
21
|
+
cause
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const deserializeInputValue = (rawValue, transformer)=>{
|
|
26
|
+
return typeof rawValue !== 'undefined' ? transformer.input.deserialize(rawValue) : rawValue;
|
|
27
|
+
};
|
|
28
|
+
const getJsonContentTypeInputs = (opts)=>{
|
|
29
|
+
const rawInput = getRawProcedureInputOrThrow(opts);
|
|
30
|
+
const transformer = opts.router._def._config.transformer;
|
|
31
|
+
if (!opts.isBatchCall) {
|
|
32
|
+
return {
|
|
33
|
+
0: deserializeInputValue(rawInput, transformer)
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/* istanbul ignore if */ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
|
|
37
|
+
throw new TRPCError({
|
|
38
|
+
code: 'BAD_REQUEST',
|
|
39
|
+
message: '"input" needs to be an object when doing a batch call'
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const input = {};
|
|
43
|
+
for(const key in rawInput){
|
|
44
|
+
const k = key;
|
|
45
|
+
const rawValue = rawInput[k];
|
|
46
|
+
const value = deserializeInputValue(rawValue, transformer);
|
|
47
|
+
input[k] = value;
|
|
48
|
+
}
|
|
49
|
+
return input;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export { getJsonContentTypeInputs };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { TRPCError } from '../error/TRPCError';
|
|
2
|
+
import type { TRPCResponse } from '../rpc';
|
|
3
|
+
export declare function getHTTPStatusCode(json: TRPCResponse | TRPCResponse[]): any;
|
|
4
|
+
export declare function getHTTPStatusCodeFromError(error: TRPCError): number;
|
|
5
|
+
//# sourceMappingURL=getHTTPStatusCode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getHTTPStatusCode.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/getHTTPStatusCode.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAA2B,YAAY,EAAE,MAAM,QAAQ,CAAC;AA4BpE,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,YAAY,GAAG,YAAY,EAAE,OAuBpE;AAED,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,SAAS,UAE1D"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var codes = require('../rpc/codes.js');
|
|
4
|
+
|
|
5
|
+
const JSONRPC2_TO_HTTP_CODE = {
|
|
6
|
+
PARSE_ERROR: 400,
|
|
7
|
+
BAD_REQUEST: 400,
|
|
8
|
+
UNAUTHORIZED: 401,
|
|
9
|
+
NOT_FOUND: 404,
|
|
10
|
+
FORBIDDEN: 403,
|
|
11
|
+
METHOD_NOT_SUPPORTED: 405,
|
|
12
|
+
TIMEOUT: 408,
|
|
13
|
+
CONFLICT: 409,
|
|
14
|
+
PRECONDITION_FAILED: 412,
|
|
15
|
+
PAYLOAD_TOO_LARGE: 413,
|
|
16
|
+
UNPROCESSABLE_CONTENT: 422,
|
|
17
|
+
TOO_MANY_REQUESTS: 429,
|
|
18
|
+
CLIENT_CLOSED_REQUEST: 499,
|
|
19
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
20
|
+
NOT_IMPLEMENTED: 501
|
|
21
|
+
};
|
|
22
|
+
function getStatusCodeFromKey(code) {
|
|
23
|
+
return JSONRPC2_TO_HTTP_CODE[code] ?? 500;
|
|
24
|
+
}
|
|
25
|
+
function getHTTPStatusCode(json) {
|
|
26
|
+
const arr = Array.isArray(json) ? json : [
|
|
27
|
+
json
|
|
28
|
+
];
|
|
29
|
+
const httpStatuses = new Set(arr.map((res)=>{
|
|
30
|
+
if ('error' in res) {
|
|
31
|
+
const data = res.error.data;
|
|
32
|
+
if (typeof data['httpStatus'] === 'number') {
|
|
33
|
+
return data['httpStatus'];
|
|
34
|
+
}
|
|
35
|
+
const code = codes.TRPC_ERROR_CODES_BY_NUMBER[res.error.code];
|
|
36
|
+
return getStatusCodeFromKey(code);
|
|
37
|
+
}
|
|
38
|
+
return 200;
|
|
39
|
+
}));
|
|
40
|
+
if (httpStatuses.size !== 1) {
|
|
41
|
+
return 207;
|
|
42
|
+
}
|
|
43
|
+
const httpStatus = httpStatuses.values().next().value;
|
|
44
|
+
return httpStatus;
|
|
45
|
+
}
|
|
46
|
+
function getHTTPStatusCodeFromError(error) {
|
|
47
|
+
return getStatusCodeFromKey(error.code);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
exports.getHTTPStatusCode = getHTTPStatusCode;
|
|
51
|
+
exports.getHTTPStatusCodeFromError = getHTTPStatusCodeFromError;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { TRPC_ERROR_CODES_BY_NUMBER } from '../rpc/codes.mjs';
|
|
2
|
+
|
|
3
|
+
const JSONRPC2_TO_HTTP_CODE = {
|
|
4
|
+
PARSE_ERROR: 400,
|
|
5
|
+
BAD_REQUEST: 400,
|
|
6
|
+
UNAUTHORIZED: 401,
|
|
7
|
+
NOT_FOUND: 404,
|
|
8
|
+
FORBIDDEN: 403,
|
|
9
|
+
METHOD_NOT_SUPPORTED: 405,
|
|
10
|
+
TIMEOUT: 408,
|
|
11
|
+
CONFLICT: 409,
|
|
12
|
+
PRECONDITION_FAILED: 412,
|
|
13
|
+
PAYLOAD_TOO_LARGE: 413,
|
|
14
|
+
UNPROCESSABLE_CONTENT: 422,
|
|
15
|
+
TOO_MANY_REQUESTS: 429,
|
|
16
|
+
CLIENT_CLOSED_REQUEST: 499,
|
|
17
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
18
|
+
NOT_IMPLEMENTED: 501
|
|
19
|
+
};
|
|
20
|
+
function getStatusCodeFromKey(code) {
|
|
21
|
+
return JSONRPC2_TO_HTTP_CODE[code] ?? 500;
|
|
22
|
+
}
|
|
23
|
+
function getHTTPStatusCode(json) {
|
|
24
|
+
const arr = Array.isArray(json) ? json : [
|
|
25
|
+
json
|
|
26
|
+
];
|
|
27
|
+
const httpStatuses = new Set(arr.map((res)=>{
|
|
28
|
+
if ('error' in res) {
|
|
29
|
+
const data = res.error.data;
|
|
30
|
+
if (typeof data['httpStatus'] === 'number') {
|
|
31
|
+
return data['httpStatus'];
|
|
32
|
+
}
|
|
33
|
+
const code = TRPC_ERROR_CODES_BY_NUMBER[res.error.code];
|
|
34
|
+
return getStatusCodeFromKey(code);
|
|
35
|
+
}
|
|
36
|
+
return 200;
|
|
37
|
+
}));
|
|
38
|
+
if (httpStatuses.size !== 1) {
|
|
39
|
+
return 207;
|
|
40
|
+
}
|
|
41
|
+
const httpStatus = httpStatuses.values().next().value;
|
|
42
|
+
return httpStatus;
|
|
43
|
+
}
|
|
44
|
+
function getHTTPStatusCodeFromError(error) {
|
|
45
|
+
return getStatusCodeFromKey(error.code);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { getHTTPStatusCode, getHTTPStatusCodeFromError };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { getHTTPStatusCode, getHTTPStatusCodeFromError, } from './getHTTPStatusCode';
|
|
2
|
+
export { resolveHTTPResponse } from './resolveHTTPResponse';
|
|
3
|
+
export type { BaseHandlerOptions, HTTPBaseHandlerOptions, HTTPHeaders, HTTPRequest, HTTPResponse, OnErrorFunction, ProcedureCall, ResolveHTTPRequestOptionsContextFn, ResponseChunk, ResponseMeta, ResponseMetaFn, TRPCRequestInfo, } from './types';
|
|
4
|
+
export { getBatchStreamFormatter } from './batchStreamFormatter';
|
|
5
|
+
export type { BaseContentTypeHandler, BodyResult } from './contentType';
|
|
6
|
+
export { getJsonContentTypeInputs } from './contentType';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,0BAA0B,GAC3B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,YAAY,EACV,kBAAkB,EAClB,sBAAsB,EACtB,WAAW,EACX,WAAW,EACX,YAAY,EACZ,eAAe,EACf,aAAa,EACb,kCAAkC,EAClC,aAAa,EACb,YAAY,EACZ,cAAc,EACd,eAAe,GAChB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,YAAY,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACxE,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { TRPCError } from '../error/TRPCError';
|
|
2
|
+
import type { AnyRouter } from '../router';
|
|
3
|
+
import type { Maybe } from '../types';
|
|
4
|
+
import type { BaseContentTypeHandler } from './contentType';
|
|
5
|
+
import type { HTTPBaseHandlerOptions, HTTPRequest, HTTPResponse, ResolveHTTPRequestOptionsContextFn, ResponseChunk } from './types';
|
|
6
|
+
interface ResolveHTTPRequestOptions<TRouter extends AnyRouter, TRequest extends HTTPRequest> extends HTTPBaseHandlerOptions<TRouter, TRequest> {
|
|
7
|
+
createContext: ResolveHTTPRequestOptionsContextFn<TRouter>;
|
|
8
|
+
req: TRequest;
|
|
9
|
+
path: string;
|
|
10
|
+
error?: Maybe<TRPCError>;
|
|
11
|
+
contentTypeHandler?: BaseContentTypeHandler<any>;
|
|
12
|
+
preprocessedBody?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Called as soon as the response head is known.
|
|
15
|
+
* When streaming, headers will have been generated
|
|
16
|
+
* **without** knowing the response body.
|
|
17
|
+
*
|
|
18
|
+
* Without this callback, streaming is disabled.
|
|
19
|
+
*/
|
|
20
|
+
unstable_onHead: (headResponse: Omit<HTTPResponse, 'body'>, isStreaming: boolean) => void;
|
|
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;
|
|
31
|
+
}
|
|
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>;
|
|
50
|
+
export {};
|
|
51
|
+
//# sourceMappingURL=resolveHTTPResponse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveHTTPResponse.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/http/resolveHTTPResponse.ts"],"names":[],"mappings":"AACA,OAAO,EAA2B,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAExE,OAAO,KAAK,EACV,SAAS,EAGV,MAAM,WAAW,CAAC;AAInB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAG5D,OAAO,KAAK,EACV,sBAAsB,EAEtB,WAAW,EACX,YAAY,EACZ,kCAAkC,EAClC,aAAa,EAEd,MAAM,SAAS,CAAC;AAoBjB,UAAU,yBAAyB,CACjC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,WAAW,CAC5B,SAAQ,sBAAsB,CAAC,OAAO,EAAE,QAAQ,CAAC;IACjD,aAAa,EAAE,kCAAkC,CAAC,OAAO,CAAC,CAAC;IAC3D,GAAG,EAAE,QAAQ,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;IACzB,kBAAkB,CAAC,EAAE,sBAAsB,CAAC,GAAG,CAAC,CAAC;IACjD,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B;;;;;;OAMG;IACH,eAAe,EAAE,CACf,YAAY,EAAE,IAAI,CAAC,YAAY,EAAE,MAAM,CAAC,EACxC,WAAW,EAAE,OAAO,KACjB,IAAI,CAAC;IACV;;;;;;;;OAQG;IACH,gBAAgB,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;CAClD;AAyJD;;;GAGG;AAEH;;;;;GAKG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,WAAW,EAE5B,IAAI,EAAE,IAAI,CACR,yBAAyB,CAAC,OAAO,EAAE,QAAQ,CAAC,EAC5C,kBAAkB,GAAG,iBAAiB,CACvC,GACA,OAAO,CAAC,YAAY,CAAC,CAAC;AACzB;;;;;GAKG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,WAAW,EAC5B,IAAI,EAAE,yBAAyB,CAAC,OAAO,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var getErrorShape = require('../error/getErrorShape.js');
|
|
4
|
+
var TRPCError = require('../error/TRPCError.js');
|
|
5
|
+
var router = require('../router.js');
|
|
6
|
+
var transformer = require('../transformer.js');
|
|
7
|
+
var contentType = require('./contentType.js');
|
|
8
|
+
var getHTTPStatusCode = require('./getHTTPStatusCode.js');
|
|
9
|
+
|
|
10
|
+
const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
|
|
11
|
+
GET: 'query',
|
|
12
|
+
POST: 'mutation'
|
|
13
|
+
};
|
|
14
|
+
const fallbackContentTypeHandler = {
|
|
15
|
+
getInputs: contentType.getJsonContentTypeInputs
|
|
16
|
+
};
|
|
17
|
+
function initResponse(initOpts) {
|
|
18
|
+
const { ctx , paths , type , responseMeta , untransformedJSON , errors =[] , } = initOpts;
|
|
19
|
+
let status = untransformedJSON ? getHTTPStatusCode.getHTTPStatusCode(untransformedJSON) : 200;
|
|
20
|
+
const headers = {
|
|
21
|
+
'Content-Type': 'application/json'
|
|
22
|
+
};
|
|
23
|
+
const eagerGeneration = !untransformedJSON;
|
|
24
|
+
const data = eagerGeneration ? [] : Array.isArray(untransformedJSON) ? untransformedJSON : [
|
|
25
|
+
untransformedJSON
|
|
26
|
+
];
|
|
27
|
+
const meta = responseMeta?.({
|
|
28
|
+
ctx,
|
|
29
|
+
paths,
|
|
30
|
+
type,
|
|
31
|
+
data,
|
|
32
|
+
errors,
|
|
33
|
+
eagerGeneration
|
|
34
|
+
}) ?? {};
|
|
35
|
+
for (const [key, value] of Object.entries(meta.headers ?? {})){
|
|
36
|
+
headers[key] = value;
|
|
37
|
+
}
|
|
38
|
+
if (meta.status) {
|
|
39
|
+
status = meta.status;
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
status,
|
|
43
|
+
headers
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async function inputToProcedureCall(procedureOpts) {
|
|
47
|
+
const { opts , ctx , type , input , path } = procedureOpts;
|
|
48
|
+
try {
|
|
49
|
+
const data = await router.callProcedure({
|
|
50
|
+
procedures: opts.router._def.procedures,
|
|
51
|
+
path,
|
|
52
|
+
getRawInput: async ()=>input,
|
|
53
|
+
ctx,
|
|
54
|
+
type
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
result: {
|
|
58
|
+
data
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
} catch (cause) {
|
|
62
|
+
const error = TRPCError.getTRPCErrorFromUnknown(cause);
|
|
63
|
+
opts.onError?.({
|
|
64
|
+
error,
|
|
65
|
+
path,
|
|
66
|
+
input,
|
|
67
|
+
ctx,
|
|
68
|
+
type: type,
|
|
69
|
+
req: opts.req
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
error: getErrorShape.getErrorShape({
|
|
73
|
+
config: opts.router._def._config,
|
|
74
|
+
error,
|
|
75
|
+
type,
|
|
76
|
+
path,
|
|
77
|
+
input,
|
|
78
|
+
ctx
|
|
79
|
+
})
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function caughtErrorToData(cause, errorOpts) {
|
|
84
|
+
const { router , req , onError } = errorOpts.opts;
|
|
85
|
+
const error = TRPCError.getTRPCErrorFromUnknown(cause);
|
|
86
|
+
onError?.({
|
|
87
|
+
error,
|
|
88
|
+
path: errorOpts.path,
|
|
89
|
+
input: errorOpts.input,
|
|
90
|
+
ctx: errorOpts.ctx,
|
|
91
|
+
type: errorOpts.type,
|
|
92
|
+
req
|
|
93
|
+
});
|
|
94
|
+
const untransformedJSON = {
|
|
95
|
+
error: getErrorShape.getErrorShape({
|
|
96
|
+
config: router._def._config,
|
|
97
|
+
error,
|
|
98
|
+
type: errorOpts.type,
|
|
99
|
+
path: errorOpts.path,
|
|
100
|
+
input: errorOpts.input,
|
|
101
|
+
ctx: errorOpts.ctx
|
|
102
|
+
})
|
|
103
|
+
};
|
|
104
|
+
const transformedJSON = transformer.transformTRPCResponse(router._def._config, untransformedJSON);
|
|
105
|
+
const body = JSON.stringify(transformedJSON);
|
|
106
|
+
return {
|
|
107
|
+
error,
|
|
108
|
+
untransformedJSON,
|
|
109
|
+
body
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
// implementation
|
|
113
|
+
async function resolveHTTPResponse(opts) {
|
|
114
|
+
const { router , req , unstable_onHead , unstable_onChunk } = opts;
|
|
115
|
+
if (req.method === 'HEAD') {
|
|
116
|
+
// can be used for lambda warmup
|
|
117
|
+
const headResponse = {
|
|
118
|
+
status: 204
|
|
119
|
+
};
|
|
120
|
+
unstable_onHead?.(headResponse, false);
|
|
121
|
+
unstable_onChunk?.([
|
|
122
|
+
-1,
|
|
123
|
+
''
|
|
124
|
+
]);
|
|
125
|
+
return headResponse;
|
|
126
|
+
}
|
|
127
|
+
const contentTypeHandler = opts.contentTypeHandler ?? fallbackContentTypeHandler;
|
|
128
|
+
const batchingEnabled = opts.batching?.enabled ?? true;
|
|
129
|
+
const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
|
|
130
|
+
let ctx = undefined;
|
|
131
|
+
let paths;
|
|
132
|
+
const isBatchCall = !!req.query.get('batch');
|
|
133
|
+
const isStreamCall = isBatchCall && unstable_onHead && unstable_onChunk && req.headers['trpc-batch-mode'] === 'stream';
|
|
134
|
+
try {
|
|
135
|
+
if (opts.error) {
|
|
136
|
+
throw opts.error;
|
|
137
|
+
}
|
|
138
|
+
if (isBatchCall && !batchingEnabled) {
|
|
139
|
+
throw new Error(`Batching is not enabled on the server`);
|
|
140
|
+
}
|
|
141
|
+
/* istanbul ignore if -- @preserve */ if (type === 'subscription') {
|
|
142
|
+
throw new TRPCError.TRPCError({
|
|
143
|
+
message: 'Subscriptions should use wsLink',
|
|
144
|
+
code: 'METHOD_NOT_SUPPORTED'
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (type === 'unknown') {
|
|
148
|
+
throw new TRPCError.TRPCError({
|
|
149
|
+
message: `Unexpected request method ${req.method}`,
|
|
150
|
+
code: 'METHOD_NOT_SUPPORTED'
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
const inputs = await contentTypeHandler.getInputs({
|
|
154
|
+
isBatchCall,
|
|
155
|
+
req,
|
|
156
|
+
router,
|
|
157
|
+
preprocessedBody: opts.preprocessedBody ?? false
|
|
158
|
+
});
|
|
159
|
+
paths = isBatchCall ? decodeURIComponent(opts.path).split(',') : [
|
|
160
|
+
opts.path
|
|
161
|
+
];
|
|
162
|
+
const info = {
|
|
163
|
+
isBatchCall,
|
|
164
|
+
calls: paths.map((path, idx)=>({
|
|
165
|
+
path,
|
|
166
|
+
type,
|
|
167
|
+
input: inputs[idx] ?? undefined
|
|
168
|
+
}))
|
|
169
|
+
};
|
|
170
|
+
ctx = await opts.createContext({
|
|
171
|
+
info
|
|
172
|
+
});
|
|
173
|
+
const promises = paths.map((path, index)=>inputToProcedureCall({
|
|
174
|
+
opts,
|
|
175
|
+
ctx,
|
|
176
|
+
type,
|
|
177
|
+
input: inputs[index],
|
|
178
|
+
path
|
|
179
|
+
}));
|
|
180
|
+
if (!isStreamCall) {
|
|
181
|
+
/**
|
|
182
|
+
* Non-streaming response:
|
|
183
|
+
* - await all responses in parallel, blocking on the slowest one
|
|
184
|
+
* - create headers with known response body
|
|
185
|
+
* - return a complete HTTPResponse
|
|
186
|
+
*/ const untransformedJSON = await Promise.all(promises);
|
|
187
|
+
const errors = untransformedJSON.flatMap((response)=>'error' in response ? [
|
|
188
|
+
response.error
|
|
189
|
+
] : []);
|
|
190
|
+
const headResponse1 = initResponse({
|
|
191
|
+
ctx,
|
|
192
|
+
paths,
|
|
193
|
+
type,
|
|
194
|
+
responseMeta: opts.responseMeta,
|
|
195
|
+
untransformedJSON,
|
|
196
|
+
errors
|
|
197
|
+
});
|
|
198
|
+
unstable_onHead?.(headResponse1, false);
|
|
199
|
+
// return body stuff
|
|
200
|
+
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
|
|
201
|
+
const transformedJSON = transformer.transformTRPCResponse(router._def._config, result);
|
|
202
|
+
const body = JSON.stringify(transformedJSON);
|
|
203
|
+
unstable_onChunk?.([
|
|
204
|
+
-1,
|
|
205
|
+
body
|
|
206
|
+
]);
|
|
207
|
+
return {
|
|
208
|
+
status: headResponse1.status,
|
|
209
|
+
headers: headResponse1.headers,
|
|
210
|
+
body
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Streaming response:
|
|
215
|
+
* - block on none, call `onChunk` as soon as each response is ready
|
|
216
|
+
* - create headers with minimal data (cannot know the response body in advance)
|
|
217
|
+
* - return void
|
|
218
|
+
*/ const headResponse2 = initResponse({
|
|
219
|
+
ctx,
|
|
220
|
+
paths,
|
|
221
|
+
type,
|
|
222
|
+
responseMeta: opts.responseMeta
|
|
223
|
+
});
|
|
224
|
+
unstable_onHead(headResponse2, true);
|
|
225
|
+
const indexedPromises = new Map(promises.map((promise, index)=>[
|
|
226
|
+
index,
|
|
227
|
+
promise.then((r)=>[
|
|
228
|
+
index,
|
|
229
|
+
r
|
|
230
|
+
])
|
|
231
|
+
]));
|
|
232
|
+
for (const _ of paths){
|
|
233
|
+
const [index, untransformedJSON1] = await Promise.race(indexedPromises.values());
|
|
234
|
+
indexedPromises.delete(index);
|
|
235
|
+
try {
|
|
236
|
+
const transformedJSON1 = transformer.transformTRPCResponse(router._def._config, untransformedJSON1);
|
|
237
|
+
const body1 = JSON.stringify(transformedJSON1);
|
|
238
|
+
unstable_onChunk([
|
|
239
|
+
index,
|
|
240
|
+
body1
|
|
241
|
+
]);
|
|
242
|
+
} catch (cause) {
|
|
243
|
+
const path = paths[index];
|
|
244
|
+
const input = inputs[index];
|
|
245
|
+
const { body: body2 } = caughtErrorToData(cause, {
|
|
246
|
+
opts,
|
|
247
|
+
ctx,
|
|
248
|
+
type,
|
|
249
|
+
path,
|
|
250
|
+
input
|
|
251
|
+
});
|
|
252
|
+
unstable_onChunk([
|
|
253
|
+
index,
|
|
254
|
+
body2
|
|
255
|
+
]);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return;
|
|
259
|
+
} catch (cause1) {
|
|
260
|
+
// we get here if
|
|
261
|
+
// - batching is called when it's not enabled
|
|
262
|
+
// - `createContext()` throws
|
|
263
|
+
// - `router._def._config.transformer.output.serialize()` throws
|
|
264
|
+
// - post body is too large
|
|
265
|
+
// - input deserialization fails
|
|
266
|
+
// - `errorFormatter` return value is malformed
|
|
267
|
+
const { error , untransformedJSON: untransformedJSON2 , body: body3 } = caughtErrorToData(cause1, {
|
|
268
|
+
opts,
|
|
269
|
+
ctx,
|
|
270
|
+
type
|
|
271
|
+
});
|
|
272
|
+
const headResponse3 = initResponse({
|
|
273
|
+
ctx,
|
|
274
|
+
paths,
|
|
275
|
+
type,
|
|
276
|
+
responseMeta: opts.responseMeta,
|
|
277
|
+
untransformedJSON: untransformedJSON2,
|
|
278
|
+
errors: [
|
|
279
|
+
error
|
|
280
|
+
]
|
|
281
|
+
});
|
|
282
|
+
unstable_onHead?.(headResponse3, false);
|
|
283
|
+
unstable_onChunk?.([
|
|
284
|
+
-1,
|
|
285
|
+
body3
|
|
286
|
+
]);
|
|
287
|
+
return {
|
|
288
|
+
status: headResponse3.status,
|
|
289
|
+
headers: headResponse3.headers,
|
|
290
|
+
body: body3
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
exports.resolveHTTPResponse = resolveHTTPResponse;
|