@trpc/server 10.0.0-alpha.41 → 10.0.0-alpha.44

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 (87) hide show
  1. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +4 -4
  2. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +4 -4
  3. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +4 -4
  4. package/adapters/aws-lambda/index.js +1 -1
  5. package/adapters/express/dist/trpc-server-adapters-express.cjs.dev.js +3 -3
  6. package/adapters/express/dist/trpc-server-adapters-express.cjs.prod.js +3 -3
  7. package/adapters/express/dist/trpc-server-adapters-express.esm.js +3 -3
  8. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +3 -3
  9. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +3 -3
  10. package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +3 -3
  11. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.dev.js +4 -4
  12. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.prod.js +4 -4
  13. package/adapters/lambda/dist/trpc-server-adapters-lambda.esm.js +4 -4
  14. package/adapters/next/dist/trpc-server-adapters-next.cjs.dev.js +3 -3
  15. package/adapters/next/dist/trpc-server-adapters-next.cjs.prod.js +3 -3
  16. package/adapters/next/dist/trpc-server-adapters-next.esm.js +3 -3
  17. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.dev.js +3 -3
  18. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.prod.js +3 -3
  19. package/adapters/node-http/dist/trpc-server-adapters-node-http.esm.js +3 -3
  20. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.dev.js +3 -3
  21. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js +3 -3
  22. package/adapters/standalone/dist/trpc-server-adapters-standalone.esm.js +3 -3
  23. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.dev.js +2 -2
  24. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.prod.js +2 -2
  25. package/adapters/ws/dist/trpc-server-adapters-ws.esm.js +2 -2
  26. package/dist/{TRPCError-b7fa0c31.mjs → TRPCError-97212592.mjs} +1 -1
  27. package/dist/adapters/aws-lambda/index.js +190 -0
  28. package/dist/adapters/aws-lambda/index.mjs +186 -0
  29. package/dist/adapters/aws-lambda/index.ts.js +1 -0
  30. package/dist/adapters/express.js +3 -3
  31. package/dist/adapters/express.mjs +4 -4
  32. package/dist/adapters/fastify/index.js +2 -2
  33. package/dist/adapters/fastify/index.mjs +3 -3
  34. package/dist/adapters/fetch/index.js +2 -2
  35. package/dist/adapters/fetch/index.mjs +3 -3
  36. package/dist/adapters/next.js +3 -3
  37. package/dist/adapters/next.mjs +4 -4
  38. package/dist/adapters/node-http/index.js +3 -3
  39. package/dist/adapters/node-http/index.mjs +4 -4
  40. package/dist/adapters/standalone.js +3 -3
  41. package/dist/adapters/standalone.mjs +4 -4
  42. package/dist/adapters/ws.mjs +1 -1
  43. package/dist/core/index.d.ts +1 -1
  44. package/dist/core/index.d.ts.map +1 -1
  45. package/dist/core/initTRPC.d.ts +8 -0
  46. package/dist/core/initTRPC.d.ts.map +1 -1
  47. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +20 -0
  48. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -1
  49. package/dist/core/internals/procedureBuilder.d.ts +1 -6
  50. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  51. package/dist/core/procedure.d.ts +1 -0
  52. package/dist/core/procedure.d.ts.map +1 -1
  53. package/dist/core/router.d.ts +31 -23
  54. package/dist/core/router.d.ts.map +1 -1
  55. package/dist/deprecated/interop.d.ts +29 -2
  56. package/dist/deprecated/interop.d.ts.map +1 -1
  57. package/dist/deprecated/router.d.ts.map +1 -1
  58. package/dist/{envelopes-b6dd0367.js → envelopes-2ae27c9f.js} +1 -4
  59. package/dist/{envelopes-af8c2959.mjs → envelopes-b9f49d4d.mjs} +1 -4
  60. package/dist/error/TRPCError.d.ts +1 -1
  61. package/dist/index.js +22 -1114
  62. package/dist/index.mjs +8 -1101
  63. package/dist/internals/transformTRPCResponse.d.ts +2 -2
  64. package/dist/internals/transformTRPCResponse.d.ts.map +1 -1
  65. package/dist/{nodeHTTPRequestHandler-6b7c8622.js → nodeHTTPRequestHandler-bee7aacc.js} +1 -1
  66. package/dist/{nodeHTTPRequestHandler-174459c5.mjs → nodeHTTPRequestHandler-f1656eba.mjs} +2 -2
  67. package/dist/{resolveHTTPResponse-8657b8de.mjs → resolveHTTPResponse-455f1e51.mjs} +2 -2
  68. package/dist/{resolveHTTPResponse-fcc8f644.js → resolveHTTPResponse-5d90abc5.js} +1 -1
  69. package/dist/router-4e50a6ac.js +1104 -0
  70. package/dist/router-a3db8f3a.mjs +1094 -0
  71. package/dist/rpc/envelopes.d.ts +22 -21
  72. package/dist/rpc/envelopes.d.ts.map +1 -1
  73. package/dist/rpc/index.js +1 -1
  74. package/dist/rpc/index.mjs +1 -1
  75. package/dist/subscription.mjs +1 -1
  76. package/dist/types.d.ts.map +1 -1
  77. package/package.json +6 -5
  78. package/src/core/index.ts +5 -1
  79. package/src/core/initTRPC.ts +0 -1
  80. package/src/core/internals/__generated__/mergeRoutersGeneric.ts +20 -0
  81. package/src/core/internals/procedureBuilder.ts +0 -22
  82. package/src/core/procedure.ts +1 -0
  83. package/src/core/router.ts +29 -44
  84. package/src/deprecated/interop.ts +47 -4
  85. package/src/deprecated/router.ts +0 -2
  86. package/src/rpc/envelopes.ts +32 -25
  87. package/src/types.ts +0 -1
package/dist/index.mjs CHANGED
@@ -1,1102 +1,10 @@
1
- import { a as assertNotBrowser, g as getHTTPStatusCodeFromError } from './resolveHTTPResponse-8657b8de.mjs';
2
- export { a as assertNotBrowser, r as resolveHTTPResponse } from './resolveHTTPResponse-8657b8de.mjs';
3
- import { T as TRPC_ERROR_CODES_BY_KEY } from './envelopes-af8c2959.mjs';
4
- import { T as TRPCError, g as getCauseFromUnknown, a as getErrorFromUnknown } from './TRPCError-b7fa0c31.mjs';
5
- export { T as TRPCError } from './TRPCError-b7fa0c31.mjs';
6
- import { c as createProxy } from './index-a3bd54c7.mjs';
1
+ export { a as assertNotBrowser, r as resolveHTTPResponse } from './resolveHTTPResponse-455f1e51.mjs';
2
+ import { m as mergeWithoutOverrides, d as defaultFormatter, a as defaultTransformer, c as createRouterFactory, g as getDataTransformer, b as createBuilder } from './router-a3db8f3a.mjs';
3
+ export { e as callProcedure, a as defaultTransformer, g as getDataTransformer, p as procedureTypes, r as router } from './router-a3db8f3a.mjs';
4
+ export { T as TRPCError } from './TRPCError-97212592.mjs';
7
5
  import './transformTRPCResponse-747b9a68.mjs';
8
-
9
- assertNotBrowser();
10
-
11
- /* eslint-disable @typescript-eslint/no-explicit-any */
12
-
13
- /**
14
- * @public
15
- */
16
-
17
- /**
18
- * @public
19
- */
20
-
21
- /**
22
- * @public
23
- */
24
-
25
- /**
26
- * @public
27
- */
28
-
29
- /**
30
- * @public
31
- */
32
-
33
- /**
34
- * @internal
35
- */
36
- function getDataTransformer$1(transformer) {
37
- if ('input' in transformer) {
38
- return transformer;
39
- }
40
-
41
- return {
42
- input: transformer,
43
- output: transformer
44
- };
45
- }
46
- /**
47
- * @internal
48
- */
49
-
50
- /**
51
- * @internal
52
- */
53
- const defaultTransformer = {
54
- _default: true,
55
- input: {
56
- serialize: obj => obj,
57
- deserialize: obj => obj
58
- },
59
- output: {
60
- serialize: obj => obj,
61
- deserialize: obj => obj
62
- }
63
- };
64
-
65
- /**
66
- * @internal
67
- */
68
-
69
- /**
70
- * @internal
71
- */
72
-
73
- /**
74
- * @internal
75
- */
76
- const defaultFormatter = ({
77
- shape
78
- }) => {
79
- return shape;
80
- };
81
-
82
- /**
83
- * @deprecated
84
- */
85
- const middlewareMarker$1 = 'middlewareMarker';
86
-
87
- /* eslint-disable @typescript-eslint/no-explicit-any */
88
- assertNotBrowser();
89
-
90
- function getParseFn$1(procedureParser) {
91
- const parser = procedureParser;
92
-
93
- if (typeof parser === 'function') {
94
- // ProcedureParserCustomValidatorEsque
95
- return parser;
96
- }
97
-
98
- if (typeof parser.parseAsync === 'function') {
99
- // ProcedureParserZodEsque
100
- return parser.parseAsync.bind(parser);
101
- }
102
-
103
- if (typeof parser.parse === 'function') {
104
- // ProcedureParserZodEsque
105
- return parser.parse.bind(parser);
106
- }
107
-
108
- if (typeof parser.validateSync === 'function') {
109
- // ProcedureParserYupEsque
110
- return parser.validateSync.bind(parser);
111
- }
112
-
113
- if (typeof parser.create === 'function') {
114
- // ProcedureParserSuperstructEsque
115
- return parser.create.bind(parser);
116
- }
117
-
118
- throw new Error('Could not find a validator fn');
119
- }
120
- /**
121
- * @internal
122
- * @deprecated
123
- */
124
-
125
-
126
- class Procedure {
127
- constructor(opts) {
128
- this.middlewares = void 0;
129
- this.resolver = void 0;
130
- this.inputParser = void 0;
131
- this.parseInputFn = void 0;
132
- this.outputParser = void 0;
133
- this.parseOutputFn = void 0;
134
- this.meta = void 0;
135
- this.middlewares = opts.middlewares;
136
- this.resolver = opts.resolver;
137
- this.inputParser = opts.inputParser;
138
- this.parseInputFn = getParseFn$1(this.inputParser);
139
- this.outputParser = opts.outputParser;
140
- this.parseOutputFn = getParseFn$1(this.outputParser);
141
- this.meta = opts.meta;
142
- }
143
-
144
- _def() {
145
- return {
146
- middlewares: this.middlewares,
147
- resolver: this.resolver,
148
- inputParser: this.inputParser,
149
- outputParser: this.outputParser,
150
- meta: this.meta
151
- };
152
- }
153
-
154
- async parseInput(rawInput) {
155
- try {
156
- return await this.parseInputFn(rawInput);
157
- } catch (cause) {
158
- throw new TRPCError({
159
- code: 'BAD_REQUEST',
160
- cause: getCauseFromUnknown(cause)
161
- });
162
- }
163
- }
164
-
165
- async parseOutput(rawOutput) {
166
- try {
167
- return await this.parseOutputFn(rawOutput);
168
- } catch (cause) {
169
- throw new TRPCError({
170
- code: 'INTERNAL_SERVER_ERROR',
171
- cause: getCauseFromUnknown(cause),
172
- message: 'Output validation failed'
173
- });
174
- }
175
- }
176
- /**
177
- * Trigger middlewares in order, parse raw input, call resolver & parse raw output
178
- * @internal
179
- */
180
-
181
-
182
- async call(opts) {
183
- // wrap the actual resolver and treat as the last "middleware"
184
- const middlewaresWithResolver = this.middlewares.concat([async ({
185
- ctx
186
- }) => {
187
- const input = await this.parseInput(opts.rawInput);
188
- const rawOutput = await this.resolver({ ...opts,
189
- ctx,
190
- input
191
- });
192
- const data = await this.parseOutput(rawOutput);
193
- return {
194
- marker: middlewareMarker$1,
195
- ok: true,
196
- data,
197
- ctx
198
- };
199
- }]); // run the middlewares recursively with the resolver as the last one
200
-
201
- const callRecursive = async (callOpts = {
202
- index: 0,
203
- ctx: opts.ctx
204
- }) => {
205
- try {
206
- const result = await middlewaresWithResolver[callOpts.index]({
207
- ctx: callOpts.ctx,
208
- type: opts.type,
209
- path: opts.path,
210
- rawInput: opts.rawInput,
211
- meta: this.meta,
212
- next: async nextOpts => {
213
- return await callRecursive({
214
- index: callOpts.index + 1,
215
- ctx: nextOpts ? nextOpts.ctx : callOpts.ctx
216
- });
217
- }
218
- });
219
- return result;
220
- } catch (cause) {
221
- return {
222
- ctx: callOpts.ctx,
223
- ok: false,
224
- error: getErrorFromUnknown(cause),
225
- marker: middlewareMarker$1
226
- };
227
- }
228
- }; // there's always at least one "next" since we wrap this.resolver in a middleware
229
-
230
-
231
- const result = await callRecursive();
232
-
233
- if (!result) {
234
- throw new TRPCError({
235
- code: 'INTERNAL_SERVER_ERROR',
236
- message: 'No result from middlewares - did you forget to `return next()`?'
237
- });
238
- }
239
-
240
- if (!result.ok) {
241
- // re-throw original error
242
- throw result.error;
243
- }
244
-
245
- return result.data;
246
- }
247
- /**
248
- * Create new procedure with passed middlewares
249
- * @param middlewares
250
- */
251
-
252
-
253
- inheritMiddlewares(middlewares) {
254
- const Constructor = this.constructor;
255
- const instance = new Constructor({
256
- middlewares: [...middlewares, ...this.middlewares],
257
- resolver: this.resolver,
258
- inputParser: this.inputParser,
259
- outputParser: this.outputParser,
260
- meta: this.meta
261
- });
262
- return instance;
263
- }
264
-
265
- }
266
- function createProcedure(opts) {
267
- const inputParser = 'input' in opts ? opts.input : input => {
268
- if (input != null) {
269
- throw new TRPCError({
270
- code: 'BAD_REQUEST',
271
- message: 'No input expected'
272
- });
273
- }
274
-
275
- return undefined;
276
- };
277
- const outputParser = 'output' in opts && opts.output ? opts.output : output => output;
278
- return new Procedure({
279
- inputParser: inputParser,
280
- resolver: opts.resolve,
281
- middlewares: [],
282
- outputParser: outputParser,
283
- meta: opts.meta
284
- });
285
- }
286
-
287
- function getParseFn(procedureParser) {
288
- const parser = procedureParser;
289
-
290
- if (typeof parser === 'function') {
291
- // ProcedureParserCustomValidatorEsque
292
- return parser;
293
- }
294
-
295
- if (typeof parser.parseAsync === 'function') {
296
- // ProcedureParserZodEsque
297
- return parser.parseAsync.bind(parser);
298
- }
299
-
300
- if (typeof parser.parse === 'function') {
301
- // ProcedureParserZodEsque
302
- return parser.parse.bind(parser);
303
- }
304
-
305
- if (typeof parser.validateSync === 'function') {
306
- // ProcedureParserYupEsque
307
- return parser.validateSync.bind(parser);
308
- }
309
-
310
- if (typeof parser.create === 'function') {
311
- // ProcedureParserSuperstructEsque
312
- return parser.create.bind(parser);
313
- }
314
-
315
- throw new Error('Could not find a validator fn');
316
- }
317
- /**
318
- * @deprecated only for backwards compat
319
- * @internal
320
- */
321
-
322
- function getParseFnOrPassThrough(procedureParser) {
323
- if (!procedureParser) {
324
- return v => v;
325
- }
326
-
327
- return getParseFn(procedureParser);
328
- }
329
-
330
- /**
331
- * Ensures there are no duplicate keys when building a procedure.
332
- */
333
- function mergeWithoutOverrides(obj1, ...objs) {
334
- const newObj = Object.assign(Object.create(null), obj1);
335
-
336
- for (const overrides of objs) {
337
- for (const key in overrides) {
338
- if (key in newObj && newObj[key] !== overrides[key]) {
339
- throw new Error(`Duplicate key ${key}`);
340
- }
341
-
342
- newObj[key] = overrides[key];
343
- }
344
- }
345
-
346
- return newObj;
347
- }
348
-
349
- /**
350
- * @internal
351
- */
352
-
353
- /**
354
- * @internal
355
- */
356
-
357
- /**
358
- * @internal
359
- */
360
- const middlewareMarker = 'middlewareMarker';
361
- /**
362
- * @internal
363
- */
364
-
365
- function createNewBuilder(def1, def2) {
366
- const {
367
- middlewares = [],
368
- ...rest
369
- } = def2; // TODO: maybe have a fn here to warn about calls
370
-
371
- return createBuilder({ ...mergeWithoutOverrides(def1, rest),
372
- middlewares: [...def1.middlewares, ...middlewares]
373
- });
374
- }
375
-
376
- function createBuilder(initDef) {
377
- const _def = initDef || {
378
- middlewares: []
379
- };
380
-
381
- return {
382
- _def,
383
-
384
- input(input) {
385
- const parser = getParseFn(input);
386
- return createNewBuilder(_def, {
387
- input,
388
- middlewares: [createInputMiddleware(parser)]
389
- });
390
- },
391
-
392
- output(output) {
393
- const parseOutput = getParseFn(output);
394
- return createNewBuilder(_def, {
395
- output,
396
- middlewares: [createOutputMiddleware(parseOutput)]
397
- });
398
- },
399
-
400
- meta(meta) {
401
- return createNewBuilder(_def, {
402
- meta: meta
403
- });
404
- },
405
-
406
- /** @deprecated **/
407
- resolve(resolver) {
408
- return createResolver(_def, resolver);
409
- },
410
-
411
- unstable_concat(builder) {
412
- return createNewBuilder(_def, builder._def);
413
- },
414
-
415
- use(middleware) {
416
- return createNewBuilder(_def, {
417
- middlewares: [middleware]
418
- });
419
- },
420
-
421
- query(resolver) {
422
- return createResolver({ ..._def,
423
- query: true
424
- }, resolver);
425
- },
426
-
427
- mutation(resolver) {
428
- return createResolver({ ..._def,
429
- mutation: true
430
- }, resolver);
431
- },
432
-
433
- subscription(resolver) {
434
- return createResolver({ ..._def,
435
- subscription: true
436
- }, resolver);
437
- }
438
-
439
- };
440
- }
441
- function createInputMiddleware(parse) {
442
- return async function inputMiddleware({
443
- next,
444
- rawInput
445
- }) {
446
- let input;
447
-
448
- try {
449
- input = await parse(rawInput);
450
- } catch (cause) {
451
- throw new TRPCError({
452
- code: 'BAD_REQUEST',
453
- cause: getCauseFromUnknown(cause)
454
- });
455
- } // TODO fix this typing?
456
-
457
-
458
- return next({
459
- input
460
- });
461
- };
462
- }
463
- function createOutputMiddleware(parse) {
464
- return async function outputMiddleware({
465
- next
466
- }) {
467
- const result = await next();
468
-
469
- if (!result.ok) {
470
- // pass through failures without validating
471
- return result;
472
- }
473
-
474
- try {
475
- const data = await parse(result.data);
476
- return { ...result,
477
- data
478
- };
479
- } catch (cause) {
480
- throw new TRPCError({
481
- message: 'Output validation failed',
482
- code: 'INTERNAL_SERVER_ERROR',
483
- cause: getCauseFromUnknown(cause)
484
- });
485
- }
486
- };
487
- }
488
-
489
- function createResolver(_def, resolver) {
490
- const finalBuilder = createNewBuilder(_def, {
491
- resolver,
492
- middlewares: [async function resolveMiddleware(opts) {
493
- const data = await resolver(opts);
494
- return {
495
- marker: middlewareMarker,
496
- ok: true,
497
- data,
498
- ctx: opts.ctx
499
- };
500
- }]
501
- });
502
- return createProcedureCaller(finalBuilder._def);
503
- }
504
- /**
505
- * @internal
506
- */
507
-
508
-
509
- const codeblock = /*#__PURE__*/`
510
- If you want to call this function on the server, you do the following:
511
- This is a client-only function.
512
-
513
- const caller = appRouter.createCaller({
514
- /* ... your context */
515
- });
516
-
517
- const result = await caller.call('myProcedure', input);
518
- `.trim();
519
-
520
- function createProcedureCaller(_def) {
521
- const procedure = async function resolve(opts) {
522
- // is direct server-side call
523
- if (!opts || !('rawInput' in opts)) {
524
- throw new Error(codeblock);
525
- } // run the middlewares recursively with the resolver as the last one
526
-
527
-
528
- const callRecursive = async (callOpts = {
529
- index: 0,
530
- ctx: opts.ctx
531
- }) => {
532
- try {
533
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
534
- const middleware = _def.middlewares[callOpts.index];
535
- const result = await middleware({
536
- ctx: callOpts.ctx,
537
- type: opts.type,
538
- path: opts.path,
539
- rawInput: opts.rawInput,
540
- meta: _def.meta,
541
- input: callOpts.input,
542
- next: async nextOpts => {
543
- return await callRecursive({
544
- index: callOpts.index + 1,
545
- ctx: nextOpts && 'ctx' in nextOpts ? { ...callOpts.ctx,
546
- ...nextOpts.ctx
547
- } : callOpts.ctx,
548
- input: nextOpts && 'input' in nextOpts ? nextOpts.input : callOpts.input
549
- });
550
- }
551
- });
552
- return result;
553
- } catch (cause) {
554
- return {
555
- ok: false,
556
- error: getErrorFromUnknown(cause),
557
- marker: middlewareMarker
558
- };
559
- }
560
- }; // there's always at least one "next" since we wrap this.resolver in a middleware
561
-
562
-
563
- const result = await callRecursive();
564
-
565
- if (!result) {
566
- throw new TRPCError({
567
- code: 'INTERNAL_SERVER_ERROR',
568
- message: 'No result from middlewares - did you forget to `return next()`?'
569
- });
570
- }
571
-
572
- if (!result.ok) {
573
- // re-throw original error
574
- throw result.error;
575
- }
576
-
577
- return result.data;
578
- };
579
-
580
- procedure._def = _def;
581
- procedure.meta = _def.meta;
582
- return procedure;
583
- }
584
-
585
- /**
586
- * Create an object without inheriting anything from `Object.prototype`
587
- * @internal
588
- */
589
- function omitPrototype(obj1) {
590
- return Object.assign(Object.create(null), obj1);
591
- }
592
-
593
- const procedureTypes = ['query', 'mutation', 'subscription'];
594
- /**
595
- * @public
596
- */
597
-
598
- /* eslint-disable @typescript-eslint/ban-types */
599
-
600
- function isRouter(procedureOrRouter) {
601
- return 'router' in procedureOrRouter._def;
602
- }
603
-
604
- const emptyRouter = {
605
- _ctx: null,
606
- _errorShape: null,
607
- _meta: null,
608
- queries: {},
609
- mutations: {},
610
- subscriptions: {},
611
- errorFormatter: defaultFormatter,
612
- transformer: defaultTransformer
613
- };
614
- /**
615
- *
616
- * @internal
617
- */
618
-
619
- function createRouterFactory(defaults) {
620
- return function createRouterInner(opts) {
621
- var _defaults$transformer, _defaults$errorFormat;
622
-
623
- const routerProcedures = omitPrototype({});
624
-
625
- function recursiveGetPaths(procedures, path = '') {
626
- for (const [key, procedureOrRouter] of Object.entries(procedures !== null && procedures !== void 0 ? procedures : {})) {
627
- const newPath = `${path}${key}`;
628
-
629
- if (isRouter(procedureOrRouter)) {
630
- recursiveGetPaths(procedureOrRouter._def.procedures, `${newPath}.`);
631
- continue;
632
- }
633
-
634
- routerProcedures[newPath] = procedureOrRouter;
635
- }
636
- }
637
-
638
- recursiveGetPaths(opts);
639
- const result = mergeWithoutOverrides({
640
- transformer: (_defaults$transformer = defaults === null || defaults === void 0 ? void 0 : defaults.transformer) !== null && _defaults$transformer !== void 0 ? _defaults$transformer : defaultTransformer,
641
- errorFormatter: (_defaults$errorFormat = defaults === null || defaults === void 0 ? void 0 : defaults.errorFormatter) !== null && _defaults$errorFormat !== void 0 ? _defaults$errorFormat : defaultFormatter
642
- }, {
643
- procedures: routerProcedures
644
- });
645
- const _def = {
646
- router: true,
647
- procedures: {},
648
- ...emptyRouter,
649
- ...result,
650
- record: opts,
651
- queries: Object.entries(result.procedures || {}).filter(pair => pair[1]._def.query).reduce((acc, [key, val]) => ({ ...acc,
652
- [key]: val
653
- }), {}),
654
- mutations: Object.entries(result.procedures || {}).filter(pair => pair[1]._def.mutation).reduce((acc, [key, val]) => ({ ...acc,
655
- [key]: val
656
- }), {}),
657
- subscriptions: Object.entries(result.procedures || {}).filter(pair => pair[1]._def.subscription).reduce((acc, [key, val]) => ({ ...acc,
658
- [key]: val
659
- }), {}),
660
- routers: Object.entries(result.procedures || {}).filter(pair => pair[1]._def._router).reduce((acc, [key, val]) => ({ ...acc,
661
- [key]: val
662
- }), {})
663
- };
664
- const router = { ...opts,
665
- _def,
666
- transformer: _def.transformer,
667
- errorFormatter: _def.errorFormatter,
668
-
669
- createCaller(ctx) {
670
- const proxy = createProxy(({
671
- path,
672
- args
673
- }) => {
674
- // interop mode
675
- if (path.length === 1 && procedureTypes.includes(path[0])) {
676
- return callProcedure({
677
- procedures: _def.procedures,
678
- path: args[0],
679
- rawInput: args[1],
680
- ctx,
681
- type: path[0]
682
- });
683
- }
684
-
685
- const fullPath = path.join('.');
686
- const procedure = _def.procedures[fullPath];
687
- let type = 'query';
688
-
689
- if (procedure._def.mutation) {
690
- type = 'mutation';
691
- } else if (procedure._def.subscription) {
692
- type = 'subscription';
693
- }
694
-
695
- return procedure({
696
- path: fullPath,
697
- rawInput: args[0],
698
- ctx,
699
- type
700
- });
701
- });
702
- return proxy;
703
- },
704
-
705
- getErrorShape(opts) {
706
- const {
707
- path,
708
- error
709
- } = opts;
710
- const {
711
- code
712
- } = opts.error;
713
- const shape = {
714
- message: error.message,
715
- code: TRPC_ERROR_CODES_BY_KEY[code],
716
- data: {
717
- code,
718
- httpStatus: getHTTPStatusCodeFromError(error)
719
- }
720
- };
721
-
722
- if (process.env.NODE_ENV !== 'production' && typeof opts.error.stack === 'string') {
723
- shape.data.stack = opts.error.stack;
724
- }
725
-
726
- if (typeof path === 'string') {
727
- shape.data.path = path;
728
- }
729
-
730
- return this._def.errorFormatter({ ...opts,
731
- shape
732
- });
733
- }
734
-
735
- };
736
- return router;
737
- };
738
- }
739
- /**
740
- * @internal
741
- */
742
-
743
- function callProcedure(opts) {
744
- var _opts$procedures$path;
745
-
746
- const {
747
- type,
748
- path
749
- } = opts;
750
-
751
- if (!(path in opts.procedures) || !((_opts$procedures$path = opts.procedures[path]) !== null && _opts$procedures$path !== void 0 && _opts$procedures$path._def[type])) {
752
- throw new TRPCError({
753
- code: 'NOT_FOUND',
754
- message: `No "${type}"-procedure on path "${path}"`
755
- });
756
- }
757
-
758
- const procedure = opts.procedures[path];
759
- return procedure(opts);
760
- }
761
-
762
- function migrateProcedure(oldProc, type) {
763
- const def = oldProc._def();
764
-
765
- const inputParser = getParseFnOrPassThrough(def.inputParser);
766
- const outputParser = getParseFnOrPassThrough(def.outputParser);
767
- const inputMiddleware = createInputMiddleware(inputParser);
768
- const builder = createBuilder({
769
- input: def.inputParser,
770
- middlewares: [...def.middlewares, inputMiddleware, createOutputMiddleware(outputParser)],
771
- meta: def.meta,
772
- output: def.outputParser,
773
- mutation: type === 'mutation',
774
- query: type === 'query',
775
- subscription: type === 'subscription'
776
- });
777
- const proc = builder[type](opts => def.resolver(opts));
778
- return proc;
779
- }
780
-
781
- function migrateRouter(oldRouter) {
782
- const errorFormatter = oldRouter._def.errorFormatter;
783
- const transformer = oldRouter._def.transformer;
784
- const queries = {};
785
- const mutations = {};
786
- const subscriptions = {};
787
-
788
- for (const [name, procedure] of Object.entries(oldRouter._def.queries)) {
789
- queries[name] = migrateProcedure(procedure, 'query');
790
- }
791
-
792
- for (const [name, procedure] of Object.entries(oldRouter._def.mutations)) {
793
- mutations[name] = migrateProcedure(procedure, 'mutation');
794
- }
795
-
796
- for (const [name, procedure] of Object.entries(oldRouter._def.subscriptions)) {
797
- subscriptions[name] = migrateProcedure(procedure, 'subscription');
798
- }
799
-
800
- const procedures = mergeWithoutOverrides(queries, mutations, subscriptions);
801
- const newRouter = createRouterFactory({
802
- transformer,
803
- errorFormatter
804
- })(procedures);
805
- return newRouter;
806
- }
807
-
808
- /* eslint-disable @typescript-eslint/ban-types */
809
- assertNotBrowser();
810
-
811
- function getDataTransformer(transformer) {
812
- if ('input' in transformer) {
813
- return transformer;
814
- }
815
-
816
- return {
817
- input: transformer,
818
- output: transformer
819
- };
820
- }
821
- /**
822
- * @internal
823
- * @deprecated
824
- */
825
-
826
-
827
- const PROCEDURE_DEFINITION_MAP = {
828
- query: 'queries',
829
- mutation: 'mutations',
830
- subscription: 'subscriptions'
831
- };
832
- /**
833
- * @internal
834
- * @deprecated
835
- */
836
-
837
- function safeObject(...args) {
838
- return Object.assign(Object.create(null), ...args);
839
- }
840
-
841
- /**
842
- * @internal The type signature of this class may change without warning.
843
- * @deprecated
844
- */
845
- class Router {
846
- constructor(def) {
847
- var _def$queries, _def$mutations, _def$subscriptions, _def$middlewares, _def$errorFormatter, _def$transformer;
848
-
849
- this._def = void 0;
850
- this._def = {
851
- queries: (_def$queries = def === null || def === void 0 ? void 0 : def.queries) !== null && _def$queries !== void 0 ? _def$queries : safeObject(),
852
- mutations: (_def$mutations = def === null || def === void 0 ? void 0 : def.mutations) !== null && _def$mutations !== void 0 ? _def$mutations : safeObject(),
853
- subscriptions: (_def$subscriptions = def === null || def === void 0 ? void 0 : def.subscriptions) !== null && _def$subscriptions !== void 0 ? _def$subscriptions : safeObject(),
854
- middlewares: (_def$middlewares = def === null || def === void 0 ? void 0 : def.middlewares) !== null && _def$middlewares !== void 0 ? _def$middlewares : [],
855
- errorFormatter: (_def$errorFormatter = def === null || def === void 0 ? void 0 : def.errorFormatter) !== null && _def$errorFormatter !== void 0 ? _def$errorFormatter : defaultFormatter,
856
- transformer: (_def$transformer = def === null || def === void 0 ? void 0 : def.transformer) !== null && _def$transformer !== void 0 ? _def$transformer : defaultTransformer
857
- };
858
- }
859
-
860
- static prefixProcedures(procedures, prefix) {
861
- const eps = safeObject();
862
-
863
- for (const [key, procedure] of Object.entries(procedures)) {
864
- eps[prefix + key] = procedure;
865
- }
866
-
867
- return eps;
868
- }
869
-
870
- query(path, procedure) {
871
- const router = new Router({
872
- queries: safeObject({
873
- [path]: createProcedure(procedure)
874
- })
875
- });
876
- return this.merge(router);
877
- }
878
-
879
- mutation(path, procedure) {
880
- const router = new Router({
881
- mutations: safeObject({
882
- [path]: createProcedure(procedure)
883
- })
884
- });
885
- return this.merge(router);
886
- }
887
- /**
888
- * @beta Might change without a major version bump
889
- */
890
-
891
-
892
- subscription(path, procedure) {
893
- const router = new Router({
894
- subscriptions: safeObject({
895
- [path]: createProcedure(procedure)
896
- })
897
- });
898
- return this.merge(router);
899
- }
900
- /**
901
- * Merge router with other router
902
- * @param router
903
- */
904
-
905
-
906
- merge(prefixOrRouter, maybeRouter) {
907
- let prefix = '';
908
- let childRouter;
909
-
910
- if (typeof prefixOrRouter === 'string' && maybeRouter instanceof Router) {
911
- prefix = prefixOrRouter;
912
- childRouter = maybeRouter;
913
- } else if (prefixOrRouter instanceof Router) {
914
- childRouter = prefixOrRouter;
915
- }
916
- /* istanbul ignore next */
917
- else {
918
- throw new Error('Invalid args');
919
- }
920
-
921
- const duplicateQueries = Object.keys(childRouter._def.queries).filter(key => !!this._def['queries'][prefix + key]);
922
- const duplicateMutations = Object.keys(childRouter._def.mutations).filter(key => !!this._def['mutations'][prefix + key]);
923
- const duplicateSubscriptions = Object.keys(childRouter._def.subscriptions).filter(key => !!this._def['subscriptions'][prefix + key]);
924
- const duplicates = [...duplicateQueries, ...duplicateMutations, ...duplicateSubscriptions];
925
-
926
- if (duplicates.length) {
927
- throw new Error(`Duplicate endpoint(s): ${duplicates.join(', ')}`);
928
- }
929
-
930
- const mergeProcedures = defs => {
931
- const newDefs = safeObject();
932
-
933
- for (const [key, procedure] of Object.entries(defs)) {
934
- const newProcedure = procedure.inheritMiddlewares(this._def.middlewares);
935
- newDefs[key] = newProcedure;
936
- }
937
-
938
- return Router.prefixProcedures(newDefs, prefix);
939
- };
940
-
941
- return new Router({ ...this._def,
942
- queries: safeObject(this._def.queries, mergeProcedures(childRouter._def.queries)),
943
- mutations: safeObject(this._def.mutations, mergeProcedures(childRouter._def.mutations)),
944
- subscriptions: safeObject(this._def.subscriptions, mergeProcedures(childRouter._def.subscriptions))
945
- });
946
- }
947
- /**
948
- * Invoke procedure. Only for internal use within library.
949
- */
950
-
951
-
952
- async call(opts) {
953
- const {
954
- type,
955
- path
956
- } = opts;
957
- const defTarget = PROCEDURE_DEFINITION_MAP[type];
958
- const defs = this._def[defTarget];
959
- const procedure = defs[path];
960
-
961
- if (!procedure) {
962
- throw new TRPCError({
963
- code: 'NOT_FOUND',
964
- message: `No "${type}"-procedure on path "${path}"`
965
- });
966
- }
967
-
968
- return procedure.call(opts);
969
- }
970
-
971
- createCaller(ctx) {
972
- return {
973
- query: (path, ...args) => {
974
- return this.call({
975
- type: 'query',
976
- ctx,
977
- path,
978
- rawInput: args[0]
979
- });
980
- },
981
- mutation: (path, ...args) => {
982
- return this.call({
983
- type: 'mutation',
984
- ctx,
985
- path,
986
- rawInput: args[0]
987
- });
988
- },
989
- subscription: (path, ...args) => {
990
- return this.call({
991
- type: 'subscription',
992
- ctx,
993
- path,
994
- rawInput: args[0]
995
- });
996
- }
997
- };
998
- }
999
- /**
1000
- * Function to be called before any procedure is invoked
1001
- * @link https://trpc.io/docs/middlewares
1002
- */
1003
-
1004
-
1005
- middleware(middleware) {
1006
- return new Router({ ...this._def,
1007
- middlewares: [...this._def.middlewares, middleware]
1008
- });
1009
- }
1010
- /**
1011
- * Format errors
1012
- * @link https://trpc.io/docs/error-formatting
1013
- */
1014
-
1015
-
1016
- formatError(errorFormatter) {
1017
- if (this._def.errorFormatter !== defaultFormatter) {
1018
- throw new Error('You seem to have double `formatError()`-calls in your router tree');
1019
- }
1020
-
1021
- return new Router({ ...this._def,
1022
- errorFormatter: errorFormatter
1023
- });
1024
- }
1025
-
1026
- getErrorShape(opts) {
1027
- const {
1028
- path,
1029
- error
1030
- } = opts;
1031
- const {
1032
- code
1033
- } = opts.error;
1034
- const shape = {
1035
- message: error.message,
1036
- code: TRPC_ERROR_CODES_BY_KEY[code],
1037
- data: {
1038
- code,
1039
- httpStatus: getHTTPStatusCodeFromError(error)
1040
- }
1041
- };
1042
-
1043
- if (process.env.NODE_ENV !== 'production' && typeof opts.error.stack === 'string') {
1044
- shape.data.stack = opts.error.stack;
1045
- }
1046
-
1047
- if (typeof path === 'string') {
1048
- shape.data.path = path;
1049
- }
1050
-
1051
- return this._def.errorFormatter({ ...opts,
1052
- shape
1053
- });
1054
- }
1055
- /**
1056
- * Add data transformer to serialize/deserialize input args + output
1057
- * @link https://trpc.io/docs/data-transformers
1058
- */
1059
-
1060
-
1061
- transformer(_transformer) {
1062
- const transformer = getDataTransformer(_transformer);
1063
-
1064
- if (this._def.transformer !== defaultTransformer) {
1065
- throw new Error('You seem to have double `transformer()`-calls in your router tree');
1066
- }
1067
-
1068
- return new Router({ ...this._def,
1069
- transformer
1070
- });
1071
- }
1072
- /**
1073
- * Flattens the generics of TQueries/TMutations/TSubscriptions.
1074
- * ⚠️ Experimental - might disappear. ⚠️
1075
- *
1076
- * @alpha
1077
- */
1078
-
1079
-
1080
- flat() {
1081
- return this;
1082
- }
1083
- /**
1084
- * Interop mode for v9.x -> v10.x
1085
- */
1086
-
1087
-
1088
- interop() {
1089
- return migrateRouter(this);
1090
- }
1091
-
1092
- }
1093
- /**
1094
- * @deprecated
1095
- */
1096
-
1097
- function router() {
1098
- return new Router();
1099
- }
6
+ import './envelopes-b9f49d4d.mjs';
7
+ import './index-a3bd54c7.mjs';
1100
8
 
1101
9
  function mergeRouters(...routerList) {
1102
10
  const record = mergeWithoutOverrides({}, ...routerList.map(r => r._def.record));
@@ -1163,13 +71,12 @@ function createMiddlewareFactory() {
1163
71
  };
1164
72
  }
1165
73
 
1166
- /* eslint-disable @typescript-eslint/ban-types */
1167
74
  function initTRPC() {
1168
75
  return function initTRPCInner(options) {
1169
76
  var _options$errorFormatt, _options$transformer;
1170
77
 
1171
78
  const errorFormatter = (_options$errorFormatt = options === null || options === void 0 ? void 0 : options.errorFormatter) !== null && _options$errorFormatt !== void 0 ? _options$errorFormatt : defaultFormatter;
1172
- const transformer = getDataTransformer$1((_options$transformer = options === null || options === void 0 ? void 0 : options.transformer) !== null && _options$transformer !== void 0 ? _options$transformer : defaultTransformer);
79
+ const transformer = getDataTransformer((_options$transformer = options === null || options === void 0 ? void 0 : options.transformer) !== null && _options$transformer !== void 0 ? _options$transformer : defaultTransformer);
1173
80
  const _config = {
1174
81
  transformer,
1175
82
  errorShape: null,
@@ -1209,4 +116,4 @@ function initTRPC() {
1209
116
  };
1210
117
  }
1211
118
 
1212
- export { callProcedure, defaultTransformer, getDataTransformer$1 as getDataTransformer, initTRPC, procedureTypes, router };
119
+ export { initTRPC };