fix(desktop): adapt request mocking to express 5

This commit is contained in:
Elian Doran 2025-06-07 14:58:07 +03:00
parent 0bc91349f6
commit ebeabe2b54
No known key found for this signature in database

View File

@ -1,42 +1,63 @@
import electron from "electron"; import electron from "electron";
import type { Application } from "express";
import type { ParamsDictionary, Request, Response } from "express-serve-static-core";
import type QueryString from "qs";
import { Session, SessionData } from "express-session";
interface Response { type MockedResponse = Response<any, Record<string, any>, number>;
statusCode: number;
getHeader: (name: string) => string; function init(app: Application) {
setHeader: (name: string, value: string) => Response; const fakeSession: Session & Partial<SessionData> = {
header: (name: string, value: string) => Response; id: "session-id", // Placeholder for session ID
status: (statusCode: number) => Response; cookie: {
send: (obj: {}) => void; // eslint-disable-line @typescript-eslint/no-empty-object-type originalMaxAge: 3600000, // 1 hour
} },
loggedIn: true,
regenerate(callback) {
callback?.(null);
return fakeSession;
},
destroy(callback) {
callback?.(null);
return fakeSession;
},
reload(callback) {
callback?.(null);
return fakeSession;
},
save(callback) {
callback?.(null);
return fakeSession;
},
resetMaxAge: () => fakeSession,
touch: () => fakeSession
};
function init(app: Express.Application) {
electron.ipcMain.on("server-request", (event, arg) => { electron.ipcMain.on("server-request", (event, arg) => {
const req = { const req: Pick<Request<ParamsDictionary, any, any, QueryString.ParsedQs, Record<string, any>>, "url" | "method" | "body" | "headers" | "session"> = {
url: arg.url, url: arg.url,
method: arg.method, method: arg.method,
body: arg.data, body: arg.data,
headers: arg.headers, headers: arg.headers,
session: { session: fakeSession
loggedIn: true
}
}; };
const respHeaders: Record<string, string> = {}; const respHeaders: Record<string, string | string[]> = {};
const res: Response = { const res: Pick<Response<any, Record<string, any>, number>, "statusCode" | "getHeader" | "setHeader" | "header" | "status" | "send" | "locals" | "json"> = {
statusCode: 200, statusCode: 200,
getHeader: (name) => respHeaders[name], getHeader: (name) => respHeaders[name],
setHeader: (name, value) => { setHeader: (name, value) => {
respHeaders[name] = value.toString(); respHeaders[name] = value.toString();
return res; return res as MockedResponse;
}, },
header: (name, value) => { header(name: string, value?: string | string[]) {
respHeaders[name] = value.toString(); respHeaders[name] = value ?? "";
return res; return res as MockedResponse;
}, },
status: (statusCode) => { status: (statusCode) => {
res.statusCode = statusCode; res.statusCode = statusCode;
return res; return res as MockedResponse;
}, },
send: (obj) => { send: (obj) => {
event.sender.send("server-response", { event.sender.send("server-response", {
@ -47,10 +68,16 @@ function init(app: Express.Application) {
headers: respHeaders, headers: respHeaders,
body: obj body: obj
}); });
return res as MockedResponse;
},
locals: {},
json: (obj) => {
res.send(JSON.stringify(obj));
return res as MockedResponse;
} }
}; };
return (app as any)._router.handle(req, res, () => {}); return app.router(req as any, res as any, () => {});
}); });
} }