@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,206 @@
|
|
|
1
|
+
import { createFlatProxy } from './createProxy';
|
|
2
|
+
import {
|
|
3
|
+
defaultFormatter,
|
|
4
|
+
type DefaultErrorShape,
|
|
5
|
+
type ErrorFormatter,
|
|
6
|
+
type ErrorFormatterShape,
|
|
7
|
+
} from './error/formatter';
|
|
8
|
+
import { createMiddlewareFactory } from './middleware';
|
|
9
|
+
import { createBuilder } from './procedureBuilder';
|
|
10
|
+
import {
|
|
11
|
+
isServerDefault,
|
|
12
|
+
type CreateRootConfigTypes,
|
|
13
|
+
type RootConfig,
|
|
14
|
+
type RootConfigTypes,
|
|
15
|
+
type RuntimeConfig,
|
|
16
|
+
} from './rootConfig';
|
|
17
|
+
import {
|
|
18
|
+
createCallerFactory,
|
|
19
|
+
createRouterFactory,
|
|
20
|
+
mergeRouters,
|
|
21
|
+
} from './router';
|
|
22
|
+
import {
|
|
23
|
+
defaultTransformer,
|
|
24
|
+
getDataTransformer,
|
|
25
|
+
type DataTransformerOptions,
|
|
26
|
+
type DefaultDataTransformer,
|
|
27
|
+
} from './transformer';
|
|
28
|
+
import type {
|
|
29
|
+
Overwrite,
|
|
30
|
+
PickFirstDefined,
|
|
31
|
+
Unwrap,
|
|
32
|
+
ValidateShape,
|
|
33
|
+
} from './types';
|
|
34
|
+
|
|
35
|
+
type PartialRootConfigTypes = Partial<RootConfigTypes>;
|
|
36
|
+
|
|
37
|
+
type CreateRootConfigTypesFromPartial<TTypes extends PartialRootConfigTypes> =
|
|
38
|
+
CreateRootConfigTypes<{
|
|
39
|
+
ctx: TTypes['ctx'] extends RootConfigTypes['ctx'] ? TTypes['ctx'] : object;
|
|
40
|
+
meta: TTypes['meta'] extends RootConfigTypes['meta']
|
|
41
|
+
? TTypes['meta']
|
|
42
|
+
: object;
|
|
43
|
+
errorShape: TTypes['errorShape'];
|
|
44
|
+
transformer: DataTransformerOptions;
|
|
45
|
+
}>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* TODO: This can be improved:
|
|
49
|
+
* - We should be able to chain `.meta()`/`.context()` only once
|
|
50
|
+
* - Simplify typings
|
|
51
|
+
* - Doesn't need to be a class but it doesn't really hurt either
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
class TRPCBuilder<TParams extends PartialRootConfigTypes = object> {
|
|
55
|
+
/**
|
|
56
|
+
* Add a context shape as a generic to the root object
|
|
57
|
+
* @link https://trpc.io/docs/v11/server/context
|
|
58
|
+
*/
|
|
59
|
+
context<
|
|
60
|
+
TNewContext extends
|
|
61
|
+
| RootConfigTypes['ctx']
|
|
62
|
+
| ((...args: unknown[]) => RootConfigTypes['ctx']),
|
|
63
|
+
>() {
|
|
64
|
+
type NextParams = Overwrite<TParams, { ctx: Unwrap<TNewContext> }>;
|
|
65
|
+
|
|
66
|
+
return new TRPCBuilder<NextParams>();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Add a meta shape as a generic to the root object
|
|
71
|
+
* @link https://trpc.io/docs/v11/quickstart
|
|
72
|
+
*/
|
|
73
|
+
meta<TNewMeta extends RootConfigTypes['meta']>() {
|
|
74
|
+
type NextParams = Overwrite<TParams, { meta: TNewMeta }>;
|
|
75
|
+
|
|
76
|
+
return new TRPCBuilder<NextParams>();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Create the root object
|
|
81
|
+
* @link https://trpc.io/docs/v11/server/routers#initialize-trpc
|
|
82
|
+
*/
|
|
83
|
+
create<
|
|
84
|
+
TOptions extends Partial<
|
|
85
|
+
RuntimeConfig<CreateRootConfigTypesFromPartial<TParams>>
|
|
86
|
+
>,
|
|
87
|
+
>(
|
|
88
|
+
options?:
|
|
89
|
+
| ValidateShape<
|
|
90
|
+
TOptions,
|
|
91
|
+
Partial<RuntimeConfig<CreateRootConfigTypesFromPartial<TParams>>>
|
|
92
|
+
>
|
|
93
|
+
| undefined,
|
|
94
|
+
) {
|
|
95
|
+
return createTRPCInner<TParams>()<TOptions>(options);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Builder to initialize the tRPC root object - use this exactly once per backend
|
|
101
|
+
* @link https://trpc.io/docs/v11/quickstart
|
|
102
|
+
*/
|
|
103
|
+
export const initTRPC = new TRPCBuilder();
|
|
104
|
+
|
|
105
|
+
function createTRPCInner<TParams extends PartialRootConfigTypes>() {
|
|
106
|
+
type $Generics = CreateRootConfigTypesFromPartial<TParams>;
|
|
107
|
+
|
|
108
|
+
type $Context = $Generics['ctx'];
|
|
109
|
+
type $Meta = $Generics['meta'];
|
|
110
|
+
type $Runtime = Partial<RuntimeConfig<$Generics>>;
|
|
111
|
+
|
|
112
|
+
return function initTRPCInner<TOptions extends $Runtime>(
|
|
113
|
+
runtime?: ValidateShape<TOptions, $Runtime>,
|
|
114
|
+
) {
|
|
115
|
+
type $Formatter = PickFirstDefined<
|
|
116
|
+
TOptions['errorFormatter'],
|
|
117
|
+
ErrorFormatter<$Context, DefaultErrorShape>
|
|
118
|
+
>;
|
|
119
|
+
type $Transformer = TOptions['transformer'] extends DataTransformerOptions
|
|
120
|
+
? TOptions['transformer']
|
|
121
|
+
: DefaultDataTransformer;
|
|
122
|
+
type $ErrorShape = ErrorFormatterShape<$Formatter>;
|
|
123
|
+
|
|
124
|
+
type $Config = RootConfig<{
|
|
125
|
+
ctx: $Context;
|
|
126
|
+
meta: $Meta;
|
|
127
|
+
errorShape: $ErrorShape;
|
|
128
|
+
transformer: $Transformer;
|
|
129
|
+
}>;
|
|
130
|
+
|
|
131
|
+
const errorFormatter = runtime?.errorFormatter ?? defaultFormatter;
|
|
132
|
+
const transformer = getDataTransformer(
|
|
133
|
+
runtime?.transformer ?? defaultTransformer,
|
|
134
|
+
) as $Transformer;
|
|
135
|
+
|
|
136
|
+
const config: $Config = {
|
|
137
|
+
transformer,
|
|
138
|
+
isDev:
|
|
139
|
+
runtime?.isDev ??
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
141
|
+
globalThis.process?.env?.['NODE_ENV'] !== 'production',
|
|
142
|
+
allowOutsideOfServer: runtime?.allowOutsideOfServer ?? false,
|
|
143
|
+
errorFormatter,
|
|
144
|
+
isServer: runtime?.isServer ?? isServerDefault,
|
|
145
|
+
/**
|
|
146
|
+
* @internal
|
|
147
|
+
*/
|
|
148
|
+
$types: createFlatProxy((key) => {
|
|
149
|
+
throw new Error(
|
|
150
|
+
`Tried to access "$types.${key}" which is not available at runtime`,
|
|
151
|
+
);
|
|
152
|
+
}),
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
{
|
|
156
|
+
// Server check
|
|
157
|
+
const isServer: boolean = runtime?.isServer ?? isServerDefault;
|
|
158
|
+
|
|
159
|
+
if (!isServer && runtime?.allowOutsideOfServer !== true) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
`You're trying to use @trpc/server in a non-server environment. This is not supported by default.`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
/**
|
|
167
|
+
* These are just types, they can't be used
|
|
168
|
+
* @internal
|
|
169
|
+
*/
|
|
170
|
+
_config: config,
|
|
171
|
+
/**
|
|
172
|
+
* Builder object for creating procedures
|
|
173
|
+
* @link https://trpc.io/docs/v11/server/procedures
|
|
174
|
+
*/
|
|
175
|
+
procedure: createBuilder<
|
|
176
|
+
$Config['$types']['ctx'],
|
|
177
|
+
$Config['$types']['meta']
|
|
178
|
+
>({
|
|
179
|
+
meta: runtime?.defaultMeta,
|
|
180
|
+
}),
|
|
181
|
+
/**
|
|
182
|
+
* Create reusable middlewares
|
|
183
|
+
* @link https://trpc.io/docs/v11/server/middlewares
|
|
184
|
+
*/
|
|
185
|
+
middleware: createMiddlewareFactory<
|
|
186
|
+
$Config['$types']['ctx'],
|
|
187
|
+
$Config['$types']['meta']
|
|
188
|
+
>(),
|
|
189
|
+
/**
|
|
190
|
+
* Create a router
|
|
191
|
+
* @link https://trpc.io/docs/v11/server/routers
|
|
192
|
+
*/
|
|
193
|
+
router: createRouterFactory<$Config>(config),
|
|
194
|
+
/**
|
|
195
|
+
* Merge Routers
|
|
196
|
+
* @link https://trpc.io/docs/v11/server/merging-routers
|
|
197
|
+
*/
|
|
198
|
+
mergeRouters,
|
|
199
|
+
/**
|
|
200
|
+
* Create a server-side caller for a router
|
|
201
|
+
* @link https://trpc.io/docs/v11/server/server-side-calls
|
|
202
|
+
*/
|
|
203
|
+
createCallerFactory: createCallerFactory<$Config>(),
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { TRPCError } from './error/TRPCError';
|
|
2
|
+
import type { ParseFn } from './parser';
|
|
3
|
+
import type { ProcedureType } from './procedure';
|
|
4
|
+
import type { GetRawInputFn, Overwrite, Simplify } from './types';
|
|
5
|
+
import { isObject } from './utils';
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const middlewareMarker = 'middlewareMarker' as 'middlewareMarker' & {
|
|
9
|
+
__brand: 'middlewareMarker';
|
|
10
|
+
};
|
|
11
|
+
type MiddlewareMarker = typeof middlewareMarker;
|
|
12
|
+
|
|
13
|
+
interface MiddlewareResultBase {
|
|
14
|
+
/**
|
|
15
|
+
* All middlewares should pass through their `next()`'s output.
|
|
16
|
+
* Requiring this marker makes sure that can't be forgotten at compile-time.
|
|
17
|
+
*/
|
|
18
|
+
readonly marker: MiddlewareMarker;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface MiddlewareOKResult<_TContextOverride> extends MiddlewareResultBase {
|
|
22
|
+
ok: true;
|
|
23
|
+
data: unknown;
|
|
24
|
+
// this could be extended with `input`/`rawInput` later
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface MiddlewareErrorResult<_TContextOverride>
|
|
28
|
+
extends MiddlewareResultBase {
|
|
29
|
+
ok: false;
|
|
30
|
+
error: TRPCError;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
export type MiddlewareResult<_TContextOverride> =
|
|
37
|
+
| MiddlewareErrorResult<_TContextOverride>
|
|
38
|
+
| MiddlewareOKResult<_TContextOverride>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export interface MiddlewareBuilder<
|
|
44
|
+
TContext,
|
|
45
|
+
TMeta,
|
|
46
|
+
TContextOverrides,
|
|
47
|
+
TInputIn,
|
|
48
|
+
> {
|
|
49
|
+
/**
|
|
50
|
+
* Create a new builder based on the current middleware builder
|
|
51
|
+
*/
|
|
52
|
+
unstable_pipe<$ContextOverridesOut>(
|
|
53
|
+
fn:
|
|
54
|
+
| MiddlewareFunction<
|
|
55
|
+
TContext,
|
|
56
|
+
TMeta,
|
|
57
|
+
TContextOverrides,
|
|
58
|
+
$ContextOverridesOut,
|
|
59
|
+
TInputIn
|
|
60
|
+
>
|
|
61
|
+
| MiddlewareBuilder<
|
|
62
|
+
Overwrite<TContext, TContextOverrides>,
|
|
63
|
+
TMeta,
|
|
64
|
+
$ContextOverridesOut,
|
|
65
|
+
TInputIn
|
|
66
|
+
>,
|
|
67
|
+
): MiddlewareBuilder<
|
|
68
|
+
TContext,
|
|
69
|
+
TMeta,
|
|
70
|
+
Overwrite<TContextOverrides, $ContextOverridesOut>,
|
|
71
|
+
TInputIn
|
|
72
|
+
>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* List of middlewares within this middleware builder
|
|
76
|
+
*/
|
|
77
|
+
_middlewares: MiddlewareFunction<
|
|
78
|
+
TContext,
|
|
79
|
+
TMeta,
|
|
80
|
+
TContextOverrides,
|
|
81
|
+
object,
|
|
82
|
+
TInputIn
|
|
83
|
+
>[];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
export type MiddlewareFunction<
|
|
90
|
+
TContext,
|
|
91
|
+
TMeta,
|
|
92
|
+
TContextOverridesIn,
|
|
93
|
+
$ContextOverridesOut,
|
|
94
|
+
TInputIn,
|
|
95
|
+
> = {
|
|
96
|
+
(opts: {
|
|
97
|
+
ctx: Simplify<Overwrite<TContext, TContextOverridesIn>>;
|
|
98
|
+
type: ProcedureType;
|
|
99
|
+
path: string;
|
|
100
|
+
input: TInputIn;
|
|
101
|
+
getRawInput: GetRawInputFn;
|
|
102
|
+
meta: TMeta | undefined;
|
|
103
|
+
next: {
|
|
104
|
+
(): Promise<MiddlewareResult<TContextOverridesIn>>;
|
|
105
|
+
<$ContextOverride>(opts: {
|
|
106
|
+
ctx?: $ContextOverride;
|
|
107
|
+
input?: unknown;
|
|
108
|
+
}): Promise<MiddlewareResult<$ContextOverride>>;
|
|
109
|
+
(opts: { getRawInput: GetRawInputFn }): Promise<
|
|
110
|
+
MiddlewareResult<TContextOverridesIn>
|
|
111
|
+
>;
|
|
112
|
+
};
|
|
113
|
+
}): Promise<MiddlewareResult<$ContextOverridesOut>>;
|
|
114
|
+
_type?: string | undefined;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export type AnyMiddlewareFunction = MiddlewareFunction<any, any, any, any, any>;
|
|
118
|
+
export type AnyMiddlewareBuilder = MiddlewareBuilder<any, any, any, any>;
|
|
119
|
+
/**
|
|
120
|
+
* @internal
|
|
121
|
+
*/
|
|
122
|
+
export function createMiddlewareFactory<TContext, TMeta, TInputIn = unknown>() {
|
|
123
|
+
function createMiddlewareInner(
|
|
124
|
+
middlewares: AnyMiddlewareFunction[],
|
|
125
|
+
): AnyMiddlewareBuilder {
|
|
126
|
+
return {
|
|
127
|
+
_middlewares: middlewares,
|
|
128
|
+
unstable_pipe(middlewareBuilderOrFn) {
|
|
129
|
+
const pipedMiddleware =
|
|
130
|
+
'_middlewares' in middlewareBuilderOrFn
|
|
131
|
+
? middlewareBuilderOrFn._middlewares
|
|
132
|
+
: [middlewareBuilderOrFn];
|
|
133
|
+
|
|
134
|
+
return createMiddlewareInner([...middlewares, ...pipedMiddleware]);
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function createMiddleware<$ContextOverrides>(
|
|
140
|
+
fn: MiddlewareFunction<
|
|
141
|
+
TContext,
|
|
142
|
+
TMeta,
|
|
143
|
+
object,
|
|
144
|
+
$ContextOverrides,
|
|
145
|
+
TInputIn
|
|
146
|
+
>,
|
|
147
|
+
): MiddlewareBuilder<TContext, TMeta, $ContextOverrides, TInputIn> {
|
|
148
|
+
return createMiddlewareInner([fn]);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return createMiddleware;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Create a standalone middleware
|
|
156
|
+
* @link https://trpc.io/docs/v11/server/middlewares#experimental-standalone-middlewares
|
|
157
|
+
*/
|
|
158
|
+
export const experimental_standaloneMiddleware = <
|
|
159
|
+
TCtx extends {
|
|
160
|
+
ctx?: object;
|
|
161
|
+
meta?: object;
|
|
162
|
+
input?: unknown;
|
|
163
|
+
},
|
|
164
|
+
>() => ({
|
|
165
|
+
create: createMiddlewareFactory<
|
|
166
|
+
TCtx extends { ctx: infer T extends object } ? T : any,
|
|
167
|
+
TCtx extends { meta: infer T extends object } ? T : object,
|
|
168
|
+
TCtx extends { input: infer T } ? T : unknown
|
|
169
|
+
>(),
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @internal
|
|
174
|
+
* Please note, `trpc-openapi` uses this function.
|
|
175
|
+
*/
|
|
176
|
+
export function createInputMiddleware<TInput>(parse: ParseFn<TInput>) {
|
|
177
|
+
const inputMiddleware: AnyMiddlewareFunction =
|
|
178
|
+
async function inputValidatorMiddleware(opts) {
|
|
179
|
+
let parsedInput: ReturnType<typeof parse>;
|
|
180
|
+
|
|
181
|
+
const rawInput = await opts.getRawInput();
|
|
182
|
+
try {
|
|
183
|
+
parsedInput = await parse(rawInput);
|
|
184
|
+
} catch (cause) {
|
|
185
|
+
throw new TRPCError({
|
|
186
|
+
code: 'BAD_REQUEST',
|
|
187
|
+
cause,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Multiple input parsers
|
|
192
|
+
const combinedInput =
|
|
193
|
+
isObject(opts.input) && isObject(parsedInput)
|
|
194
|
+
? {
|
|
195
|
+
...opts.input,
|
|
196
|
+
...parsedInput,
|
|
197
|
+
}
|
|
198
|
+
: parsedInput;
|
|
199
|
+
|
|
200
|
+
return opts.next({ input: combinedInput });
|
|
201
|
+
};
|
|
202
|
+
inputMiddleware._type = 'input';
|
|
203
|
+
return inputMiddleware;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @internal
|
|
208
|
+
*/
|
|
209
|
+
export function createOutputMiddleware<TOutput>(parse: ParseFn<TOutput>) {
|
|
210
|
+
const outputMiddleware: AnyMiddlewareFunction =
|
|
211
|
+
async function outputValidatorMiddleware({ next }) {
|
|
212
|
+
const result = await next();
|
|
213
|
+
if (!result.ok) {
|
|
214
|
+
// pass through failures without validating
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
const data = await parse(result.data);
|
|
219
|
+
return {
|
|
220
|
+
...result,
|
|
221
|
+
data,
|
|
222
|
+
};
|
|
223
|
+
} catch (cause) {
|
|
224
|
+
throw new TRPCError({
|
|
225
|
+
message: 'Output validation failed',
|
|
226
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
227
|
+
cause,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
outputMiddleware._type = 'output';
|
|
232
|
+
return outputMiddleware;
|
|
233
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// zod / @decs/typeschema
|
|
2
|
+
export type ParserZodEsque<TInput, TParsedInput> = {
|
|
3
|
+
_input: TInput;
|
|
4
|
+
_output: TParsedInput;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type ParserMyZodEsque<TInput> = {
|
|
8
|
+
parse: (input: any) => TInput;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type ParserSuperstructEsque<TInput> = {
|
|
12
|
+
create: (input: unknown) => TInput;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type ParserCustomValidatorEsque<TInput> = (
|
|
16
|
+
input: unknown,
|
|
17
|
+
) => Promise<TInput> | TInput;
|
|
18
|
+
|
|
19
|
+
export type ParserYupEsque<TInput> = {
|
|
20
|
+
validateSync: (input: unknown) => TInput;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type ParserScaleEsque<TInput> = {
|
|
24
|
+
assert(value: unknown): asserts value is TInput;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type ParserWithoutInput<TInput> =
|
|
28
|
+
| ParserCustomValidatorEsque<TInput>
|
|
29
|
+
| ParserMyZodEsque<TInput>
|
|
30
|
+
| ParserScaleEsque<TInput>
|
|
31
|
+
| ParserSuperstructEsque<TInput>
|
|
32
|
+
| ParserYupEsque<TInput>;
|
|
33
|
+
|
|
34
|
+
export type ParserWithInputOutput<TInput, TParsedInput> = ParserZodEsque<
|
|
35
|
+
TInput,
|
|
36
|
+
TParsedInput
|
|
37
|
+
>;
|
|
38
|
+
|
|
39
|
+
export type Parser = ParserWithInputOutput<any, any> | ParserWithoutInput<any>;
|
|
40
|
+
|
|
41
|
+
export type inferParser<TParser extends Parser> =
|
|
42
|
+
TParser extends ParserWithInputOutput<infer $TIn, infer $TOut>
|
|
43
|
+
? {
|
|
44
|
+
in: $TIn;
|
|
45
|
+
out: $TOut;
|
|
46
|
+
}
|
|
47
|
+
: TParser extends ParserWithoutInput<infer $InOut>
|
|
48
|
+
? {
|
|
49
|
+
in: $InOut;
|
|
50
|
+
out: $InOut;
|
|
51
|
+
}
|
|
52
|
+
: never;
|
|
53
|
+
|
|
54
|
+
export type ParseFn<TType> = (value: unknown) => Promise<TType> | TType;
|
|
55
|
+
|
|
56
|
+
export function getParseFn<TType>(procedureParser: Parser): ParseFn<TType> {
|
|
57
|
+
const parser = procedureParser as any;
|
|
58
|
+
|
|
59
|
+
if (typeof parser === 'function') {
|
|
60
|
+
// ParserCustomValidatorEsque
|
|
61
|
+
return parser;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (typeof parser.parseAsync === 'function') {
|
|
65
|
+
// ParserZodEsque
|
|
66
|
+
return parser.parseAsync.bind(parser);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (typeof parser.parse === 'function') {
|
|
70
|
+
// ParserZodEsque
|
|
71
|
+
// ParserValibotEsque (<= v0.12.X)
|
|
72
|
+
return parser.parse.bind(parser);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (typeof parser.validateSync === 'function') {
|
|
76
|
+
// ParserYupEsque
|
|
77
|
+
return parser.validateSync.bind(parser);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (typeof parser.create === 'function') {
|
|
81
|
+
// ParserSuperstructEsque
|
|
82
|
+
return parser.create.bind(parser);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (typeof parser.assert === 'function') {
|
|
86
|
+
// ParserScaleEsque
|
|
87
|
+
return (value) => {
|
|
88
|
+
parser.assert(value);
|
|
89
|
+
return value as TType;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
throw new Error('Could not find a validator fn');
|
|
94
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { inferObservableValue } from '../observable';
|
|
2
|
+
import type { ProcedureCallOptions } from './procedureBuilder';
|
|
3
|
+
import type { Serialize } from './serialize';
|
|
4
|
+
import type { DefaultDataTransformer } from './transformer';
|
|
5
|
+
import type { inferConfig, TRPCInferrable } from './TRPCInferrable';
|
|
6
|
+
|
|
7
|
+
export const procedureTypes = ['query', 'mutation', 'subscription'] as const;
|
|
8
|
+
/**
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export type ProcedureType = (typeof procedureTypes)[number];
|
|
12
|
+
|
|
13
|
+
type ClientContext = Record<string, unknown>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
export interface ProcedureOptions {
|
|
19
|
+
/**
|
|
20
|
+
* Client-side context
|
|
21
|
+
*/
|
|
22
|
+
context?: ClientContext;
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export type ProcedureArgs<TParams extends AnyProcedure['_def']> =
|
|
30
|
+
void extends TParams['_input_in']
|
|
31
|
+
? [input?: undefined | void, opts?: ProcedureOptions]
|
|
32
|
+
: undefined extends TParams['_input_in']
|
|
33
|
+
? [input?: TParams['_input_in'] | void, opts?: ProcedureOptions]
|
|
34
|
+
: [input: TParams['_input_in'], opts?: ProcedureOptions];
|
|
35
|
+
|
|
36
|
+
interface BuiltProcedureDef {
|
|
37
|
+
input: unknown;
|
|
38
|
+
output: unknown;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export interface Procedure<
|
|
45
|
+
TType extends ProcedureType,
|
|
46
|
+
TDef extends BuiltProcedureDef,
|
|
47
|
+
> {
|
|
48
|
+
_def: {
|
|
49
|
+
_input_in: TDef['input'];
|
|
50
|
+
_output_out: TDef['output'];
|
|
51
|
+
procedure: true;
|
|
52
|
+
type: TType;
|
|
53
|
+
/**
|
|
54
|
+
* @internal
|
|
55
|
+
* Meta is not inferrable on individual procedures, only on the router
|
|
56
|
+
*/
|
|
57
|
+
meta: unknown;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
(opts: ProcedureCallOptions): Promise<unknown>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface QueryProcedure<TDef extends BuiltProcedureDef>
|
|
66
|
+
extends Procedure<'query', TDef> {}
|
|
67
|
+
|
|
68
|
+
export interface MutationProcedure<TDef extends BuiltProcedureDef>
|
|
69
|
+
extends Procedure<'mutation', TDef> {}
|
|
70
|
+
|
|
71
|
+
export interface SubscriptionProcedure<TDef extends BuiltProcedureDef>
|
|
72
|
+
extends Procedure<'subscription', TDef> {}
|
|
73
|
+
|
|
74
|
+
export type AnyQueryProcedure = QueryProcedure<any>;
|
|
75
|
+
export type AnyMutationProcedure = MutationProcedure<any>;
|
|
76
|
+
export type AnySubscriptionProcedure = SubscriptionProcedure<any>;
|
|
77
|
+
export type AnyProcedure = Procedure<ProcedureType, any>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
export type inferTransformedProcedureOutput<
|
|
83
|
+
TInferrable extends TRPCInferrable,
|
|
84
|
+
TProcedure extends AnyProcedure,
|
|
85
|
+
> = inferConfig<TInferrable>['transformer'] extends DefaultDataTransformer
|
|
86
|
+
? Serialize<TProcedure['_def']['_output_out']>
|
|
87
|
+
: TProcedure['_def']['_output_out'];
|
|
88
|
+
|
|
89
|
+
/** @internal */
|
|
90
|
+
export type inferTransformedSubscriptionOutput<
|
|
91
|
+
TInferrable extends TRPCInferrable,
|
|
92
|
+
TProcedure extends AnyProcedure,
|
|
93
|
+
> = inferConfig<TInferrable>['transformer'] extends DefaultDataTransformer
|
|
94
|
+
? Serialize<inferObservableValue<TProcedure['_def']['_output_out']>>
|
|
95
|
+
: inferObservableValue<TProcedure['_def']['_output_out']>;
|
|
96
|
+
|
|
97
|
+
export type inferHandlerInput<TProcedure extends AnyProcedure> = ProcedureArgs<
|
|
98
|
+
inferProcedureParams<TProcedure>
|
|
99
|
+
>;
|
|
100
|
+
|
|
101
|
+
export type inferProcedureInput<TProcedure extends AnyProcedure> =
|
|
102
|
+
inferProcedureParams<TProcedure>['_input_in'];
|
|
103
|
+
|
|
104
|
+
export type inferProcedureParams<TProcedure> = TProcedure extends AnyProcedure
|
|
105
|
+
? TProcedure['_def']
|
|
106
|
+
: never;
|
|
107
|
+
export type inferProcedureOutput<TProcedure> =
|
|
108
|
+
inferProcedureParams<TProcedure>['_output_out'];
|