@triophore/falconjs 1.0.4 → 1.0.5
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/falcon.js +31 -12
- package/package.json +1 -1
package/falcon.js
CHANGED
|
@@ -666,24 +666,43 @@ class Falcon {
|
|
|
666
666
|
try {
|
|
667
667
|
const routeModule = require(filePath);
|
|
668
668
|
|
|
669
|
-
|
|
670
|
-
// OR legacy support: function that takes context (but we need to manually register)
|
|
669
|
+
let handler, options = {}, finalMethod = method, finalPath = routePath;
|
|
671
670
|
|
|
672
|
-
|
|
671
|
+
// NEW PATTERN: module.exports.route = async function(context) { return { ... } }
|
|
672
|
+
if (routeModule.route && typeof routeModule.route === 'function') {
|
|
673
|
+
const routeConfig = await routeModule.route(this.CONTEXT);
|
|
673
674
|
|
|
674
|
-
|
|
675
|
-
|
|
675
|
+
if (!routeConfig) {
|
|
676
|
+
this.CONTEXT["logger"].warn(`Skipping ${relativePath}: route() returned null/undefined`);
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// Extract handler and options from returned config
|
|
681
|
+
handler = routeConfig.handler;
|
|
682
|
+
options = routeConfig.options || {};
|
|
683
|
+
|
|
684
|
+
// Allow optional method/path override from return value
|
|
685
|
+
if (routeConfig.method) {
|
|
686
|
+
finalMethod = routeConfig.method;
|
|
687
|
+
this.CONTEXT["logger"].debug(`Method overridden to ${finalMethod} for ${relativePath}`);
|
|
688
|
+
}
|
|
689
|
+
if (routeConfig.path) {
|
|
690
|
+
finalPath = routeConfig.path;
|
|
691
|
+
this.CONTEXT["logger"].debug(`Path overridden to ${finalPath} for ${relativePath}`);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
// LEGACY PATTERN: Direct handler function export
|
|
695
|
+
else if (typeof routeModule === 'function') {
|
|
676
696
|
handler = routeModule;
|
|
677
|
-
}
|
|
697
|
+
}
|
|
698
|
+
// LEGACY PATTERN: Object with handler/options
|
|
699
|
+
else if (typeof routeModule === 'object') {
|
|
678
700
|
if (routeModule.handler) handler = routeModule.handler;
|
|
679
701
|
if (routeModule.options) options = routeModule.options;
|
|
680
|
-
|
|
681
|
-
// If user exported .route function (legacy), we can't really use it easily with new params
|
|
682
|
-
// unless we assume they migrated. We'll stick to handler/options pattern for new files.
|
|
683
702
|
}
|
|
684
703
|
|
|
685
704
|
if (!handler) {
|
|
686
|
-
this.CONTEXT["logger"].warn(`Skipping ${relativePath}: No handler
|
|
705
|
+
this.CONTEXT["logger"].warn(`Skipping ${relativePath}: No handler found`);
|
|
687
706
|
continue;
|
|
688
707
|
}
|
|
689
708
|
|
|
@@ -705,8 +724,8 @@ class Falcon {
|
|
|
705
724
|
|
|
706
725
|
// Register the route
|
|
707
726
|
this.httpServer.route({
|
|
708
|
-
method:
|
|
709
|
-
path:
|
|
727
|
+
method: finalMethod,
|
|
728
|
+
path: finalPath,
|
|
710
729
|
handler: handler,
|
|
711
730
|
options: {
|
|
712
731
|
tags: [tag],
|