@trpc/server 9.21.0 → 9.23.1-alpha.0
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/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 +4 -4
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +4 -4
- package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +4 -4
- 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/{codes-33cef953.cjs.dev.js → codes-130e62df.cjs.dev.js} +2 -0
- package/dist/{codes-fe07bf82.esm.js → codes-5678cc97.esm.js} +2 -0
- package/dist/{codes-e0df67c4.cjs.prod.js → codes-aff770a3.cjs.prod.js} +2 -0
- package/dist/declarations/src/TRPCError.d.ts +1 -1
- package/dist/declarations/src/TRPCError.d.ts.map +1 -1
- package/dist/declarations/src/adapters/fastify/fastifyRequestHandler.d.ts +1 -1
- package/dist/declarations/src/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/declarations/src/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
- package/dist/declarations/src/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/declarations/src/adapters/next.d.ts.map +1 -1
- package/dist/declarations/src/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
- package/dist/declarations/src/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/declarations/src/adapters/standalone.d.ts.map +1 -1
- package/dist/declarations/src/adapters/ws.d.ts.map +1 -1
- package/dist/declarations/src/deprecated/createHttpServer.d.ts +1 -1
- package/dist/declarations/src/deprecated/createHttpServer.d.ts.map +1 -1
- package/dist/declarations/src/deprecated/requestHandler.d.ts +1 -1
- package/dist/declarations/src/deprecated/requestHandler.d.ts.map +1 -1
- package/dist/declarations/src/http/internals/getHTTPStatusCode.d.ts +2 -1
- package/dist/declarations/src/http/internals/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/declarations/src/http/internals/types.d.ts +1 -1
- package/dist/declarations/src/http/internals/types.d.ts.map +1 -1
- package/dist/declarations/src/http/resolveHTTPResponse.d.ts +2 -2
- package/dist/declarations/src/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/declarations/src/internals/BaseHandlerOptions.d.ts +1 -1
- package/dist/declarations/src/internals/BaseHandlerOptions.d.ts.map +1 -1
- package/dist/declarations/src/internals/OnErrorFunction.d.ts +1 -1
- package/dist/declarations/src/internals/OnErrorFunction.d.ts.map +1 -1
- package/dist/declarations/src/internals/middlewares.d.ts +3 -2
- package/dist/declarations/src/internals/middlewares.d.ts.map +1 -1
- package/dist/declarations/src/internals/procedure.d.ts +20 -14
- package/dist/declarations/src/internals/procedure.d.ts.map +1 -1
- package/dist/declarations/src/router.d.ts +33 -29
- package/dist/declarations/src/router.d.ts.map +1 -1
- package/dist/declarations/src/rpc/codes.d.ts +2 -0
- package/dist/declarations/src/rpc/codes.d.ts.map +1 -1
- package/dist/declarations/src/types.d.ts +8 -0
- package/dist/declarations/src/types.d.ts.map +1 -1
- package/dist/{nodeHTTPRequestHandler-2cc444d8.cjs.dev.js → nodeHTTPRequestHandler-36ddaba2.cjs.dev.js} +2 -2
- package/dist/{nodeHTTPRequestHandler-d00c1126.esm.js → nodeHTTPRequestHandler-372a4a4f.esm.js} +2 -2
- package/dist/{nodeHTTPRequestHandler-8ddacc19.cjs.prod.js → nodeHTTPRequestHandler-91e7ca3d.cjs.prod.js} +2 -2
- package/dist/{resolveHTTPResponse-562c3d62.cjs.prod.js → resolveHTTPResponse-02e61299.cjs.prod.js} +3 -2
- package/dist/{resolveHTTPResponse-3867844c.cjs.dev.js → resolveHTTPResponse-55b2c9bd.cjs.dev.js} +3 -2
- package/dist/{resolveHTTPResponse-85f3f5df.esm.js → resolveHTTPResponse-ddb56677.esm.js} +3 -2
- package/dist/{subscription-a81120d6.esm.js → subscription-2ea469a3.esm.js} +1 -1
- package/dist/{subscription-9e007280.cjs.dev.js → subscription-7de48383.cjs.dev.js} +1 -1
- package/dist/{subscription-12fb50be.cjs.prod.js → subscription-f03bec71.cjs.prod.js} +1 -1
- package/dist/{transformTRPCResponse-8248515e.cjs.dev.js → transformTRPCResponse-1e125b28.cjs.prod.js} +38 -38
- package/dist/{transformTRPCResponse-f73dc4a2.esm.js → transformTRPCResponse-45c225c3.esm.js} +38 -38
- package/dist/{transformTRPCResponse-dcbf66c5.cjs.prod.js → transformTRPCResponse-c654ea92.cjs.dev.js} +38 -38
- package/dist/trpc-server.cjs.dev.js +13 -7
- package/dist/trpc-server.cjs.prod.js +13 -7
- package/dist/trpc-server.esm.js +15 -9
- package/package.json +4 -4
- 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 +1 -1
- package/src/adapters/fastify/fastifyRequestHandler.ts +2 -2
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +3 -3
- package/src/adapters/next.ts +1 -1
- package/src/adapters/node-http/internals/getPostBody.test.ts +2 -1
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +5 -5
- package/src/adapters/standalone.ts +1 -0
- package/src/adapters/ws.ts +2 -2
- package/src/deprecated/createHttpServer.ts +2 -2
- package/src/deprecated/requestHandler.ts +2 -2
- package/src/http/internals/getHTTPStatusCode.ts +2 -1
- package/src/http/internals/types.ts +2 -2
- package/src/http/resolveHTTPResponse.ts +3 -3
- package/src/internals/BaseHandlerOptions.ts +1 -1
- package/src/internals/OnErrorFunction.ts +1 -1
- package/src/internals/middlewares.ts +3 -2
- package/src/internals/procedure.ts +64 -16
- package/src/router.ts +96 -32
- package/src/rpc/codes.ts +1 -0
- package/src/types.ts +7 -0
- package/ws/dist/trpc-server-ws.cjs.dev.js +2 -2
- package/ws/dist/trpc-server-ws.cjs.prod.js +2 -2
- package/ws/dist/trpc-server-ws.esm.js +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AnyRouter, inferRouterContext, ProcedureType } from '../router';
|
|
2
1
|
import { TRPCError } from '../TRPCError';
|
|
2
|
+
import { AnyRouter, ProcedureType, inferRouterContext } from '../router';
|
|
3
3
|
|
|
4
4
|
export type OnErrorFunction<TRouter extends AnyRouter, TRequest> = (opts: {
|
|
5
5
|
error: TRPCError;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ProcedureType } from '../router';
|
|
2
1
|
import { TRPCError } from '../TRPCError';
|
|
2
|
+
import { ProcedureType } from '../router';
|
|
3
3
|
|
|
4
4
|
export const middlewareMarker = Symbol('middlewareMarker');
|
|
5
5
|
interface MiddlewareResultBase<TContext> {
|
|
@@ -27,11 +27,12 @@ export type MiddlewareResult<TContext> =
|
|
|
27
27
|
| MiddlewareOKResult<TContext>
|
|
28
28
|
| MiddlewareErrorResult<TContext>;
|
|
29
29
|
|
|
30
|
-
export type MiddlewareFunction<TInputContext, TContext> = (opts: {
|
|
30
|
+
export type MiddlewareFunction<TInputContext, TContext, TMeta> = (opts: {
|
|
31
31
|
ctx: TInputContext;
|
|
32
32
|
type: ProcedureType;
|
|
33
33
|
path: string;
|
|
34
34
|
rawInput: unknown;
|
|
35
|
+
meta?: TMeta;
|
|
35
36
|
next: {
|
|
36
37
|
(): Promise<MiddlewareResult<TInputContext>>;
|
|
37
38
|
<T>(opts: { ctx: T }): Promise<MiddlewareResult<T>>;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { TRPCError } from '../TRPCError';
|
|
3
3
|
import { assertNotBrowser } from '../assertNotBrowser';
|
|
4
4
|
import { ProcedureType } from '../router';
|
|
5
|
+
import { InferLast } from '../types';
|
|
5
6
|
import { getErrorFromUnknown } from './errors';
|
|
6
7
|
import { MiddlewareFunction, middlewareMarker } from './middlewares';
|
|
7
8
|
import { wrapCallSafe } from './wrapCallSafe';
|
|
@@ -44,11 +45,12 @@ export type ProcedureResolver<TContext, TInput, TOutput> = (opts: {
|
|
|
44
45
|
type: ProcedureType;
|
|
45
46
|
}) => Promise<TOutput> | TOutput;
|
|
46
47
|
|
|
47
|
-
interface ProcedureOptions<TContext, TInput, TOutput, TParsedOutput> {
|
|
48
|
-
middlewares: Array<MiddlewareFunction<any, any>>;
|
|
48
|
+
interface ProcedureOptions<TContext, TMeta, TInput, TOutput, TParsedOutput> {
|
|
49
|
+
middlewares: Array<MiddlewareFunction<any, any, any>>;
|
|
49
50
|
resolver: ProcedureResolver<TContext, TInput, TOutput>;
|
|
50
51
|
inputParser: ProcedureParser<TInput>;
|
|
51
52
|
outputParser: ProcedureParser<TParsedOutput>;
|
|
53
|
+
meta: TMeta;
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
/**
|
|
@@ -100,21 +102,29 @@ function getParseFn<T>(procedureParser: ProcedureParser<T>): ParseFn<T> {
|
|
|
100
102
|
export class Procedure<
|
|
101
103
|
TInputContext,
|
|
102
104
|
TContext,
|
|
105
|
+
TMeta,
|
|
103
106
|
TInput,
|
|
104
107
|
TParsedInput,
|
|
105
108
|
TOutput,
|
|
106
109
|
TParsedOutput,
|
|
107
110
|
TFinalOutput = unknown extends TParsedOutput ? TOutput : TParsedOutput,
|
|
108
111
|
> {
|
|
109
|
-
private middlewares: Readonly<Array<MiddlewareFunction<any, any>>>;
|
|
112
|
+
private middlewares: Readonly<Array<MiddlewareFunction<any, any, any>>>;
|
|
110
113
|
private resolver: ProcedureResolver<TContext, TParsedInput, TOutput>;
|
|
111
114
|
private readonly inputParser: ProcedureParser<TParsedInput>;
|
|
112
115
|
private parseInputFn: ParseFn<TParsedInput>;
|
|
113
116
|
private readonly outputParser: ProcedureParser<TFinalOutput>;
|
|
114
117
|
private parseOutputFn: ParseFn<TFinalOutput>;
|
|
118
|
+
public readonly meta: TMeta;
|
|
115
119
|
|
|
116
120
|
constructor(
|
|
117
|
-
opts: ProcedureOptions<
|
|
121
|
+
opts: ProcedureOptions<
|
|
122
|
+
TContext,
|
|
123
|
+
TMeta,
|
|
124
|
+
TParsedInput,
|
|
125
|
+
TOutput,
|
|
126
|
+
TFinalOutput
|
|
127
|
+
>,
|
|
118
128
|
) {
|
|
119
129
|
this.middlewares = opts.middlewares;
|
|
120
130
|
this.resolver = opts.resolver;
|
|
@@ -122,6 +132,7 @@ export class Procedure<
|
|
|
122
132
|
this.parseInputFn = getParseFn(this.inputParser);
|
|
123
133
|
this.outputParser = opts.outputParser;
|
|
124
134
|
this.parseOutputFn = getParseFn(this.outputParser);
|
|
135
|
+
this.meta = opts.meta;
|
|
125
136
|
}
|
|
126
137
|
|
|
127
138
|
private async parseInput(rawInput: unknown): Promise<TParsedInput> {
|
|
@@ -158,7 +169,11 @@ export class Procedure<
|
|
|
158
169
|
const middlewaresWithResolver = this.middlewares.concat([
|
|
159
170
|
async ({ ctx }: { ctx: TContext }) => {
|
|
160
171
|
const input = await this.parseInput(opts.rawInput);
|
|
161
|
-
const rawOutput = await this.resolver({
|
|
172
|
+
const rawOutput = await this.resolver({
|
|
173
|
+
...opts,
|
|
174
|
+
ctx,
|
|
175
|
+
input,
|
|
176
|
+
});
|
|
162
177
|
const data = await this.parseOutput(rawOutput);
|
|
163
178
|
return {
|
|
164
179
|
marker: middlewareMarker,
|
|
@@ -178,6 +193,7 @@ export class Procedure<
|
|
|
178
193
|
type: opts.type,
|
|
179
194
|
path: opts.path,
|
|
180
195
|
rawInput: opts.rawInput,
|
|
196
|
+
meta: this.meta,
|
|
181
197
|
next: nextFns[index + 1],
|
|
182
198
|
}),
|
|
183
199
|
);
|
|
@@ -213,14 +229,21 @@ export class Procedure<
|
|
|
213
229
|
* @param middlewares
|
|
214
230
|
*/
|
|
215
231
|
public inheritMiddlewares(
|
|
216
|
-
middlewares: MiddlewareFunction<TInputContext, TContext>[],
|
|
232
|
+
middlewares: MiddlewareFunction<TInputContext, TContext, TMeta>[],
|
|
217
233
|
): this {
|
|
218
234
|
const Constructor: {
|
|
219
235
|
new (
|
|
220
|
-
opts: ProcedureOptions<
|
|
236
|
+
opts: ProcedureOptions<
|
|
237
|
+
TContext,
|
|
238
|
+
TMeta,
|
|
239
|
+
TParsedInput,
|
|
240
|
+
TOutput,
|
|
241
|
+
TFinalOutput
|
|
242
|
+
>,
|
|
221
243
|
): Procedure<
|
|
222
244
|
TInputContext,
|
|
223
245
|
TContext,
|
|
246
|
+
TMeta,
|
|
224
247
|
TInput,
|
|
225
248
|
TParsedInput,
|
|
226
249
|
TOutput,
|
|
@@ -233,20 +256,23 @@ export class Procedure<
|
|
|
233
256
|
resolver: this.resolver,
|
|
234
257
|
inputParser: this.inputParser,
|
|
235
258
|
outputParser: this.outputParser,
|
|
259
|
+
meta: this.meta,
|
|
236
260
|
});
|
|
237
261
|
|
|
238
262
|
return instance as any;
|
|
239
263
|
}
|
|
240
264
|
}
|
|
241
265
|
|
|
242
|
-
export type CreateProcedureWithInput<TContext, TInput, TOutput> = {
|
|
266
|
+
export type CreateProcedureWithInput<TContext, TMeta, TInput, TOutput> = {
|
|
243
267
|
input: ProcedureParser<TInput>;
|
|
244
268
|
output?: ProcedureParser<TOutput>;
|
|
245
|
-
|
|
269
|
+
meta?: TMeta;
|
|
270
|
+
resolve: ProcedureResolver<TContext, TInput, InferLast<TOutput>>;
|
|
246
271
|
};
|
|
247
272
|
|
|
248
273
|
export type CreateProcedureWithInputOutputParser<
|
|
249
274
|
TContext,
|
|
275
|
+
TMeta,
|
|
250
276
|
TInput,
|
|
251
277
|
TParsedInput,
|
|
252
278
|
TOutput,
|
|
@@ -254,18 +280,26 @@ export type CreateProcedureWithInputOutputParser<
|
|
|
254
280
|
> = {
|
|
255
281
|
input: ProcedureParserWithInputOutput<TInput, TParsedInput>;
|
|
256
282
|
output?: ProcedureParserWithInputOutput<TOutput, TParsedOutput>;
|
|
257
|
-
|
|
283
|
+
meta?: TMeta;
|
|
284
|
+
resolve: ProcedureResolver<TContext, TParsedInput, InferLast<TOutput>>;
|
|
258
285
|
};
|
|
259
286
|
|
|
260
|
-
export type CreateProcedureWithoutInput<
|
|
287
|
+
export type CreateProcedureWithoutInput<
|
|
288
|
+
TContext,
|
|
289
|
+
TMeta,
|
|
290
|
+
TOutput,
|
|
291
|
+
TParsedOutput,
|
|
292
|
+
> = {
|
|
261
293
|
output?:
|
|
262
294
|
| ProcedureParserWithInputOutput<TOutput, TParsedOutput>
|
|
263
295
|
| ProcedureParser<TOutput>;
|
|
264
|
-
|
|
296
|
+
meta?: TMeta;
|
|
297
|
+
resolve: ProcedureResolver<TContext, undefined, InferLast<TOutput>>;
|
|
265
298
|
};
|
|
266
299
|
|
|
267
300
|
export type CreateProcedureOptions<
|
|
268
301
|
TContext,
|
|
302
|
+
TMeta = undefined,
|
|
269
303
|
TInput = undefined,
|
|
270
304
|
TParsedInput = undefined,
|
|
271
305
|
TOutput = undefined,
|
|
@@ -273,16 +307,18 @@ export type CreateProcedureOptions<
|
|
|
273
307
|
> =
|
|
274
308
|
| CreateProcedureWithInputOutputParser<
|
|
275
309
|
TContext,
|
|
310
|
+
TMeta,
|
|
276
311
|
TInput,
|
|
277
312
|
TParsedInput,
|
|
278
313
|
TOutput,
|
|
279
314
|
TParsedOutput
|
|
280
315
|
>
|
|
281
|
-
| CreateProcedureWithInput<TContext, TInput, TOutput>
|
|
282
|
-
| CreateProcedureWithoutInput<TContext, TOutput, TParsedOutput>;
|
|
316
|
+
| CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>
|
|
317
|
+
| CreateProcedureWithoutInput<TContext, TMeta, TOutput, TParsedOutput>;
|
|
283
318
|
|
|
284
319
|
export function createProcedure<
|
|
285
320
|
TContext,
|
|
321
|
+
TMeta,
|
|
286
322
|
TInput,
|
|
287
323
|
TParsedInput,
|
|
288
324
|
TOutput,
|
|
@@ -290,12 +326,21 @@ export function createProcedure<
|
|
|
290
326
|
>(
|
|
291
327
|
opts: CreateProcedureOptions<
|
|
292
328
|
TContext,
|
|
329
|
+
TMeta,
|
|
293
330
|
TInput,
|
|
294
331
|
TParsedInput,
|
|
295
332
|
TOutput,
|
|
296
333
|
TParsedOutput
|
|
297
334
|
>,
|
|
298
|
-
): Procedure<
|
|
335
|
+
): Procedure<
|
|
336
|
+
unknown,
|
|
337
|
+
TContext,
|
|
338
|
+
TMeta,
|
|
339
|
+
TInput,
|
|
340
|
+
TParsedInput,
|
|
341
|
+
TOutput,
|
|
342
|
+
TParsedOutput
|
|
343
|
+
> {
|
|
299
344
|
const inputParser =
|
|
300
345
|
'input' in opts
|
|
301
346
|
? opts.input
|
|
@@ -319,14 +364,16 @@ export function createProcedure<
|
|
|
319
364
|
resolver: opts.resolve as any,
|
|
320
365
|
middlewares: [],
|
|
321
366
|
outputParser: outputParser as any,
|
|
367
|
+
meta: opts.meta as any,
|
|
322
368
|
});
|
|
323
369
|
}
|
|
324
370
|
|
|
325
371
|
export type inferProcedureFromOptions<
|
|
326
372
|
TInputContext,
|
|
327
|
-
TOptions extends CreateProcedureOptions<any, any, any, any, any>,
|
|
373
|
+
TOptions extends CreateProcedureOptions<any, any, any, any, any, any>,
|
|
328
374
|
> = TOptions extends CreateProcedureOptions<
|
|
329
375
|
infer TContext,
|
|
376
|
+
infer TMeta,
|
|
330
377
|
infer TInput,
|
|
331
378
|
infer TParsedInput,
|
|
332
379
|
infer TOutput,
|
|
@@ -335,6 +382,7 @@ export type inferProcedureFromOptions<
|
|
|
335
382
|
? Procedure<
|
|
336
383
|
TInputContext,
|
|
337
384
|
TContext,
|
|
385
|
+
TMeta,
|
|
338
386
|
unknown extends TInput ? undefined : TInput,
|
|
339
387
|
unknown extends TParsedInput ? undefined : TParsedInput,
|
|
340
388
|
TOutput,
|