@trpc/server 10.0.0-alpha.38 → 10.0.0-alpha.39

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 (35) hide show
  1. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +20 -0
  2. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +20 -0
  3. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +20 -0
  4. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  5. package/dist/core/index.d.ts +2 -1
  6. package/dist/core/index.d.ts.map +1 -1
  7. package/dist/core/initTRPC.d.ts +1 -1
  8. package/dist/core/internals/procedureBuilder.d.ts +37 -13
  9. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  10. package/dist/core/procedure.d.ts +12 -8
  11. package/dist/core/procedure.d.ts.map +1 -1
  12. package/dist/core/router.d.ts +17 -3
  13. package/dist/core/router.d.ts.map +1 -1
  14. package/dist/core/types.d.ts +3 -1
  15. package/dist/core/types.d.ts.map +1 -1
  16. package/dist/index-1a25ccbd.js +33 -0
  17. package/dist/index-a3bd54c7.mjs +31 -0
  18. package/dist/index.js +186 -191
  19. package/dist/index.mjs +185 -192
  20. package/dist/shared/index.js +2 -28
  21. package/dist/shared/index.mjs +1 -31
  22. package/dist/types.d.ts +10 -0
  23. package/dist/types.d.ts.map +1 -1
  24. package/package.json +2 -2
  25. package/src/adapters/aws-lambda/index.ts +17 -0
  26. package/src/core/index.ts +2 -1
  27. package/src/core/internals/procedureBuilder.ts +264 -28
  28. package/src/core/procedure.ts +17 -10
  29. package/src/core/router.ts +79 -78
  30. package/src/core/types.ts +5 -1
  31. package/src/deprecated/interop.ts +4 -4
  32. package/src/types.ts +12 -0
  33. package/dist/core/internals/internalProcedure.d.ts +0 -78
  34. package/dist/core/internals/internalProcedure.d.ts.map +0 -1
  35. package/src/core/internals/internalProcedure.ts +0 -297
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var resolveHTTPResponse = require('./resolveHTTPResponse-fcc8f644.js');
6
6
  var envelopes = require('./envelopes-b6dd0367.js');
7
7
  var TRPCError = require('./TRPCError-9050a68d.js');
8
+ var index = require('./index-1a25ccbd.js');
8
9
  require('./transformTRPCResponse-0c261655.js');
9
10
 
10
11
  resolveHTTPResponse.assertNotBrowser();
@@ -363,97 +364,82 @@ const middlewareMarker = 'middlewareMarker';
363
364
  * @internal
364
365
  */
365
366
 
366
- /**
367
- * This is the actual internal representation of a builder
368
- * FIXME - I want to get rid of this and use the public API instead
369
- */
370
-
371
- function createNewInternalBuilder(def1, def2) {
367
+ function createNewBuilder(def1, def2) {
372
368
  const {
373
369
  middlewares = [],
374
370
  ...rest
375
371
  } = def2; // TODO: maybe have a fn here to warn about calls
376
372
 
377
- return createInternalBuilder({ ...mergeWithoutOverrides(def1, rest),
373
+ return createBuilder({ ...mergeWithoutOverrides(def1, rest),
378
374
  middlewares: [...def1.middlewares, ...middlewares]
379
375
  });
380
376
  }
381
377
 
382
- const codeblock = /*#__PURE__*/`
383
- If you want to call this function on the server, you do the following:
384
- This is a client-only function.
385
-
386
- const caller = appRouter.createCaller({
387
- /* ... your context */
388
- });
378
+ function createBuilder(initDef) {
379
+ const _def = initDef || {
380
+ middlewares: []
381
+ };
389
382
 
390
- const result = await caller.call('myProcedure', input);
391
- `.trim();
383
+ return {
384
+ _def,
392
385
 
393
- function createProcedureCaller(_def) {
394
- const procedure = async function resolve(opts) {
395
- if (!opts || !('rawInput' in opts)) {
396
- throw new Error(codeblock);
397
- } // run the middlewares recursively with the resolver as the last one
386
+ input(input) {
387
+ const parser = getParseFn(input);
388
+ return createNewBuilder(_def, {
389
+ input,
390
+ middlewares: [createInputMiddleware(parser)]
391
+ });
392
+ },
398
393
 
394
+ output(output) {
395
+ const parseOutput = getParseFn(output);
396
+ return createNewBuilder(_def, {
397
+ output,
398
+ middlewares: [createOutputMiddleware(parseOutput)]
399
+ });
400
+ },
399
401
 
400
- const callRecursive = async (callOpts = {
401
- index: 0,
402
- ctx: opts.ctx
403
- }) => {
404
- try {
405
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
406
- const middleware = _def.middlewares[callOpts.index];
407
- const result = await middleware({
408
- ctx: callOpts.ctx,
409
- type: opts.type,
410
- path: opts.path,
411
- rawInput: opts.rawInput,
412
- meta: _def.meta,
413
- input: callOpts.input,
414
- next: async nextOpts => {
415
- return await callRecursive({
416
- index: callOpts.index + 1,
417
- ctx: nextOpts && 'ctx' in nextOpts ? { ...callOpts.ctx,
418
- ...nextOpts.ctx
419
- } : callOpts.ctx,
420
- input: nextOpts && 'input' in nextOpts ? nextOpts.input : callOpts.input
421
- });
422
- }
423
- });
424
- return result;
425
- } catch (cause) {
426
- return {
427
- ok: false,
428
- error: TRPCError.getErrorFromUnknown(cause),
429
- marker: middlewareMarker
430
- };
431
- }
432
- }; // there's always at least one "next" since we wrap this.resolver in a middleware
402
+ meta(meta) {
403
+ return createNewBuilder(_def, {
404
+ meta: meta
405
+ });
406
+ },
433
407
 
408
+ /** @deprecated **/
409
+ resolve(resolver) {
410
+ return createResolver(_def, resolver);
411
+ },
434
412
 
435
- const result = await callRecursive();
413
+ unstable_concat(builder) {
414
+ return createNewBuilder(_def, builder._def);
415
+ },
436
416
 
437
- if (!result) {
438
- throw new TRPCError.TRPCError({
439
- code: 'INTERNAL_SERVER_ERROR',
440
- message: 'No result from middlewares - did you forget to `return next()`?'
417
+ use(middleware) {
418
+ return createNewBuilder(_def, {
419
+ middlewares: [middleware]
441
420
  });
442
- }
421
+ },
443
422
 
444
- if (!result.ok) {
445
- // re-throw original error
446
- throw result.error;
423
+ query(resolver) {
424
+ return createResolver({ ..._def,
425
+ query: true
426
+ }, resolver);
427
+ },
428
+
429
+ mutation(resolver) {
430
+ return createResolver({ ..._def,
431
+ mutation: true
432
+ }, resolver);
433
+ },
434
+
435
+ subscription(resolver) {
436
+ return createResolver({ ..._def,
437
+ subscription: true
438
+ }, resolver);
447
439
  }
448
440
 
449
- return result.data;
450
441
  };
451
-
452
- procedure._def = _def;
453
- procedure.meta = _def.meta;
454
- return procedure;
455
442
  }
456
-
457
443
  function createInputMiddleware(parse) {
458
444
  return async function inputMiddleware({
459
445
  next,
@@ -503,7 +489,7 @@ function createOutputMiddleware(parse) {
503
489
  }
504
490
 
505
491
  function createResolver(_def, resolver) {
506
- const finalBuilder = createNewInternalBuilder(_def, {
492
+ const finalBuilder = createNewBuilder(_def, {
507
493
  resolver,
508
494
  middlewares: [async function resolveMiddleware(opts) {
509
495
  const data = await resolver(opts);
@@ -517,72 +503,85 @@ function createResolver(_def, resolver) {
517
503
  });
518
504
  return createProcedureCaller(finalBuilder._def);
519
505
  }
506
+ /**
507
+ * @internal
508
+ */
520
509
 
521
- function createInternalBuilder(initDef) {
522
- const _def = initDef || {
523
- middlewares: []
524
- };
525
510
 
526
- return {
527
- _def,
511
+ const codeblock = /*#__PURE__*/`
512
+ If you want to call this function on the server, you do the following:
513
+ This is a client-only function.
528
514
 
529
- // _call: createProcedureCaller(_def),
530
- input(input) {
531
- const parser = getParseFn(input);
532
- return createNewInternalBuilder(_def, {
533
- input,
534
- middlewares: [createInputMiddleware(parser)]
535
- });
536
- },
515
+ const caller = appRouter.createCaller({
516
+ /* ... your context */
517
+ });
537
518
 
538
- output(output) {
539
- const parseOutput = getParseFn(output);
540
- return createNewInternalBuilder(_def, {
541
- output,
542
- middlewares: [createOutputMiddleware(parseOutput)]
543
- });
544
- },
519
+ const result = await caller.call('myProcedure', input);
520
+ `.trim();
545
521
 
546
- meta(meta) {
547
- return createNewInternalBuilder(_def, {
548
- meta
549
- });
550
- },
522
+ function createProcedureCaller(_def) {
523
+ const procedure = async function resolve(opts) {
524
+ // is direct server-side call
525
+ if (!opts || !('rawInput' in opts)) {
526
+ throw new Error(codeblock);
527
+ } // run the middlewares recursively with the resolver as the last one
551
528
 
552
- /** @deprecated **/
553
- resolve(resolver) {
554
- return createResolver(_def, resolver);
555
- },
556
529
 
557
- unstable_concat(builder) {
558
- return createNewInternalBuilder(_def, builder._def);
559
- },
530
+ const callRecursive = async (callOpts = {
531
+ index: 0,
532
+ ctx: opts.ctx
533
+ }) => {
534
+ try {
535
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
536
+ const middleware = _def.middlewares[callOpts.index];
537
+ const result = await middleware({
538
+ ctx: callOpts.ctx,
539
+ type: opts.type,
540
+ path: opts.path,
541
+ rawInput: opts.rawInput,
542
+ meta: _def.meta,
543
+ input: callOpts.input,
544
+ next: async nextOpts => {
545
+ return await callRecursive({
546
+ index: callOpts.index + 1,
547
+ ctx: nextOpts && 'ctx' in nextOpts ? { ...callOpts.ctx,
548
+ ...nextOpts.ctx
549
+ } : callOpts.ctx,
550
+ input: nextOpts && 'input' in nextOpts ? nextOpts.input : callOpts.input
551
+ });
552
+ }
553
+ });
554
+ return result;
555
+ } catch (cause) {
556
+ return {
557
+ ok: false,
558
+ error: TRPCError.getErrorFromUnknown(cause),
559
+ marker: middlewareMarker
560
+ };
561
+ }
562
+ }; // there's always at least one "next" since we wrap this.resolver in a middleware
560
563
 
561
- use(middleware) {
562
- return createNewInternalBuilder(_def, {
563
- middlewares: [middleware]
564
- });
565
- },
566
564
 
567
- query(resolver) {
568
- return createResolver({ ..._def,
569
- query: true
570
- }, resolver);
571
- },
565
+ const result = await callRecursive();
572
566
 
573
- mutation(resolver) {
574
- return createResolver({ ..._def,
575
- mutation: true
576
- }, resolver);
577
- },
567
+ if (!result) {
568
+ throw new TRPCError.TRPCError({
569
+ code: 'INTERNAL_SERVER_ERROR',
570
+ message: 'No result from middlewares - did you forget to `return next()`?'
571
+ });
572
+ }
578
573
 
579
- subscription(resolver) {
580
- return createResolver({ ..._def,
581
- subscription: true
582
- }, resolver);
574
+ if (!result.ok) {
575
+ // re-throw original error
576
+ throw result.error;
583
577
  }
584
578
 
579
+ return result.data;
585
580
  };
581
+
582
+ procedure._def = _def;
583
+ procedure.meta = _def.meta;
584
+ return procedure;
586
585
  }
587
586
 
588
587
  /**
@@ -593,15 +592,15 @@ function omitPrototype(obj1) {
593
592
  return Object.assign(Object.create(null), obj1);
594
593
  }
595
594
 
596
- /* eslint-disable @typescript-eslint/ban-types */
595
+ const procedureTypes = ['query', 'mutation', 'subscription'];
596
+ /**
597
+ * @public
598
+ */
597
599
 
598
- function createRouterProxy(callback) {
599
- return new Proxy({}, {
600
- get(_, path) {
601
- return (...args) => callback(path, ...args);
602
- }
600
+ /* eslint-disable @typescript-eslint/ban-types */
603
601
 
604
- });
602
+ function isRouter(procedureOrRouter) {
603
+ return 'router' in procedureOrRouter._def;
605
604
  }
606
605
 
607
606
  const emptyRouter = {
@@ -629,9 +628,8 @@ function createRouterFactory(defaults) {
629
628
  for (const [key, procedureOrRouter] of Object.entries(procedures !== null && procedures !== void 0 ? procedures : {})) {
630
629
  const newPath = `${path}${key}`;
631
630
 
632
- if (typeof procedureOrRouter === 'object') {
633
- const router = procedureOrRouter;
634
- recursiveGetPaths(router._def.procedures, `${newPath}.`);
631
+ if (isRouter(procedureOrRouter)) {
632
+ recursiveGetPaths(procedureOrRouter._def.procedures, `${newPath}.`);
635
633
  continue;
636
634
  }
637
635
 
@@ -665,68 +663,45 @@ function createRouterFactory(defaults) {
665
663
  [key]: val
666
664
  }), {})
667
665
  };
668
-
669
- function callProcedure(opts) {
670
- const {
671
- type,
672
- path
673
- } = opts;
674
-
675
- if (!(path in _def.procedures) || !_def.procedures[path]['_def'][type]) {
676
- throw new TRPCError.TRPCError({
677
- code: 'NOT_FOUND',
678
- message: `No "${type}"-procedure on path "${path}"`
679
- });
680
- }
681
-
682
- const procedure = _def.procedures[path];
683
- return procedure(opts);
684
- }
685
-
686
666
  const router = { ...opts,
687
667
  _def,
688
668
  transformer: _def.transformer,
689
669
  errorFormatter: _def.errorFormatter,
690
670
 
691
671
  createCaller(ctx) {
692
- return {
693
- query: (path, ...args) => callProcedure({
694
- path,
695
- rawInput: args[0],
696
- ctx,
697
- type: 'query'
698
- }),
699
- mutation: (path, ...args) => callProcedure({
700
- path,
701
- rawInput: args[0],
702
- ctx,
703
- type: 'mutation'
704
- }),
705
- subscription: (path, ...args) => callProcedure({
706
- path,
672
+ const proxy = index.createProxy(({
673
+ path,
674
+ args
675
+ }) => {
676
+ // interop mode
677
+ if (path.length === 1 && procedureTypes.includes(path[0])) {
678
+ return callProcedure({
679
+ procedures: _def.procedures,
680
+ path: args[0],
681
+ rawInput: args[1],
682
+ ctx,
683
+ type: path[0]
684
+ });
685
+ }
686
+
687
+ const fullPath = path.join('.');
688
+ const procedure = _def.procedures[fullPath];
689
+ let type = 'query';
690
+
691
+ if (procedure._def.mutation) {
692
+ type = 'mutation';
693
+ } else if (procedure._def.subscription) {
694
+ type = 'subscription';
695
+ }
696
+
697
+ return procedure({
698
+ path: fullPath,
707
699
  rawInput: args[0],
708
700
  ctx,
709
- type: 'subscription'
710
- }),
711
- queries: createRouterProxy((path, rawInput) => callProcedure({
712
- path,
713
- rawInput,
714
- ctx,
715
- type: 'query'
716
- })),
717
- mutations: createRouterProxy((path, rawInput) => callProcedure({
718
- path,
719
- rawInput,
720
- ctx,
721
- type: 'mutation'
722
- })),
723
- subscriptions: createRouterProxy((path, rawInput) => callProcedure({
724
- path,
725
- rawInput,
726
- ctx,
727
- type: 'subscription'
728
- }))
729
- };
701
+ type
702
+ });
703
+ });
704
+ return proxy;
730
705
  },
731
706
 
732
707
  getErrorShape(opts) {
@@ -763,6 +738,28 @@ function createRouterFactory(defaults) {
763
738
  return router;
764
739
  };
765
740
  }
741
+ /**
742
+ * @internal
743
+ */
744
+
745
+ function callProcedure(opts) {
746
+ var _opts$procedures$path;
747
+
748
+ const {
749
+ type,
750
+ path
751
+ } = opts;
752
+
753
+ if (!(path in opts.procedures) || !((_opts$procedures$path = opts.procedures[path]) !== null && _opts$procedures$path !== void 0 && _opts$procedures$path._def[type])) {
754
+ throw new TRPCError.TRPCError({
755
+ code: 'NOT_FOUND',
756
+ message: `No "${type}"-procedure on path "${path}"`
757
+ });
758
+ }
759
+
760
+ const procedure = opts.procedures[path];
761
+ return procedure(opts);
762
+ }
766
763
 
767
764
  function migrateProcedure(oldProc, type) {
768
765
  const def = oldProc._def();
@@ -770,7 +767,7 @@ function migrateProcedure(oldProc, type) {
770
767
  const inputParser = getParseFnOrPassThrough(def.inputParser);
771
768
  const outputParser = getParseFnOrPassThrough(def.outputParser);
772
769
  const inputMiddleware = createInputMiddleware(inputParser);
773
- const builder = createInternalBuilder({
770
+ const builder = createBuilder({
774
771
  input: def.inputParser,
775
772
  middlewares: [...def.middlewares, inputMiddleware, createOutputMiddleware(outputParser)],
776
773
  meta: def.meta,
@@ -1138,10 +1135,6 @@ function mergeRoutersGeneric(...args) {
1138
1135
  return mergeRouters(...args);
1139
1136
  }
1140
1137
 
1141
- function createBuilder() {
1142
- return createInternalBuilder();
1143
- }
1144
-
1145
1138
  /**
1146
1139
  * @internal
1147
1140
  */
@@ -1221,7 +1214,9 @@ function initTRPC() {
1221
1214
  exports.assertNotBrowser = resolveHTTPResponse.assertNotBrowser;
1222
1215
  exports.resolveHTTPResponse = resolveHTTPResponse.resolveHTTPResponse;
1223
1216
  exports.TRPCError = TRPCError.TRPCError;
1217
+ exports.callProcedure = callProcedure;
1224
1218
  exports.defaultTransformer = defaultTransformer;
1225
1219
  exports.getDataTransformer = getDataTransformer$1;
1226
1220
  exports.initTRPC = initTRPC;
1221
+ exports.procedureTypes = procedureTypes;
1227
1222
  exports.router = router;