@emeryld/rrroutes-server 2.6.8 → 2.6.9

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.cjs CHANGED
@@ -540,7 +540,8 @@ function getRegisteredRouteEntriesStore(router) {
540
540
  function toParsedRouteSegments(path) {
541
541
  const segments = path.split("/").filter(Boolean);
542
542
  return segments.map((segment) => {
543
- if (segment.startsWith("*") && segment.length > 1) return { kind: "wildcard" };
543
+ if (segment.startsWith("*") && segment.length > 1)
544
+ return { kind: "wildcard" };
544
545
  if (segment.startsWith(":") && segment.length > 1) return { kind: "param" };
545
546
  return { kind: "static", value: segment };
546
547
  });
@@ -658,7 +659,7 @@ function createRRRoute(router, config) {
658
659
  });
659
660
  });
660
661
  };
661
- function register(leaf, def) {
662
+ function register(leaf, def, globalBefore = []) {
662
663
  const method = leaf.method;
663
664
  const methodUpper = method.toUpperCase();
664
665
  const path = leaf.path;
@@ -684,7 +685,11 @@ function createRRRoute(router, config) {
684
685
  const emit = (event) => activeEmit(event, debugName);
685
686
  const isVerboseDebug = activeDebugMode === "complete";
686
687
  const origin = getRegistrationOrigin();
687
- const shadowingEntry = findShadowingPriorRoute(registeredEntries, methodUpper, path);
688
+ const shadowingEntry = findShadowingPriorRoute(
689
+ registeredEntries,
690
+ methodUpper,
691
+ path
692
+ );
688
693
  if (shadowingEntry) {
689
694
  console.warn(
690
695
  formatRouteShadowWarning({
@@ -696,6 +701,9 @@ function createRRRoute(router, config) {
696
701
  );
697
702
  }
698
703
  emit({ type: "register", method: methodUpper, path });
704
+ const globalRouteSpecific = globalBefore.map(
705
+ (mw) => adaptRouteBeforeMw(mw)
706
+ );
699
707
  const routeSpecific = (def?.before ?? []).map(
700
708
  (mw) => adaptRouteBeforeMw(mw)
701
709
  );
@@ -818,6 +826,7 @@ function createRRRoute(router, config) {
818
826
  resolvePayloadMw,
819
827
  ctxMw,
820
828
  ...postCtxMws,
829
+ ...globalRouteSpecific,
821
830
  ...routeSpecific
822
831
  ];
823
832
  const wrapped = async (req, res, next) => {
@@ -946,7 +955,8 @@ function createRRRoute(router, config) {
946
955
  registeredEntries.push({ method: methodUpper, path, key, origin });
947
956
  registeredDefs.set(key, {
948
957
  leaf,
949
- def
958
+ def,
959
+ globalBefore
950
960
  });
951
961
  }
952
962
  async function invoke(key, args) {
@@ -954,7 +964,7 @@ function createRRRoute(router, config) {
954
964
  if (!registration) {
955
965
  throw new Error(`No controller registered for route: ${key}`);
956
966
  }
957
- const { leaf, def } = registration;
967
+ const { leaf, def, globalBefore } = registration;
958
968
  const req = args.req;
959
969
  const res = args.res;
960
970
  const next = args.next ?? (() => void 0);
@@ -989,6 +999,14 @@ function createRRRoute(router, config) {
989
999
  res
990
1000
  });
991
1001
  res.locals[CTX_SYMBOL] = ctx;
1002
+ for (const before of globalBefore) {
1003
+ await runRouteBeforeHandler(before, {
1004
+ req,
1005
+ res,
1006
+ ctx,
1007
+ ...payload
1008
+ });
1009
+ }
992
1010
  for (const before of def.before ?? []) {
993
1011
  await runRouteBeforeHandler(before, {
994
1012
  req,
@@ -1010,7 +1028,10 @@ function createRRRoute(router, config) {
1010
1028
  const output = validateOutput && leaf.cfg.outputSchema ? (0, import_rrroutes_contract.lowProfileParse)(leaf.cfg.outputSchema, result) : result;
1011
1029
  return output;
1012
1030
  }
1013
- function registerControllers(registry, controllers, all) {
1031
+ function registerControllers(registry, controllers, allOrOptions) {
1032
+ const options = typeof allOrOptions === "object" && allOrOptions !== null ? allOrOptions : void 0;
1033
+ const all = typeof allOrOptions === "boolean" ? allOrOptions : Boolean(options?.all);
1034
+ const globalBefore = options?.before ?? [];
1014
1035
  for (const leaf of registry.all) {
1015
1036
  const key = (0, import_rrroutes_contract.keyOf)(leaf.method, leaf.path, false);
1016
1037
  knownLeaves.set(key, leaf);
@@ -1024,7 +1045,19 @@ function createRRRoute(router, config) {
1024
1045
  }
1025
1046
  const def = controllers[key];
1026
1047
  if (!def) return;
1027
- register(leaf, def);
1048
+ const leafTyped = leaf;
1049
+ const method = leafTyped.method.toUpperCase();
1050
+ const path = leafTyped.path;
1051
+ const globalBeforeForLeaf = globalBefore.map((mw) => {
1052
+ const wrapped = (args) => mw({
1053
+ ...args,
1054
+ key,
1055
+ method,
1056
+ path
1057
+ });
1058
+ return wrapped;
1059
+ });
1060
+ register(leafTyped, def, globalBeforeForLeaf);
1028
1061
  });
1029
1062
  if (all && missingLeaves.length > 0) {
1030
1063
  throw new Error(