@trpc/server 10.0.0-alpha.20 → 10.0.0-alpha.23
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/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.d.ts +1 -0
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +90 -0
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.js +7 -0
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +90 -0
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +86 -0
- package/adapters/express/dist/trpc-server-adapters-express.cjs.dev.js +4 -4
- package/adapters/express/dist/trpc-server-adapters-express.cjs.prod.js +4 -4
- package/adapters/express/dist/trpc-server-adapters-express.esm.js +4 -4
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +5 -5
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +5 -5
- package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +5 -5
- package/adapters/fastify/index.js +1 -1
- package/adapters/fetch/index.d.ts +1 -0
- package/adapters/fetch/index.js +1 -0
- package/adapters/next/dist/trpc-server-adapters-next.cjs.dev.js +4 -4
- package/adapters/next/dist/trpc-server-adapters-next.cjs.prod.js +4 -4
- package/adapters/next/dist/trpc-server-adapters-next.esm.js +4 -4
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.dev.js +4 -4
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.prod.js +4 -4
- package/adapters/node-http/dist/trpc-server-adapters-node-http.esm.js +4 -4
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.dev.js +4 -4
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js +4 -4
- package/adapters/standalone/dist/trpc-server-adapters-standalone.esm.js +4 -4
- package/adapters/ws/dist/trpc-server-adapters-ws.cjs.dev.js +2 -2
- package/adapters/ws/dist/trpc-server-adapters-ws.cjs.prod.js +2 -2
- package/adapters/ws/dist/trpc-server-adapters-ws.esm.js +2 -2
- package/dist/TRPCError-7971540a.mjs +65 -0
- package/dist/{transformTRPCResponse-43a869de.js → TRPCError-ddec3395.js} +18 -59
- package/dist/TRPCError.d.ts +3 -3
- package/dist/TRPCError.d.ts.map +1 -1
- package/dist/adapters/express.js +5 -5
- package/dist/adapters/express.mjs +5 -5
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +7 -8
- package/dist/adapters/fastify/index.mjs +6 -7
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +8 -0
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -0
- package/dist/adapters/fetch/index.d.ts +3 -0
- package/dist/adapters/fetch/index.d.ts.map +1 -0
- package/dist/adapters/fetch/index.js +69 -0
- package/dist/adapters/fetch/index.mjs +65 -0
- package/dist/adapters/fetch/index.ts.js +1 -0
- package/dist/adapters/fetch/types.d.ts +21 -0
- package/dist/adapters/fetch/types.d.ts.map +1 -0
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +9 -10
- package/dist/adapters/next.mjs +8 -9
- package/dist/adapters/node-http/index.js +5 -5
- package/dist/adapters/node-http/index.mjs +5 -5
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/types.d.ts +9 -7
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +11 -8
- package/dist/adapters/standalone.mjs +11 -7
- package/dist/adapters/ws.d.ts +4 -4
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +117 -80
- package/dist/adapters/ws.mjs +112 -75
- package/dist/assertNotBrowser.d.ts.map +1 -1
- package/dist/{codes-7a3d4f65.mjs → envelopes-1deb2fa7.mjs} +19 -1
- package/dist/{codes-7f46544e.js → envelopes-a953b930.js} +18 -0
- package/dist/http/index.d.ts +1 -1
- package/dist/http/internals/getHTTPStatusCode.d.ts +1 -0
- package/dist/http/internals/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/http/internals/types.d.ts +5 -4
- package/dist/http/internals/types.d.ts.map +1 -1
- package/dist/http/resolveHTTPResponse.d.ts +1 -1
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/http/{ResponseMeta.d.ts → responseMeta.d.ts} +1 -1
- package/dist/http/responseMeta.d.ts.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +45 -54
- package/dist/index.mjs +40 -47
- package/dist/internals/{BaseHandlerOptions.d.ts → baseHandlerOptions.d.ts} +2 -2
- package/dist/internals/{BaseHandlerOptions.d.ts.map → baseHandlerOptions.d.ts.map} +1 -1
- package/dist/internals/callProcedure.d.ts +2 -2
- package/dist/internals/callProcedure.d.ts.map +1 -1
- package/dist/internals/errors.d.ts +1 -0
- package/dist/internals/errors.d.ts.map +1 -1
- package/dist/internals/middlewares.d.ts +5 -2
- package/dist/internals/middlewares.d.ts.map +1 -1
- package/dist/internals/{OnErrorFunction.d.ts → onErrorFunction.d.ts} +1 -1
- package/dist/internals/{OnErrorFunction.d.ts.map → onErrorFunction.d.ts.map} +1 -1
- package/dist/internals/procedure.d.ts +20 -14
- package/dist/internals/procedure.d.ts.map +1 -1
- package/dist/internals/transformTRPCResponse.d.ts +4 -2
- package/dist/internals/transformTRPCResponse.d.ts.map +1 -1
- package/dist/{nodeHTTPRequestHandler-5fccb3bb.mjs → nodeHTTPRequestHandler-1cdb6383.mjs} +2 -3
- package/dist/{nodeHTTPRequestHandler-7d71ac09.js → nodeHTTPRequestHandler-848f07b5.js} +4 -5
- package/dist/observable/index.d.ts +5 -0
- package/dist/observable/index.d.ts.map +1 -0
- package/dist/observable/index.js +204 -0
- package/dist/observable/index.mjs +195 -0
- package/dist/observable/index.ts.js +1 -0
- package/dist/observable/internals/identity.d.ts +2 -0
- package/dist/observable/internals/identity.d.ts.map +1 -0
- package/dist/observable/internals/observableToPromise.d.ts +10 -0
- package/dist/observable/internals/observableToPromise.d.ts.map +1 -0
- package/dist/observable/internals/pipe.d.ts +4 -0
- package/dist/observable/internals/pipe.d.ts.map +1 -0
- package/dist/observable/observable.d.ts +5 -0
- package/dist/observable/observable.d.ts.map +1 -0
- package/dist/observable/operators/index.d.ts +4 -0
- package/dist/observable/operators/index.d.ts.map +1 -0
- package/dist/observable/operators/map.d.ts +3 -0
- package/dist/observable/operators/map.d.ts.map +1 -0
- package/dist/observable/operators/share.d.ts +6 -0
- package/dist/observable/operators/share.d.ts.map +1 -0
- package/dist/observable/operators/tap.d.ts +3 -0
- package/dist/observable/operators/tap.d.ts.map +1 -0
- package/dist/observable/types.d.ts +29 -0
- package/dist/observable/types.d.ts.map +1 -0
- package/dist/observable-137ea1bd.mjs +105 -0
- package/dist/observable-3aa4d911.js +108 -0
- package/dist/{resolveHTTPResponse-3292a84a.mjs → resolveHTTPResponse-54a79e6c.mjs} +18 -23
- package/dist/{resolveHTTPResponse-c3777a47.js → resolveHTTPResponse-ec3dccaa.js} +24 -29
- package/dist/router.d.ts +31 -27
- package/dist/router.d.ts.map +1 -1
- package/dist/rpc/codes.d.ts +2 -0
- package/dist/rpc/codes.d.ts.map +1 -1
- package/dist/rpc/envelopes.d.ts +61 -39
- package/dist/rpc/envelopes.d.ts.map +1 -1
- package/dist/rpc/index.js +7 -3
- package/dist/rpc/index.mjs +1 -1
- package/dist/subscription.d.ts +3 -52
- package/dist/subscription.d.ts.map +1 -1
- package/dist/subscription.js +44 -0
- package/dist/subscription.mjs +40 -0
- package/dist/subscription.ts.js +1 -0
- package/dist/transformTRPCResponse-333bed6c.js +56 -0
- package/dist/transformTRPCResponse-e0cba527.mjs +53 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/observable/index.d.ts +1 -0
- package/observable/index.js +1 -0
- package/package.json +30 -8
- package/rpc/dist/trpc-server-rpc.cjs.dev.js +1 -1
- package/rpc/dist/trpc-server-rpc.cjs.prod.js +1 -1
- package/rpc/dist/trpc-server-rpc.esm.js +1 -1
- package/src/TRPCError.ts +2 -2
- package/src/adapters/fastify/fastifyRequestHandler.ts +0 -1
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
- package/src/adapters/fetch/fetchRequestHandler.ts +61 -0
- package/src/adapters/fetch/index.ts +2 -0
- package/src/adapters/fetch/types.ts +28 -0
- package/src/adapters/next.ts +3 -5
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +0 -1
- package/src/adapters/node-http/types.ts +9 -7
- package/src/adapters/standalone.ts +10 -3
- package/src/adapters/ws.ts +115 -77
- package/src/assertNotBrowser.ts +1 -0
- package/src/http/index.ts +1 -1
- package/src/http/internals/getHTTPStatusCode.ts +1 -0
- package/src/http/internals/types.ts +5 -6
- package/src/http/resolveHTTPResponse.ts +20 -24
- package/src/http/{ResponseMeta.ts → responseMeta.ts} +0 -0
- package/src/index.ts +0 -1
- package/src/internals/{BaseHandlerOptions.ts → baseHandlerOptions.ts} +1 -1
- package/src/internals/callProcedure.ts +6 -3
- package/src/internals/errors.ts +20 -4
- package/src/internals/middlewares.ts +6 -2
- package/src/internals/{OnErrorFunction.ts → onErrorFunction.ts} +0 -0
- package/src/internals/procedure.ts +102 -42
- package/src/internals/transformTRPCResponse.ts +20 -15
- package/src/observable/index.ts +4 -0
- package/src/observable/internals/identity.ts +3 -0
- package/src/observable/internals/observableToPromise.ts +49 -0
- package/src/observable/internals/pipe.ts +22 -0
- package/src/observable/observable.test.ts +101 -0
- package/src/observable/observable.ts +75 -0
- package/src/observable/operators/index.ts +3 -0
- package/src/observable/operators/map.test.ts +92 -0
- package/src/observable/operators/map.ts +25 -0
- package/src/observable/operators/share.test.ts +60 -0
- package/src/observable/operators/share.ts +67 -0
- package/src/observable/operators/tap.ts +26 -0
- package/src/observable/types.ts +69 -0
- package/src/router.ts +96 -37
- package/src/rpc/codes.ts +1 -0
- package/src/rpc/envelopes.ts +87 -68
- package/src/subscription.ts +5 -123
- package/src/types.ts +7 -0
- package/dist/http/ResponseMeta.d.ts.map +0 -1
- package/dist/internals/wrapCallSafe.d.ts +0 -16
- package/dist/internals/wrapCallSafe.d.ts.map +0 -1
- package/dist/subscription-0d8a48ec.mjs +0 -133
- package/dist/subscription-41611bc2.js +0 -136
- package/dist/transformTRPCResponse-ff424ff4.mjs +0 -105
- package/src/internals/wrapCallSafe.ts +0 -19
- package/ws/dist/trpc-server-ws.cjs.d.ts +0 -1
- package/ws/dist/trpc-server-ws.cjs.dev.js +0 -15
- package/ws/dist/trpc-server-ws.cjs.js +0 -7
- package/ws/dist/trpc-server-ws.cjs.prod.js +0 -15
- package/ws/dist/trpc-server-ws.esm.js +0 -11
- package/ws/index.d.ts +0 -1
- package/ws/index.js +0 -1
package/src/router.ts
CHANGED
|
@@ -15,13 +15,13 @@ import {
|
|
|
15
15
|
createProcedure,
|
|
16
16
|
inferProcedureFromOptions,
|
|
17
17
|
} from './internals/procedure';
|
|
18
|
+
import { Observable, inferObservableValue } from './observable';
|
|
18
19
|
import {
|
|
19
20
|
TRPCErrorShape,
|
|
20
21
|
TRPC_ERROR_CODES_BY_KEY,
|
|
21
22
|
TRPC_ERROR_CODE_KEY,
|
|
22
23
|
TRPC_ERROR_CODE_NUMBER,
|
|
23
24
|
} from './rpc';
|
|
24
|
-
import { Subscription } from './subscription';
|
|
25
25
|
import { CombinedDataTransformer, DataTransformerOptions } from './transformer';
|
|
26
26
|
import { Prefixer, ThenArg, flatten } from './types';
|
|
27
27
|
|
|
@@ -38,6 +38,7 @@ export type ProcedureType = 'query' | 'mutation' | 'subscription';
|
|
|
38
38
|
export type ProcedureRecord<
|
|
39
39
|
TInputContext = any,
|
|
40
40
|
TContext = any,
|
|
41
|
+
TMeta = any,
|
|
41
42
|
TInput = any,
|
|
42
43
|
TParsedInput = any,
|
|
43
44
|
TOutput = any,
|
|
@@ -47,6 +48,7 @@ export type ProcedureRecord<
|
|
|
47
48
|
Procedure<
|
|
48
49
|
TInputContext,
|
|
49
50
|
TContext,
|
|
51
|
+
TMeta,
|
|
50
52
|
TInput,
|
|
51
53
|
TParsedInput,
|
|
52
54
|
TOutput,
|
|
@@ -58,8 +60,8 @@ export type ProcedureRecord<
|
|
|
58
60
|
* @public
|
|
59
61
|
*/
|
|
60
62
|
export type inferProcedureInput<
|
|
61
|
-
TProcedure extends Procedure<any, any, any, any, any, any>,
|
|
62
|
-
> = TProcedure extends Procedure<any, any, infer Input, any, any, any>
|
|
63
|
+
TProcedure extends Procedure<any, any, any, any, any, any, any>,
|
|
64
|
+
> = TProcedure extends Procedure<any, any, any, infer Input, any, any, any>
|
|
63
65
|
? undefined extends Input
|
|
64
66
|
? Input | null | void // void is necessary to allow procedures with nullish input to be called without an input
|
|
65
67
|
: Input
|
|
@@ -75,7 +77,7 @@ export type inferAsyncReturnType<TFunction extends (...args: any) => any> =
|
|
|
75
77
|
* @public
|
|
76
78
|
*/
|
|
77
79
|
export type inferProcedureOutput<
|
|
78
|
-
TProcedure extends Procedure<any, any, any, any, any, any>,
|
|
80
|
+
TProcedure extends Procedure<any, any, any, any, any, any, any>,
|
|
79
81
|
> = inferAsyncReturnType<TProcedure['call']>;
|
|
80
82
|
|
|
81
83
|
/**
|
|
@@ -85,10 +87,8 @@ export type inferProcedureOutput<
|
|
|
85
87
|
export type inferSubscriptionOutput<
|
|
86
88
|
TRouter extends AnyRouter,
|
|
87
89
|
TPath extends keyof TRouter['_def']['subscriptions'],
|
|
88
|
-
> =
|
|
89
|
-
inferAsyncReturnType<
|
|
90
|
-
TRouter['_def']['subscriptions'][TPath]['call']
|
|
91
|
-
>['output']
|
|
90
|
+
> = inferObservableValue<
|
|
91
|
+
inferAsyncReturnType<TRouter['_def']['subscriptions'][TPath]['call']>
|
|
92
92
|
>;
|
|
93
93
|
|
|
94
94
|
function getDataTransformer(
|
|
@@ -103,8 +103,8 @@ function getDataTransformer(
|
|
|
103
103
|
* @internal
|
|
104
104
|
*/
|
|
105
105
|
export type inferHandlerInput<
|
|
106
|
-
TProcedure extends Procedure<any, any, any, any, any, any>,
|
|
107
|
-
> = TProcedure extends Procedure<any, any, infer TInput, any, any, any>
|
|
106
|
+
TProcedure extends Procedure<any, any, any, any, any, any, any>,
|
|
107
|
+
> = TProcedure extends Procedure<any, any, any, infer TInput, any, any, any>
|
|
108
108
|
? undefined extends TInput // ? is input optional
|
|
109
109
|
? unknown extends TInput // ? is input unset
|
|
110
110
|
? [(null | undefined)?] // -> there is no input
|
|
@@ -127,6 +127,21 @@ export type inferRouterContext<TRouter extends AnyRouter> = Parameters<
|
|
|
127
127
|
TRouter['createCaller']
|
|
128
128
|
>[0];
|
|
129
129
|
|
|
130
|
+
/**
|
|
131
|
+
* @internal
|
|
132
|
+
*/
|
|
133
|
+
export type inferRouterMeta<TRouter extends AnyRouter> = TRouter extends Router<
|
|
134
|
+
any,
|
|
135
|
+
any,
|
|
136
|
+
infer TMeta,
|
|
137
|
+
any,
|
|
138
|
+
any,
|
|
139
|
+
any,
|
|
140
|
+
any
|
|
141
|
+
>
|
|
142
|
+
? TMeta
|
|
143
|
+
: {};
|
|
144
|
+
|
|
130
145
|
/**
|
|
131
146
|
* @public
|
|
132
147
|
*/
|
|
@@ -136,6 +151,7 @@ export type AnyRouter<TContext = any> = Router<
|
|
|
136
151
|
any,
|
|
137
152
|
any,
|
|
138
153
|
any,
|
|
154
|
+
any,
|
|
139
155
|
any
|
|
140
156
|
>;
|
|
141
157
|
|
|
@@ -214,12 +230,13 @@ const defaultTransformer: CombinedDataTransformer = {
|
|
|
214
230
|
};
|
|
215
231
|
|
|
216
232
|
type SwapProcedureContext<
|
|
217
|
-
TProcedure extends Procedure<any, any, any, any, any, any>,
|
|
233
|
+
TProcedure extends Procedure<any, any, any, any, any, any, any>,
|
|
218
234
|
TNewContext,
|
|
219
235
|
> = TProcedure extends Procedure<
|
|
220
236
|
infer TInputContext,
|
|
221
237
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
222
238
|
infer _TOldContext,
|
|
239
|
+
infer TMeta,
|
|
223
240
|
infer TInput,
|
|
224
241
|
infer TParsedInput,
|
|
225
242
|
infer TOutput,
|
|
@@ -228,6 +245,7 @@ type SwapProcedureContext<
|
|
|
228
245
|
? Procedure<
|
|
229
246
|
TInputContext,
|
|
230
247
|
TNewContext,
|
|
248
|
+
TMeta,
|
|
231
249
|
TInput,
|
|
232
250
|
TParsedInput,
|
|
233
251
|
TOutput,
|
|
@@ -248,13 +266,23 @@ type SwapContext<
|
|
|
248
266
|
export class Router<
|
|
249
267
|
TInputContext,
|
|
250
268
|
TContext,
|
|
251
|
-
|
|
269
|
+
TMeta extends Record<string, any>,
|
|
270
|
+
TQueries extends ProcedureRecord<
|
|
271
|
+
TInputContext,
|
|
272
|
+
TContext,
|
|
273
|
+
any,
|
|
274
|
+
any,
|
|
275
|
+
any,
|
|
276
|
+
any,
|
|
277
|
+
any
|
|
278
|
+
>,
|
|
252
279
|
TMutations extends ProcedureRecord<
|
|
253
280
|
TInputContext,
|
|
254
281
|
TContext,
|
|
255
282
|
any,
|
|
256
283
|
any,
|
|
257
284
|
any,
|
|
285
|
+
any,
|
|
258
286
|
any
|
|
259
287
|
>,
|
|
260
288
|
TSubscriptions extends ProcedureRecord<
|
|
@@ -262,7 +290,8 @@ export class Router<
|
|
|
262
290
|
TContext,
|
|
263
291
|
unknown,
|
|
264
292
|
unknown,
|
|
265
|
-
|
|
293
|
+
Observable<unknown, unknown>,
|
|
294
|
+
unknown,
|
|
266
295
|
unknown
|
|
267
296
|
>,
|
|
268
297
|
TErrorShape extends TRPCErrorShape<number>,
|
|
@@ -271,7 +300,7 @@ export class Router<
|
|
|
271
300
|
queries: TQueries;
|
|
272
301
|
mutations: TMutations;
|
|
273
302
|
subscriptions: TSubscriptions;
|
|
274
|
-
middlewares: MiddlewareFunction<TInputContext, TContext>[];
|
|
303
|
+
middlewares: MiddlewareFunction<TInputContext, TContext, TMeta>[];
|
|
275
304
|
errorFormatter: ErrorFormatter<TContext, TErrorShape>;
|
|
276
305
|
transformer: CombinedDataTransformer;
|
|
277
306
|
};
|
|
@@ -280,7 +309,7 @@ export class Router<
|
|
|
280
309
|
queries?: TQueries;
|
|
281
310
|
mutations?: TMutations;
|
|
282
311
|
subscriptions?: TSubscriptions;
|
|
283
|
-
middlewares?: MiddlewareFunction<TInputContext, TContext>[];
|
|
312
|
+
middlewares?: MiddlewareFunction<TInputContext, TContext, TMeta>[];
|
|
284
313
|
errorFormatter?: ErrorFormatter<TContext, TErrorShape>;
|
|
285
314
|
transformer?: CombinedDataTransformer;
|
|
286
315
|
}) {
|
|
@@ -315,6 +344,7 @@ export class Router<
|
|
|
315
344
|
path: TPath,
|
|
316
345
|
procedure: CreateProcedureWithInputOutputParser<
|
|
317
346
|
TContext,
|
|
347
|
+
TMeta,
|
|
318
348
|
TInput,
|
|
319
349
|
TParsedInput,
|
|
320
350
|
TOutput,
|
|
@@ -323,6 +353,7 @@ export class Router<
|
|
|
323
353
|
): Router<
|
|
324
354
|
TInputContext,
|
|
325
355
|
TContext,
|
|
356
|
+
TMeta,
|
|
326
357
|
TQueries &
|
|
327
358
|
Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
|
|
328
359
|
TMutations,
|
|
@@ -332,10 +363,11 @@ export class Router<
|
|
|
332
363
|
|
|
333
364
|
public query<TPath extends string, TInput, TOutput>(
|
|
334
365
|
path: TPath,
|
|
335
|
-
procedure: CreateProcedureWithInput<TContext, TInput, TOutput>,
|
|
366
|
+
procedure: CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>,
|
|
336
367
|
): Router<
|
|
337
368
|
TInputContext,
|
|
338
369
|
TContext,
|
|
370
|
+
TMeta,
|
|
339
371
|
TQueries &
|
|
340
372
|
Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
|
|
341
373
|
TMutations,
|
|
@@ -345,10 +377,16 @@ export class Router<
|
|
|
345
377
|
|
|
346
378
|
public query<TPath extends string, TOutput, TParsedOutput>(
|
|
347
379
|
path: TPath,
|
|
348
|
-
procedure: CreateProcedureWithoutInput<
|
|
380
|
+
procedure: CreateProcedureWithoutInput<
|
|
381
|
+
TContext,
|
|
382
|
+
TMeta,
|
|
383
|
+
TOutput,
|
|
384
|
+
TParsedOutput
|
|
385
|
+
>,
|
|
349
386
|
): Router<
|
|
350
387
|
TInputContext,
|
|
351
388
|
TContext,
|
|
389
|
+
TMeta,
|
|
352
390
|
TQueries &
|
|
353
391
|
Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
|
|
354
392
|
TMutations,
|
|
@@ -358,9 +396,9 @@ export class Router<
|
|
|
358
396
|
|
|
359
397
|
query(
|
|
360
398
|
path: string,
|
|
361
|
-
procedure: CreateProcedureOptions<TContext, any, any, any, any>,
|
|
399
|
+
procedure: CreateProcedureOptions<TContext, TMeta, any, any, any, any>,
|
|
362
400
|
) {
|
|
363
|
-
const router = new Router<TContext, TContext, any, {}, {}, any>({
|
|
401
|
+
const router = new Router<TContext, TContext, TMeta, any, {}, {}, any>({
|
|
364
402
|
queries: safeObject({
|
|
365
403
|
[path]: createProcedure(procedure),
|
|
366
404
|
}),
|
|
@@ -379,6 +417,7 @@ export class Router<
|
|
|
379
417
|
path: TPath,
|
|
380
418
|
procedure: CreateProcedureWithInputOutputParser<
|
|
381
419
|
TContext,
|
|
420
|
+
TMeta,
|
|
382
421
|
TInput,
|
|
383
422
|
TParsedInput,
|
|
384
423
|
TOutput,
|
|
@@ -387,6 +426,7 @@ export class Router<
|
|
|
387
426
|
): Router<
|
|
388
427
|
TInputContext,
|
|
389
428
|
TContext,
|
|
429
|
+
TMeta,
|
|
390
430
|
TQueries,
|
|
391
431
|
TMutations &
|
|
392
432
|
Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
|
|
@@ -396,10 +436,11 @@ export class Router<
|
|
|
396
436
|
|
|
397
437
|
public mutation<TPath extends string, TInput, TOutput>(
|
|
398
438
|
path: TPath,
|
|
399
|
-
procedure: CreateProcedureWithInput<TContext, TInput, TOutput>,
|
|
439
|
+
procedure: CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>,
|
|
400
440
|
): Router<
|
|
401
441
|
TInputContext,
|
|
402
442
|
TContext,
|
|
443
|
+
TMeta,
|
|
403
444
|
TQueries,
|
|
404
445
|
TMutations &
|
|
405
446
|
Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
|
|
@@ -409,10 +450,16 @@ export class Router<
|
|
|
409
450
|
|
|
410
451
|
public mutation<TPath extends string, TOutput, TParsedOutput>(
|
|
411
452
|
path: TPath,
|
|
412
|
-
procedure: CreateProcedureWithoutInput<
|
|
453
|
+
procedure: CreateProcedureWithoutInput<
|
|
454
|
+
TContext,
|
|
455
|
+
TMeta,
|
|
456
|
+
TOutput,
|
|
457
|
+
TParsedOutput
|
|
458
|
+
>,
|
|
413
459
|
): Router<
|
|
414
460
|
TInputContext,
|
|
415
461
|
TContext,
|
|
462
|
+
TMeta,
|
|
416
463
|
TQueries,
|
|
417
464
|
TMutations &
|
|
418
465
|
Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
|
|
@@ -422,9 +469,9 @@ export class Router<
|
|
|
422
469
|
|
|
423
470
|
public mutation(
|
|
424
471
|
path: string,
|
|
425
|
-
procedure: CreateProcedureOptions<TContext, any, any, any, any>,
|
|
472
|
+
procedure: CreateProcedureOptions<TContext, TMeta, any, any, any, any>,
|
|
426
473
|
) {
|
|
427
|
-
const router = new Router<TContext, TContext, {}, any, {}, any>({
|
|
474
|
+
const router = new Router<TContext, TContext, TMeta, {}, any, {}, any>({
|
|
428
475
|
mutations: safeObject({
|
|
429
476
|
[path]: createProcedure(procedure),
|
|
430
477
|
}),
|
|
@@ -440,12 +487,13 @@ export class Router<
|
|
|
440
487
|
TPath extends string,
|
|
441
488
|
TInput,
|
|
442
489
|
TParsedInput,
|
|
443
|
-
TOutput extends
|
|
490
|
+
TOutput extends Observable<unknown, unknown>,
|
|
444
491
|
>(
|
|
445
492
|
path: TPath,
|
|
446
493
|
procedure: Omit<
|
|
447
494
|
CreateProcedureWithInputOutputParser<
|
|
448
495
|
TContext,
|
|
496
|
+
TMeta,
|
|
449
497
|
TInput,
|
|
450
498
|
TParsedInput,
|
|
451
499
|
TOutput,
|
|
@@ -456,6 +504,7 @@ export class Router<
|
|
|
456
504
|
): Router<
|
|
457
505
|
TInputContext,
|
|
458
506
|
TContext,
|
|
507
|
+
TMeta,
|
|
459
508
|
TQueries,
|
|
460
509
|
TMutations,
|
|
461
510
|
TSubscriptions &
|
|
@@ -469,16 +518,17 @@ export class Router<
|
|
|
469
518
|
public subscription<
|
|
470
519
|
TPath extends string,
|
|
471
520
|
TInput,
|
|
472
|
-
TOutput extends
|
|
521
|
+
TOutput extends Observable<unknown, unknown>,
|
|
473
522
|
>(
|
|
474
523
|
path: TPath,
|
|
475
524
|
procedure: Omit<
|
|
476
|
-
CreateProcedureWithInput<TContext, TInput, TOutput>,
|
|
525
|
+
CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>,
|
|
477
526
|
'output'
|
|
478
527
|
>,
|
|
479
528
|
): Router<
|
|
480
529
|
TInputContext,
|
|
481
530
|
TContext,
|
|
531
|
+
TMeta,
|
|
482
532
|
TQueries,
|
|
483
533
|
TMutations,
|
|
484
534
|
TSubscriptions &
|
|
@@ -491,16 +541,17 @@ export class Router<
|
|
|
491
541
|
*/
|
|
492
542
|
public subscription<
|
|
493
543
|
TPath extends string,
|
|
494
|
-
TOutput extends
|
|
544
|
+
TOutput extends Observable<unknown, unknown>,
|
|
495
545
|
>(
|
|
496
546
|
path: TPath,
|
|
497
547
|
procedure: Omit<
|
|
498
|
-
CreateProcedureWithoutInput<TContext, TOutput, unknown>,
|
|
548
|
+
CreateProcedureWithoutInput<TContext, TMeta, TOutput, unknown>,
|
|
499
549
|
'output'
|
|
500
550
|
>,
|
|
501
551
|
): Router<
|
|
502
552
|
TInputContext,
|
|
503
553
|
TContext,
|
|
554
|
+
TMeta,
|
|
504
555
|
TQueries,
|
|
505
556
|
TMutations,
|
|
506
557
|
TSubscriptions &
|
|
@@ -511,11 +562,11 @@ export class Router<
|
|
|
511
562
|
public subscription(
|
|
512
563
|
path: string,
|
|
513
564
|
procedure: Omit<
|
|
514
|
-
CreateProcedureOptions<TContext, any, any, any, any>,
|
|
565
|
+
CreateProcedureOptions<TContext, TMeta, any, any, any, any>,
|
|
515
566
|
'output'
|
|
516
567
|
>,
|
|
517
568
|
) {
|
|
518
|
-
const router = new Router<TContext, TContext, {}, {}, any, any>({
|
|
569
|
+
const router = new Router<TContext, TContext, TMeta, {}, {}, any, any>({
|
|
519
570
|
subscriptions: safeObject({
|
|
520
571
|
[path]: createProcedure(procedure),
|
|
521
572
|
}),
|
|
@@ -528,11 +579,14 @@ export class Router<
|
|
|
528
579
|
* Merge router with other router
|
|
529
580
|
* @param router
|
|
530
581
|
*/
|
|
531
|
-
public merge<
|
|
582
|
+
public merge<
|
|
583
|
+
TChildRouter extends Router<TContext, any, TMeta, any, any, any, any>,
|
|
584
|
+
>(
|
|
532
585
|
router: TChildRouter,
|
|
533
586
|
): Router<
|
|
534
587
|
TInputContext,
|
|
535
588
|
inferRouterContext<TChildRouter>,
|
|
589
|
+
TMeta,
|
|
536
590
|
TQueries & TChildRouter['_def']['queries'],
|
|
537
591
|
TMutations & TChildRouter['_def']['mutations'],
|
|
538
592
|
TSubscriptions & TChildRouter['_def']['subscriptions'],
|
|
@@ -546,13 +600,14 @@ export class Router<
|
|
|
546
600
|
*/
|
|
547
601
|
public merge<
|
|
548
602
|
TPath extends string,
|
|
549
|
-
TChildRouter extends Router<TContext, any, any, any, any, any>,
|
|
603
|
+
TChildRouter extends Router<TContext, any, TMeta, any, any, any, any>,
|
|
550
604
|
>(
|
|
551
605
|
prefix: TPath,
|
|
552
606
|
router: TChildRouter,
|
|
553
607
|
): Router<
|
|
554
608
|
TInputContext,
|
|
555
609
|
inferRouterContext<TChildRouter>,
|
|
610
|
+
TMeta,
|
|
556
611
|
TQueries & Prefixer<TChildRouter['_def']['queries'], `${TPath}`>,
|
|
557
612
|
TMutations & Prefixer<TChildRouter['_def']['mutations'], `${TPath}`>,
|
|
558
613
|
TSubscriptions &
|
|
@@ -605,7 +660,7 @@ export class Router<
|
|
|
605
660
|
return Router.prefixProcedures(newDefs, prefix);
|
|
606
661
|
};
|
|
607
662
|
|
|
608
|
-
return new Router<TInputContext, any, any, any, any, TErrorShape>({
|
|
663
|
+
return new Router<TInputContext, any, TMeta, any, any, any, TErrorShape>({
|
|
609
664
|
...this._def,
|
|
610
665
|
queries: safeObject(
|
|
611
666
|
this._def.queries,
|
|
@@ -632,7 +687,7 @@ export class Router<
|
|
|
632
687
|
const defTarget = PROCEDURE_DEFINITION_MAP[type];
|
|
633
688
|
const defs = this._def[defTarget];
|
|
634
689
|
const procedure = defs[path] as
|
|
635
|
-
| Procedure<TInputContext, TContext, any, any, any, any>
|
|
690
|
+
| Procedure<TInputContext, TContext, TMeta, any, any, any, any>
|
|
636
691
|
| undefined;
|
|
637
692
|
|
|
638
693
|
if (!procedure) {
|
|
@@ -683,10 +738,11 @@ export class Router<
|
|
|
683
738
|
* @link https://trpc.io/docs/middlewares
|
|
684
739
|
*/
|
|
685
740
|
public middleware<TNewContext>(
|
|
686
|
-
middleware: MiddlewareFunction<TContext, TNewContext>,
|
|
741
|
+
middleware: MiddlewareFunction<TContext, TNewContext, TMeta>,
|
|
687
742
|
): Router<
|
|
688
743
|
TInputContext,
|
|
689
744
|
TNewContext,
|
|
745
|
+
TMeta,
|
|
690
746
|
SwapContext<TQueries, TNewContext>,
|
|
691
747
|
SwapContext<TMutations, TNewContext>,
|
|
692
748
|
SwapContext<TSubscriptions, TNewContext>,
|
|
@@ -713,6 +769,7 @@ export class Router<
|
|
|
713
769
|
return new Router<
|
|
714
770
|
TInputContext,
|
|
715
771
|
TContext,
|
|
772
|
+
TMeta,
|
|
716
773
|
TQueries,
|
|
717
774
|
TMutations,
|
|
718
775
|
TSubscriptions,
|
|
@@ -767,6 +824,7 @@ export class Router<
|
|
|
767
824
|
return new Router<
|
|
768
825
|
TInputContext,
|
|
769
826
|
TContext,
|
|
827
|
+
TMeta,
|
|
770
828
|
TQueries,
|
|
771
829
|
TMutations,
|
|
772
830
|
TSubscriptions,
|
|
@@ -786,6 +844,7 @@ export class Router<
|
|
|
786
844
|
public flat(): Router<
|
|
787
845
|
TInputContext,
|
|
788
846
|
TContext,
|
|
847
|
+
TMeta,
|
|
789
848
|
flatten<{}, TQueries>,
|
|
790
849
|
flatten<{}, TMutations>,
|
|
791
850
|
flatten<{}, TSubscriptions>,
|
|
@@ -795,6 +854,6 @@ export class Router<
|
|
|
795
854
|
}
|
|
796
855
|
}
|
|
797
856
|
|
|
798
|
-
export function router<TContext>() {
|
|
799
|
-
return new Router<TContext, TContext, {}, {}, {}, DefaultErrorShape>();
|
|
857
|
+
export function router<TContext, TMeta extends Record<string, any> = {}>() {
|
|
858
|
+
return new Router<TContext, TContext, TMeta, {}, {}, {}, DefaultErrorShape>();
|
|
800
859
|
}
|
package/src/rpc/codes.ts
CHANGED
|
@@ -28,6 +28,7 @@ export const TRPC_ERROR_CODES_BY_KEY = {
|
|
|
28
28
|
NOT_FOUND: -32004, // 404
|
|
29
29
|
METHOD_NOT_SUPPORTED: -32005, // 405
|
|
30
30
|
TIMEOUT: -32008, // 408
|
|
31
|
+
CONFLICT: -32009, // 409
|
|
31
32
|
PRECONDITION_FAILED: -32012, // 412
|
|
32
33
|
PAYLOAD_TOO_LARGE: -32013, // 413
|
|
33
34
|
CLIENT_CLOSED_REQUEST: -32099, // 499
|
package/src/rpc/envelopes.ts
CHANGED
|
@@ -1,31 +1,10 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
1
2
|
import { ProcedureType } from '../router';
|
|
2
3
|
import { TRPC_ERROR_CODE_NUMBER } from './codes';
|
|
3
4
|
|
|
4
|
-
type JSONRPC2RequestId = number | string | null;
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
|
-
*
|
|
6
|
+
* Error response
|
|
8
7
|
*/
|
|
9
|
-
interface JSONRPC2BaseEnvelope {
|
|
10
|
-
id: JSONRPC2RequestId;
|
|
11
|
-
jsonrpc?: '2.0';
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface JSONRPC2BaseRequest<TMethod extends string = string>
|
|
15
|
-
extends JSONRPC2BaseEnvelope {
|
|
16
|
-
method: TMethod;
|
|
17
|
-
}
|
|
18
|
-
interface JSONRPC2Request<TMethod extends string = string, TParams = unknown>
|
|
19
|
-
extends JSONRPC2BaseRequest<TMethod> {
|
|
20
|
-
params: TParams;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface JSONRPC2ResultResponse<TResult = unknown>
|
|
24
|
-
extends JSONRPC2BaseEnvelope {
|
|
25
|
-
result: TResult;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// inner types
|
|
29
8
|
export interface TRPCErrorShape<
|
|
30
9
|
TCode extends number = TRPC_ERROR_CODE_NUMBER,
|
|
31
10
|
TData extends Record<string, unknown> = Record<string, unknown>,
|
|
@@ -36,74 +15,114 @@ export interface TRPCErrorShape<
|
|
|
36
15
|
}
|
|
37
16
|
|
|
38
17
|
/**
|
|
39
|
-
*
|
|
18
|
+
* JSON-RPC 2.0 Specification
|
|
40
19
|
*/
|
|
41
|
-
export
|
|
42
|
-
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
20
|
+
export namespace JSONRPC2 {
|
|
21
|
+
export type RequestId = number | string | null;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* All requests/responses extends this shape
|
|
25
|
+
*/
|
|
26
|
+
export interface BaseEnvelope {
|
|
27
|
+
id?: RequestId;
|
|
28
|
+
jsonrpc?: '2.0';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface BaseRequest<TMethod extends string = string>
|
|
32
|
+
extends BaseEnvelope {
|
|
33
|
+
method: TMethod;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface Request<TMethod extends string = string, TParams = unknown>
|
|
37
|
+
extends BaseRequest<TMethod> {
|
|
38
|
+
params: TParams;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ResultResponse<TResult = unknown> extends BaseEnvelope {
|
|
42
|
+
result: TResult;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ErrorResponse<TError extends TRPCErrorShape = TRPCErrorShape>
|
|
46
|
+
extends BaseEnvelope {
|
|
47
|
+
error: TError;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
52
50
|
|
|
53
51
|
/**
|
|
54
|
-
*
|
|
52
|
+
* TRPC HTTP Envelopes
|
|
55
53
|
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
54
|
+
|
|
55
|
+
export type TRPCRequest = JSONRPC2.Request<
|
|
56
|
+
ProcedureType,
|
|
57
|
+
{ path: string; input: unknown }
|
|
58
|
+
>;
|
|
59
|
+
|
|
60
|
+
export type TRPCResult<TData = unknown> = { data: TData };
|
|
61
|
+
|
|
62
|
+
export type TRPCErrorResponse<TError extends TRPCErrorShape = TRPCErrorShape> =
|
|
63
|
+
JSONRPC2.ErrorResponse<TError>;
|
|
64
|
+
|
|
65
|
+
export type TRPCResponse<
|
|
66
|
+
TResult = unknown,
|
|
67
|
+
TError extends TRPCErrorShape = TRPCErrorShape,
|
|
68
|
+
> = JSONRPC2.ResultResponse<TRPCResult<TResult>> | TRPCErrorResponse<TError>;
|
|
65
69
|
|
|
66
70
|
/**
|
|
67
|
-
*
|
|
71
|
+
* HTTP WS Envelopes
|
|
68
72
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
|
|
74
|
+
export type TRPCRequestMessage = {
|
|
75
|
+
id: JSONRPC2.RequestId;
|
|
76
|
+
} & TRPCRequest;
|
|
72
77
|
|
|
73
78
|
/**
|
|
74
|
-
*
|
|
79
|
+
* The client asked the server to unsubscribe
|
|
75
80
|
*/
|
|
76
|
-
export type
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
> = TRPCResultResponse<TData> | TRPCErrorResponse<TError>;
|
|
81
|
+
export type TRPCSubscriptionStopNotification = {
|
|
82
|
+
id: null;
|
|
83
|
+
} & JSONRPC2.BaseRequest<'subscription.stop'>;
|
|
80
84
|
|
|
81
85
|
/**
|
|
82
|
-
*
|
|
86
|
+
* The client's outgoing request types
|
|
83
87
|
*/
|
|
84
|
-
export
|
|
88
|
+
export type TRPCClientOutgoingRequest = TRPCSubscriptionStopNotification;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The client's sent messages shape
|
|
92
|
+
*/
|
|
93
|
+
export type TRPCClientOutgoingMessage =
|
|
94
|
+
| TRPCRequestMessage
|
|
95
|
+
| ({ id: JSONRPC2.RequestId } & JSONRPC2.BaseRequest<'subscription.stop'>);
|
|
96
|
+
|
|
97
|
+
export type TRPCResultMessage<TData = unknown> =
|
|
98
|
+
| ({ type: 'data' } & TRPCResult<TData>)
|
|
99
|
+
| { type: 'started' }
|
|
100
|
+
| { type: 'stopped' };
|
|
101
|
+
|
|
102
|
+
export type TRPCResponseMessage<
|
|
103
|
+
TResult = unknown,
|
|
85
104
|
TError extends TRPCErrorShape = TRPCErrorShape,
|
|
86
|
-
>
|
|
87
|
-
|
|
88
|
-
|
|
105
|
+
> = { id: JSONRPC2.RequestId } & (
|
|
106
|
+
| JSONRPC2.ResultResponse<TRPCResultMessage<TResult>>
|
|
107
|
+
| TRPCErrorResponse<TError>
|
|
108
|
+
);
|
|
89
109
|
|
|
90
110
|
/**
|
|
91
111
|
* The server asked the client to reconnect - useful when restarting/redeploying service
|
|
92
112
|
*/
|
|
93
|
-
export
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
113
|
+
export type TRPCReconnectNotification = {
|
|
114
|
+
id: JSONRPC2.RequestId;
|
|
115
|
+
} & JSONRPC2.BaseRequest<'reconnect'>;
|
|
97
116
|
|
|
98
117
|
/**
|
|
99
118
|
* The client's incoming request types
|
|
100
119
|
*/
|
|
101
|
-
export type TRPCClientIncomingRequest =
|
|
102
|
-
TRPCReconnectNotification /* could be extended in future */;
|
|
120
|
+
export type TRPCClientIncomingRequest = TRPCReconnectNotification;
|
|
103
121
|
|
|
104
122
|
/**
|
|
105
123
|
* The client's received messages shape
|
|
106
124
|
*/
|
|
107
|
-
export type TRPCClientIncomingMessage
|
|
108
|
-
|
|
109
|
-
|
|
125
|
+
export type TRPCClientIncomingMessage<
|
|
126
|
+
TResult = unknown,
|
|
127
|
+
TError extends TRPCErrorShape = TRPCErrorShape,
|
|
128
|
+
> = TRPCResponseMessage<TResult, TError> | TRPCClientIncomingRequest;
|