Skip to content

Commit d5a2a34

Browse files
committed
dev(extensions): add extension.use
The extension.use method allows registering routers and generic handlers (not wrapped by eggspress) to express.
1 parent 601eb01 commit d5a2a34

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/backend/src/Extension.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ class Extension extends AdvancedBase {
218218
methods: ['POST'],
219219
});
220220
}
221+
222+
use (...args) {
223+
this.ensure_service_();
224+
this.service.expressThings_.push({
225+
type: 'router',
226+
value: args,
227+
});
228+
}
221229

222230
get preinit() {
223231
return (function (callback) {

src/backend/src/ExtensionService.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ExtensionServiceState extends AdvancedBase {
3636

3737
this.extension = a[0].extension;
3838

39-
this.endpoints_ = [];
39+
this.expressThings_ = [];
4040

4141
// Values shared between the `extension` global and its service
4242
this.values = new Context();
@@ -66,7 +66,7 @@ class ExtensionServiceState extends AdvancedBase {
6666
...(options.subdomain ? { subdomain: options.subdomain } : {}),
6767
});
6868

69-
this.endpoints_.push(endpoint);
69+
this.expressThings_.push({ type: 'endpoint', value: endpoint });
7070
}
7171
}
7272

@@ -77,7 +77,7 @@ class ExtensionServiceState extends AdvancedBase {
7777
*/
7878
class ExtensionService extends BaseService {
7979
_construct () {
80-
this.endpoints_ = [];
80+
this.expressThings_ = [];
8181
}
8282
async _init (args) {
8383
this.state = args.state;
@@ -170,8 +170,15 @@ class ExtensionService extends BaseService {
170170

171171
['__on_install.routes'] (_, { app }) {
172172
if ( ! this.state ) debugger;
173-
for ( const endpoint of this.state.endpoints_ ) {
174-
endpoint.attach(app);
173+
for ( const thing of this.state.expressThings_ ) {
174+
if ( thing.type === 'endpoint' ) {
175+
thing.value.attach(app);
176+
continue;
177+
}
178+
if ( thing.type === 'router' ) {
179+
app.use(...thing.value);
180+
continue;
181+
}
175182
}
176183
}
177184

0 commit comments

Comments
 (0)