Notes/src/routes/electron.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

import { ipcMain } from "electron";
2024-04-07 14:09:37 +03:00
import { Application } from "express";
interface Response {
statusCode: number;
getHeader: (name: string) => string;
setHeader: (name: string, value: string) => Response;
header: (name: string, value: string) => Response;
status: (statusCode: number) => Response;
send: (obj: {}) => void;
}
function init(app: Application) {
2025-01-09 18:07:02 +02:00
ipcMain.on("server-request", (event, arg) => {
2019-03-31 12:49:42 +02:00
const req = {
url: arg.url,
method: arg.method,
body: arg.data,
headers: arg.headers,
session: {
loggedIn: true
}
};
2024-04-07 14:09:37 +03:00
const respHeaders: Record<string, string> = {};
2024-04-07 14:09:37 +03:00
const res: Response = {
2019-03-31 12:49:42 +02:00
statusCode: 200,
2025-01-09 18:07:02 +02:00
getHeader: (name) => respHeaders[name],
setHeader: (name, value) => {
respHeaders[name] = value.toString();
return res;
},
header: (name, value) => {
respHeaders[name] = value.toString();
return res;
},
2025-01-09 18:07:02 +02:00
status: (statusCode) => {
2019-03-31 12:49:42 +02:00
res.statusCode = statusCode;
return res;
},
2025-01-09 18:07:02 +02:00
send: (obj) => {
event.sender.send("server-response", {
2021-02-14 11:43:31 +01:00
url: arg.url,
method: arg.method,
2019-03-31 12:49:42 +02:00
requestId: arg.requestId,
statusCode: res.statusCode,
headers: respHeaders,
2019-03-31 12:49:42 +02:00
body: obj
});
}
};
return app._router.handle(req, res, () => {});
});
}
export default init;