@trpc/server 10.28.0 → 10.28.2
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/{TRPCError-61026c9a.js → TRPCError-7c3909ea.js} +47 -47
- package/dist/adapters/aws-lambda/index.d.ts +9 -9
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/utils.d.ts +37 -37
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/express.d.ts +5 -5
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +12 -12
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +11 -11
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/index.d.ts +2 -2
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +7 -7
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/index.d.ts +2 -2
- package/dist/adapters/fetch/types.d.ts +19 -19
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/lambda/index.d.ts +11 -11
- package/dist/adapters/lambda/index.d.ts.map +1 -1
- package/dist/adapters/next.d.ts +5 -5
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +69 -69
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +23 -23
- package/dist/adapters/node-http/content-type/form-data/index.js +1 -3
- package/dist/adapters/node-http/content-type/form-data/index.mjs +1 -3
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +30 -30
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +10 -10
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +40 -39
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +6 -6
- package/dist/adapters/node-http/content-type/json/index.d.ts +1 -1
- package/dist/adapters/node-http/index.d.ts +2 -2
- package/dist/adapters/node-http/internals/contentType.d.ts +8 -8
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +3 -3
- package/dist/adapters/node-http/types.d.ts +63 -63
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts +13 -13
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/ws.d.ts +21 -21
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/{codes-b3c262d8.js → codes-1c9ad84e.js} +44 -44
- package/dist/{config-1a5a494d.js → config-54786004.js} +168 -168
- package/dist/{contentType-f27e4bfc.js → contentType-24c44bba.js} +2 -2
- package/dist/{contentType-a75c990c.js → contentType-7354963c.js} +54 -54
- package/dist/core/index.d.ts +8 -8
- package/dist/core/initTRPC.d.ts +104 -104
- package/dist/core/initTRPC.d.ts.map +1 -1
- package/dist/core/internals/config.d.ts +74 -74
- package/dist/core/internals/config.d.ts.map +1 -1
- package/dist/core/internals/getParseFn.d.ts +8 -8
- package/dist/core/internals/getParseFn.d.ts.map +1 -1
- package/dist/core/internals/mergeRouters.d.ts +17 -17
- package/dist/core/internals/mergeRouters.d.ts.map +1 -1
- package/dist/core/internals/mergeWithoutOverrides.d.ts +4 -4
- package/dist/core/internals/omitPrototype.d.ts +5 -5
- package/dist/core/internals/procedureBuilder.d.ts +115 -115
- package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
- package/dist/core/internals/utils.d.ts +51 -51
- package/dist/core/internals/utils.d.ts.map +1 -1
- package/dist/core/middleware.d.ts +127 -127
- package/dist/core/middleware.d.ts.map +1 -1
- package/dist/core/parser.d.ts +27 -27
- package/dist/core/parser.d.ts.map +1 -1
- package/dist/core/procedure.d.ts +72 -72
- package/dist/core/procedure.d.ts.map +1 -1
- package/dist/core/router.d.ts +111 -111
- package/dist/core/router.d.ts.map +1 -1
- package/dist/core/types.d.ts +28 -28
- package/dist/core/types.d.ts.map +1 -1
- package/dist/deprecated/internals/middlewares.d.ts +51 -51
- package/dist/deprecated/internals/middlewares.d.ts.map +1 -1
- package/dist/deprecated/internals/procedure.d.ts +95 -95
- package/dist/deprecated/internals/procedure.d.ts.map +1 -1
- package/dist/deprecated/interop.d.ts +43 -43
- package/dist/deprecated/interop.d.ts.map +1 -1
- package/dist/deprecated/router.d.ts +203 -203
- package/dist/deprecated/router.d.ts.map +1 -1
- package/dist/error/TRPCError.d.ts +11 -11
- package/dist/error/formatter.d.ts +32 -32
- package/dist/error/formatter.d.ts.map +1 -1
- package/dist/error/utils.d.ts +5 -5
- package/dist/http/contentType.d.ts +28 -28
- package/dist/http/contentType.d.ts.map +1 -1
- package/dist/http/getHTTPStatusCode.d.ts +23 -23
- package/dist/http/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/http/index.d.ts +3 -3
- package/dist/http/internals/types.d.ts +24 -24
- package/dist/http/internals/types.d.ts.map +1 -1
- package/dist/http/resolveHTTPResponse.d.ts +16 -16
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/http/types.d.ts +24 -24
- package/dist/{index-6d5795e9.js → index-ca9a128e.js} +88 -88
- package/dist/index.d.ts +6 -6
- package/dist/internals/invert.d.ts +11 -11
- package/dist/internals/invert.d.ts.map +1 -1
- package/dist/internals/types.d.ts +25 -25
- package/dist/internals/types.d.ts.map +1 -1
- package/dist/internals.d.ts +9 -9
- package/dist/{nodeHTTPRequestHandler-e424955c.js → nodeHTTPRequestHandler-bd47641d.js} +62 -62
- package/dist/observable/index.d.ts +4 -4
- package/dist/observable/internals/identity.d.ts +1 -1
- package/dist/observable/internals/observableToPromise.d.ts +9 -9
- package/dist/observable/internals/pipe.d.ts +3 -3
- package/dist/observable/observable.d.ts +4 -4
- package/dist/observable/observable.d.ts.map +1 -1
- package/dist/observable/operators/index.d.ts +3 -3
- package/dist/observable/operators/map.d.ts +2 -2
- package/dist/observable/operators/share.d.ts +5 -5
- package/dist/observable/operators/tap.d.ts +2 -2
- package/dist/observable/types.d.ts +28 -28
- package/dist/observable/types.d.ts.map +1 -1
- package/dist/{observable-a1c2103f.js → observable-15588f28.js} +76 -76
- package/dist/{resolveHTTPResponse-4e05c4f8.js → resolveHTTPResponse-ba557d3e.js} +162 -162
- package/dist/rpc/codes.d.ts +52 -52
- package/dist/rpc/codes.d.ts.map +1 -1
- package/dist/rpc/envelopes.d.ts +98 -98
- package/dist/rpc/envelopes.d.ts.map +1 -1
- package/dist/rpc/index.d.ts +2 -2
- package/dist/shared/createProxy/index.d.ts +18 -18
- package/dist/shared/createProxy/index.d.ts.map +1 -1
- package/dist/shared/getErrorShape.d.ts +14 -14
- package/dist/shared/index.d.ts +5 -5
- package/dist/shared/internal/serialize.d.ts +29 -29
- package/dist/shared/internal/serialize.d.ts.map +1 -1
- package/dist/shared/jsonify.d.ts +9 -9
- package/dist/shared/jsonify.d.ts.map +1 -1
- package/dist/shared/transformTRPCResponse.d.ts +8 -8
- package/dist/shared/transformTRPCResponse.d.ts.map +1 -1
- package/dist/subscription.d.ts +12 -12
- package/dist/subscription.d.ts.map +1 -1
- package/dist/{transformTRPCResponse-64a4fd96.js → transformTRPCResponse-7fcbe472.js} +48 -48
- package/dist/transformer.d.ts +73 -73
- package/dist/transformer.d.ts.map +1 -1
- package/dist/types.d.ts +77 -77
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/adapters/aws-lambda/index.ts +2 -2
- package/src/adapters/aws-lambda/utils.ts +1 -1
- package/src/adapters/express.ts +1 -1
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
- package/src/adapters/lambda/index.ts +1 -1
- package/src/adapters/next.ts +1 -1
- package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +4 -4
- package/src/adapters/node-http/content-type/form-data/index.ts +1 -1
- package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +3 -1
- package/src/adapters/ws.ts +3 -3
- package/src/core/initTRPC.ts +2 -2
- package/src/core/internals/mergeRouters.ts +1 -1
- package/src/core/internals/procedureBuilder.ts +5 -5
- package/src/core/router.ts +2 -2
- package/src/deprecated/internals/procedure.ts +2 -2
- package/src/deprecated/interop.ts +1 -1
- package/src/deprecated/router.ts +6 -6
- package/src/error/formatter.ts +1 -1
- package/src/http/getHTTPStatusCode.ts +1 -1
- package/src/http/internals/types.ts +1 -1
- package/src/http/resolveHTTPResponse.ts +2 -2
- package/src/internals/types.ts +1 -1
- package/src/subscription.ts +1 -1
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @internal
|
|
3
|
-
*/
|
|
4
|
-
function invert(obj) {
|
|
5
|
-
const newObj = Object.create(null);
|
|
6
|
-
for (const key in obj) {
|
|
7
|
-
const v = obj[key];
|
|
8
|
-
newObj[v] = key;
|
|
9
|
-
}
|
|
10
|
-
return newObj;
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
function invert(obj) {
|
|
5
|
+
const newObj = Object.create(null);
|
|
6
|
+
for (const key in obj) {
|
|
7
|
+
const v = obj[key];
|
|
8
|
+
newObj[v] = key;
|
|
9
|
+
}
|
|
10
|
+
return newObj;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
// reference: https://www.jsonrpc.org/specification
|
|
14
|
-
/**
|
|
15
|
-
* JSON-RPC 2.0 Error codes
|
|
16
|
-
*
|
|
17
|
-
* `-32000` to `-32099` are reserved for implementation-defined server-errors.
|
|
18
|
-
* For tRPC we're copying the last digits of HTTP 4XX errors.
|
|
19
|
-
*/
|
|
20
|
-
const TRPC_ERROR_CODES_BY_KEY = {
|
|
21
|
-
/**
|
|
22
|
-
* Invalid JSON was received by the server.
|
|
23
|
-
* An error occurred on the server while parsing the JSON text.
|
|
24
|
-
*/
|
|
25
|
-
PARSE_ERROR: -32700,
|
|
26
|
-
/**
|
|
27
|
-
* The JSON sent is not a valid Request object.
|
|
28
|
-
*/
|
|
29
|
-
BAD_REQUEST: -32600,
|
|
30
|
-
/**
|
|
31
|
-
* Internal JSON-RPC error.
|
|
32
|
-
*/
|
|
33
|
-
INTERNAL_SERVER_ERROR: -32603,
|
|
34
|
-
// Implementation specific errors
|
|
35
|
-
UNAUTHORIZED: -32001,
|
|
36
|
-
FORBIDDEN: -32003,
|
|
37
|
-
NOT_FOUND: -32004,
|
|
38
|
-
METHOD_NOT_SUPPORTED: -32005,
|
|
39
|
-
TIMEOUT: -32008,
|
|
40
|
-
CONFLICT: -32009,
|
|
41
|
-
PRECONDITION_FAILED: -32012,
|
|
42
|
-
PAYLOAD_TOO_LARGE: -32013,
|
|
43
|
-
UNPROCESSABLE_CONTENT: -32022,
|
|
44
|
-
TOO_MANY_REQUESTS: -32029,
|
|
45
|
-
CLIENT_CLOSED_REQUEST: -32099, // 499
|
|
46
|
-
};
|
|
13
|
+
// reference: https://www.jsonrpc.org/specification
|
|
14
|
+
/**
|
|
15
|
+
* JSON-RPC 2.0 Error codes
|
|
16
|
+
*
|
|
17
|
+
* `-32000` to `-32099` are reserved for implementation-defined server-errors.
|
|
18
|
+
* For tRPC we're copying the last digits of HTTP 4XX errors.
|
|
19
|
+
*/
|
|
20
|
+
const TRPC_ERROR_CODES_BY_KEY = {
|
|
21
|
+
/**
|
|
22
|
+
* Invalid JSON was received by the server.
|
|
23
|
+
* An error occurred on the server while parsing the JSON text.
|
|
24
|
+
*/
|
|
25
|
+
PARSE_ERROR: -32700,
|
|
26
|
+
/**
|
|
27
|
+
* The JSON sent is not a valid Request object.
|
|
28
|
+
*/
|
|
29
|
+
BAD_REQUEST: -32600,
|
|
30
|
+
/**
|
|
31
|
+
* Internal JSON-RPC error.
|
|
32
|
+
*/
|
|
33
|
+
INTERNAL_SERVER_ERROR: -32603,
|
|
34
|
+
// Implementation specific errors
|
|
35
|
+
UNAUTHORIZED: -32001,
|
|
36
|
+
FORBIDDEN: -32003,
|
|
37
|
+
NOT_FOUND: -32004,
|
|
38
|
+
METHOD_NOT_SUPPORTED: -32005,
|
|
39
|
+
TIMEOUT: -32008,
|
|
40
|
+
CONFLICT: -32009,
|
|
41
|
+
PRECONDITION_FAILED: -32012,
|
|
42
|
+
PAYLOAD_TOO_LARGE: -32013,
|
|
43
|
+
UNPROCESSABLE_CONTENT: -32022,
|
|
44
|
+
TOO_MANY_REQUESTS: -32029,
|
|
45
|
+
CLIENT_CLOSED_REQUEST: -32099, // 499
|
|
46
|
+
};
|
|
47
47
|
const TRPC_ERROR_CODES_BY_NUMBER = invert(TRPC_ERROR_CODES_BY_KEY);
|
|
48
48
|
|
|
49
49
|
export { TRPC_ERROR_CODES_BY_KEY as T, TRPC_ERROR_CODES_BY_NUMBER as a, invert as i };
|
|
@@ -1,182 +1,182 @@
|
|
|
1
|
-
import { T as TRPCError } from './TRPCError-
|
|
2
|
-
import { a as createRecursiveProxy, g as getHTTPStatusCodeFromError } from './index-
|
|
3
|
-
import { T as TRPC_ERROR_CODES_BY_KEY } from './codes-
|
|
1
|
+
import { T as TRPCError } from './TRPCError-7c3909ea.js';
|
|
2
|
+
import { a as createRecursiveProxy, g as getHTTPStatusCodeFromError } from './index-ca9a128e.js';
|
|
3
|
+
import { T as TRPC_ERROR_CODES_BY_KEY } from './codes-1c9ad84e.js';
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* @internal
|
|
7
|
-
*/
|
|
8
|
-
function getDataTransformer(transformer) {
|
|
9
|
-
if ('input' in transformer) {
|
|
10
|
-
return transformer;
|
|
11
|
-
}
|
|
12
|
-
return { input: transformer, output: transformer };
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* @internal
|
|
16
|
-
*/
|
|
17
|
-
const defaultTransformer = {
|
|
18
|
-
_default: true,
|
|
19
|
-
input: { serialize: (obj) => obj, deserialize: (obj) => obj },
|
|
20
|
-
output: { serialize: (obj) => obj, deserialize: (obj) => obj },
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
function getDataTransformer(transformer) {
|
|
9
|
+
if ('input' in transformer) {
|
|
10
|
+
return transformer;
|
|
11
|
+
}
|
|
12
|
+
return { input: transformer, output: transformer };
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
const defaultTransformer = {
|
|
18
|
+
_default: true,
|
|
19
|
+
input: { serialize: (obj) => obj, deserialize: (obj) => obj },
|
|
20
|
+
output: { serialize: (obj) => obj, deserialize: (obj) => obj },
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
const defaultFormatter = ({ shape, }) => {
|
|
24
|
-
return shape;
|
|
23
|
+
const defaultFormatter = ({ shape, }) => {
|
|
24
|
+
return shape;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
/**
|
|
28
|
-
* Create an object without inheriting anything from `Object.prototype`
|
|
29
|
-
* @internal
|
|
30
|
-
*/
|
|
31
|
-
function omitPrototype(obj) {
|
|
32
|
-
return Object.assign(Object.create(null), obj);
|
|
27
|
+
/**
|
|
28
|
+
* Create an object without inheriting anything from `Object.prototype`
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
function omitPrototype(obj) {
|
|
32
|
+
return Object.assign(Object.create(null), obj);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
const procedureTypes = ['query', 'mutation', 'subscription'];
|
|
36
36
|
|
|
37
|
-
function isRouter(procedureOrRouter) {
|
|
38
|
-
return 'router' in procedureOrRouter._def;
|
|
39
|
-
}
|
|
40
|
-
const emptyRouter = {
|
|
41
|
-
_ctx: null,
|
|
42
|
-
_errorShape: null,
|
|
43
|
-
_meta: null,
|
|
44
|
-
queries: {},
|
|
45
|
-
mutations: {},
|
|
46
|
-
subscriptions: {},
|
|
47
|
-
errorFormatter: defaultFormatter,
|
|
48
|
-
transformer: defaultTransformer,
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* Reserved words that can't be used as router or procedure names
|
|
52
|
-
*/
|
|
53
|
-
const reservedWords = [
|
|
54
|
-
/**
|
|
55
|
-
* Then is a reserved word because otherwise we can't return a promise that returns a Proxy
|
|
56
|
-
* since JS will think that `.then` is something that exists
|
|
57
|
-
*/
|
|
58
|
-
'then',
|
|
59
|
-
];
|
|
60
|
-
/**
|
|
61
|
-
* @internal
|
|
62
|
-
*/
|
|
63
|
-
function createRouterFactory(config) {
|
|
64
|
-
return function createRouterInner(procedures) {
|
|
65
|
-
const reservedWordsUsed = new Set(Object.keys(procedures).filter((v) => reservedWords.includes(v)));
|
|
66
|
-
if (reservedWordsUsed.size > 0) {
|
|
67
|
-
throw new Error('Reserved words used in `router({})` call: ' +
|
|
68
|
-
Array.from(reservedWordsUsed).join(', '));
|
|
69
|
-
}
|
|
70
|
-
const routerProcedures = omitPrototype({});
|
|
71
|
-
function recursiveGetPaths(procedures, path = '') {
|
|
72
|
-
for (const [key, procedureOrRouter] of Object.entries(procedures ?? {})) {
|
|
73
|
-
const newPath = `${path}${key}`;
|
|
74
|
-
if (isRouter(procedureOrRouter)) {
|
|
75
|
-
recursiveGetPaths(procedureOrRouter._def.procedures, `${newPath}.`);
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
if (routerProcedures[newPath]) {
|
|
79
|
-
throw new Error(`Duplicate key: ${newPath}`);
|
|
80
|
-
}
|
|
81
|
-
routerProcedures[newPath] = procedureOrRouter;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
recursiveGetPaths(procedures);
|
|
85
|
-
const _def = {
|
|
86
|
-
_config: config,
|
|
87
|
-
router: true,
|
|
88
|
-
procedures: routerProcedures,
|
|
89
|
-
...emptyRouter,
|
|
90
|
-
record: procedures,
|
|
91
|
-
queries: Object.entries(routerProcedures)
|
|
92
|
-
.filter((pair) => pair[1]._def.query)
|
|
93
|
-
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
|
|
94
|
-
mutations: Object.entries(routerProcedures)
|
|
95
|
-
.filter((pair) => pair[1]._def.mutation)
|
|
96
|
-
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
|
|
97
|
-
subscriptions: Object.entries(routerProcedures)
|
|
98
|
-
.filter((pair) => pair[1]._def.subscription)
|
|
99
|
-
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
|
|
100
|
-
};
|
|
101
|
-
const router = {
|
|
102
|
-
...procedures,
|
|
103
|
-
_def,
|
|
104
|
-
createCaller(ctx) {
|
|
105
|
-
const proxy = createRecursiveProxy(({ path, args }) => {
|
|
106
|
-
// interop mode
|
|
107
|
-
if (path.length === 1 &&
|
|
108
|
-
procedureTypes.includes(path[0])) {
|
|
109
|
-
return callProcedure({
|
|
110
|
-
procedures: _def.procedures,
|
|
111
|
-
path: args[0],
|
|
112
|
-
rawInput: args[1],
|
|
113
|
-
ctx,
|
|
114
|
-
type: path[0],
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
const fullPath = path.join('.');
|
|
118
|
-
const procedure = _def.procedures[fullPath];
|
|
119
|
-
let type = 'query';
|
|
120
|
-
if (procedure._def.mutation) {
|
|
121
|
-
type = 'mutation';
|
|
122
|
-
}
|
|
123
|
-
else if (procedure._def.subscription) {
|
|
124
|
-
type = 'subscription';
|
|
125
|
-
}
|
|
126
|
-
return procedure({
|
|
127
|
-
path: fullPath,
|
|
128
|
-
rawInput: args[0],
|
|
129
|
-
ctx,
|
|
130
|
-
type,
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
return proxy;
|
|
134
|
-
},
|
|
135
|
-
getErrorShape(opts) {
|
|
136
|
-
const { path, error } = opts;
|
|
137
|
-
const { code } = opts.error;
|
|
138
|
-
const shape = {
|
|
139
|
-
message: error.message,
|
|
140
|
-
code: TRPC_ERROR_CODES_BY_KEY[code],
|
|
141
|
-
data: {
|
|
142
|
-
code,
|
|
143
|
-
httpStatus: getHTTPStatusCodeFromError(error),
|
|
144
|
-
},
|
|
145
|
-
};
|
|
146
|
-
if (config.isDev && typeof opts.error.stack === 'string') {
|
|
147
|
-
shape.data.stack = opts.error.stack;
|
|
148
|
-
}
|
|
149
|
-
if (typeof path === 'string') {
|
|
150
|
-
shape.data.path = path;
|
|
151
|
-
}
|
|
152
|
-
return this._def._config.errorFormatter({ ...opts, shape });
|
|
153
|
-
},
|
|
154
|
-
};
|
|
155
|
-
return router;
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* @internal
|
|
160
|
-
*/
|
|
161
|
-
function callProcedure(opts) {
|
|
162
|
-
const { type, path } = opts;
|
|
163
|
-
if (!(path in opts.procedures) || !opts.procedures[path]?._def[type]) {
|
|
164
|
-
throw new TRPCError({
|
|
165
|
-
code: 'NOT_FOUND',
|
|
166
|
-
message: `No "${type}"-procedure on path "${path}"`,
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
const procedure = opts.procedures[path];
|
|
170
|
-
return procedure(opts);
|
|
37
|
+
function isRouter(procedureOrRouter) {
|
|
38
|
+
return 'router' in procedureOrRouter._def;
|
|
39
|
+
}
|
|
40
|
+
const emptyRouter = {
|
|
41
|
+
_ctx: null,
|
|
42
|
+
_errorShape: null,
|
|
43
|
+
_meta: null,
|
|
44
|
+
queries: {},
|
|
45
|
+
mutations: {},
|
|
46
|
+
subscriptions: {},
|
|
47
|
+
errorFormatter: defaultFormatter,
|
|
48
|
+
transformer: defaultTransformer,
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Reserved words that can't be used as router or procedure names
|
|
52
|
+
*/
|
|
53
|
+
const reservedWords = [
|
|
54
|
+
/**
|
|
55
|
+
* Then is a reserved word because otherwise we can't return a promise that returns a Proxy
|
|
56
|
+
* since JS will think that `.then` is something that exists
|
|
57
|
+
*/
|
|
58
|
+
'then',
|
|
59
|
+
];
|
|
60
|
+
/**
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
function createRouterFactory(config) {
|
|
64
|
+
return function createRouterInner(procedures) {
|
|
65
|
+
const reservedWordsUsed = new Set(Object.keys(procedures).filter((v) => reservedWords.includes(v)));
|
|
66
|
+
if (reservedWordsUsed.size > 0) {
|
|
67
|
+
throw new Error('Reserved words used in `router({})` call: ' +
|
|
68
|
+
Array.from(reservedWordsUsed).join(', '));
|
|
69
|
+
}
|
|
70
|
+
const routerProcedures = omitPrototype({});
|
|
71
|
+
function recursiveGetPaths(procedures, path = '') {
|
|
72
|
+
for (const [key, procedureOrRouter] of Object.entries(procedures ?? {})) {
|
|
73
|
+
const newPath = `${path}${key}`;
|
|
74
|
+
if (isRouter(procedureOrRouter)) {
|
|
75
|
+
recursiveGetPaths(procedureOrRouter._def.procedures, `${newPath}.`);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (routerProcedures[newPath]) {
|
|
79
|
+
throw new Error(`Duplicate key: ${newPath}`);
|
|
80
|
+
}
|
|
81
|
+
routerProcedures[newPath] = procedureOrRouter;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
recursiveGetPaths(procedures);
|
|
85
|
+
const _def = {
|
|
86
|
+
_config: config,
|
|
87
|
+
router: true,
|
|
88
|
+
procedures: routerProcedures,
|
|
89
|
+
...emptyRouter,
|
|
90
|
+
record: procedures,
|
|
91
|
+
queries: Object.entries(routerProcedures)
|
|
92
|
+
.filter((pair) => pair[1]._def.query)
|
|
93
|
+
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
|
|
94
|
+
mutations: Object.entries(routerProcedures)
|
|
95
|
+
.filter((pair) => pair[1]._def.mutation)
|
|
96
|
+
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
|
|
97
|
+
subscriptions: Object.entries(routerProcedures)
|
|
98
|
+
.filter((pair) => pair[1]._def.subscription)
|
|
99
|
+
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
|
|
100
|
+
};
|
|
101
|
+
const router = {
|
|
102
|
+
...procedures,
|
|
103
|
+
_def,
|
|
104
|
+
createCaller(ctx) {
|
|
105
|
+
const proxy = createRecursiveProxy(({ path, args }) => {
|
|
106
|
+
// interop mode
|
|
107
|
+
if (path.length === 1 &&
|
|
108
|
+
procedureTypes.includes(path[0])) {
|
|
109
|
+
return callProcedure({
|
|
110
|
+
procedures: _def.procedures,
|
|
111
|
+
path: args[0],
|
|
112
|
+
rawInput: args[1],
|
|
113
|
+
ctx,
|
|
114
|
+
type: path[0],
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const fullPath = path.join('.');
|
|
118
|
+
const procedure = _def.procedures[fullPath];
|
|
119
|
+
let type = 'query';
|
|
120
|
+
if (procedure._def.mutation) {
|
|
121
|
+
type = 'mutation';
|
|
122
|
+
}
|
|
123
|
+
else if (procedure._def.subscription) {
|
|
124
|
+
type = 'subscription';
|
|
125
|
+
}
|
|
126
|
+
return procedure({
|
|
127
|
+
path: fullPath,
|
|
128
|
+
rawInput: args[0],
|
|
129
|
+
ctx,
|
|
130
|
+
type,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
return proxy;
|
|
134
|
+
},
|
|
135
|
+
getErrorShape(opts) {
|
|
136
|
+
const { path, error } = opts;
|
|
137
|
+
const { code } = opts.error;
|
|
138
|
+
const shape = {
|
|
139
|
+
message: error.message,
|
|
140
|
+
code: TRPC_ERROR_CODES_BY_KEY[code],
|
|
141
|
+
data: {
|
|
142
|
+
code,
|
|
143
|
+
httpStatus: getHTTPStatusCodeFromError(error),
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
if (config.isDev && typeof opts.error.stack === 'string') {
|
|
147
|
+
shape.data.stack = opts.error.stack;
|
|
148
|
+
}
|
|
149
|
+
if (typeof path === 'string') {
|
|
150
|
+
shape.data.path = path;
|
|
151
|
+
}
|
|
152
|
+
return this._def._config.errorFormatter({ ...opts, shape });
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
return router;
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @internal
|
|
160
|
+
*/
|
|
161
|
+
function callProcedure(opts) {
|
|
162
|
+
const { type, path } = opts;
|
|
163
|
+
if (!(path in opts.procedures) || !opts.procedures[path]?._def[type]) {
|
|
164
|
+
throw new TRPCError({
|
|
165
|
+
code: 'NOT_FOUND',
|
|
166
|
+
message: `No "${type}"-procedure on path "${path}"`,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const procedure = opts.procedures[path];
|
|
170
|
+
return procedure(opts);
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
/**
|
|
174
|
-
* The default check to see if we're in a server
|
|
175
|
-
*/
|
|
176
|
-
const isServerDefault = typeof window === 'undefined' ||
|
|
177
|
-
'Deno' in window ||
|
|
178
|
-
globalThis.process?.env?.NODE_ENV === 'test' ||
|
|
179
|
-
!!globalThis.process?.env?.JEST_WORKER_ID ||
|
|
173
|
+
/**
|
|
174
|
+
* The default check to see if we're in a server
|
|
175
|
+
*/
|
|
176
|
+
const isServerDefault = typeof window === 'undefined' ||
|
|
177
|
+
'Deno' in window ||
|
|
178
|
+
globalThis.process?.env?.NODE_ENV === 'test' ||
|
|
179
|
+
!!globalThis.process?.env?.JEST_WORKER_ID ||
|
|
180
180
|
!!globalThis.process?.env?.VITEST_WORKER_ID;
|
|
181
181
|
|
|
182
182
|
export { defaultTransformer as a, callProcedure as b, createRouterFactory as c, defaultFormatter as d, getDataTransformer as g, isServerDefault as i, procedureTypes as p };
|
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { T as TRPCError, g as getCauseFromUnknown } from './TRPCError-
|
|
1
|
+
import { T as TRPCError, g as getCauseFromUnknown } from './TRPCError-7c3909ea.js';
|
|
2
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
|
-
}
|
|
19
|
-
catch (err) {
|
|
20
|
-
throw new TRPCError({
|
|
21
|
-
code: 'PARSE_ERROR',
|
|
22
|
-
cause: getCauseFromUnknown(err),
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
const deserializeInputValue = (rawValue, transformer) => {
|
|
27
|
-
return typeof rawValue !== 'undefined'
|
|
28
|
-
? transformer.input.deserialize(rawValue)
|
|
29
|
-
: rawValue;
|
|
30
|
-
};
|
|
31
|
-
const getJsonContentTypeInputs = (opts) => {
|
|
32
|
-
const rawInput = getRawProcedureInputOrThrow(opts);
|
|
33
|
-
const transformer = opts.router._def._config.transformer;
|
|
34
|
-
if (!opts.isBatchCall) {
|
|
35
|
-
return {
|
|
36
|
-
0: deserializeInputValue(rawInput, transformer),
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
/* istanbul ignore if */
|
|
40
|
-
if (rawInput == null ||
|
|
41
|
-
typeof rawInput !== 'object' ||
|
|
42
|
-
Array.isArray(rawInput)) {
|
|
43
|
-
throw new TRPCError({
|
|
44
|
-
code: 'BAD_REQUEST',
|
|
45
|
-
message: '"input" needs to be an object when doing a batch call',
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
const input = {};
|
|
49
|
-
for (const key in rawInput) {
|
|
50
|
-
const k = key;
|
|
51
|
-
const rawValue = rawInput[k];
|
|
52
|
-
const value = deserializeInputValue(rawValue, transformer);
|
|
53
|
-
input[k] = value;
|
|
54
|
-
}
|
|
55
|
-
return input;
|
|
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
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
throw new TRPCError({
|
|
21
|
+
code: 'PARSE_ERROR',
|
|
22
|
+
cause: getCauseFromUnknown(err),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const deserializeInputValue = (rawValue, transformer) => {
|
|
27
|
+
return typeof rawValue !== 'undefined'
|
|
28
|
+
? transformer.input.deserialize(rawValue)
|
|
29
|
+
: rawValue;
|
|
30
|
+
};
|
|
31
|
+
const getJsonContentTypeInputs = (opts) => {
|
|
32
|
+
const rawInput = getRawProcedureInputOrThrow(opts);
|
|
33
|
+
const transformer = opts.router._def._config.transformer;
|
|
34
|
+
if (!opts.isBatchCall) {
|
|
35
|
+
return {
|
|
36
|
+
0: deserializeInputValue(rawInput, transformer),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/* istanbul ignore if */
|
|
40
|
+
if (rawInput == null ||
|
|
41
|
+
typeof rawInput !== 'object' ||
|
|
42
|
+
Array.isArray(rawInput)) {
|
|
43
|
+
throw new TRPCError({
|
|
44
|
+
code: 'BAD_REQUEST',
|
|
45
|
+
message: '"input" needs to be an object when doing a batch call',
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const input = {};
|
|
49
|
+
for (const key in rawInput) {
|
|
50
|
+
const k = key;
|
|
51
|
+
const rawValue = rawInput[k];
|
|
52
|
+
const value = deserializeInputValue(rawValue, transformer);
|
|
53
|
+
input[k] = value;
|
|
54
|
+
}
|
|
55
|
+
return input;
|
|
56
56
|
};
|
|
57
57
|
|
|
58
58
|
export { getJsonContentTypeInputs as g };
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export type { AnyRouter, ProcedureRecord, ProcedureRouterRecord, CreateRouterInner, Router, } from './router';
|
|
2
|
-
export { callProcedure } from './router';
|
|
3
|
-
export type { Procedure, AnyProcedure, AnyQueryProcedure, AnyMutationProcedure, AnySubscriptionProcedure, ProcedureParams, ProcedureArgs, ProcedureOptions, } from './procedure';
|
|
4
|
-
export type { inferParser } from './parser';
|
|
5
|
-
export { createInputMiddleware, createOutputMiddleware } from './middleware';
|
|
6
|
-
export type { MiddlewareFunction, MiddlewareBuilder } from './middleware';
|
|
7
|
-
export { initTRPC } from './initTRPC';
|
|
8
|
-
export * from './types';
|
|
1
|
+
export type { AnyRouter, ProcedureRecord, ProcedureRouterRecord, CreateRouterInner, Router, } from './router';
|
|
2
|
+
export { callProcedure } from './router';
|
|
3
|
+
export type { Procedure, AnyProcedure, AnyQueryProcedure, AnyMutationProcedure, AnySubscriptionProcedure, ProcedureParams, ProcedureArgs, ProcedureOptions, } from './procedure';
|
|
4
|
+
export type { inferParser } from './parser';
|
|
5
|
+
export { createInputMiddleware, createOutputMiddleware } from './middleware';
|
|
6
|
+
export type { MiddlewareFunction, MiddlewareBuilder } from './middleware';
|
|
7
|
+
export { initTRPC } from './initTRPC';
|
|
8
|
+
export * from './types';
|
|
9
9
|
//# sourceMappingURL=index.d.ts.map
|