@emeryld/rrroutes-server 2.6.7 → 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.js CHANGED
@@ -497,7 +497,8 @@ function getRegisteredRouteEntriesStore(router) {
497
497
  function toParsedRouteSegments(path) {
498
498
  const segments = path.split("/").filter(Boolean);
499
499
  return segments.map((segment) => {
500
- if (segment.startsWith("*") && segment.length > 1) return { kind: "wildcard" };
500
+ if (segment.startsWith("*") && segment.length > 1)
501
+ return { kind: "wildcard" };
501
502
  if (segment.startsWith(":") && segment.length > 1) return { kind: "param" };
502
503
  return { kind: "static", value: segment };
503
504
  });
@@ -615,7 +616,7 @@ function createRRRoute(router, config) {
615
616
  });
616
617
  });
617
618
  };
618
- function register(leaf, def) {
619
+ function register(leaf, def, globalBefore = []) {
619
620
  const method = leaf.method;
620
621
  const methodUpper = method.toUpperCase();
621
622
  const path = leaf.path;
@@ -641,7 +642,11 @@ function createRRRoute(router, config) {
641
642
  const emit = (event) => activeEmit(event, debugName);
642
643
  const isVerboseDebug = activeDebugMode === "complete";
643
644
  const origin = getRegistrationOrigin();
644
- const shadowingEntry = findShadowingPriorRoute(registeredEntries, methodUpper, path);
645
+ const shadowingEntry = findShadowingPriorRoute(
646
+ registeredEntries,
647
+ methodUpper,
648
+ path
649
+ );
645
650
  if (shadowingEntry) {
646
651
  console.warn(
647
652
  formatRouteShadowWarning({
@@ -653,6 +658,9 @@ function createRRRoute(router, config) {
653
658
  );
654
659
  }
655
660
  emit({ type: "register", method: methodUpper, path });
661
+ const globalRouteSpecific = globalBefore.map(
662
+ (mw) => adaptRouteBeforeMw(mw)
663
+ );
656
664
  const routeSpecific = (def?.before ?? []).map(
657
665
  (mw) => adaptRouteBeforeMw(mw)
658
666
  );
@@ -775,6 +783,7 @@ function createRRRoute(router, config) {
775
783
  resolvePayloadMw,
776
784
  ctxMw,
777
785
  ...postCtxMws,
786
+ ...globalRouteSpecific,
778
787
  ...routeSpecific
779
788
  ];
780
789
  const wrapped = async (req, res, next) => {
@@ -903,7 +912,8 @@ function createRRRoute(router, config) {
903
912
  registeredEntries.push({ method: methodUpper, path, key, origin });
904
913
  registeredDefs.set(key, {
905
914
  leaf,
906
- def
915
+ def,
916
+ globalBefore
907
917
  });
908
918
  }
909
919
  async function invoke(key, args) {
@@ -911,7 +921,7 @@ function createRRRoute(router, config) {
911
921
  if (!registration) {
912
922
  throw new Error(`No controller registered for route: ${key}`);
913
923
  }
914
- const { leaf, def } = registration;
924
+ const { leaf, def, globalBefore } = registration;
915
925
  const req = args.req;
916
926
  const res = args.res;
917
927
  const next = args.next ?? (() => void 0);
@@ -946,6 +956,14 @@ function createRRRoute(router, config) {
946
956
  res
947
957
  });
948
958
  res.locals[CTX_SYMBOL] = ctx;
959
+ for (const before of globalBefore) {
960
+ await runRouteBeforeHandler(before, {
961
+ req,
962
+ res,
963
+ ctx,
964
+ ...payload
965
+ });
966
+ }
949
967
  for (const before of def.before ?? []) {
950
968
  await runRouteBeforeHandler(before, {
951
969
  req,
@@ -967,7 +985,10 @@ function createRRRoute(router, config) {
967
985
  const output = validateOutput && leaf.cfg.outputSchema ? lowProfileParse(leaf.cfg.outputSchema, result) : result;
968
986
  return output;
969
987
  }
970
- function registerControllers(registry, controllers, all) {
988
+ function registerControllers(registry, controllers, allOrOptions) {
989
+ const options = typeof allOrOptions === "object" && allOrOptions !== null ? allOrOptions : void 0;
990
+ const all = typeof allOrOptions === "boolean" ? allOrOptions : Boolean(options?.all);
991
+ const globalBefore = options?.before ?? [];
971
992
  for (const leaf of registry.all) {
972
993
  const key = keyOf(leaf.method, leaf.path, false);
973
994
  knownLeaves.set(key, leaf);
@@ -981,7 +1002,19 @@ function createRRRoute(router, config) {
981
1002
  }
982
1003
  const def = controllers[key];
983
1004
  if (!def) return;
984
- register(leaf, def);
1005
+ const leafTyped = leaf;
1006
+ const method = leafTyped.method.toUpperCase();
1007
+ const path = leafTyped.path;
1008
+ const globalBeforeForLeaf = globalBefore.map((mw) => {
1009
+ const wrapped = (args) => mw({
1010
+ ...args,
1011
+ key,
1012
+ method,
1013
+ path
1014
+ });
1015
+ return wrapped;
1016
+ });
1017
+ register(leafTyped, def, globalBeforeForLeaf);
985
1018
  });
986
1019
  if (all && missingLeaves.length > 0) {
987
1020
  throw new Error(