@trpc/server 11.0.0-alpha-tmp-export-from-main.211 → 11.0.0-alpha-tmp-export-from-main-nuke-core.229
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/TRPCError-54f51dec.mjs +63 -0
- package/dist/TRPCError-6f02e18d.js +67 -0
- package/dist/TRPCError-792b0901.js +61 -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 +10 -7
- package/dist/adapters/aws-lambda/index.mjs +6 -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/express.d.ts +1 -2
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +8 -2
- package/dist/adapters/express.mjs +8 -2
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +2 -2
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -2
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +11 -4
- package/dist/adapters/fastify/index.mjs +9 -2
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -2
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/index.js +9 -4
- package/dist/adapters/fetch/index.mjs +7 -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 +1 -2
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +10 -5
- package/dist/adapters/next.mjs +8 -3
- 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/index.js +5 -3
- package/dist/adapters/node-http/content-type/json/index.mjs +3 -1
- package/dist/adapters/node-http/index.js +8 -2
- package/dist/adapters/node-http/index.mjs +8 -2
- 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/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 +9 -2
- package/dist/adapters/standalone.mjs +9 -2
- package/dist/adapters/ws.d.ts +3 -2
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +31 -26
- package/dist/adapters/ws.mjs +6 -1
- package/dist/batchStreamFormatter-1971db27.js +32 -0
- package/dist/batchStreamFormatter-378277dc.mjs +30 -0
- package/dist/batchStreamFormatter-4375dc8b.js +31 -0
- package/dist/bundle-analysis.json +757 -0
- package/dist/contentType-19471015.js +59 -0
- package/dist/contentType-2e62862d.mjs +52 -0
- package/dist/contentType-4cd0be82.js +54 -0
- package/dist/createProxy-562377d3.js +127 -0
- package/dist/createProxy-72161f42.js +116 -0
- package/dist/createProxy-f7ba8b4d.mjs +121 -0
- package/dist/http.d.ts +1 -6
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +12 -21
- package/dist/http.mjs +7 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +15 -41
- package/dist/index.mjs +5 -1
- package/dist/initTRPC-12d9099b.js +371 -0
- package/dist/initTRPC-69a60dfb.js +384 -0
- package/dist/initTRPC-dcdfb896.mjs +376 -0
- package/dist/{nodeHTTPRequestHandler-55f05150.js → nodeHTTPRequestHandler-71a3f758.js} +5 -4
- package/dist/{nodeHTTPRequestHandler-2d5c8791.mjs → nodeHTTPRequestHandler-c5d1d163.mjs} +3 -2
- package/dist/{nodeHTTPRequestHandler-7691fc79.js → nodeHTTPRequestHandler-f60f0b99.js} +3 -1
- package/dist/observable/index.d.ts +5 -0
- package/dist/observable/index.d.ts.map +1 -0
- package/dist/observable/index.js +110 -0
- package/dist/observable/index.mjs +101 -0
- package/dist/observable/observable.d.ts +13 -0
- package/dist/observable/observable.d.ts.map +1 -0
- package/dist/observable/operators.d.ts +8 -0
- package/dist/observable/operators.d.ts.map +1 -0
- package/dist/observable/types.d.ts +26 -0
- package/dist/observable/types.d.ts.map +1 -0
- package/dist/observable-25f4098f.js +126 -0
- package/dist/observable-bf75028f.mjs +122 -0
- package/dist/observable-e837fe43.js +126 -0
- package/dist/parseTRPCMessage-aeb00dbd.js +67 -0
- package/dist/parseTRPCMessage-ddbcc719.mjs +62 -0
- package/dist/parseTRPCMessage-ee50b281.js +65 -0
- package/dist/resolveHTTPResponse-104460bf.js +293 -0
- package/dist/resolveHTTPResponse-6a85f0a7.js +265 -0
- package/dist/resolveHTTPResponse-eb29e5cf.mjs +291 -0
- package/dist/rootConfig-52aed7d7.js +88 -0
- package/dist/rootConfig-7d3e1fc8.mjs +76 -0
- package/dist/rootConfig-ccd626d6.js +83 -0
- package/dist/router-5a3e1181.mjs +252 -0
- package/dist/router-5e93cd14.js +259 -0
- package/dist/router-d9e22315.js +262 -0
- package/dist/rpc.d.ts +1 -2
- package/dist/rpc.d.ts.map +1 -1
- package/dist/rpc.js +5 -13
- package/dist/rpc.mjs +2 -1
- package/dist/shared.d.ts +1 -1
- package/dist/shared.js +4 -9
- package/dist/shared.mjs +2 -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/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/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/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/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/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/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/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/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 +43 -0
- package/dist/unstable-core-do-not-import/index.mjs +9 -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/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/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/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/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/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/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/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/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/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/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/package.json +53 -125
- 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 +2 -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 +14 -13
- package/src/http.ts +1 -26
- 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 -26
- 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 +233 -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 -3
- package/dist/observable.d.ts.map +0 -1
- package/dist/observable.js +0 -32
- 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 -16
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ProcedureType } from '../procedure';
|
|
2
|
+
import type {
|
|
3
|
+
TRPC_ERROR_CODE_KEY,
|
|
4
|
+
TRPC_ERROR_CODE_NUMBER,
|
|
5
|
+
TRPCErrorShape,
|
|
6
|
+
} from '../rpc';
|
|
7
|
+
import type { TRPCError } from './TRPCError';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export type ErrorFormatter<
|
|
13
|
+
TContext,
|
|
14
|
+
TShape extends TRPCErrorShape<number>,
|
|
15
|
+
> = (args: {
|
|
16
|
+
error: TRPCError;
|
|
17
|
+
type: ProcedureType | 'unknown';
|
|
18
|
+
path: string | undefined;
|
|
19
|
+
input: unknown;
|
|
20
|
+
ctx: TContext | undefined;
|
|
21
|
+
shape: DefaultErrorShape;
|
|
22
|
+
}) => TShape;
|
|
23
|
+
|
|
24
|
+
export type ErrorFormatterShape<TType> = TType extends ErrorFormatter<
|
|
25
|
+
any,
|
|
26
|
+
infer TShape
|
|
27
|
+
>
|
|
28
|
+
? TShape
|
|
29
|
+
: DefaultErrorShape;
|
|
30
|
+
/**
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export type DefaultErrorData = {
|
|
34
|
+
code: TRPC_ERROR_CODE_KEY;
|
|
35
|
+
httpStatus: number;
|
|
36
|
+
path?: string;
|
|
37
|
+
stack?: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export interface DefaultErrorShape
|
|
44
|
+
extends TRPCErrorShape<TRPC_ERROR_CODE_NUMBER, DefaultErrorData> {
|
|
45
|
+
message: string;
|
|
46
|
+
code: TRPC_ERROR_CODE_NUMBER;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const defaultFormatter: ErrorFormatter<any, any> = ({ shape }) => {
|
|
50
|
+
return shape;
|
|
51
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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';
|