effect-orpc 1.0.0-effect-v4.2 → 1.0.0-effect-v4.4

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/dist/index.js CHANGED
@@ -1,15 +1,13 @@
1
1
  import {
2
2
  getCurrentServices
3
- } from "./chunk-E5YLLTJI.js";
3
+ } from "./chunk-I5EWBI42.js";
4
+
5
+ // src/contract.ts
6
+ import { isContractProcedure as isContractProcedure2 } from "@orpc/contract";
7
+ import { implement } from "@orpc/server";
4
8
 
5
9
  // src/effect-builder.ts
6
- import {
7
- mergeMeta as mergeMeta2,
8
- mergePrefix as mergePrefix2,
9
- mergeRoute as mergeRoute2,
10
- mergeTags,
11
- ORPCError as ORPCError2
12
- } from "@orpc/contract";
10
+ import { mergeMeta as mergeMeta2, mergePrefix as mergePrefix2, mergeRoute as mergeRoute2, mergeTags } from "@orpc/contract";
13
11
  import {
14
12
  addMiddleware as addMiddleware2,
15
13
  Builder,
@@ -17,7 +15,6 @@ import {
17
15
  fallbackConfig,
18
16
  lazy as lazy2
19
17
  } from "@orpc/server";
20
- import { Cause as Cause2, Effect, Exit, ServiceMap } from "effect";
21
18
 
22
19
  // src/effect-enhance-router.ts
23
20
  import {
@@ -323,6 +320,89 @@ function enhanceEffectRouter(router, options) {
323
320
  return enhanced;
324
321
  }
325
322
 
323
+ // src/effect-runtime.ts
324
+ import { ORPCError as ORPCError2 } from "@orpc/contract";
325
+ import { Cause as Cause2, Effect, Exit, Result, Context } from "effect";
326
+ function toORPCErrorFromCause(cause) {
327
+ if (Cause2.hasFails(cause)) {
328
+ const reason = Cause2.findFail(cause);
329
+ if (Result.isFailure(reason)) {
330
+ return new ORPCError2("INTERNAL_SERVER_ERROR");
331
+ }
332
+ const error = reason.success.error;
333
+ if (isORPCTaggedError(error)) {
334
+ return error.toORPCError();
335
+ }
336
+ if (error instanceof ORPCError2) {
337
+ return error;
338
+ }
339
+ return new ORPCError2("INTERNAL_SERVER_ERROR", {
340
+ cause: error
341
+ });
342
+ }
343
+ if (Cause2.hasDies(cause)) {
344
+ const reason = Cause2.findDie(cause);
345
+ if (Result.isFailure(reason)) {
346
+ return new ORPCError2("INTERNAL_SERVER_ERROR", {
347
+ cause: new Error(`Died by unknown reason`)
348
+ });
349
+ }
350
+ return new ORPCError2("INTERNAL_SERVER_ERROR", {
351
+ cause: reason.success.defect
352
+ });
353
+ }
354
+ if (Cause2.hasInterrupts(cause)) {
355
+ const reason = Cause2.findInterrupt(cause);
356
+ if (Result.isFailure(reason)) {
357
+ return new ORPCError2("INTERNAL_SERVER_ERROR", {
358
+ cause: new Error(`Unknown fiber got interrupted`)
359
+ });
360
+ }
361
+ return new ORPCError2("INTERNAL_SERVER_ERROR", {
362
+ cause: new Error(`${reason.success.fiberId} got interrupted`)
363
+ });
364
+ }
365
+ return new ORPCError2("INTERNAL_SERVER_ERROR");
366
+ }
367
+ function createEffectProcedureHandler(options) {
368
+ const {
369
+ runtime,
370
+ effectErrorMap,
371
+ effectFn,
372
+ spanConfig,
373
+ defaultCaptureStackTrace
374
+ } = options;
375
+ return async (opts) => {
376
+ const effectOpts = {
377
+ context: opts.context,
378
+ input: opts.input,
379
+ path: opts.path,
380
+ procedure: opts.procedure,
381
+ signal: opts.signal,
382
+ lastEventId: opts.lastEventId,
383
+ errors: createEffectErrorConstructorMap(effectErrorMap)
384
+ };
385
+ const spanName = spanConfig?.name ?? opts.path.join(".");
386
+ const captureStackTrace = spanConfig?.captureStackTrace ?? defaultCaptureStackTrace;
387
+ const resolver = Effect.fnUntraced(effectFn);
388
+ const tracedEffect = Effect.withSpan(resolver(effectOpts), spanName, {
389
+ captureStackTrace
390
+ });
391
+ const parentServices = getCurrentServices();
392
+ const exit = parentServices ? await Effect.runPromiseExitWith(
393
+ Context.merge(await runtime.context(), parentServices)
394
+ )(tracedEffect, {
395
+ signal: opts.signal
396
+ }) : await runtime.runPromiseExit(tracedEffect, {
397
+ signal: opts.signal
398
+ });
399
+ if (Exit.isFailure(exit)) {
400
+ throw toORPCErrorFromCause(exit.cause);
401
+ }
402
+ return exit.value;
403
+ };
404
+ }
405
+
326
406
  // src/effect-builder.ts
327
407
  function addSpanStackTrace() {
328
408
  const ErrorConstructor = Error;
@@ -344,32 +424,6 @@ function addSpanStackTrace() {
344
424
  }
345
425
  };
346
426
  }
347
- function toORPCErrorFromCause(cause) {
348
- const reason = cause.reasons[0];
349
- if (reason === void 0) {
350
- return new ORPCError2("INTERNAL_SERVER_ERROR");
351
- }
352
- if (Cause2.isDieReason(reason)) {
353
- return new ORPCError2("INTERNAL_SERVER_ERROR", {
354
- cause: reason.defect
355
- });
356
- }
357
- if (Cause2.isFailReason(reason)) {
358
- const error = reason.error;
359
- if (isORPCTaggedError(error)) {
360
- return error.toORPCError();
361
- }
362
- if (error instanceof ORPCError2) {
363
- return error;
364
- }
365
- return new ORPCError2("INTERNAL_SERVER_ERROR", {
366
- cause: error
367
- });
368
- }
369
- return new ORPCError2("INTERNAL_SERVER_ERROR", {
370
- cause: new Error(`${reason.fiberId} Interrupted`)
371
- });
372
- }
373
427
  var EffectBuilder = class _EffectBuilder {
374
428
  constructor(def) {
375
429
  const { runtime, spanConfig, effectErrorMap, ...orpcDef } = def;
@@ -462,6 +516,14 @@ var EffectBuilder = class _EffectBuilder {
462
516
  inputSchema: initialInputSchema
463
517
  });
464
518
  }
519
+ /**
520
+ * Creates a middleware.
521
+ *
522
+ * @see {@link https://orpc.dev/docs/middleware Middleware Docs}
523
+ */
524
+ middleware(middleware) {
525
+ return decorateMiddleware2(middleware);
526
+ }
465
527
  /**
466
528
  * Adds type-safe custom errors.
467
529
  * Supports both traditional oRPC error definitions and ORPCTaggedError classes.
@@ -609,35 +671,13 @@ var EffectBuilder = class _EffectBuilder {
609
671
  return new EffectDecoratedProcedure({
610
672
  ...this["~effect"],
611
673
  handler: async (opts) => {
612
- const effectOpts = {
613
- context: opts.context,
614
- input: opts.input,
615
- path: opts.path,
616
- procedure: opts.procedure,
617
- signal: opts.signal,
618
- lastEventId: opts.lastEventId,
619
- errors: createEffectErrorConstructorMap(
620
- this["~effect"].effectErrorMap
621
- )
622
- };
623
- const spanName = spanConfig?.name ?? opts.path.join(".");
624
- const captureStackTrace = spanConfig?.captureStackTrace ?? defaultCaptureStackTrace;
625
- const resolver = Effect.fnUntraced(effectFn);
626
- const tracedEffect = Effect.withSpan(resolver(effectOpts), spanName, {
627
- captureStackTrace
628
- });
629
- const parentServices = getCurrentServices();
630
- const exit = parentServices ? await Effect.runPromiseExitWith(
631
- ServiceMap.merge(await runtime.services(), parentServices)
632
- )(tracedEffect, {
633
- signal: opts.signal
634
- }) : await runtime.runPromiseExit(tracedEffect, {
635
- signal: opts.signal
636
- });
637
- if (Exit.isFailure(exit)) {
638
- throw toORPCErrorFromCause(exit.cause);
639
- }
640
- return exit.value;
674
+ return createEffectProcedureHandler({
675
+ runtime,
676
+ effectErrorMap: this["~effect"].effectErrorMap,
677
+ effectFn,
678
+ spanConfig,
679
+ defaultCaptureStackTrace
680
+ })(opts);
641
681
  }
642
682
  });
643
683
  }
@@ -710,6 +750,256 @@ function emptyBuilder() {
710
750
  dedupeLeadingMiddlewares: true
711
751
  });
712
752
  }
753
+
754
+ // src/eoc.ts
755
+ import { isContractProcedure, oc } from "@orpc/contract";
756
+ var effectContractSymbol = /* @__PURE__ */ Symbol.for(
757
+ "@orpc/effect/contract"
758
+ );
759
+ function isWrappableContractBuilder(value) {
760
+ return typeof value === "object" && value !== null && "~orpc" in value;
761
+ }
762
+ function mergeEffectErrorMaps(left, right) {
763
+ if (!left) {
764
+ return right;
765
+ }
766
+ if (!right) {
767
+ return left;
768
+ }
769
+ return {
770
+ ...left,
771
+ ...right
772
+ };
773
+ }
774
+ function setEffectContractErrorMap(value, effectErrorMap) {
775
+ if (!effectErrorMap) {
776
+ return;
777
+ }
778
+ Object.defineProperty(value, effectContractSymbol, {
779
+ value: { errorMap: effectErrorMap },
780
+ enumerable: false,
781
+ configurable: true
782
+ });
783
+ }
784
+ function getEffectContractErrorMap(value) {
785
+ if (typeof value !== "object" || value === null) {
786
+ return void 0;
787
+ }
788
+ return value[effectContractSymbol]?.errorMap;
789
+ }
790
+ function applyEffectContractErrorMapToRouter(router, source, inheritedEffectErrorMap) {
791
+ const routerRecord = router;
792
+ const sourceRecord = source;
793
+ for (const key of Object.keys(routerRecord)) {
794
+ const routerValue = routerRecord[key];
795
+ const sourceValue = sourceRecord && typeof sourceRecord === "object" ? sourceRecord[key] : void 0;
796
+ if (!routerValue) {
797
+ continue;
798
+ }
799
+ if (isContractProcedure(routerValue)) {
800
+ const sourceEffectErrorMap = getEffectContractErrorMap(sourceValue);
801
+ setEffectContractErrorMap(
802
+ routerValue,
803
+ mergeEffectErrorMaps(inheritedEffectErrorMap, sourceEffectErrorMap)
804
+ );
805
+ continue;
806
+ }
807
+ if (typeof routerValue === "object") {
808
+ applyEffectContractErrorMapToRouter(
809
+ routerValue,
810
+ sourceValue,
811
+ inheritedEffectErrorMap
812
+ );
813
+ }
814
+ }
815
+ }
816
+ function wrapEffectContractBuilder(builder, inheritedEffectErrorMap) {
817
+ const currentEffectErrorMap = inheritedEffectErrorMap ?? getEffectContractErrorMap(builder);
818
+ if (typeof builder === "object" && builder !== null) {
819
+ setEffectContractErrorMap(builder, currentEffectErrorMap);
820
+ }
821
+ const proxy = new Proxy(builder, {
822
+ get(target, prop, receiver) {
823
+ if (prop === effectContractSymbol) {
824
+ return currentEffectErrorMap ? { errorMap: currentEffectErrorMap } : void 0;
825
+ }
826
+ if (prop === "errors") {
827
+ return (errors) => {
828
+ const nextEffectErrorMap = mergeEffectErrorMaps(
829
+ currentEffectErrorMap,
830
+ errors
831
+ );
832
+ return wrapEffectContractBuilder(
833
+ Reflect.apply(Reflect.get(target, prop, receiver), target, [
834
+ effectErrorMapToErrorMap(errors)
835
+ ]),
836
+ nextEffectErrorMap
837
+ );
838
+ };
839
+ }
840
+ if (prop === "router") {
841
+ return (router) => {
842
+ const result = Reflect.apply(
843
+ Reflect.get(target, prop, receiver),
844
+ target,
845
+ [router]
846
+ );
847
+ applyEffectContractErrorMapToRouter(
848
+ result,
849
+ router,
850
+ currentEffectErrorMap
851
+ );
852
+ return result;
853
+ };
854
+ }
855
+ const value = Reflect.get(target, prop, receiver);
856
+ if (typeof value !== "function") {
857
+ return value;
858
+ }
859
+ return (...args) => {
860
+ const result = Reflect.apply(value, target, args);
861
+ return isWrappableContractBuilder(result) ? wrapEffectContractBuilder(result, currentEffectErrorMap) : result;
862
+ };
863
+ }
864
+ });
865
+ setEffectContractErrorMap(proxy, currentEffectErrorMap);
866
+ return proxy;
867
+ }
868
+ var eoc = wrapEffectContractBuilder(
869
+ oc,
870
+ {}
871
+ );
872
+
873
+ // src/contract.ts
874
+ var CONTRACT_HIDDEN_METHODS = /* @__PURE__ */ new Set([
875
+ "$config",
876
+ "$context",
877
+ "$input",
878
+ "$meta",
879
+ "$route",
880
+ "errors",
881
+ "input",
882
+ "lazy",
883
+ "meta",
884
+ "middleware",
885
+ "output",
886
+ "prefix",
887
+ "route",
888
+ "router",
889
+ "tag"
890
+ ]);
891
+ function makeEnhanceOptions(runtime) {
892
+ return {
893
+ middlewares: [],
894
+ errorMap: {},
895
+ dedupeLeadingMiddlewares: true,
896
+ runtime
897
+ };
898
+ }
899
+ function wrapContractNode(contract, target, runtime) {
900
+ const cache = /* @__PURE__ */ new Map();
901
+ return new Proxy(target, {
902
+ get(currentTarget, prop, receiver) {
903
+ if (cache.has(prop)) {
904
+ return cache.get(prop);
905
+ }
906
+ if (isContractProcedure2(contract)) {
907
+ if (prop === "effect") {
908
+ const effect = (effectFn) => {
909
+ const effectErrorMap = getEffectContractErrorMap(contract) ?? currentTarget["~orpc"].errorMap;
910
+ return new EffectDecoratedProcedure({
911
+ ...currentTarget["~orpc"],
912
+ errorMap: effectErrorMapToErrorMap(effectErrorMap),
913
+ effectErrorMap,
914
+ runtime,
915
+ handler: createEffectProcedureHandler({
916
+ runtime,
917
+ effectErrorMap,
918
+ effectFn,
919
+ defaultCaptureStackTrace: addSpanStackTrace()
920
+ })
921
+ });
922
+ };
923
+ cache.set(prop, effect);
924
+ return effect;
925
+ }
926
+ if (prop === "use") {
927
+ const use = (...args) => wrapContractNode(
928
+ contract,
929
+ Reflect.apply(
930
+ Reflect.get(currentTarget, prop, currentTarget),
931
+ currentTarget,
932
+ args
933
+ ),
934
+ runtime
935
+ );
936
+ cache.set(prop, use);
937
+ return use;
938
+ }
939
+ if (CONTRACT_HIDDEN_METHODS.has(String(prop))) {
940
+ return void 0;
941
+ }
942
+ } else {
943
+ if (prop === "$context" || prop === "$config" || prop === "use") {
944
+ const wrappedMethod = (...args) => wrapContractNode(
945
+ contract,
946
+ Reflect.apply(
947
+ Reflect.get(currentTarget, prop, currentTarget),
948
+ currentTarget,
949
+ args
950
+ ),
951
+ runtime
952
+ );
953
+ cache.set(prop, wrappedMethod);
954
+ return wrappedMethod;
955
+ }
956
+ if (prop === "router" || prop === "lazy") {
957
+ const wrappedMethod = (...args) => enhanceEffectRouter(
958
+ Reflect.apply(
959
+ Reflect.get(currentTarget, prop, currentTarget),
960
+ currentTarget,
961
+ args
962
+ ),
963
+ makeEnhanceOptions(runtime)
964
+ );
965
+ cache.set(prop, wrappedMethod);
966
+ return wrappedMethod;
967
+ }
968
+ if (typeof prop === "string" && prop in contract) {
969
+ const child = wrapContractNode(
970
+ contract[prop],
971
+ Reflect.get(currentTarget, prop, receiver),
972
+ runtime
973
+ );
974
+ cache.set(prop, child);
975
+ return child;
976
+ }
977
+ }
978
+ const value = Reflect.get(currentTarget, prop, receiver);
979
+ return typeof value === "function" ? value.bind(currentTarget) : value;
980
+ },
981
+ has(currentTarget, prop) {
982
+ if (isContractProcedure2(contract)) {
983
+ if (prop === "effect") {
984
+ return true;
985
+ }
986
+ if (CONTRACT_HIDDEN_METHODS.has(String(prop))) {
987
+ return false;
988
+ }
989
+ } else if (typeof prop === "string" && prop in contract) {
990
+ return true;
991
+ }
992
+ return Reflect.has(currentTarget, prop);
993
+ }
994
+ });
995
+ }
996
+ function implementEffect(contract, runtime) {
997
+ return wrapContractNode(
998
+ contract,
999
+ implement(contract),
1000
+ runtime
1001
+ );
1002
+ }
713
1003
  export {
714
1004
  EffectBuilder,
715
1005
  EffectDecoratedProcedure,
@@ -718,6 +1008,8 @@ export {
718
1008
  addSpanStackTrace,
719
1009
  createEffectErrorConstructorMap,
720
1010
  effectErrorMapToErrorMap,
1011
+ eoc,
1012
+ implementEffect,
721
1013
  isORPCTaggedError,
722
1014
  isORPCTaggedErrorClass,
723
1015
  makeEffectORPC,