@trpc/server 11.0.0-alpha-tmp-export-from-main.218 → 11.0.0-alpha-tmp-export-from-main-nuke-core.239
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/index.d.ts +69 -0
- package/dist/@trpc/server/index.d.ts.map +1 -0
- package/dist/@trpc/server/rpc.d.ts +3 -0
- package/dist/@trpc/server/rpc.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/index.d.ts +1 -2
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +6 -4
- package/dist/adapters/aws-lambda/index.mjs +4 -2
- package/dist/adapters/aws-lambda/utils.d.ts +2 -3
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/utils.js +5 -4
- package/dist/adapters/aws-lambda/utils.mjs +3 -2
- package/dist/adapters/express.d.ts +1 -2
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +2 -2
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +6 -3
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +4 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -2
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +0 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +0 -1
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -2
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +6 -4
- package/dist/adapters/fetch/fetchRequestHandler.mjs +4 -2
- package/dist/adapters/fetch/types.d.ts +2 -3
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next.d.ts +1 -3
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +6 -4
- package/dist/adapters/next.mjs +4 -2
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.js +4 -2
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +3 -1
- package/dist/adapters/node-http/content-type/json/index.js +4 -2
- package/dist/adapters/node-http/content-type/json/index.mjs +3 -1
- package/dist/adapters/node-http/internals/contentType.d.ts +2 -2
- package/dist/adapters/node-http/internals/contentType.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -2
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +6 -4
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +4 -2
- package/dist/adapters/node-http/types.d.ts +3 -4
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts +1 -2
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +1 -0
- package/dist/adapters/standalone.mjs +1 -0
- package/dist/adapters/ws.d.ts +3 -2
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +33 -26
- package/dist/adapters/ws.mjs +8 -1
- package/dist/bundle-analysis.json +757 -0
- package/dist/http.d.ts +1 -6
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +11 -21
- package/dist/http.mjs +6 -1
- package/dist/index.d.ts +1 -68
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -41
- package/dist/index.mjs +8 -1
- package/dist/observable/index.d.ts +5 -0
- package/dist/observable/index.d.ts.map +1 -0
- package/dist/observable/index.js +13 -0
- package/dist/observable/index.mjs +2 -0
- package/dist/observable/observable.d.ts +13 -0
- package/dist/observable/observable.d.ts.map +1 -0
- package/dist/observable/observable.js +126 -0
- package/dist/observable/observable.mjs +122 -0
- package/dist/observable/operators.d.ts +8 -0
- package/dist/observable/operators.d.ts.map +1 -0
- package/dist/observable/operators.js +103 -0
- package/dist/observable/operators.mjs +99 -0
- package/dist/observable/types.d.ts +26 -0
- package/dist/observable/types.d.ts.map +1 -0
- package/dist/rpc.d.ts +1 -2
- package/dist/rpc.d.ts.map +1 -1
- package/dist/rpc.js +6 -13
- package/dist/rpc.mjs +3 -1
- package/dist/shared.d.ts +1 -1
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +6 -9
- package/dist/shared.mjs +4 -1
- package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts +6 -0
- package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/createProxy.d.ts +19 -0
- package/dist/unstable-core-do-not-import/createProxy.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/createProxy.js +52 -0
- package/dist/unstable-core-do-not-import/createProxy.mjs +49 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +13 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.js +67 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.mjs +63 -0
- package/dist/unstable-core-do-not-import/error/formatter.d.ts +33 -0
- package/dist/unstable-core-do-not-import/error/formatter.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/error/formatter.js +7 -0
- package/dist/unstable-core-do-not-import/error/formatter.mjs +5 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts +15 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.js +31 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.mjs +29 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +24 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.js +32 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.mjs +30 -0
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +29 -0
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/contentType.js +54 -0
- package/dist/unstable-core-do-not-import/http/contentType.mjs +52 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +5 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +51 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +48 -0
- package/dist/unstable-core-do-not-import/http/index.d.ts +7 -0
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +51 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +295 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +293 -0
- package/dist/unstable-core-do-not-import/http/types.d.ts +96 -0
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/index.d.ts +37 -0
- package/dist/unstable-core-do-not-import/index.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/index.js +48 -0
- package/dist/unstable-core-do-not-import/index.mjs +16 -0
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +96 -0
- package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/initTRPC.js +95 -0
- package/dist/unstable-core-do-not-import/initTRPC.mjs +93 -0
- package/dist/unstable-core-do-not-import/middleware.d.ts +105 -0
- package/dist/unstable-core-do-not-import/middleware.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/middleware.js +95 -0
- package/dist/unstable-core-do-not-import/middleware.mjs +89 -0
- package/dist/unstable-core-do-not-import/parser.d.ts +30 -0
- package/dist/unstable-core-do-not-import/parser.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/parser.js +37 -0
- package/dist/unstable-core-do-not-import/parser.mjs +35 -0
- package/dist/unstable-core-do-not-import/procedure.d.ts +72 -0
- package/dist/unstable-core-do-not-import/procedure.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/procedure.js +9 -0
- package/dist/unstable-core-do-not-import/procedure.mjs +7 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +101 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.js +176 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +173 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +75 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rootConfig.js +8 -0
- package/dist/unstable-core-do-not-import/rootConfig.mjs +6 -0
- package/dist/unstable-core-do-not-import/router.d.ts +85 -0
- package/dist/unstable-core-do-not-import/router.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/router.js +156 -0
- package/dist/unstable-core-do-not-import/router.mjs +151 -0
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts +51 -0
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/codes.js +38 -0
- package/dist/unstable-core-do-not-import/rpc/codes.mjs +35 -0
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +97 -0
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/index.d.ts +5 -0
- package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts +5 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.js +59 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.mjs +57 -0
- package/dist/unstable-core-do-not-import/serialize.d.ts +62 -0
- package/dist/unstable-core-do-not-import/serialize.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/transformer.d.ts +107 -0
- package/dist/unstable-core-do-not-import/transformer.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/transformer.js +107 -0
- package/dist/unstable-core-do-not-import/transformer.mjs +102 -0
- package/dist/unstable-core-do-not-import/types.d.ts +98 -0
- package/dist/unstable-core-do-not-import/types.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/utils.d.ts +27 -0
- package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/utils.js +44 -0
- package/dist/unstable-core-do-not-import/utils.mjs +39 -0
- package/package.json +53 -125
- package/src/@trpc/server/http.ts +26 -0
- package/src/@trpc/server/index.ts +105 -0
- 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 -4
- 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 -105
- 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 -10
- 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/observable.d.ts +0 -3
- package/dist/observable.d.ts.map +0 -1
- package/dist/observable.js +0 -30
- package/dist/observable.mjs +0 -1
- package/src/@trpc-core-unstable-do-not-import-this-please.ts +0 -146
- package/src/@trpc-server/http.ts +0 -2
- package/src/observable.ts +0 -16
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-RPC 2.0 Error codes
|
|
3
|
+
*
|
|
4
|
+
* `-32000` to `-32099` are reserved for implementation-defined server-errors.
|
|
5
|
+
* For tRPC we're copying the last digits of HTTP 4XX errors.
|
|
6
|
+
*/
|
|
7
|
+
export declare const TRPC_ERROR_CODES_BY_KEY: {
|
|
8
|
+
/**
|
|
9
|
+
* Invalid JSON was received by the server.
|
|
10
|
+
* An error occurred on the server while parsing the JSON text.
|
|
11
|
+
*/
|
|
12
|
+
readonly PARSE_ERROR: -32700;
|
|
13
|
+
/**
|
|
14
|
+
* The JSON sent is not a valid Request object.
|
|
15
|
+
*/
|
|
16
|
+
readonly BAD_REQUEST: -32600;
|
|
17
|
+
readonly INTERNAL_SERVER_ERROR: -32603;
|
|
18
|
+
readonly NOT_IMPLEMENTED: -32603;
|
|
19
|
+
readonly UNAUTHORIZED: -32001;
|
|
20
|
+
readonly FORBIDDEN: -32003;
|
|
21
|
+
readonly NOT_FOUND: -32004;
|
|
22
|
+
readonly METHOD_NOT_SUPPORTED: -32005;
|
|
23
|
+
readonly TIMEOUT: -32008;
|
|
24
|
+
readonly CONFLICT: -32009;
|
|
25
|
+
readonly PRECONDITION_FAILED: -32012;
|
|
26
|
+
readonly PAYLOAD_TOO_LARGE: -32013;
|
|
27
|
+
readonly UNPROCESSABLE_CONTENT: -32022;
|
|
28
|
+
readonly TOO_MANY_REQUESTS: -32029;
|
|
29
|
+
readonly CLIENT_CLOSED_REQUEST: -32099;
|
|
30
|
+
};
|
|
31
|
+
export declare const TRPC_ERROR_CODES_BY_NUMBER: {
|
|
32
|
+
[-32700]: "PARSE_ERROR";
|
|
33
|
+
[-32600]: "BAD_REQUEST";
|
|
34
|
+
[-32603]: "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED";
|
|
35
|
+
[-32001]: "UNAUTHORIZED";
|
|
36
|
+
[-32003]: "FORBIDDEN";
|
|
37
|
+
[-32004]: "NOT_FOUND";
|
|
38
|
+
[-32005]: "METHOD_NOT_SUPPORTED";
|
|
39
|
+
[-32008]: "TIMEOUT";
|
|
40
|
+
[-32009]: "CONFLICT";
|
|
41
|
+
[-32012]: "PRECONDITION_FAILED";
|
|
42
|
+
[-32013]: "PAYLOAD_TOO_LARGE";
|
|
43
|
+
[-32022]: "UNPROCESSABLE_CONTENT";
|
|
44
|
+
[-32029]: "TOO_MANY_REQUESTS";
|
|
45
|
+
[-32099]: "CLIENT_CLOSED_REQUEST";
|
|
46
|
+
};
|
|
47
|
+
type ValueOf<TObj> = TObj[keyof TObj];
|
|
48
|
+
export type TRPC_ERROR_CODE_NUMBER = ValueOf<typeof TRPC_ERROR_CODES_BY_KEY>;
|
|
49
|
+
export type TRPC_ERROR_CODE_KEY = keyof typeof TRPC_ERROR_CODES_BY_KEY;
|
|
50
|
+
export {};
|
|
51
|
+
//# sourceMappingURL=codes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codes.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/rpc/codes.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB;IAClC;;;OAGG;;IAEH;;OAEG;;;;;;;;;;;;;;;CAmBK,CAAC;AAEX,eAAO,MAAM,0BAA0B;;;;;;;;;;;;;;;CAAkC,CAAC;AAE1E,KAAK,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;AACtC,MAAM,MAAM,sBAAsB,GAAG,OAAO,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAC7E,MAAM,MAAM,mBAAmB,GAAG,MAAM,OAAO,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('../utils.js');
|
|
4
|
+
|
|
5
|
+
// reference: https://www.jsonrpc.org/specification
|
|
6
|
+
/**
|
|
7
|
+
* JSON-RPC 2.0 Error codes
|
|
8
|
+
*
|
|
9
|
+
* `-32000` to `-32099` are reserved for implementation-defined server-errors.
|
|
10
|
+
* For tRPC we're copying the last digits of HTTP 4XX errors.
|
|
11
|
+
*/ const TRPC_ERROR_CODES_BY_KEY = {
|
|
12
|
+
/**
|
|
13
|
+
* Invalid JSON was received by the server.
|
|
14
|
+
* An error occurred on the server while parsing the JSON text.
|
|
15
|
+
*/ PARSE_ERROR: -32700,
|
|
16
|
+
/**
|
|
17
|
+
* The JSON sent is not a valid Request object.
|
|
18
|
+
*/ BAD_REQUEST: -32600,
|
|
19
|
+
// Internal JSON-RPC error
|
|
20
|
+
INTERNAL_SERVER_ERROR: -32603,
|
|
21
|
+
NOT_IMPLEMENTED: -32603,
|
|
22
|
+
// Implementation specific errors
|
|
23
|
+
UNAUTHORIZED: -32001,
|
|
24
|
+
FORBIDDEN: -32003,
|
|
25
|
+
NOT_FOUND: -32004,
|
|
26
|
+
METHOD_NOT_SUPPORTED: -32005,
|
|
27
|
+
TIMEOUT: -32008,
|
|
28
|
+
CONFLICT: -32009,
|
|
29
|
+
PRECONDITION_FAILED: -32012,
|
|
30
|
+
PAYLOAD_TOO_LARGE: -32013,
|
|
31
|
+
UNPROCESSABLE_CONTENT: -32022,
|
|
32
|
+
TOO_MANY_REQUESTS: -32029,
|
|
33
|
+
CLIENT_CLOSED_REQUEST: -32099
|
|
34
|
+
};
|
|
35
|
+
const TRPC_ERROR_CODES_BY_NUMBER = utils.invert(TRPC_ERROR_CODES_BY_KEY);
|
|
36
|
+
|
|
37
|
+
exports.TRPC_ERROR_CODES_BY_KEY = TRPC_ERROR_CODES_BY_KEY;
|
|
38
|
+
exports.TRPC_ERROR_CODES_BY_NUMBER = TRPC_ERROR_CODES_BY_NUMBER;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { invert } from '../utils.mjs';
|
|
2
|
+
|
|
3
|
+
// reference: https://www.jsonrpc.org/specification
|
|
4
|
+
/**
|
|
5
|
+
* JSON-RPC 2.0 Error codes
|
|
6
|
+
*
|
|
7
|
+
* `-32000` to `-32099` are reserved for implementation-defined server-errors.
|
|
8
|
+
* For tRPC we're copying the last digits of HTTP 4XX errors.
|
|
9
|
+
*/ const TRPC_ERROR_CODES_BY_KEY = {
|
|
10
|
+
/**
|
|
11
|
+
* Invalid JSON was received by the server.
|
|
12
|
+
* An error occurred on the server while parsing the JSON text.
|
|
13
|
+
*/ PARSE_ERROR: -32700,
|
|
14
|
+
/**
|
|
15
|
+
* The JSON sent is not a valid Request object.
|
|
16
|
+
*/ BAD_REQUEST: -32600,
|
|
17
|
+
// Internal JSON-RPC error
|
|
18
|
+
INTERNAL_SERVER_ERROR: -32603,
|
|
19
|
+
NOT_IMPLEMENTED: -32603,
|
|
20
|
+
// Implementation specific errors
|
|
21
|
+
UNAUTHORIZED: -32001,
|
|
22
|
+
FORBIDDEN: -32003,
|
|
23
|
+
NOT_FOUND: -32004,
|
|
24
|
+
METHOD_NOT_SUPPORTED: -32005,
|
|
25
|
+
TIMEOUT: -32008,
|
|
26
|
+
CONFLICT: -32009,
|
|
27
|
+
PRECONDITION_FAILED: -32012,
|
|
28
|
+
PAYLOAD_TOO_LARGE: -32013,
|
|
29
|
+
UNPROCESSABLE_CONTENT: -32022,
|
|
30
|
+
TOO_MANY_REQUESTS: -32029,
|
|
31
|
+
CLIENT_CLOSED_REQUEST: -32099
|
|
32
|
+
};
|
|
33
|
+
const TRPC_ERROR_CODES_BY_NUMBER = invert(TRPC_ERROR_CODES_BY_KEY);
|
|
34
|
+
|
|
35
|
+
export { TRPC_ERROR_CODES_BY_KEY, TRPC_ERROR_CODES_BY_NUMBER };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { ProcedureType } from '../procedure';
|
|
2
|
+
import type { TRPC_ERROR_CODE_NUMBER } from './codes';
|
|
3
|
+
/**
|
|
4
|
+
* Error response
|
|
5
|
+
*/
|
|
6
|
+
export interface TRPCErrorShape<TCode extends number = TRPC_ERROR_CODE_NUMBER, TData extends Record<string, unknown> = Record<string, unknown>> {
|
|
7
|
+
code: TCode;
|
|
8
|
+
message: string;
|
|
9
|
+
data: TData;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* JSON-RPC 2.0 Specification
|
|
13
|
+
*/
|
|
14
|
+
export declare namespace JSONRPC2 {
|
|
15
|
+
type RequestId = number | string | null;
|
|
16
|
+
/**
|
|
17
|
+
* All requests/responses extends this shape
|
|
18
|
+
*/
|
|
19
|
+
interface BaseEnvelope {
|
|
20
|
+
id?: RequestId;
|
|
21
|
+
jsonrpc?: '2.0';
|
|
22
|
+
}
|
|
23
|
+
interface BaseRequest<TMethod extends string = string> extends BaseEnvelope {
|
|
24
|
+
method: TMethod;
|
|
25
|
+
}
|
|
26
|
+
interface Request<TMethod extends string = string, TParams = unknown> extends BaseRequest<TMethod> {
|
|
27
|
+
params: TParams;
|
|
28
|
+
}
|
|
29
|
+
interface ResultResponse<TResult = unknown> extends BaseEnvelope {
|
|
30
|
+
result: TResult;
|
|
31
|
+
}
|
|
32
|
+
interface ErrorResponse<TError extends TRPCErrorShape = TRPCErrorShape> extends BaseEnvelope {
|
|
33
|
+
error: TError;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export interface TRPCRequest extends JSONRPC2.Request<ProcedureType, {
|
|
37
|
+
path: string;
|
|
38
|
+
input: unknown;
|
|
39
|
+
}> {
|
|
40
|
+
}
|
|
41
|
+
export interface TRPCResult<TData = unknown> {
|
|
42
|
+
data: TData;
|
|
43
|
+
}
|
|
44
|
+
export interface TRPCSuccessResponse<TData> extends JSONRPC2.ResultResponse<TRPCResult<TData> & {
|
|
45
|
+
type?: 'data';
|
|
46
|
+
}> {
|
|
47
|
+
}
|
|
48
|
+
export interface TRPCErrorResponse<TError extends TRPCErrorShape = TRPCErrorShape> extends JSONRPC2.ErrorResponse<TError> {
|
|
49
|
+
}
|
|
50
|
+
export type TRPCResponse<TData = unknown, TError extends TRPCErrorShape = TRPCErrorShape> = TRPCErrorResponse<TError> | TRPCSuccessResponse<TData>;
|
|
51
|
+
export type TRPCRequestMessage = TRPCRequest & {
|
|
52
|
+
id: JSONRPC2.RequestId;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* The client asked the server to unsubscribe
|
|
56
|
+
*/
|
|
57
|
+
export interface TRPCSubscriptionStopNotification extends JSONRPC2.BaseRequest<'subscription.stop'> {
|
|
58
|
+
id: null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The client's outgoing request types
|
|
62
|
+
*/
|
|
63
|
+
export type TRPCClientOutgoingRequest = TRPCSubscriptionStopNotification;
|
|
64
|
+
/**
|
|
65
|
+
* The client's sent messages shape
|
|
66
|
+
*/
|
|
67
|
+
export type TRPCClientOutgoingMessage = TRPCRequestMessage | (JSONRPC2.BaseRequest<'subscription.stop'> & {
|
|
68
|
+
id: JSONRPC2.RequestId;
|
|
69
|
+
});
|
|
70
|
+
export interface TRPCResultMessage<TData> extends JSONRPC2.ResultResponse<{
|
|
71
|
+
type: 'started';
|
|
72
|
+
data?: never;
|
|
73
|
+
} | {
|
|
74
|
+
type: 'stopped';
|
|
75
|
+
data?: never;
|
|
76
|
+
} | (TRPCResult<TData> & {
|
|
77
|
+
type: 'data';
|
|
78
|
+
})> {
|
|
79
|
+
}
|
|
80
|
+
export type TRPCResponseMessage<TData = unknown, TError extends TRPCErrorShape = TRPCErrorShape> = {
|
|
81
|
+
id: JSONRPC2.RequestId;
|
|
82
|
+
} & (TRPCErrorResponse<TError> | TRPCResultMessage<TData>);
|
|
83
|
+
/**
|
|
84
|
+
* The server asked the client to reconnect - useful when restarting/redeploying service
|
|
85
|
+
*/
|
|
86
|
+
export interface TRPCReconnectNotification extends JSONRPC2.BaseRequest<'reconnect'> {
|
|
87
|
+
id: JSONRPC2.RequestId;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The client's incoming request types
|
|
91
|
+
*/
|
|
92
|
+
export type TRPCClientIncomingRequest = TRPCReconnectNotification;
|
|
93
|
+
/**
|
|
94
|
+
* The client's received messages shape
|
|
95
|
+
*/
|
|
96
|
+
export type TRPCClientIncomingMessage<TResult = unknown, TError extends TRPCErrorShape = TRPCErrorShape> = TRPCClientIncomingRequest | TRPCResponseMessage<TResult, TError>;
|
|
97
|
+
//# sourceMappingURL=envelopes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envelopes.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/rpc/envelopes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,cAAc,CAC7B,KAAK,SAAS,MAAM,GAAG,sBAAsB,EAC7C,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAE/D,IAAI,EAAE,KAAK,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,KAAK,CAAC;CACb;AAED;;GAEG;AACH,yBAAiB,QAAQ,CAAC;IACxB,KAAY,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAE/C;;OAEG;IACH,UAAiB,YAAY;QAC3B,EAAE,CAAC,EAAE,SAAS,CAAC;QACf,OAAO,CAA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,yBAAyB,GAAG,mBAAmB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { TRPC_ERROR_CODES_BY_KEY, TRPC_ERROR_CODES_BY_NUMBER } from './codes';
|
|
2
|
+
export type { TRPC_ERROR_CODE_KEY, TRPC_ERROR_CODE_NUMBER } from './codes';
|
|
3
|
+
export type { JSONRPC2, TRPCClientIncomingMessage, TRPCClientIncomingRequest, TRPCClientOutgoingMessage, TRPCClientOutgoingRequest, TRPCErrorResponse, TRPCErrorShape, TRPCReconnectNotification, TRPCRequest, TRPCRequestMessage, TRPCResponse, TRPCResponseMessage, TRPCResult, TRPCResultMessage, TRPCSubscriptionStopNotification, TRPCSuccessResponse, } from './envelopes';
|
|
4
|
+
export { parseTRPCMessage } from './parseTRPCMessage';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/rpc/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAC;AAC9E,YAAY,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAC3E,YAAY,EACV,QAAQ,EACR,yBAAyB,EACzB,yBAAyB,EACzB,yBAAyB,EACzB,yBAAyB,EACzB,iBAAiB,EACjB,cAAc,EACd,yBAAyB,EACzB,WAAW,EACX,kBAAkB,EAClB,YAAY,EACZ,mBAAmB,EACnB,UAAU,EACV,iBAAiB,EACjB,gCAAgC,EAChC,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { CombinedDataTransformer } from '../transformer';
|
|
2
|
+
import type { TRPCClientOutgoingMessage } from './envelopes';
|
|
3
|
+
/** @public */
|
|
4
|
+
export declare function parseTRPCMessage(obj: unknown, transformer: CombinedDataTransformer): TRPCClientOutgoingMessage;
|
|
5
|
+
//# sourceMappingURL=parseTRPCMessage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseTRPCMessage.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAE9D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AA8C7D,cAAc;AACd,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,OAAO,EACZ,WAAW,EAAE,uBAAuB,GACnC,yBAAyB,CA+B3B"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var procedure = require('../procedure.js');
|
|
4
|
+
var utils = require('../utils.js');
|
|
5
|
+
|
|
6
|
+
/* istanbul ignore next -- @preserve */ function assertIsObject(obj) {
|
|
7
|
+
if (!utils.isObject(obj)) {
|
|
8
|
+
throw new Error('Not an object');
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/* istanbul ignore next -- @preserve */ function assertIsProcedureType(obj) {
|
|
12
|
+
if (!procedure.procedureTypes.includes(obj)) {
|
|
13
|
+
throw new Error('Invalid procedure type');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/* istanbul ignore next -- @preserve */ function assertIsRequestId(obj) {
|
|
17
|
+
if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
|
|
18
|
+
throw new Error('Invalid request id');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/* istanbul ignore next -- @preserve */ function assertIsString(obj) {
|
|
22
|
+
if (typeof obj !== 'string') {
|
|
23
|
+
throw new Error('Invalid string');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/* istanbul ignore next -- @preserve */ function assertIsJSONRPC2OrUndefined(obj) {
|
|
27
|
+
if (typeof obj !== 'undefined' && obj !== '2.0') {
|
|
28
|
+
throw new Error('Must be JSONRPC 2.0');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** @public */ function parseTRPCMessage(obj, transformer) {
|
|
32
|
+
assertIsObject(obj);
|
|
33
|
+
const { id , jsonrpc , method , params } = obj;
|
|
34
|
+
assertIsRequestId(id);
|
|
35
|
+
assertIsJSONRPC2OrUndefined(jsonrpc);
|
|
36
|
+
if (method === 'subscription.stop') {
|
|
37
|
+
return {
|
|
38
|
+
id,
|
|
39
|
+
jsonrpc,
|
|
40
|
+
method
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
assertIsProcedureType(method);
|
|
44
|
+
assertIsObject(params);
|
|
45
|
+
const { input: rawInput , path } = params;
|
|
46
|
+
assertIsString(path);
|
|
47
|
+
const input = transformer.input.deserialize(rawInput);
|
|
48
|
+
return {
|
|
49
|
+
id,
|
|
50
|
+
jsonrpc,
|
|
51
|
+
method,
|
|
52
|
+
params: {
|
|
53
|
+
input,
|
|
54
|
+
path
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
exports.parseTRPCMessage = parseTRPCMessage;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { procedureTypes } from '../procedure.mjs';
|
|
2
|
+
import { isObject } from '../utils.mjs';
|
|
3
|
+
|
|
4
|
+
/* istanbul ignore next -- @preserve */ function assertIsObject(obj) {
|
|
5
|
+
if (!isObject(obj)) {
|
|
6
|
+
throw new Error('Not an object');
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/* istanbul ignore next -- @preserve */ function assertIsProcedureType(obj) {
|
|
10
|
+
if (!procedureTypes.includes(obj)) {
|
|
11
|
+
throw new Error('Invalid procedure type');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/* istanbul ignore next -- @preserve */ function assertIsRequestId(obj) {
|
|
15
|
+
if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
|
|
16
|
+
throw new Error('Invalid request id');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/* istanbul ignore next -- @preserve */ function assertIsString(obj) {
|
|
20
|
+
if (typeof obj !== 'string') {
|
|
21
|
+
throw new Error('Invalid string');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/* istanbul ignore next -- @preserve */ function assertIsJSONRPC2OrUndefined(obj) {
|
|
25
|
+
if (typeof obj !== 'undefined' && obj !== '2.0') {
|
|
26
|
+
throw new Error('Must be JSONRPC 2.0');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** @public */ function parseTRPCMessage(obj, transformer) {
|
|
30
|
+
assertIsObject(obj);
|
|
31
|
+
const { id , jsonrpc , method , params } = obj;
|
|
32
|
+
assertIsRequestId(id);
|
|
33
|
+
assertIsJSONRPC2OrUndefined(jsonrpc);
|
|
34
|
+
if (method === 'subscription.stop') {
|
|
35
|
+
return {
|
|
36
|
+
id,
|
|
37
|
+
jsonrpc,
|
|
38
|
+
method
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
assertIsProcedureType(method);
|
|
42
|
+
assertIsObject(params);
|
|
43
|
+
const { input: rawInput , path } = params;
|
|
44
|
+
assertIsString(path);
|
|
45
|
+
const input = transformer.input.deserialize(rawInput);
|
|
46
|
+
return {
|
|
47
|
+
id,
|
|
48
|
+
jsonrpc,
|
|
49
|
+
method,
|
|
50
|
+
params: {
|
|
51
|
+
input,
|
|
52
|
+
path
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { parseTRPCMessage };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Simplify, WithoutIndexSignature } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* @link https://github.com/remix-run/remix/blob/2248669ed59fd716e267ea41df5d665d4781f4a9/packages/remix-server-runtime/serialize.ts
|
|
4
|
+
*/
|
|
5
|
+
type JsonPrimitive = boolean | number | string | null;
|
|
6
|
+
type NonJsonPrimitive = Function | symbol | undefined;
|
|
7
|
+
type IsAny<T> = 0 extends T & 1 ? true : false;
|
|
8
|
+
type JsonReturnable = JsonPrimitive | undefined;
|
|
9
|
+
type IsRecord<T extends object> = keyof WithoutIndexSignature<T> extends never ? true : false;
|
|
10
|
+
export type Serialize<T> = IsAny<T> extends true ? any : unknown extends T ? unknown : T extends JsonReturnable ? T : T extends Map<any, any> | Set<any> ? object : T extends NonJsonPrimitive ? never : T extends {
|
|
11
|
+
toJSON(): infer U;
|
|
12
|
+
} ? U : T extends [] ? [] : T extends [unknown, ...unknown[]] ? SerializeTuple<T> : T extends readonly (infer U)[] ? (U extends NonJsonPrimitive ? null : Serialize<U>)[] : T extends object ? IsRecord<T> extends true ? Record<keyof T, Serialize<T[keyof T]>> : Simplify<SerializeObject<UndefinedToOptional<T>>> : never;
|
|
13
|
+
/** JSON serialize [tuples](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) */
|
|
14
|
+
type SerializeTuple<T extends [unknown, ...unknown[]]> = {
|
|
15
|
+
[K in keyof T]: T[K] extends NonJsonPrimitive ? null : Serialize<T[K]>;
|
|
16
|
+
};
|
|
17
|
+
type SerializeObjectKey<T extends Record<any, any>, K> = K extends symbol ? never : IsAny<T[K]> extends true ? K : unknown extends T[K] ? K : T[K] extends NonJsonPrimitive ? never : K;
|
|
18
|
+
/**
|
|
19
|
+
* JSON serialize objects (not including arrays) and classes
|
|
20
|
+
* @internal
|
|
21
|
+
**/
|
|
22
|
+
export type SerializeObject<T extends object> = {
|
|
23
|
+
[K in keyof T as SerializeObjectKey<T, K>]: Serialize<T[K]>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Extract keys from T where the value dosen't extend undefined
|
|
27
|
+
* Note: Can't parse IndexSignature or Record types
|
|
28
|
+
*/
|
|
29
|
+
type FilterDefinedKeys<T extends object> = Exclude<{
|
|
30
|
+
[K in keyof T]: undefined extends T[K] ? never : K;
|
|
31
|
+
}[keyof T], undefined>;
|
|
32
|
+
/**
|
|
33
|
+
* Get value of exactOptionalPropertyTypes config
|
|
34
|
+
*/
|
|
35
|
+
type ExactOptionalPropertyTypes = {
|
|
36
|
+
a?: 0 | undefined;
|
|
37
|
+
} extends {
|
|
38
|
+
a?: 0;
|
|
39
|
+
} ? false : true;
|
|
40
|
+
/**
|
|
41
|
+
* Check if T has an index signature
|
|
42
|
+
*/
|
|
43
|
+
type HasIndexSignature<T extends object> = string extends keyof T ? true : false;
|
|
44
|
+
/**
|
|
45
|
+
* { [key: string]: number | undefined } --> { [key: string]: number }
|
|
46
|
+
*/
|
|
47
|
+
type HandleIndexSignature<T extends object> = {
|
|
48
|
+
[K in keyof Omit<T, keyof WithoutIndexSignature<T>>]: Exclude<T[K], undefined>;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* { a: number | undefined } --> { a?: number }
|
|
52
|
+
* Note: Can't parse IndexSignature or Record types
|
|
53
|
+
*/
|
|
54
|
+
type HandleUndefined<T extends object> = {
|
|
55
|
+
[K in keyof Omit<T, FilterDefinedKeys<T>>]?: Exclude<T[K], undefined>;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Handle undefined, index signature and records
|
|
59
|
+
*/
|
|
60
|
+
type UndefinedToOptional<T extends object> = Pick<WithoutIndexSignature<T>, FilterDefinedKeys<WithoutIndexSignature<T>>> & (ExactOptionalPropertyTypes extends true ? HandleIndexSignature<T> & HandleUndefined<WithoutIndexSignature<T>> : HasIndexSignature<T> extends true ? HandleIndexSignature<T> : HandleUndefined<T>);
|
|
61
|
+
export {};
|
|
62
|
+
//# sourceMappingURL=serialize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../../src/unstable-core-do-not-import/serialize.ts"],"names":[],"mappings":"A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AE,SAAS;IAC9D,CAAC,CAAC,EAAE,CAAC,CAAC;CACP,GACG,KAAK,GACL,IAAI,CAAC;AAET;;GAEG;AACH,KAAK,iBAAiB,CAAC,CAAC,SAAS,MAAM,IAAI,MAAM,SAAS,MAAM,CAAC,GAC7D,IAAI,GACJ,KAAK,CAAC;AAEV;;GAEG;AACH,KAAK,oBAAoB,CAAC,CAAC,SAAS,MAAM,IAAI;KAC3C,CAAC,IAAI,MAAM,IAAI,CAAC,CAAC,EAAE,MAAM,qBAAqB,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAC3D,CAAC,CAAC,CAAC,CAAC,EACJ,SAAS,CACV;CACF,CAAC;AAEF;;;GAGG;AACH,KAAK,eAAe,CAAC,CAAC,SAAS,MAAM,IAAI;KACtC,CAAC,IAAI,MAAM,IAAI,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC;CACtE,CAAC;AAEF;;GAEG;AACH,KAAK,mBAAmB,CAAC,CAAC,SAAS,MAAM,IAEvC,IAAI,CAAC,qBAAqB,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC,GAEzE,CAAC,0BAA0B,SAAS,IAAI,GACpC,oBAAoB,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,GACnE,iBAAiB,CAAC,CAAC,CAAC,SAAS,IAAI,GACjC,oBAAoB,CAAC,CAAC,CAAC,GACvB,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { AnyRootConfig } from './rootConfig';
|
|
2
|
+
import type { AnyRouter, inferRouterError } from './router';
|
|
3
|
+
import type { TRPCResponse, TRPCResponseMessage, TRPCResultMessage } from './rpc';
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export interface DataTransformer {
|
|
8
|
+
serialize: (object: any) => any;
|
|
9
|
+
deserialize: (object: any) => any;
|
|
10
|
+
}
|
|
11
|
+
interface InputDataTransformer extends DataTransformer {
|
|
12
|
+
/**
|
|
13
|
+
* This function runs **on the client** before sending the data to the server.
|
|
14
|
+
*/
|
|
15
|
+
serialize: (object: any) => any;
|
|
16
|
+
/**
|
|
17
|
+
* This function runs **on the server** to transform the data before it is passed to the resolver
|
|
18
|
+
*/
|
|
19
|
+
deserialize: (object: any) => any;
|
|
20
|
+
}
|
|
21
|
+
interface OutputDataTransformer extends DataTransformer {
|
|
22
|
+
/**
|
|
23
|
+
* This function runs **on the server** before sending the data to the client.
|
|
24
|
+
*/
|
|
25
|
+
serialize: (object: any) => any;
|
|
26
|
+
/**
|
|
27
|
+
* This function runs **only on the client** to transform the data sent from the server.
|
|
28
|
+
*/
|
|
29
|
+
deserialize: (object: any) => any;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export interface CombinedDataTransformer {
|
|
35
|
+
/**
|
|
36
|
+
* Specify how the data sent from the client to the server should be transformed.
|
|
37
|
+
*/
|
|
38
|
+
input: InputDataTransformer;
|
|
39
|
+
/**
|
|
40
|
+
* Specify how the data sent from the server to the client should be transformed.
|
|
41
|
+
*/
|
|
42
|
+
output: OutputDataTransformer;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
export type CombinedDataTransformerClient = {
|
|
48
|
+
input: Pick<CombinedDataTransformer['input'], 'serialize'>;
|
|
49
|
+
output: Pick<CombinedDataTransformer['output'], 'deserialize'>;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
export type DataTransformerOptions = CombinedDataTransformer | DataTransformer;
|
|
55
|
+
/**
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
export declare function getDataTransformer(transformer: DataTransformerOptions): CombinedDataTransformer;
|
|
59
|
+
/**
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
export type DefaultDataTransformer = CombinedDataTransformer & {
|
|
63
|
+
_default: true;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
export declare const defaultTransformer: DefaultDataTransformer;
|
|
69
|
+
/**
|
|
70
|
+
* Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
|
|
71
|
+
**/
|
|
72
|
+
export declare function transformTRPCResponse<TResponse extends TRPCResponse | TRPCResponse[] | TRPCResponseMessage | TRPCResponseMessage[]>(config: AnyRootConfig, itemOrItems: TResponse): import("./rpc").TRPCErrorResponse<import("./rpc").TRPCErrorShape<import("./rpc").TRPC_ERROR_CODE_NUMBER, Record<string, unknown>>> | import("./rpc").TRPCSuccessResponse<unknown> | ({
|
|
73
|
+
id: import("./rpc").JSONRPC2.RequestId;
|
|
74
|
+
} & TRPCResultMessage<unknown>) | (TRPCResponse | TRPCResponseMessage)[];
|
|
75
|
+
/** @internal */
|
|
76
|
+
declare function transformResultInner<TRouter extends AnyRouter, TOutput>(response: TRPCResponse<TOutput, inferRouterError<TRouter>> | TRPCResponseMessage<TOutput, inferRouterError<TRouter>>, transformer: DataTransformer): {
|
|
77
|
+
readonly ok: false;
|
|
78
|
+
readonly error: {
|
|
79
|
+
readonly error: inferRouterError<TRouter>;
|
|
80
|
+
readonly id?: import("./rpc").JSONRPC2.RequestId | undefined;
|
|
81
|
+
readonly jsonrpc?: "2.0" | undefined;
|
|
82
|
+
} | {
|
|
83
|
+
readonly error: inferRouterError<TRouter>;
|
|
84
|
+
readonly id: string | number | null;
|
|
85
|
+
readonly jsonrpc?: "2.0" | undefined;
|
|
86
|
+
};
|
|
87
|
+
readonly result?: undefined;
|
|
88
|
+
} | {
|
|
89
|
+
readonly ok: true;
|
|
90
|
+
readonly result: {
|
|
91
|
+
type: "started";
|
|
92
|
+
data?: undefined;
|
|
93
|
+
} | {
|
|
94
|
+
type: "stopped";
|
|
95
|
+
data?: undefined;
|
|
96
|
+
} | (import("./rpc").TRPCResult<TOutput> & {
|
|
97
|
+
type: "data";
|
|
98
|
+
});
|
|
99
|
+
readonly error?: undefined;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Transforms and validates that the result is a valid TRPCResponse
|
|
103
|
+
* @internal
|
|
104
|
+
*/
|
|
105
|
+
export declare function transformResult<TRouter extends AnyRouter, TOutput>(response: TRPCResponse<TOutput, inferRouterError<TRouter>> | TRPCResponseMessage<TOutput, inferRouterError<TRouter>>, transformer: DataTransformer): ReturnType<typeof transformResultInner>;
|
|
106
|
+
export {};
|
|
107
|
+
//# sourceMappingURL=transformer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../../src/unstable-core-do-not-import/transformer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,KAAK,EACV,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EAClB,MAAM,OAAO,CAAC;AAGf;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,GAAG,CAAC;IAChC,WAAW,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,GAAG,CAAC;CACnC;AAED,UAAU,oBAAqB,SAAQ,eAAe;IACpD;;OAEG;IACH,SAAS,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,GAAG,CAAC;IAChC;;OAEG;IACH,WAAW,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,GAAG,CAAC;CACnC;AAED,UAAU,qBAAsB,SAAQ,eAAe;IACrD;;OAEG;IACH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;;;;;;;;EAuB7B;AAQD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,EAChE,QAAQ,EACJ,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,GAChD,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAC3D,WAAW,EAAE,eAAe,GAC3B,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAqBzC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('./utils.js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/ function getDataTransformer(transformer) {
|
|
8
|
+
if ('input' in transformer) {
|
|
9
|
+
return transformer;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
input: transformer,
|
|
13
|
+
output: transformer
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
*/ const defaultTransformer = {
|
|
19
|
+
_default: true,
|
|
20
|
+
input: {
|
|
21
|
+
serialize: (obj)=>obj,
|
|
22
|
+
deserialize: (obj)=>obj
|
|
23
|
+
},
|
|
24
|
+
output: {
|
|
25
|
+
serialize: (obj)=>obj,
|
|
26
|
+
deserialize: (obj)=>obj
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
function transformTRPCResponseItem(config, item) {
|
|
30
|
+
if ('error' in item) {
|
|
31
|
+
return {
|
|
32
|
+
...item,
|
|
33
|
+
error: config.transformer.output.serialize(item.error)
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if ('data' in item.result) {
|
|
37
|
+
return {
|
|
38
|
+
...item,
|
|
39
|
+
result: {
|
|
40
|
+
...item.result,
|
|
41
|
+
data: config.transformer.output.serialize(item.result.data)
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return item;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
|
|
49
|
+
**/ function transformTRPCResponse(config, itemOrItems) {
|
|
50
|
+
return Array.isArray(itemOrItems) ? itemOrItems.map((item)=>transformTRPCResponseItem(config, item)) : transformTRPCResponseItem(config, itemOrItems);
|
|
51
|
+
}
|
|
52
|
+
// FIXME:
|
|
53
|
+
// - the generics here are probably unnecessary
|
|
54
|
+
// - the RPC-spec could probably be simplified to combine HTTP + WS
|
|
55
|
+
/** @internal */ function transformResultInner(response, transformer) {
|
|
56
|
+
if ('error' in response) {
|
|
57
|
+
const error = transformer.deserialize(response.error);
|
|
58
|
+
return {
|
|
59
|
+
ok: false,
|
|
60
|
+
error: {
|
|
61
|
+
...response,
|
|
62
|
+
error
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const result = {
|
|
67
|
+
...response.result,
|
|
68
|
+
...(!response.result.type || response.result.type === 'data') && {
|
|
69
|
+
type: 'data',
|
|
70
|
+
data: transformer.deserialize(response.result.data)
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
ok: true,
|
|
75
|
+
result
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
class TransformResultError extends Error {
|
|
79
|
+
constructor(){
|
|
80
|
+
super('Unable to transform response from server');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Transforms and validates that the result is a valid TRPCResponse
|
|
85
|
+
* @internal
|
|
86
|
+
*/ function transformResult(response, transformer) {
|
|
87
|
+
let result;
|
|
88
|
+
try {
|
|
89
|
+
// Use the data transformers on the JSON-response
|
|
90
|
+
result = transformResultInner(response, transformer);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
throw new TransformResultError();
|
|
93
|
+
}
|
|
94
|
+
// check that output of the transformers is a valid TRPCResponse
|
|
95
|
+
if (!result.ok && (!utils.isObject(result.error.error) || typeof result.error.error['code'] !== 'number')) {
|
|
96
|
+
throw new TransformResultError();
|
|
97
|
+
}
|
|
98
|
+
if (result.ok && !utils.isObject(result.result)) {
|
|
99
|
+
throw new TransformResultError();
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
exports.defaultTransformer = defaultTransformer;
|
|
105
|
+
exports.getDataTransformer = getDataTransformer;
|
|
106
|
+
exports.transformResult = transformResult;
|
|
107
|
+
exports.transformTRPCResponse = transformTRPCResponse;
|