Skip to content

Commit 857c17f

Browse files
committed
fix: add support for pending endpoint registrations in ExpressServer
1 parent 7997475 commit 857c17f

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

adminforth/servers/express.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Express } from 'express';
55
import type { AnySchemaObject } from 'ajv';
66
import { apiReference } from '@scalar/express-api-reference';
77
import fetch from 'node-fetch';
8-
import { AdminUserAuthorizeFunction, IAdminForth, IAdminForthExpressRouteSchema, IExpressHttpServer, HttpExtra } from '../types/Back.js';
8+
import { AdminUserAuthorizeFunction, IAdminForth, IAdminForthEndpointOptions, IAdminForthExpressRouteSchema, IExpressHttpServer, HttpExtra } from '../types/Back.js';
99
import { WebSocketServer } from 'ws';
1010
import { WebSocketClient } from './common.js';
1111
import { AdminUser } from '../types/Common.js';
@@ -157,6 +157,7 @@ class ExpressServer implements IExpressHttpServer {
157157
server: http.Server;
158158
schemaAwareRouteRegistrationPatched = false;
159159
uploadParser: MulterParser;
160+
pendingEndpointRegistrations: Array<() => void> = [];
160161

161162
constructor(adminforth: IAdminForth) {
162163
this.adminforth = adminforth;
@@ -278,6 +279,7 @@ class ExpressServer implements IExpressHttpServer {
278279
serve(app) {
279280
this.expressApp = app;
280281
this.patchSchemaAwareRouteRegistration();
282+
this.flushPendingEndpointRegistrations();
281283
const stack = (this.expressApp as any)?._router?.stack;
282284
if (Array.isArray(stack)) {
283285
this.registerSchemaAwareStack(stack, '');
@@ -288,6 +290,12 @@ class ExpressServer implements IExpressHttpServer {
288290
this.setupSpaServer();
289291
}
290292

293+
flushPendingEndpointRegistrations() {
294+
this.pendingEndpointRegistrations.splice(0).forEach((registerEndpoint) => {
295+
registerEndpoint();
296+
});
297+
}
298+
291299
listen(...args) {
292300
this.server.listen(...args);
293301
}
@@ -539,7 +547,7 @@ class ExpressServer implements IExpressHttpServer {
539547
}));
540548
}
541549

542-
endpoint(options) {
550+
endpoint(options: IAdminForthEndpointOptions) {
543551
const {
544552
method='GET',
545553
path,
@@ -698,8 +706,17 @@ class ExpressServer implements IExpressHttpServer {
698706
res.json(output);
699707
}
700708

701-
afLogger.trace(`👂 Adding endpoint ${method} ${fullPath}`);
702-
this.expressApp[method.toLowerCase()](fullPath, noAuth ? expressHandler : this.authorize(expressHandler));
709+
const registerEndpoint = () => {
710+
afLogger.trace(`👂 Adding endpoint ${method} ${fullPath}`);
711+
this.expressApp[method.toLowerCase()](fullPath, noAuth ? expressHandler : this.authorize(expressHandler));
712+
};
713+
714+
if (this.expressApp) {
715+
registerEndpoint();
716+
return;
717+
}
718+
719+
this.pendingEndpointRegistrations.push(registerEndpoint);
703720
}
704721

705722
}

0 commit comments

Comments
 (0)