@trpc/server 10.0.0-proxy-beta.13 → 10.0.0-proxy-beta.15

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.
Files changed (63) hide show
  1. package/dist/{TRPCError-281ac25a.mjs → TRPCError-e99e7cbe.mjs} +1 -1
  2. package/dist/adapters/aws-lambda/index.js +2 -3
  3. package/dist/adapters/aws-lambda/index.mjs +3 -4
  4. package/dist/adapters/express.js +3 -3
  5. package/dist/adapters/express.mjs +4 -4
  6. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  7. package/dist/adapters/fastify/index.js +2 -3
  8. package/dist/adapters/fastify/index.mjs +3 -4
  9. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  10. package/dist/adapters/fetch/index.js +2 -3
  11. package/dist/adapters/fetch/index.mjs +3 -4
  12. package/dist/adapters/next.js +3 -3
  13. package/dist/adapters/next.mjs +4 -4
  14. package/dist/adapters/node-http/index.js +3 -3
  15. package/dist/adapters/node-http/index.mjs +4 -4
  16. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  17. package/dist/adapters/standalone.js +3 -3
  18. package/dist/adapters/standalone.mjs +4 -4
  19. package/dist/adapters/ws.d.ts.map +1 -1
  20. package/dist/adapters/ws.js +10 -3
  21. package/dist/adapters/ws.mjs +11 -4
  22. package/dist/core/initTRPC.d.ts +16 -4
  23. package/dist/core/initTRPC.d.ts.map +1 -1
  24. package/dist/core/internals/config.d.ts +13 -0
  25. package/dist/core/internals/config.d.ts.map +1 -1
  26. package/dist/core/middleware.d.ts +1 -1
  27. package/dist/core/middleware.d.ts.map +1 -1
  28. package/dist/deprecated/internals/procedure.d.ts.map +1 -1
  29. package/dist/deprecated/router.d.ts.map +1 -1
  30. package/dist/http/index.d.ts.map +1 -1
  31. package/dist/index.d.ts +0 -1
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +739 -25
  34. package/dist/index.mjs +724 -9
  35. package/dist/{nodeHTTPRequestHandler-1bf460cc.mjs → nodeHTTPRequestHandler-205f7545.mjs} +2 -3
  36. package/dist/{nodeHTTPRequestHandler-71717219.js → nodeHTTPRequestHandler-efc38711.js} +1 -2
  37. package/dist/resolveHTTPResponse-4ceb1785.mjs +205 -0
  38. package/dist/resolveHTTPResponse-bf4c6ceb.js +207 -0
  39. package/dist/subscription.mjs +1 -1
  40. package/dist/{resolveHTTPResponse-c6e31b55.js → transformTRPCResponse-05aa84c1.js} +22 -201
  41. package/dist/{resolveHTTPResponse-f2b378b6.mjs → transformTRPCResponse-959de787.mjs} +21 -201
  42. package/dist/types.d.ts +9 -2
  43. package/dist/types.d.ts.map +1 -1
  44. package/package.json +6 -6
  45. package/src/adapters/fastify/fastifyRequestHandler.ts +0 -3
  46. package/src/adapters/fetch/fetchRequestHandler.ts +0 -3
  47. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +0 -3
  48. package/src/adapters/ws.ts +13 -3
  49. package/src/core/initTRPC.ts +12 -0
  50. package/src/core/internals/config.ts +19 -0
  51. package/src/core/middleware.ts +1 -1
  52. package/src/deprecated/internals/procedure.ts +0 -3
  53. package/src/deprecated/router.ts +0 -3
  54. package/src/http/index.ts +0 -4
  55. package/src/index.ts +0 -1
  56. package/src/types.ts +28 -7
  57. package/dist/assertNotBrowser.d.ts +0 -2
  58. package/dist/assertNotBrowser.d.ts.map +0 -1
  59. package/dist/router-15f9618e.js +0 -724
  60. package/dist/router-28a2c129.mjs +0 -718
  61. package/dist/transformTRPCResponse-11ac69cc.js +0 -27
  62. package/dist/transformTRPCResponse-c8138d5f.mjs +0 -25
  63. package/src/assertNotBrowser.ts +0 -12
@@ -1,724 +0,0 @@
1
- 'use strict';
2
-
3
- var resolveHTTPResponse = require('./resolveHTTPResponse-c6e31b55.js');
4
- var codes = require('./codes-76c38a34.js');
5
- var TRPCError = require('./TRPCError-fe9113e5.js');
6
-
7
- /**
8
- * @internal
9
- */ // FIXME this should use RootConfig
10
- function createMiddlewareFactory() {
11
- return function createMiddleware(fn) {
12
- return fn;
13
- };
14
- }
15
- function isPlainObject(obj) {
16
- return obj && typeof obj === 'object' && !Array.isArray(obj);
17
- }
18
- /**
19
- * @internal
20
- * Please note, `trpc-openapi` uses this function.
21
- */ function createInputMiddleware(parse) {
22
- const inputMiddleware = async ({ next , rawInput , input , })=>{
23
- let parsedInput;
24
- try {
25
- parsedInput = await parse(rawInput);
26
- } catch (cause) {
27
- throw new TRPCError.TRPCError({
28
- code: 'BAD_REQUEST',
29
- cause: TRPCError.getCauseFromUnknown(cause)
30
- });
31
- }
32
- // Multiple input parsers
33
- const combinedInput = isPlainObject(input) && isPlainObject(parsedInput) ? {
34
- ...input,
35
- ...parsedInput
36
- } : parsedInput;
37
- // TODO fix this typing?
38
- return next({
39
- input: combinedInput
40
- });
41
- };
42
- inputMiddleware._type = 'input';
43
- return inputMiddleware;
44
- }
45
- /**
46
- * @internal
47
- */ function createOutputMiddleware(parse) {
48
- const outputMiddleware = async ({ next })=>{
49
- const result = await next();
50
- if (!result.ok) {
51
- // pass through failures without validating
52
- return result;
53
- }
54
- try {
55
- const data = await parse(result.data);
56
- return {
57
- ...result,
58
- data
59
- };
60
- } catch (cause) {
61
- throw new TRPCError.TRPCError({
62
- message: 'Output validation failed',
63
- code: 'INTERNAL_SERVER_ERROR',
64
- cause: TRPCError.getCauseFromUnknown(cause)
65
- });
66
- }
67
- };
68
- outputMiddleware._type = 'output';
69
- return outputMiddleware;
70
- }
71
-
72
- function getParseFn$1(procedureParser) {
73
- const parser = procedureParser;
74
- if (typeof parser === 'function') {
75
- // ProcedureParserCustomValidatorEsque
76
- return parser;
77
- }
78
- if (typeof parser.parseAsync === 'function') {
79
- // ProcedureParserZodEsque
80
- return parser.parseAsync.bind(parser);
81
- }
82
- if (typeof parser.parse === 'function') {
83
- // ProcedureParserZodEsque
84
- return parser.parse.bind(parser);
85
- }
86
- if (typeof parser.validateSync === 'function') {
87
- // ProcedureParserYupEsque
88
- return parser.validateSync.bind(parser);
89
- }
90
- if (typeof parser.create === 'function') {
91
- // ProcedureParserSuperstructEsque
92
- return parser.create.bind(parser);
93
- }
94
- throw new Error('Could not find a validator fn');
95
- }
96
- /**
97
- * @deprecated only for backwards compat
98
- * @internal
99
- */ function getParseFnOrPassThrough(procedureParser) {
100
- if (!procedureParser) {
101
- return (v)=>v;
102
- }
103
- return getParseFn$1(procedureParser);
104
- }
105
-
106
- /**
107
- * @internal
108
- */ const middlewareMarker$1 = 'middlewareMarker';
109
-
110
- function createNewBuilder(def1, def2) {
111
- const { middlewares =[] , inputs , ...rest } = def2;
112
- // TODO: maybe have a fn here to warn about calls
113
- return createBuilder({
114
- ...resolveHTTPResponse.mergeWithoutOverrides(def1, rest),
115
- inputs: [
116
- ...def1.inputs,
117
- ...inputs ?? []
118
- ],
119
- middlewares: [
120
- ...def1.middlewares,
121
- ...middlewares
122
- ]
123
- });
124
- }
125
- function createBuilder(initDef) {
126
- const _def = initDef || {
127
- inputs: [],
128
- middlewares: []
129
- };
130
- return {
131
- _def,
132
- input (input) {
133
- const parser = getParseFn$1(input);
134
- return createNewBuilder(_def, {
135
- inputs: [
136
- input
137
- ],
138
- middlewares: [
139
- createInputMiddleware(parser)
140
- ]
141
- });
142
- },
143
- output (output) {
144
- const parseOutput = getParseFn$1(output);
145
- return createNewBuilder(_def, {
146
- output,
147
- middlewares: [
148
- createOutputMiddleware(parseOutput)
149
- ]
150
- });
151
- },
152
- meta (meta) {
153
- return createNewBuilder(_def, {
154
- meta: meta
155
- });
156
- },
157
- unstable_concat (builder) {
158
- return createNewBuilder(_def, builder._def);
159
- },
160
- use (middleware) {
161
- return createNewBuilder(_def, {
162
- middlewares: [
163
- middleware
164
- ]
165
- });
166
- },
167
- query (resolver) {
168
- return createResolver({
169
- ..._def,
170
- query: true
171
- }, resolver);
172
- },
173
- mutation (resolver) {
174
- return createResolver({
175
- ..._def,
176
- mutation: true
177
- }, resolver);
178
- },
179
- subscription (resolver) {
180
- return createResolver({
181
- ..._def,
182
- subscription: true
183
- }, resolver);
184
- }
185
- };
186
- }
187
- function createResolver(_def, resolver) {
188
- const finalBuilder = createNewBuilder(_def, {
189
- resolver,
190
- middlewares: [
191
- async function resolveMiddleware(opts) {
192
- const data = await resolver(opts);
193
- return {
194
- marker: middlewareMarker$1,
195
- ok: true,
196
- data,
197
- ctx: opts.ctx
198
- };
199
- }
200
- ]
201
- });
202
- return createProcedureCaller(finalBuilder._def);
203
- }
204
- const codeblock = `
205
- If you want to call this function on the server, you do the following:
206
- This is a client-only function.
207
-
208
- const caller = appRouter.createCaller({
209
- /* ... your context */
210
- });
211
-
212
- const result = await caller.call('myProcedure', input);
213
- `.trim();
214
- function createProcedureCaller(_def) {
215
- const procedure = async function resolve(opts) {
216
- // is direct server-side call
217
- if (!opts || !('rawInput' in opts)) {
218
- throw new Error(codeblock);
219
- }
220
- // run the middlewares recursively with the resolver as the last one
221
- const callRecursive = async (callOpts = {
222
- index: 0,
223
- ctx: opts.ctx
224
- })=>{
225
- try {
226
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
227
- const middleware = _def.middlewares[callOpts.index];
228
- const result = await middleware({
229
- ctx: callOpts.ctx,
230
- type: opts.type,
231
- path: opts.path,
232
- rawInput: opts.rawInput,
233
- meta: _def.meta,
234
- input: callOpts.input,
235
- next: async (nextOpts)=>{
236
- return await callRecursive({
237
- index: callOpts.index + 1,
238
- ctx: nextOpts && 'ctx' in nextOpts ? {
239
- ...callOpts.ctx,
240
- ...nextOpts.ctx
241
- } : callOpts.ctx,
242
- input: nextOpts && 'input' in nextOpts ? nextOpts.input : callOpts.input
243
- });
244
- }
245
- });
246
- return result;
247
- } catch (cause) {
248
- return {
249
- ok: false,
250
- error: TRPCError.getTRPCErrorFromUnknown(cause),
251
- marker: middlewareMarker$1
252
- };
253
- }
254
- };
255
- // there's always at least one "next" since we wrap this.resolver in a middleware
256
- const result = await callRecursive();
257
- if (!result) {
258
- throw new TRPCError.TRPCError({
259
- code: 'INTERNAL_SERVER_ERROR',
260
- message: 'No result from middlewares - did you forget to `return next()`?'
261
- });
262
- }
263
- if (!result.ok) {
264
- // re-throw original error
265
- throw result.error;
266
- }
267
- return result.data;
268
- };
269
- procedure._def = _def;
270
- procedure.meta = _def.meta;
271
- return procedure;
272
- }
273
-
274
- resolveHTTPResponse.assertNotBrowser();
275
-
276
- /**
277
- * @deprecated
278
- */ const middlewareMarker = 'middlewareMarker';
279
-
280
- resolveHTTPResponse.assertNotBrowser();
281
- function getParseFn(procedureParser) {
282
- const parser = procedureParser;
283
- if (typeof parser === 'function') {
284
- // ProcedureParserCustomValidatorEsque
285
- return parser;
286
- }
287
- if (typeof parser.parseAsync === 'function') {
288
- // ProcedureParserZodEsque
289
- return parser.parseAsync.bind(parser);
290
- }
291
- if (typeof parser.parse === 'function') {
292
- // ProcedureParserZodEsque
293
- return parser.parse.bind(parser);
294
- }
295
- if (typeof parser.validateSync === 'function') {
296
- // ProcedureParserYupEsque
297
- return parser.validateSync.bind(parser);
298
- }
299
- if (typeof parser.create === 'function') {
300
- // ProcedureParserSuperstructEsque
301
- return parser.create.bind(parser);
302
- }
303
- throw new Error('Could not find a validator fn');
304
- }
305
- /**
306
- * @internal
307
- * @deprecated
308
- */ class Procedure {
309
- _def() {
310
- return {
311
- middlewares: this.middlewares,
312
- resolver: this.resolver,
313
- inputParser: this.inputParser,
314
- outputParser: this.outputParser,
315
- meta: this.meta
316
- };
317
- }
318
- async parseInput(rawInput) {
319
- try {
320
- return await this.parseInputFn(rawInput);
321
- } catch (cause) {
322
- throw new TRPCError.TRPCError({
323
- code: 'BAD_REQUEST',
324
- cause: TRPCError.getCauseFromUnknown(cause)
325
- });
326
- }
327
- }
328
- async parseOutput(rawOutput) {
329
- try {
330
- return await this.parseOutputFn(rawOutput);
331
- } catch (cause) {
332
- throw new TRPCError.TRPCError({
333
- code: 'INTERNAL_SERVER_ERROR',
334
- cause: TRPCError.getCauseFromUnknown(cause),
335
- message: 'Output validation failed'
336
- });
337
- }
338
- }
339
- /**
340
- * Trigger middlewares in order, parse raw input, call resolver & parse raw output
341
- * @internal
342
- */ async call(opts) {
343
- // wrap the actual resolver and treat as the last "middleware"
344
- const middlewaresWithResolver = this.middlewares.concat([
345
- async ({ ctx })=>{
346
- const input = await this.parseInput(opts.rawInput);
347
- const rawOutput = await this.resolver({
348
- ...opts,
349
- ctx,
350
- input
351
- });
352
- const data = await this.parseOutput(rawOutput);
353
- return {
354
- marker: middlewareMarker,
355
- ok: true,
356
- data,
357
- ctx
358
- };
359
- }
360
- ]);
361
- // run the middlewares recursively with the resolver as the last one
362
- const callRecursive = async (callOpts = {
363
- index: 0,
364
- ctx: opts.ctx
365
- })=>{
366
- try {
367
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
368
- const result = await middlewaresWithResolver[callOpts.index]({
369
- ctx: callOpts.ctx,
370
- type: opts.type,
371
- path: opts.path,
372
- rawInput: opts.rawInput,
373
- meta: this.meta,
374
- next: async (nextOpts)=>{
375
- return await callRecursive({
376
- index: callOpts.index + 1,
377
- ctx: nextOpts ? nextOpts.ctx : callOpts.ctx
378
- });
379
- }
380
- });
381
- return result;
382
- } catch (cause) {
383
- return {
384
- ctx: callOpts.ctx,
385
- ok: false,
386
- error: TRPCError.getTRPCErrorFromUnknown(cause),
387
- marker: middlewareMarker
388
- };
389
- }
390
- };
391
- // there's always at least one "next" since we wrap this.resolver in a middleware
392
- const result = await callRecursive();
393
- if (!result) {
394
- throw new TRPCError.TRPCError({
395
- code: 'INTERNAL_SERVER_ERROR',
396
- message: 'No result from middlewares - did you forget to `return next()`?'
397
- });
398
- }
399
- if (!result.ok) {
400
- // re-throw original error
401
- throw result.error;
402
- }
403
- return result.data;
404
- }
405
- /**
406
- * Create new procedure with passed middlewares
407
- * @param middlewares
408
- */ inheritMiddlewares(middlewares) {
409
- const Constructor = this.constructor;
410
- const instance = new Constructor({
411
- middlewares: [
412
- ...middlewares,
413
- ...this.middlewares
414
- ],
415
- resolver: this.resolver,
416
- inputParser: this.inputParser,
417
- outputParser: this.outputParser,
418
- meta: this.meta
419
- });
420
- return instance;
421
- }
422
- constructor(opts){
423
- this.middlewares = opts.middlewares;
424
- this.resolver = opts.resolver;
425
- this.inputParser = opts.inputParser;
426
- this.parseInputFn = getParseFn(this.inputParser);
427
- this.outputParser = opts.outputParser;
428
- this.parseOutputFn = getParseFn(this.outputParser);
429
- this.meta = opts.meta;
430
- }
431
- }
432
- function createProcedure(opts) {
433
- const inputParser = 'input' in opts ? opts.input : (input)=>{
434
- if (input != null) {
435
- throw new TRPCError.TRPCError({
436
- code: 'BAD_REQUEST',
437
- message: 'No input expected'
438
- });
439
- }
440
- return undefined;
441
- };
442
- const outputParser = 'output' in opts && opts.output ? opts.output : (output)=>output;
443
- return new Procedure({
444
- inputParser: inputParser,
445
- resolver: opts.resolve,
446
- middlewares: [],
447
- outputParser: outputParser,
448
- meta: opts.meta
449
- });
450
- }
451
-
452
- function migrateProcedure(oldProc, type) {
453
- const def = oldProc._def();
454
- const inputParser = getParseFnOrPassThrough(def.inputParser);
455
- const outputParser = getParseFnOrPassThrough(def.outputParser);
456
- const inputMiddleware = createInputMiddleware(inputParser);
457
- const builder = createBuilder({
458
- inputs: [
459
- def.inputParser
460
- ],
461
- middlewares: [
462
- ...def.middlewares,
463
- inputMiddleware,
464
- createOutputMiddleware(outputParser)
465
- ],
466
- meta: def.meta,
467
- output: def.outputParser,
468
- mutation: type === 'mutation',
469
- query: type === 'query',
470
- subscription: type === 'subscription'
471
- });
472
- const proc = builder[type]((opts)=>def.resolver(opts));
473
- return proc;
474
- }
475
- function migrateRouter(oldRouter) {
476
- const errorFormatter = oldRouter._def.errorFormatter;
477
- const transformer = oldRouter._def.transformer;
478
- const queries = {};
479
- const mutations = {};
480
- const subscriptions = {};
481
- for (const [name, procedure] of Object.entries(oldRouter._def.queries)){
482
- queries[name] = migrateProcedure(procedure, 'query');
483
- }
484
- for (const [name1, procedure1] of Object.entries(oldRouter._def.mutations)){
485
- mutations[name1] = migrateProcedure(procedure1, 'mutation');
486
- }
487
- for (const [name2, procedure2] of Object.entries(oldRouter._def.subscriptions)){
488
- subscriptions[name2] = migrateProcedure(procedure2, 'subscription');
489
- }
490
- const procedures = resolveHTTPResponse.mergeWithoutOverrides(queries, mutations, subscriptions);
491
- const newRouter = resolveHTTPResponse.createRouterFactory({
492
- transformer,
493
- errorFormatter
494
- })(procedures);
495
- return newRouter;
496
- }
497
-
498
- resolveHTTPResponse.assertNotBrowser();
499
- function getDataTransformer(transformer) {
500
- if ('input' in transformer) {
501
- return transformer;
502
- }
503
- return {
504
- input: transformer,
505
- output: transformer
506
- };
507
- }
508
- const PROCEDURE_DEFINITION_MAP = {
509
- query: 'queries',
510
- mutation: 'mutations',
511
- subscription: 'subscriptions'
512
- };
513
- function safeObject(...args) {
514
- return Object.assign(Object.create(null), ...args);
515
- }
516
- /**
517
- * @internal The type signature of this class may change without warning.
518
- * @deprecated
519
- */ class Router {
520
- static prefixProcedures(procedures, prefix) {
521
- const eps = safeObject();
522
- for (const [key, procedure] of Object.entries(procedures)){
523
- eps[prefix + key] = procedure;
524
- }
525
- return eps;
526
- }
527
- query(path, procedure) {
528
- const router = new Router({
529
- queries: safeObject({
530
- [path]: createProcedure(procedure)
531
- })
532
- });
533
- return this.merge(router);
534
- }
535
- mutation(path, procedure) {
536
- const router = new Router({
537
- mutations: safeObject({
538
- [path]: createProcedure(procedure)
539
- })
540
- });
541
- return this.merge(router);
542
- }
543
- subscription(path, procedure) {
544
- const router = new Router({
545
- subscriptions: safeObject({
546
- [path]: createProcedure(procedure)
547
- })
548
- });
549
- return this.merge(router);
550
- }
551
- merge(prefixOrRouter, maybeRouter) {
552
- let prefix = '';
553
- let childRouter;
554
- if (typeof prefixOrRouter === 'string' && maybeRouter instanceof Router) {
555
- prefix = prefixOrRouter;
556
- childRouter = maybeRouter;
557
- } else if (prefixOrRouter instanceof Router) {
558
- childRouter = prefixOrRouter;
559
- } else {
560
- throw new Error('Invalid args');
561
- }
562
- const duplicateQueries = Object.keys(childRouter._def.queries).filter((key)=>!!this._def['queries'][prefix + key]);
563
- const duplicateMutations = Object.keys(childRouter._def.mutations).filter((key)=>!!this._def['mutations'][prefix + key]);
564
- const duplicateSubscriptions = Object.keys(childRouter._def.subscriptions).filter((key)=>!!this._def['subscriptions'][prefix + key]);
565
- const duplicates = [
566
- ...duplicateQueries,
567
- ...duplicateMutations,
568
- ...duplicateSubscriptions
569
- ];
570
- if (duplicates.length) {
571
- throw new Error(`Duplicate endpoint(s): ${duplicates.join(', ')}`);
572
- }
573
- const mergeProcedures = (defs)=>{
574
- const newDefs = safeObject();
575
- for (const [key, procedure] of Object.entries(defs)){
576
- const newProcedure = procedure.inheritMiddlewares(this._def.middlewares);
577
- newDefs[key] = newProcedure;
578
- }
579
- return Router.prefixProcedures(newDefs, prefix);
580
- };
581
- return new Router({
582
- ...this._def,
583
- queries: safeObject(this._def.queries, mergeProcedures(childRouter._def.queries)),
584
- mutations: safeObject(this._def.mutations, mergeProcedures(childRouter._def.mutations)),
585
- subscriptions: safeObject(this._def.subscriptions, mergeProcedures(childRouter._def.subscriptions))
586
- });
587
- }
588
- /**
589
- * Invoke procedure. Only for internal use within library.
590
- */ async call(opts) {
591
- const { type , path } = opts;
592
- const defTarget = PROCEDURE_DEFINITION_MAP[type];
593
- const defs = this._def[defTarget];
594
- const procedure = defs[path];
595
- if (!procedure) {
596
- throw new TRPCError.TRPCError({
597
- code: 'NOT_FOUND',
598
- message: `No "${type}"-procedure on path "${path}"`
599
- });
600
- }
601
- return procedure.call(opts);
602
- }
603
- createCaller(ctx) {
604
- return {
605
- query: (path, ...args)=>{
606
- return this.call({
607
- type: 'query',
608
- ctx,
609
- path,
610
- rawInput: args[0]
611
- });
612
- },
613
- mutation: (path, ...args)=>{
614
- return this.call({
615
- type: 'mutation',
616
- ctx,
617
- path,
618
- rawInput: args[0]
619
- });
620
- },
621
- subscription: (path, ...args)=>{
622
- return this.call({
623
- type: 'subscription',
624
- ctx,
625
- path,
626
- rawInput: args[0]
627
- });
628
- }
629
- };
630
- }
631
- /**
632
- * Function to be called before any procedure is invoked
633
- * @link https://trpc.io/docs/middlewares
634
- */ middleware(middleware) {
635
- return new Router({
636
- ...this._def,
637
- middlewares: [
638
- ...this._def.middlewares,
639
- middleware
640
- ]
641
- });
642
- }
643
- /**
644
- * Format errors
645
- * @link https://trpc.io/docs/error-formatting
646
- */ formatError(errorFormatter) {
647
- if (this._def.errorFormatter !== resolveHTTPResponse.defaultFormatter) {
648
- throw new Error('You seem to have double `formatError()`-calls in your router tree');
649
- }
650
- return new Router({
651
- ...this._def,
652
- errorFormatter: errorFormatter
653
- });
654
- }
655
- getErrorShape(opts) {
656
- const { path , error } = opts;
657
- const { code } = opts.error;
658
- const shape = {
659
- message: error.message,
660
- code: codes.TRPC_ERROR_CODES_BY_KEY[code],
661
- data: {
662
- code,
663
- httpStatus: resolveHTTPResponse.getHTTPStatusCodeFromError(error)
664
- }
665
- };
666
- if (process.env.NODE_ENV !== 'production' && typeof opts.error.stack === 'string') {
667
- shape.data.stack = opts.error.stack;
668
- }
669
- if (typeof path === 'string') {
670
- shape.data.path = path;
671
- }
672
- return this._def.errorFormatter({
673
- ...opts,
674
- shape
675
- });
676
- }
677
- /**
678
- * Add data transformer to serialize/deserialize input args + output
679
- * @link https://trpc.io/docs/data-transformers
680
- */ transformer(_transformer) {
681
- const transformer = getDataTransformer(_transformer);
682
- if (this._def.transformer !== resolveHTTPResponse.defaultTransformer) {
683
- throw new Error('You seem to have double `transformer()`-calls in your router tree');
684
- }
685
- return new Router({
686
- ...this._def,
687
- transformer
688
- });
689
- }
690
- /**
691
- * Flattens the generics of TQueries/TMutations/TSubscriptions.
692
- * ⚠️ Experimental - might disappear. ⚠️
693
- *
694
- * @alpha
695
- */ flat() {
696
- return this;
697
- }
698
- /**
699
- * Interop mode for v9.x -> v10.x
700
- */ interop() {
701
- return migrateRouter(this);
702
- }
703
- constructor(def){
704
- this._def = {
705
- queries: def?.queries ?? safeObject(),
706
- mutations: def?.mutations ?? safeObject(),
707
- subscriptions: def?.subscriptions ?? safeObject(),
708
- middlewares: def?.middlewares ?? [],
709
- errorFormatter: def?.errorFormatter ?? resolveHTTPResponse.defaultFormatter,
710
- transformer: def?.transformer ?? resolveHTTPResponse.defaultTransformer
711
- };
712
- }
713
- }
714
- /**
715
- * @deprecated
716
- */ function router() {
717
- return new Router();
718
- }
719
-
720
- exports.createBuilder = createBuilder;
721
- exports.createInputMiddleware = createInputMiddleware;
722
- exports.createMiddlewareFactory = createMiddlewareFactory;
723
- exports.createOutputMiddleware = createOutputMiddleware;
724
- exports.router = router;