@trpc/server 11.0.0-alpha-tmp-export-from-main-nuke-core.239 → 11.0.0-alpha-tmp-export-from-main.219
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 +2 -0
- package/dist/@trpc-server/http.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/index.d.ts +2 -1
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +4 -6
- package/dist/adapters/aws-lambda/index.mjs +2 -4
- package/dist/adapters/aws-lambda/utils.d.ts +3 -2
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/utils.js +4 -5
- package/dist/adapters/aws-lambda/utils.mjs +2 -3
- package/dist/adapters/express.d.ts +2 -1
- 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 +3 -6
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +1 -4
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +2 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +1 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +1 -0
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +2 -1
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +4 -6
- package/dist/adapters/fetch/fetchRequestHandler.mjs +2 -4
- package/dist/adapters/fetch/types.d.ts +3 -2
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next.d.ts +3 -1
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +4 -6
- package/dist/adapters/next.mjs +2 -4
- 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 +2 -4
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +1 -3
- package/dist/adapters/node-http/content-type/json/index.js +2 -4
- package/dist/adapters/node-http/content-type/json/index.mjs +1 -3
- package/dist/adapters/node-http/internals/contentType.d.ts +2 -2
- package/dist/adapters/node-http/internals/contentType.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +2 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +4 -6
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +2 -4
- package/dist/adapters/node-http/types.d.ts +4 -3
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts +2 -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 -3
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +26 -33
- package/dist/adapters/ws.mjs +1 -8
- package/dist/http.d.ts +6 -1
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +21 -11
- package/dist/http.mjs +1 -6
- package/dist/index.d.ts +68 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +41 -18
- package/dist/index.mjs +1 -8
- package/dist/observable.d.ts +3 -0
- package/dist/observable.d.ts.map +1 -0
- package/dist/observable.js +30 -0
- package/dist/observable.mjs +1 -0
- package/dist/rpc.d.ts +2 -1
- package/dist/rpc.d.ts.map +1 -1
- package/dist/rpc.js +13 -6
- package/dist/rpc.mjs +1 -3
- package/dist/shared.d.ts +1 -1
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +9 -6
- package/dist/shared.mjs +1 -4
- package/dist/unstableDontImportMe.d.ts +10 -0
- package/dist/unstableDontImportMe.d.ts.map +1 -0
- package/dist/unstableDontImportMe.js +142 -0
- package/dist/unstableDontImportMe.mjs +1 -0
- package/package.json +135 -52
- package/src/@trpc-server/http.ts +2 -0
- package/src/adapters/aws-lambda/index.ts +5 -4
- package/src/adapters/aws-lambda/utils.ts +4 -3
- package/src/adapters/express.ts +3 -1
- package/src/adapters/fastify/fastifyRequestHandler.ts +4 -6
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +2 -1
- package/src/adapters/fetch/fetchRequestHandler.ts +4 -6
- package/src/adapters/fetch/types.ts +3 -2
- package/src/adapters/next.ts +4 -2
- 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 +4 -6
- package/src/adapters/node-http/types.ts +5 -4
- package/src/adapters/standalone.ts +2 -2
- package/src/adapters/ws.ts +13 -14
- package/src/http.ts +26 -1
- package/src/index.ts +105 -1
- package/src/observable.ts +16 -0
- package/src/rpc.ts +26 -1
- package/src/shared.ts +1 -1
- package/src/unstableDontImportMe.ts +146 -0
- package/unstableDontImportMe/index.d.ts +1 -0
- package/unstableDontImportMe/index.js +1 -0
- package/dist/@trpc/server/http.d.ts +0 -7
- package/dist/@trpc/server/http.d.ts.map +0 -1
- package/dist/@trpc/server/index.d.ts +0 -69
- package/dist/@trpc/server/index.d.ts.map +0 -1
- package/dist/@trpc/server/rpc.d.ts +0 -3
- package/dist/@trpc/server/rpc.d.ts.map +0 -1
- package/dist/bundle-analysis.json +0 -757
- package/dist/observable/index.d.ts +0 -5
- package/dist/observable/index.d.ts.map +0 -1
- package/dist/observable/index.js +0 -13
- package/dist/observable/index.mjs +0 -2
- package/dist/observable/observable.d.ts +0 -13
- package/dist/observable/observable.d.ts.map +0 -1
- package/dist/observable/observable.js +0 -126
- package/dist/observable/observable.mjs +0 -122
- package/dist/observable/operators.d.ts +0 -8
- package/dist/observable/operators.d.ts.map +0 -1
- package/dist/observable/operators.js +0 -103
- package/dist/observable/operators.mjs +0 -99
- package/dist/observable/types.d.ts +0 -26
- package/dist/observable/types.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts +0 -6
- package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/createProxy.d.ts +0 -19
- package/dist/unstable-core-do-not-import/createProxy.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/createProxy.js +0 -52
- package/dist/unstable-core-do-not-import/createProxy.mjs +0 -49
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +0 -13
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/error/TRPCError.js +0 -67
- package/dist/unstable-core-do-not-import/error/TRPCError.mjs +0 -63
- package/dist/unstable-core-do-not-import/error/formatter.d.ts +0 -33
- package/dist/unstable-core-do-not-import/error/formatter.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/error/formatter.js +0 -7
- package/dist/unstable-core-do-not-import/error/formatter.mjs +0 -5
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts +0 -15
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/error/getErrorShape.js +0 -31
- package/dist/unstable-core-do-not-import/error/getErrorShape.mjs +0 -29
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +0 -24
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.js +0 -32
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.mjs +0 -30
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +0 -29
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +0 -54
- package/dist/unstable-core-do-not-import/http/contentType.mjs +0 -52
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +0 -5
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +0 -51
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +0 -48
- package/dist/unstable-core-do-not-import/http/index.d.ts +0 -7
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +0 -51
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +0 -295
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +0 -293
- package/dist/unstable-core-do-not-import/http/types.d.ts +0 -96
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/index.d.ts +0 -37
- package/dist/unstable-core-do-not-import/index.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/index.js +0 -48
- package/dist/unstable-core-do-not-import/index.mjs +0 -16
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +0 -96
- package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/initTRPC.js +0 -95
- package/dist/unstable-core-do-not-import/initTRPC.mjs +0 -93
- package/dist/unstable-core-do-not-import/middleware.d.ts +0 -105
- package/dist/unstable-core-do-not-import/middleware.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/middleware.js +0 -95
- package/dist/unstable-core-do-not-import/middleware.mjs +0 -89
- package/dist/unstable-core-do-not-import/parser.d.ts +0 -30
- package/dist/unstable-core-do-not-import/parser.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/parser.js +0 -37
- package/dist/unstable-core-do-not-import/parser.mjs +0 -35
- package/dist/unstable-core-do-not-import/procedure.d.ts +0 -72
- package/dist/unstable-core-do-not-import/procedure.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/procedure.js +0 -9
- package/dist/unstable-core-do-not-import/procedure.mjs +0 -7
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +0 -101
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.js +0 -176
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +0 -173
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +0 -75
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rootConfig.js +0 -8
- package/dist/unstable-core-do-not-import/rootConfig.mjs +0 -6
- package/dist/unstable-core-do-not-import/router.d.ts +0 -85
- package/dist/unstable-core-do-not-import/router.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/router.js +0 -156
- package/dist/unstable-core-do-not-import/router.mjs +0 -151
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts +0 -51
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rpc/codes.js +0 -38
- package/dist/unstable-core-do-not-import/rpc/codes.mjs +0 -35
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +0 -97
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rpc/index.d.ts +0 -5
- package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts +0 -5
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.js +0 -59
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.mjs +0 -57
- package/dist/unstable-core-do-not-import/serialize.d.ts +0 -62
- package/dist/unstable-core-do-not-import/serialize.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/transformer.d.ts +0 -107
- package/dist/unstable-core-do-not-import/transformer.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/transformer.js +0 -107
- package/dist/unstable-core-do-not-import/transformer.mjs +0 -102
- package/dist/unstable-core-do-not-import/types.d.ts +0 -98
- package/dist/unstable-core-do-not-import/types.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/utils.d.ts +0 -27
- package/dist/unstable-core-do-not-import/utils.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/utils.js +0 -44
- package/dist/unstable-core-do-not-import/utils.mjs +0 -39
- package/src/@trpc/server/http.ts +0 -26
- package/src/@trpc/server/index.ts +0 -105
- package/src/@trpc/server/rpc.ts +0 -26
- package/src/observable/index.ts +0 -10
- package/src/observable/observable.ts +0 -158
- package/src/observable/operators.ts +0 -119
- package/src/observable/types.ts +0 -76
- package/src/unstable-core-do-not-import/TRPCInferrable.ts +0 -9
- package/src/unstable-core-do-not-import/createProxy.ts +0 -59
- package/src/unstable-core-do-not-import/error/TRPCError.ts +0 -82
- package/src/unstable-core-do-not-import/error/formatter.ts +0 -51
- package/src/unstable-core-do-not-import/error/getErrorShape.ts +0 -36
- package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +0 -29
- package/src/unstable-core-do-not-import/http/contentType.ts +0 -99
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +0 -57
- package/src/unstable-core-do-not-import/http/index.ts +0 -23
- package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +0 -458
- package/src/unstable-core-do-not-import/http/types.ts +0 -111
- package/src/unstable-core-do-not-import/index.ts +0 -110
- package/src/unstable-core-do-not-import/initTRPC.ts +0 -206
- package/src/unstable-core-do-not-import/middleware.ts +0 -233
- package/src/unstable-core-do-not-import/parser.ts +0 -94
- package/src/unstable-core-do-not-import/procedure.ts +0 -108
- package/src/unstable-core-do-not-import/procedureBuilder.ts +0 -458
- package/src/unstable-core-do-not-import/rootConfig.ts +0 -90
- package/src/unstable-core-do-not-import/router.ts +0 -370
- package/src/unstable-core-do-not-import/rpc/codes.ts +0 -44
- package/src/unstable-core-do-not-import/rpc/envelopes.ts +0 -136
- package/src/unstable-core-do-not-import/rpc/index.ts +0 -21
- package/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts +0 -85
- package/src/unstable-core-do-not-import/serialize.ts +0 -122
- package/src/unstable-core-do-not-import/transformer.ts +0 -202
- package/src/unstable-core-do-not-import/types.ts +0 -151
- package/src/unstable-core-do-not-import/utils.ts +0 -59
- package/unstable-core-do-not-import/index.d.ts +0 -1
- package/unstable-core-do-not-import/index.js +0 -1
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { getHTTPStatusCodeFromError } from '../http/getHTTPStatusCode';
|
|
2
|
-
import type { ProcedureType } from '../procedure';
|
|
3
|
-
import type { AnyRootConfig } from '../rootConfig';
|
|
4
|
-
import { TRPC_ERROR_CODES_BY_KEY } from '../rpc';
|
|
5
|
-
import type { DefaultErrorShape } from './formatter';
|
|
6
|
-
import type { TRPCError } from './TRPCError';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @internal
|
|
10
|
-
*/
|
|
11
|
-
export function getErrorShape<TConfig extends AnyRootConfig>(opts: {
|
|
12
|
-
config: TConfig;
|
|
13
|
-
error: TRPCError;
|
|
14
|
-
type: ProcedureType | 'unknown';
|
|
15
|
-
path: string | undefined;
|
|
16
|
-
input: unknown;
|
|
17
|
-
ctx: TConfig['$types']['ctx'] | undefined;
|
|
18
|
-
}): TConfig['$types']['errorShape'] {
|
|
19
|
-
const { path, error, config } = opts;
|
|
20
|
-
const { code } = opts.error;
|
|
21
|
-
const shape: DefaultErrorShape = {
|
|
22
|
-
message: error.message,
|
|
23
|
-
code: TRPC_ERROR_CODES_BY_KEY[code],
|
|
24
|
-
data: {
|
|
25
|
-
code,
|
|
26
|
-
httpStatus: getHTTPStatusCodeFromError(error),
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
if (config.isDev && typeof opts.error.stack === 'string') {
|
|
30
|
-
shape.data.stack = opts.error.stack;
|
|
31
|
-
}
|
|
32
|
-
if (typeof path === 'string') {
|
|
33
|
-
shape.data.path = path;
|
|
34
|
-
}
|
|
35
|
-
return config.errorFormatter({ ...opts, shape });
|
|
36
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Format a batch response as a line-delimited JSON stream
|
|
3
|
-
* that the `unstable_httpBatchStreamLink` can parse:
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```ts
|
|
7
|
-
* const formatter = getBatchStreamFormatter();
|
|
8
|
-
* res.send(formatter(1, 'response #2'));
|
|
9
|
-
* res.send(formatter(0, 'response #1'));
|
|
10
|
-
* res.send(formatter.end());
|
|
11
|
-
* ```
|
|
12
|
-
*
|
|
13
|
-
* Expected format:
|
|
14
|
-
* ```json
|
|
15
|
-
* {"1":"response #2"
|
|
16
|
-
* ,"0":"response #1"
|
|
17
|
-
* }
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export function getBatchStreamFormatter() {
|
|
21
|
-
let first = true;
|
|
22
|
-
function format(index: number, string: string) {
|
|
23
|
-
const prefix = first ? '{' : ',';
|
|
24
|
-
first = false;
|
|
25
|
-
return `${prefix}"${index}":${string}\n`;
|
|
26
|
-
}
|
|
27
|
-
format.end = () => '}';
|
|
28
|
-
return format;
|
|
29
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
-
import { TRPCError } from '../error/TRPCError';
|
|
3
|
-
import type { AnyRouter } from '../router';
|
|
4
|
-
import type { CombinedDataTransformer } from '../transformer';
|
|
5
|
-
import type { MaybePromise } from '../types';
|
|
6
|
-
import type { HTTPRequest } from './types';
|
|
7
|
-
|
|
8
|
-
type GetInputs = (opts: {
|
|
9
|
-
req: HTTPRequest;
|
|
10
|
-
isBatchCall: boolean;
|
|
11
|
-
router: AnyRouter;
|
|
12
|
-
preprocessedBody: boolean;
|
|
13
|
-
}) => MaybePromise<Record<number, unknown>>;
|
|
14
|
-
|
|
15
|
-
export type BodyResult =
|
|
16
|
-
| {
|
|
17
|
-
ok: true;
|
|
18
|
-
data: unknown;
|
|
19
|
-
/**
|
|
20
|
-
* If the HTTP handler has already parsed the body
|
|
21
|
-
*/
|
|
22
|
-
preprocessed: boolean;
|
|
23
|
-
}
|
|
24
|
-
| { ok: false; error: TRPCError };
|
|
25
|
-
|
|
26
|
-
export type BaseContentTypeHandler<TOptions> = {
|
|
27
|
-
isMatch(opts: TOptions): boolean;
|
|
28
|
-
getBody: (opts: TOptions) => MaybePromise<BodyResult>;
|
|
29
|
-
getInputs: GetInputs;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
function getRawProcedureInputOrThrow(opts: {
|
|
33
|
-
req: HTTPRequest;
|
|
34
|
-
preprocessedBody: boolean;
|
|
35
|
-
}) {
|
|
36
|
-
const { req } = opts;
|
|
37
|
-
try {
|
|
38
|
-
if (req.method === 'GET') {
|
|
39
|
-
if (!req.query.has('input')) {
|
|
40
|
-
return undefined;
|
|
41
|
-
}
|
|
42
|
-
const raw = req.query.get('input');
|
|
43
|
-
return JSON.parse(raw!);
|
|
44
|
-
}
|
|
45
|
-
if (!opts.preprocessedBody && typeof req.body === 'string') {
|
|
46
|
-
// A mutation with no inputs will have req.body === ''
|
|
47
|
-
return req.body.length === 0 ? undefined : JSON.parse(req.body);
|
|
48
|
-
}
|
|
49
|
-
return req.body;
|
|
50
|
-
} catch (cause) {
|
|
51
|
-
throw new TRPCError({
|
|
52
|
-
code: 'PARSE_ERROR',
|
|
53
|
-
cause,
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const deserializeInputValue = (
|
|
59
|
-
rawValue: unknown,
|
|
60
|
-
transformer: CombinedDataTransformer,
|
|
61
|
-
) => {
|
|
62
|
-
return typeof rawValue !== 'undefined'
|
|
63
|
-
? transformer.input.deserialize(rawValue)
|
|
64
|
-
: rawValue;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const getJsonContentTypeInputs: GetInputs = (opts) => {
|
|
68
|
-
const rawInput = getRawProcedureInputOrThrow(opts);
|
|
69
|
-
const transformer = opts.router._def._config.transformer;
|
|
70
|
-
|
|
71
|
-
if (!opts.isBatchCall) {
|
|
72
|
-
return {
|
|
73
|
-
0: deserializeInputValue(rawInput, transformer),
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/* istanbul ignore if */
|
|
78
|
-
if (
|
|
79
|
-
rawInput == null ||
|
|
80
|
-
typeof rawInput !== 'object' ||
|
|
81
|
-
Array.isArray(rawInput)
|
|
82
|
-
) {
|
|
83
|
-
throw new TRPCError({
|
|
84
|
-
code: 'BAD_REQUEST',
|
|
85
|
-
message: '"input" needs to be an object when doing a batch call',
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
const input: Record<number, unknown> = {};
|
|
89
|
-
for (const key in rawInput) {
|
|
90
|
-
const k = key as any as number;
|
|
91
|
-
const rawValue = rawInput[k];
|
|
92
|
-
|
|
93
|
-
const value = deserializeInputValue(rawValue, transformer);
|
|
94
|
-
|
|
95
|
-
input[k] = value;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return input;
|
|
99
|
-
};
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import type { TRPCError } from '../error/TRPCError';
|
|
2
|
-
import type { TRPC_ERROR_CODES_BY_KEY, TRPCResponse } from '../rpc';
|
|
3
|
-
import { TRPC_ERROR_CODES_BY_NUMBER } from '../rpc';
|
|
4
|
-
|
|
5
|
-
const JSONRPC2_TO_HTTP_CODE: Record<
|
|
6
|
-
keyof typeof TRPC_ERROR_CODES_BY_KEY,
|
|
7
|
-
number
|
|
8
|
-
> = {
|
|
9
|
-
PARSE_ERROR: 400,
|
|
10
|
-
BAD_REQUEST: 400,
|
|
11
|
-
UNAUTHORIZED: 401,
|
|
12
|
-
NOT_FOUND: 404,
|
|
13
|
-
FORBIDDEN: 403,
|
|
14
|
-
METHOD_NOT_SUPPORTED: 405,
|
|
15
|
-
TIMEOUT: 408,
|
|
16
|
-
CONFLICT: 409,
|
|
17
|
-
PRECONDITION_FAILED: 412,
|
|
18
|
-
PAYLOAD_TOO_LARGE: 413,
|
|
19
|
-
UNPROCESSABLE_CONTENT: 422,
|
|
20
|
-
TOO_MANY_REQUESTS: 429,
|
|
21
|
-
CLIENT_CLOSED_REQUEST: 499,
|
|
22
|
-
INTERNAL_SERVER_ERROR: 500,
|
|
23
|
-
NOT_IMPLEMENTED: 501,
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
function getStatusCodeFromKey(code: keyof typeof TRPC_ERROR_CODES_BY_KEY) {
|
|
27
|
-
return JSONRPC2_TO_HTTP_CODE[code] ?? 500;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function getHTTPStatusCode(json: TRPCResponse | TRPCResponse[]) {
|
|
31
|
-
const arr = Array.isArray(json) ? json : [json];
|
|
32
|
-
const httpStatuses = new Set(
|
|
33
|
-
arr.map((res) => {
|
|
34
|
-
if ('error' in res) {
|
|
35
|
-
const data = res.error.data;
|
|
36
|
-
if (typeof data['httpStatus'] === 'number') {
|
|
37
|
-
return data['httpStatus'];
|
|
38
|
-
}
|
|
39
|
-
const code = TRPC_ERROR_CODES_BY_NUMBER[res.error.code];
|
|
40
|
-
return getStatusCodeFromKey(code);
|
|
41
|
-
}
|
|
42
|
-
return 200;
|
|
43
|
-
}),
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
if (httpStatuses.size !== 1) {
|
|
47
|
-
return 207;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const httpStatus = httpStatuses.values().next().value;
|
|
51
|
-
|
|
52
|
-
return httpStatus;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function getHTTPStatusCodeFromError(error: TRPCError) {
|
|
56
|
-
return getStatusCodeFromKey(error.code);
|
|
57
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
getHTTPStatusCode,
|
|
3
|
-
getHTTPStatusCodeFromError,
|
|
4
|
-
} from './getHTTPStatusCode';
|
|
5
|
-
export { resolveHTTPResponse } from './resolveHTTPResponse';
|
|
6
|
-
export type {
|
|
7
|
-
BaseHandlerOptions,
|
|
8
|
-
HTTPBaseHandlerOptions,
|
|
9
|
-
HTTPHeaders,
|
|
10
|
-
HTTPRequest,
|
|
11
|
-
HTTPResponse,
|
|
12
|
-
OnErrorFunction,
|
|
13
|
-
ProcedureCall,
|
|
14
|
-
ResolveHTTPRequestOptionsContextFn,
|
|
15
|
-
ResponseChunk,
|
|
16
|
-
ResponseMeta,
|
|
17
|
-
ResponseMetaFn,
|
|
18
|
-
TRPCRequestInfo,
|
|
19
|
-
} from './types';
|
|
20
|
-
|
|
21
|
-
export { getBatchStreamFormatter } from './batchStreamFormatter';
|
|
22
|
-
export type { BaseContentTypeHandler, BodyResult } from './contentType';
|
|
23
|
-
export { getJsonContentTypeInputs } from './contentType';
|
|
@@ -1,458 +0,0 @@
|
|
|
1
|
-
import { getErrorShape } from '../error/getErrorShape';
|
|
2
|
-
import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError';
|
|
3
|
-
import type { ProcedureType } from '../procedure';
|
|
4
|
-
import type {
|
|
5
|
-
AnyRouter,
|
|
6
|
-
inferRouterContext,
|
|
7
|
-
inferRouterError,
|
|
8
|
-
} from '../router';
|
|
9
|
-
import { callProcedure } from '../router';
|
|
10
|
-
import type { TRPCResponse } from '../rpc';
|
|
11
|
-
import { transformTRPCResponse } from '../transformer';
|
|
12
|
-
import type { Maybe } from '../types';
|
|
13
|
-
import type { BaseContentTypeHandler } from './contentType';
|
|
14
|
-
import { getJsonContentTypeInputs } from './contentType';
|
|
15
|
-
import { getHTTPStatusCode } from './getHTTPStatusCode';
|
|
16
|
-
import type {
|
|
17
|
-
HTTPBaseHandlerOptions,
|
|
18
|
-
HTTPHeaders,
|
|
19
|
-
HTTPRequest,
|
|
20
|
-
HTTPResponse,
|
|
21
|
-
ResolveHTTPRequestOptionsContextFn,
|
|
22
|
-
ResponseChunk,
|
|
23
|
-
TRPCRequestInfo,
|
|
24
|
-
} from './types';
|
|
25
|
-
|
|
26
|
-
const HTTP_METHOD_PROCEDURE_TYPE_MAP: Record<
|
|
27
|
-
string,
|
|
28
|
-
ProcedureType | undefined
|
|
29
|
-
> = {
|
|
30
|
-
GET: 'query',
|
|
31
|
-
POST: 'mutation',
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const fallbackContentTypeHandler = {
|
|
35
|
-
getInputs: getJsonContentTypeInputs,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
type PartialBy<TBaseType, TKey extends keyof TBaseType> = Omit<
|
|
39
|
-
TBaseType,
|
|
40
|
-
TKey
|
|
41
|
-
> &
|
|
42
|
-
Partial<Pick<TBaseType, TKey>>;
|
|
43
|
-
|
|
44
|
-
interface ResolveHTTPRequestOptions<
|
|
45
|
-
TRouter extends AnyRouter,
|
|
46
|
-
TRequest extends HTTPRequest,
|
|
47
|
-
> extends HTTPBaseHandlerOptions<TRouter, TRequest> {
|
|
48
|
-
createContext: ResolveHTTPRequestOptionsContextFn<TRouter>;
|
|
49
|
-
req: TRequest;
|
|
50
|
-
path: string;
|
|
51
|
-
error?: Maybe<TRPCError>;
|
|
52
|
-
contentTypeHandler?: BaseContentTypeHandler<any>;
|
|
53
|
-
preprocessedBody?: boolean;
|
|
54
|
-
/**
|
|
55
|
-
* Called as soon as the response head is known.
|
|
56
|
-
* When streaming, headers will have been generated
|
|
57
|
-
* **without** knowing the response body.
|
|
58
|
-
*
|
|
59
|
-
* Without this callback, streaming is disabled.
|
|
60
|
-
*/
|
|
61
|
-
unstable_onHead: (
|
|
62
|
-
headResponse: Omit<HTTPResponse, 'body'>,
|
|
63
|
-
isStreaming: boolean,
|
|
64
|
-
) => void;
|
|
65
|
-
/**
|
|
66
|
-
* Called for every procedure with `[index, result]`.
|
|
67
|
-
*
|
|
68
|
-
* Will be called a single time with `index = -1` if
|
|
69
|
-
* - response is an error
|
|
70
|
-
* - response is empty (HEAD request)
|
|
71
|
-
*
|
|
72
|
-
* Without this callback, streaming is disabled.
|
|
73
|
-
*/
|
|
74
|
-
unstable_onChunk: (chunk: ResponseChunk) => void;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function initResponse<
|
|
78
|
-
TRouter extends AnyRouter,
|
|
79
|
-
TRequest extends HTTPRequest,
|
|
80
|
-
>(initOpts: {
|
|
81
|
-
ctx: inferRouterContext<TRouter> | undefined;
|
|
82
|
-
paths: string[] | undefined;
|
|
83
|
-
type: ProcedureType | 'unknown';
|
|
84
|
-
responseMeta?: HTTPBaseHandlerOptions<TRouter, TRequest>['responseMeta'];
|
|
85
|
-
untransformedJSON?:
|
|
86
|
-
| TRPCResponse<unknown, inferRouterError<TRouter>>
|
|
87
|
-
| TRPCResponse<unknown, inferRouterError<TRouter>>[]
|
|
88
|
-
| undefined;
|
|
89
|
-
errors?: TRPCError[];
|
|
90
|
-
}): HTTPResponse {
|
|
91
|
-
const {
|
|
92
|
-
ctx,
|
|
93
|
-
paths,
|
|
94
|
-
type,
|
|
95
|
-
responseMeta,
|
|
96
|
-
untransformedJSON,
|
|
97
|
-
errors = [],
|
|
98
|
-
} = initOpts;
|
|
99
|
-
|
|
100
|
-
let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
|
|
101
|
-
const headers: HTTPHeaders = {
|
|
102
|
-
'Content-Type': 'application/json',
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const eagerGeneration = !untransformedJSON;
|
|
106
|
-
const data = eagerGeneration
|
|
107
|
-
? []
|
|
108
|
-
: Array.isArray(untransformedJSON)
|
|
109
|
-
? untransformedJSON
|
|
110
|
-
: [untransformedJSON];
|
|
111
|
-
|
|
112
|
-
const meta =
|
|
113
|
-
responseMeta?.({
|
|
114
|
-
ctx,
|
|
115
|
-
paths,
|
|
116
|
-
type,
|
|
117
|
-
data,
|
|
118
|
-
errors,
|
|
119
|
-
eagerGeneration,
|
|
120
|
-
}) ?? {};
|
|
121
|
-
|
|
122
|
-
for (const [key, value] of Object.entries(meta.headers ?? {})) {
|
|
123
|
-
headers[key] = value;
|
|
124
|
-
}
|
|
125
|
-
if (meta.status) {
|
|
126
|
-
status = meta.status;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
status,
|
|
131
|
-
headers,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async function inputToProcedureCall<
|
|
136
|
-
TRouter extends AnyRouter,
|
|
137
|
-
TRequest extends HTTPRequest,
|
|
138
|
-
>(procedureOpts: {
|
|
139
|
-
opts: Pick<
|
|
140
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
141
|
-
'onError' | 'req' | 'router'
|
|
142
|
-
>;
|
|
143
|
-
ctx: inferRouterContext<TRouter> | undefined;
|
|
144
|
-
type: 'mutation' | 'query';
|
|
145
|
-
input: unknown;
|
|
146
|
-
path: string;
|
|
147
|
-
}): Promise<TRPCResponse<unknown, inferRouterError<TRouter>>> {
|
|
148
|
-
const { opts, ctx, type, input, path } = procedureOpts;
|
|
149
|
-
try {
|
|
150
|
-
const data = await callProcedure({
|
|
151
|
-
procedures: opts.router._def.procedures,
|
|
152
|
-
path,
|
|
153
|
-
getRawInput: async () => input,
|
|
154
|
-
ctx,
|
|
155
|
-
type,
|
|
156
|
-
});
|
|
157
|
-
return {
|
|
158
|
-
result: {
|
|
159
|
-
data,
|
|
160
|
-
},
|
|
161
|
-
};
|
|
162
|
-
} catch (cause) {
|
|
163
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
164
|
-
|
|
165
|
-
opts.onError?.({ error, path, input, ctx, type: type, req: opts.req });
|
|
166
|
-
|
|
167
|
-
return {
|
|
168
|
-
error: getErrorShape({
|
|
169
|
-
config: opts.router._def._config,
|
|
170
|
-
error,
|
|
171
|
-
type,
|
|
172
|
-
path,
|
|
173
|
-
input,
|
|
174
|
-
ctx,
|
|
175
|
-
}),
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function caughtErrorToData<
|
|
181
|
-
TRouter extends AnyRouter,
|
|
182
|
-
TRequest extends HTTPRequest,
|
|
183
|
-
>(
|
|
184
|
-
cause: unknown,
|
|
185
|
-
errorOpts: {
|
|
186
|
-
opts: Pick<
|
|
187
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
188
|
-
'onError' | 'req' | 'router'
|
|
189
|
-
>;
|
|
190
|
-
ctx: inferRouterContext<TRouter> | undefined;
|
|
191
|
-
type: ProcedureType | 'unknown';
|
|
192
|
-
path?: string;
|
|
193
|
-
input?: unknown;
|
|
194
|
-
},
|
|
195
|
-
) {
|
|
196
|
-
const { router, req, onError } = errorOpts.opts;
|
|
197
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
198
|
-
onError?.({
|
|
199
|
-
error,
|
|
200
|
-
path: errorOpts.path,
|
|
201
|
-
input: errorOpts.input,
|
|
202
|
-
ctx: errorOpts.ctx,
|
|
203
|
-
type: errorOpts.type,
|
|
204
|
-
req,
|
|
205
|
-
});
|
|
206
|
-
const untransformedJSON = {
|
|
207
|
-
error: getErrorShape({
|
|
208
|
-
config: router._def._config,
|
|
209
|
-
error,
|
|
210
|
-
type: errorOpts.type,
|
|
211
|
-
path: errorOpts.path,
|
|
212
|
-
input: errorOpts.input,
|
|
213
|
-
ctx: errorOpts.ctx,
|
|
214
|
-
}),
|
|
215
|
-
};
|
|
216
|
-
const transformedJSON = transformTRPCResponse(
|
|
217
|
-
router._def._config,
|
|
218
|
-
untransformedJSON,
|
|
219
|
-
);
|
|
220
|
-
const body = JSON.stringify(transformedJSON);
|
|
221
|
-
return {
|
|
222
|
-
error,
|
|
223
|
-
untransformedJSON,
|
|
224
|
-
body,
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Since `resolveHTTPResponse` is a public API (community adapters),
|
|
230
|
-
* let's give it a strong type signature to increase discoverability.
|
|
231
|
-
*/
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Non-streaming signature for `resolveHTTPResponse`:
|
|
235
|
-
* @param opts.unstable_onHead `undefined`
|
|
236
|
-
* @param opts.unstable_onChunk `undefined`
|
|
237
|
-
* @returns `Promise<HTTPResponse>`
|
|
238
|
-
*/
|
|
239
|
-
export async function resolveHTTPResponse<
|
|
240
|
-
TRouter extends AnyRouter,
|
|
241
|
-
TRequest extends HTTPRequest,
|
|
242
|
-
>(
|
|
243
|
-
opts: Omit<
|
|
244
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
245
|
-
'unstable_onChunk' | 'unstable_onHead'
|
|
246
|
-
>,
|
|
247
|
-
): Promise<HTTPResponse>;
|
|
248
|
-
/**
|
|
249
|
-
* Streaming signature for `resolveHTTPResponse`:
|
|
250
|
-
* @param opts.unstable_onHead called as soon as the response head is known
|
|
251
|
-
* @param opts.unstable_onChunk called for every procedure with `[index, result]`
|
|
252
|
-
* @returns `Promise<void>` since the response is streamed
|
|
253
|
-
*/
|
|
254
|
-
export async function resolveHTTPResponse<
|
|
255
|
-
TRouter extends AnyRouter,
|
|
256
|
-
TRequest extends HTTPRequest,
|
|
257
|
-
>(opts: ResolveHTTPRequestOptions<TRouter, TRequest>): Promise<void>;
|
|
258
|
-
// implementation
|
|
259
|
-
export async function resolveHTTPResponse<
|
|
260
|
-
TRouter extends AnyRouter,
|
|
261
|
-
TRequest extends HTTPRequest,
|
|
262
|
-
>(
|
|
263
|
-
opts: PartialBy<
|
|
264
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
265
|
-
'unstable_onChunk' | 'unstable_onHead'
|
|
266
|
-
>,
|
|
267
|
-
): Promise<HTTPResponse | void> {
|
|
268
|
-
const { router, req, unstable_onHead, unstable_onChunk } = opts;
|
|
269
|
-
|
|
270
|
-
if (req.method === 'HEAD') {
|
|
271
|
-
// can be used for lambda warmup
|
|
272
|
-
const headResponse: HTTPResponse = {
|
|
273
|
-
status: 204,
|
|
274
|
-
};
|
|
275
|
-
unstable_onHead?.(headResponse, false);
|
|
276
|
-
unstable_onChunk?.([-1, '']);
|
|
277
|
-
return headResponse;
|
|
278
|
-
}
|
|
279
|
-
const contentTypeHandler =
|
|
280
|
-
opts.contentTypeHandler ?? fallbackContentTypeHandler;
|
|
281
|
-
const batchingEnabled = opts.batching?.enabled ?? true;
|
|
282
|
-
const type =
|
|
283
|
-
HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? ('unknown' as const);
|
|
284
|
-
let ctx: inferRouterContext<TRouter> | undefined = undefined;
|
|
285
|
-
let paths: string[] | undefined;
|
|
286
|
-
|
|
287
|
-
const isBatchCall = !!req.query.get('batch');
|
|
288
|
-
const isStreamCall =
|
|
289
|
-
isBatchCall &&
|
|
290
|
-
unstable_onHead &&
|
|
291
|
-
unstable_onChunk &&
|
|
292
|
-
req.headers['trpc-batch-mode'] === 'stream';
|
|
293
|
-
|
|
294
|
-
try {
|
|
295
|
-
if (opts.error) {
|
|
296
|
-
throw opts.error;
|
|
297
|
-
}
|
|
298
|
-
if (isBatchCall && !batchingEnabled) {
|
|
299
|
-
throw new Error(`Batching is not enabled on the server`);
|
|
300
|
-
}
|
|
301
|
-
/* istanbul ignore if -- @preserve */
|
|
302
|
-
if (type === 'subscription') {
|
|
303
|
-
throw new TRPCError({
|
|
304
|
-
message: 'Subscriptions should use wsLink',
|
|
305
|
-
code: 'METHOD_NOT_SUPPORTED',
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
if (type === 'unknown') {
|
|
309
|
-
throw new TRPCError({
|
|
310
|
-
message: `Unexpected request method ${req.method}`,
|
|
311
|
-
code: 'METHOD_NOT_SUPPORTED',
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
const inputs = await contentTypeHandler.getInputs({
|
|
316
|
-
isBatchCall,
|
|
317
|
-
req,
|
|
318
|
-
router,
|
|
319
|
-
preprocessedBody: opts.preprocessedBody ?? false,
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
paths = isBatchCall
|
|
323
|
-
? decodeURIComponent(opts.path).split(',')
|
|
324
|
-
: [opts.path];
|
|
325
|
-
const info: TRPCRequestInfo = {
|
|
326
|
-
isBatchCall,
|
|
327
|
-
calls: paths.map((path, idx) => ({
|
|
328
|
-
path,
|
|
329
|
-
type,
|
|
330
|
-
input: inputs[idx] ?? undefined,
|
|
331
|
-
})),
|
|
332
|
-
};
|
|
333
|
-
ctx = await opts.createContext({ info });
|
|
334
|
-
const promises = paths.map((path, index) =>
|
|
335
|
-
inputToProcedureCall({ opts, ctx, type, input: inputs[index], path }),
|
|
336
|
-
);
|
|
337
|
-
|
|
338
|
-
if (!isStreamCall) {
|
|
339
|
-
/**
|
|
340
|
-
* Non-streaming response:
|
|
341
|
-
* - await all responses in parallel, blocking on the slowest one
|
|
342
|
-
* - create headers with known response body
|
|
343
|
-
* - return a complete HTTPResponse
|
|
344
|
-
*/
|
|
345
|
-
|
|
346
|
-
const untransformedJSON = await Promise.all(promises);
|
|
347
|
-
const errors = untransformedJSON.flatMap((response) =>
|
|
348
|
-
'error' in response ? [response.error] : [],
|
|
349
|
-
);
|
|
350
|
-
|
|
351
|
-
const headResponse = initResponse({
|
|
352
|
-
ctx,
|
|
353
|
-
paths,
|
|
354
|
-
type,
|
|
355
|
-
responseMeta: opts.responseMeta,
|
|
356
|
-
untransformedJSON,
|
|
357
|
-
errors,
|
|
358
|
-
});
|
|
359
|
-
unstable_onHead?.(headResponse, false);
|
|
360
|
-
|
|
361
|
-
// return body stuff
|
|
362
|
-
const result = isBatchCall ? untransformedJSON : untransformedJSON[0]!; // eslint-disable-line @typescript-eslint/no-non-null-assertion -- `untransformedJSON` should be the length of `paths` which should be at least 1 otherwise there wouldn't be a request at all
|
|
363
|
-
const transformedJSON = transformTRPCResponse(
|
|
364
|
-
router._def._config,
|
|
365
|
-
result,
|
|
366
|
-
);
|
|
367
|
-
const body = JSON.stringify(transformedJSON);
|
|
368
|
-
unstable_onChunk?.([-1, body]);
|
|
369
|
-
|
|
370
|
-
return {
|
|
371
|
-
status: headResponse.status,
|
|
372
|
-
headers: headResponse.headers,
|
|
373
|
-
body,
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Streaming response:
|
|
379
|
-
* - block on none, call `onChunk` as soon as each response is ready
|
|
380
|
-
* - create headers with minimal data (cannot know the response body in advance)
|
|
381
|
-
* - return void
|
|
382
|
-
*/
|
|
383
|
-
const headResponse = initResponse({
|
|
384
|
-
ctx,
|
|
385
|
-
paths,
|
|
386
|
-
type,
|
|
387
|
-
responseMeta: opts.responseMeta,
|
|
388
|
-
});
|
|
389
|
-
unstable_onHead(headResponse, true);
|
|
390
|
-
|
|
391
|
-
const indexedPromises = new Map(
|
|
392
|
-
promises.map((promise, index) => [
|
|
393
|
-
index,
|
|
394
|
-
promise.then((r) => [index, r] as const),
|
|
395
|
-
]),
|
|
396
|
-
);
|
|
397
|
-
for (const _ of paths) {
|
|
398
|
-
const [index, untransformedJSON] = await Promise.race(
|
|
399
|
-
indexedPromises.values(),
|
|
400
|
-
);
|
|
401
|
-
indexedPromises.delete(index);
|
|
402
|
-
|
|
403
|
-
try {
|
|
404
|
-
const transformedJSON = transformTRPCResponse(
|
|
405
|
-
router._def._config,
|
|
406
|
-
untransformedJSON,
|
|
407
|
-
);
|
|
408
|
-
const body = JSON.stringify(transformedJSON);
|
|
409
|
-
|
|
410
|
-
unstable_onChunk([index, body]);
|
|
411
|
-
} catch (cause) {
|
|
412
|
-
const path = paths[index];
|
|
413
|
-
const input = inputs[index];
|
|
414
|
-
const { body } = caughtErrorToData(cause, {
|
|
415
|
-
opts,
|
|
416
|
-
ctx,
|
|
417
|
-
type,
|
|
418
|
-
path,
|
|
419
|
-
input,
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
unstable_onChunk([index, body]);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
return;
|
|
426
|
-
} catch (cause) {
|
|
427
|
-
// we get here if
|
|
428
|
-
// - batching is called when it's not enabled
|
|
429
|
-
// - `createContext()` throws
|
|
430
|
-
// - `router._def._config.transformer.output.serialize()` throws
|
|
431
|
-
// - post body is too large
|
|
432
|
-
// - input deserialization fails
|
|
433
|
-
// - `errorFormatter` return value is malformed
|
|
434
|
-
const { error, untransformedJSON, body } = caughtErrorToData(cause, {
|
|
435
|
-
opts,
|
|
436
|
-
ctx,
|
|
437
|
-
type,
|
|
438
|
-
});
|
|
439
|
-
|
|
440
|
-
const headResponse = initResponse({
|
|
441
|
-
ctx,
|
|
442
|
-
paths,
|
|
443
|
-
type,
|
|
444
|
-
responseMeta: opts.responseMeta,
|
|
445
|
-
untransformedJSON,
|
|
446
|
-
errors: [error],
|
|
447
|
-
});
|
|
448
|
-
unstable_onHead?.(headResponse, false);
|
|
449
|
-
|
|
450
|
-
unstable_onChunk?.([-1, body]);
|
|
451
|
-
|
|
452
|
-
return {
|
|
453
|
-
status: headResponse.status,
|
|
454
|
-
headers: headResponse.headers,
|
|
455
|
-
body,
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
}
|