Skip to content

Commit 4b15bbe

Browse files
committed
Merge branch 'update-express-version' of github.com:devforth/adminforth into test
AdminForth/1781/image
2 parents a4f415a + b3b17eb commit 4b15bbe

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

adminforth/commands/createApp/templates/package.json.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
"@dotenvx/dotenvx": "^1.34.0",
2828
"adminforth": "{{adminforthVersion}}",
2929
"@adminforth/connector-{{connectorProvider}}": "latest",
30-
"express": "latest-4",
30+
"express": "^5.2.1",
3131
"zod": "^4.3.6"
3232
},
3333
"devDependencies": {
3434
"typescript": "6.0.3",
3535
"tsup": "^8.5.1",
3636
"tsx": "4.11.2",
37-
"@types/express": "^4.17.21",
37+
"@types/express": "^5.0.6",
3838
"@types/node": "latest"{{#if includePrismaMigrations}},
3939
"@prisma/client": "latest",
4040
"prisma": "^7.0.0"

adminforth/servers/express.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import { createRequire } from 'module';
3232
// instead of a static JSON import (which would hard-link a peer dep at parse time).
3333
const require = createRequire(import.meta.url);
3434
const expressVersion: string = require('express/package.json').version;
35-
35+
const expressMajor = Number(expressVersion.split('.')[0]);
36+
3637
function replaceAtStart(string, substring) {
3738
if (string.startsWith(substring)) {
3839
return string.slice(substring.length);
@@ -195,8 +196,12 @@ class ExpressServer implements IExpressHttpServer {
195196
// Express 5 (path-to-regexp v8) requires named wildcards instead of a bare `*`.
196197
// `{*splat}` is an optional catch-all so the SPA route also matches the base path itself
197198
// (e.g. `/admin` as well as `/admin/...`; `/` as well as `/foo` when baseUrl is empty).
198-
const assetsRoute = `${slashedPrefix}assets/*splat`;
199-
const spaCatchAll = prefix ? `${prefix}{*splat}` : '/{*splat}';
199+
let assetsRoute = `${slashedPrefix}assets/*splat`;
200+
let spaCatchAll = prefix ? `${prefix}{*splat}` : '/{*splat}';
201+
if (expressMajor === 4) {
202+
assetsRoute = `${slashedPrefix}assets/*`;
203+
spaCatchAll = `${prefix}*`;
204+
}
200205

201206
if (this.adminforth.runningHotReload) {
202207
const handler = async (req, res) => {
@@ -727,7 +732,43 @@ class ExpressServer implements IExpressHttpServer {
727732
const input = { body, query, headers, cookies, adminUser, response, requestUrl, _raw_express_req: req, _raw_express_res: res, tr, abortSignal: abortController.signal};
728733

729734
let output;
730-
output = await handler(input);
735+
if (expressMajor === 5) {
736+
output = await handler(input);
737+
} else if (expressMajor === 4) {
738+
// Express 4 does not support async handlers, so we need to wrap it in a try/catch
739+
try {
740+
output = await handler(input);
741+
} catch (e) {
742+
afLogger.error(`Error in handler ${e}`);
743+
afLogger.error(e.stack);
744+
745+
const expressErrorCallback = this.adminforth.config.expressErrorCallback;
746+
747+
if (expressErrorCallback) {
748+
try {
749+
await expressErrorCallback({
750+
error: e,
751+
adminforth: this.adminforth,
752+
extra: {
753+
body,
754+
query,
755+
headers,
756+
cookies: cookies as any,
757+
requestUrl,
758+
meta: {},
759+
response,
760+
},
761+
});
762+
} catch (callbackError) {
763+
afLogger.error(`Error in expressErrorCallback ${callbackError}`);
764+
afLogger.error(callbackError?.stack);
765+
}
766+
}
767+
768+
res.status(500).send('Internal server error');
769+
return;
770+
}
771+
}
731772

732773
response.headers.forEach(([name, value]) => {
733774
res.setHeader(name, value);

0 commit comments

Comments
 (0)