dev(extensions): add extension.use

The extension.use method allows registering routers and generic handlers
(not wrapped by eggspress) to express.
This commit is contained in:
KernelDeimos
2025-09-26 17:30:09 -04:00
parent 601eb019a2
commit d5a2a34f53
2 changed files with 20 additions and 5 deletions
+8
View File
@@ -218,6 +218,14 @@ class Extension extends AdvancedBase {
methods: ['POST'],
});
}
use (...args) {
this.ensure_service_();
this.service.expressThings_.push({
type: 'router',
value: args,
});
}
get preinit() {
return (function (callback) {
+12 -5
View File
@@ -36,7 +36,7 @@ class ExtensionServiceState extends AdvancedBase {
this.extension = a[0].extension;
this.endpoints_ = [];
this.expressThings_ = [];
// Values shared between the `extension` global and its service
this.values = new Context();
@@ -66,7 +66,7 @@ class ExtensionServiceState extends AdvancedBase {
...(options.subdomain ? { subdomain: options.subdomain } : {}),
});
this.endpoints_.push(endpoint);
this.expressThings_.push({ type: 'endpoint', value: endpoint });
}
}
@@ -77,7 +77,7 @@ class ExtensionServiceState extends AdvancedBase {
*/
class ExtensionService extends BaseService {
_construct () {
this.endpoints_ = [];
this.expressThings_ = [];
}
async _init (args) {
this.state = args.state;
@@ -170,8 +170,15 @@ class ExtensionService extends BaseService {
['__on_install.routes'] (_, { app }) {
if ( ! this.state ) debugger;
for ( const endpoint of this.state.endpoints_ ) {
endpoint.attach(app);
for ( const thing of this.state.expressThings_ ) {
if ( thing.type === 'endpoint' ) {
thing.value.attach(app);
continue;
}
if ( thing.type === 'router' ) {
app.use(...thing.value);
continue;
}
}
}