mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-01-11 19:56:44 +00:00
refactor: Enhance type annotations for getStatusPageData and handleRSS functions; streamline code structure
This commit is contained in:
parent
d838d377a0
commit
52c42dae1e
3 changed files with 66 additions and 33 deletions
|
|
@ -9,7 +9,10 @@ import App from "Common/Server/Utils/StartServer";
|
|||
import "ejs";
|
||||
import ObjectID from "Common/Types/ObjectID";
|
||||
import { handleRSS } from "./src/Server/API/RSS";
|
||||
import { getStatusPageData } from "./src/Server/Utils/StatusPage";
|
||||
import {
|
||||
getStatusPageData,
|
||||
StatusPageData,
|
||||
} from "./src/Server/Utils/StatusPage";
|
||||
|
||||
export const APP_NAME: string = "status-page";
|
||||
|
||||
|
|
@ -34,7 +37,8 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
|
|||
req: ExpressRequest,
|
||||
_res: ExpressResponse,
|
||||
) => {
|
||||
const statusPageData = await getStatusPageData(req);
|
||||
const statusPageData: StatusPageData | null =
|
||||
await getStatusPageData(req);
|
||||
|
||||
if (statusPageData) {
|
||||
return {
|
||||
|
|
@ -42,16 +46,14 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
|
|||
description: statusPageData.description,
|
||||
faviconUrl: statusPageData.faviconUrl,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
title: "Status Page",
|
||||
description:
|
||||
"Status Page lets you see real-time information about the status of our services.",
|
||||
faviconUrl:
|
||||
"/status-page-api/favicon/" +
|
||||
ObjectID.getZeroObjectID().toString(),
|
||||
};
|
||||
}
|
||||
return {
|
||||
title: "Status Page",
|
||||
description:
|
||||
"Status Page lets you see real-time information about the status of our services.",
|
||||
faviconUrl:
|
||||
"/status-page-api/favicon/" + ObjectID.getZeroObjectID().toString(),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
import { ExpressRequest, ExpressResponse } from "Common/Server/Utils/Express";
|
||||
import API from "Common/Utils/API";
|
||||
import { HttpProtocol, StatusPageApiClientUrl } from "Common/Server/EnvironmentConfig";
|
||||
import {
|
||||
HttpProtocol,
|
||||
StatusPageApiClientUrl,
|
||||
} from "Common/Server/EnvironmentConfig";
|
||||
import URL from "Common/Types/API/URL";
|
||||
import HTTPErrorResponse from "Common/Types/API/HTTPErrorResponse";
|
||||
import HTTPResponse from "Common/Types/API/HTTPResponse";
|
||||
|
|
@ -8,7 +11,13 @@ import { JSONObject, JSONArray } from "Common/Types/JSON";
|
|||
import logger from "Common/Server/Utils/Logger";
|
||||
import { getStatusPageData, StatusPageData } from "../Utils/StatusPage";
|
||||
|
||||
export const handleRSS = async (req: ExpressRequest, res: ExpressResponse): Promise<void> => {
|
||||
export const handleRSS: (
|
||||
req: ExpressRequest,
|
||||
res: ExpressResponse,
|
||||
) => Promise<void> = async (
|
||||
req: ExpressRequest,
|
||||
res: ExpressResponse,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
// Get status page data
|
||||
const statusPageData: StatusPageData | null = await getStatusPageData(req);
|
||||
|
|
@ -65,12 +74,17 @@ export const handleRSS = async (req: ExpressRequest, res: ExpressResponse): Prom
|
|||
|
||||
const isPreview: boolean = req.path.includes("/status-page/");
|
||||
const baseUrl: string = isPreview
|
||||
? `${req.protocol}://${req.get('host')}/status-page/${statusPageId}`
|
||||
: `${req.protocol}://${req.get('host')}`;
|
||||
? `${req.protocol}://${req.get("host")}/status-page/${statusPageId}`
|
||||
: `${req.protocol}://${req.get("host")}`;
|
||||
|
||||
// Process incidents
|
||||
if (incidentsResponse instanceof HTTPResponse && incidentsResponse.data?.["incidents"]) {
|
||||
const incidents: JSONArray = incidentsResponse.data["incidents"] as JSONArray;
|
||||
if (
|
||||
incidentsResponse instanceof HTTPResponse &&
|
||||
incidentsResponse.data?.["incidents"]
|
||||
) {
|
||||
const incidents: JSONArray = incidentsResponse.data[
|
||||
"incidents"
|
||||
] as JSONArray;
|
||||
incidents.forEach((incident: JSONObject) => {
|
||||
items.push({
|
||||
title: `Incident: ${incident["title"]}`,
|
||||
|
|
@ -82,8 +96,13 @@ export const handleRSS = async (req: ExpressRequest, res: ExpressResponse): Prom
|
|||
}
|
||||
|
||||
// Process announcements
|
||||
if (announcementsResponse instanceof HTTPResponse && announcementsResponse.data?.["announcements"]) {
|
||||
const announcements: JSONArray = announcementsResponse.data["announcements"] as JSONArray;
|
||||
if (
|
||||
announcementsResponse instanceof HTTPResponse &&
|
||||
announcementsResponse.data?.["announcements"]
|
||||
) {
|
||||
const announcements: JSONArray = announcementsResponse.data[
|
||||
"announcements"
|
||||
] as JSONArray;
|
||||
announcements.forEach((announcement: JSONObject) => {
|
||||
items.push({
|
||||
title: `Announcement: ${announcement["title"]}`,
|
||||
|
|
@ -95,8 +114,13 @@ export const handleRSS = async (req: ExpressRequest, res: ExpressResponse): Prom
|
|||
}
|
||||
|
||||
// Process scheduled maintenance
|
||||
if (scheduledResponse instanceof HTTPResponse && scheduledResponse.data?.["scheduledMaintenanceEvents"]) {
|
||||
const scheduled: JSONArray = scheduledResponse.data["scheduledMaintenanceEvents"] as JSONArray;
|
||||
if (
|
||||
scheduledResponse instanceof HTTPResponse &&
|
||||
scheduledResponse.data?.["scheduledMaintenanceEvents"]
|
||||
) {
|
||||
const scheduled: JSONArray = scheduledResponse.data[
|
||||
"scheduledMaintenanceEvents"
|
||||
] as JSONArray;
|
||||
scheduled.forEach((event: JSONObject) => {
|
||||
items.push({
|
||||
title: `Scheduled Maintenance: ${event["title"]}`,
|
||||
|
|
@ -108,12 +132,12 @@ export const handleRSS = async (req: ExpressRequest, res: ExpressResponse): Prom
|
|||
}
|
||||
|
||||
// Sort items by pubDate descending
|
||||
items.sort((a, b) => new Date(b.pubDate).getTime() - new Date(a.pubDate).getTime());
|
||||
|
||||
|
||||
items.sort((a: (typeof items)[0], b: (typeof items)[0]) => {
|
||||
return new Date(b.pubDate).getTime() - new Date(a.pubDate).getTime();
|
||||
});
|
||||
|
||||
// Generate RSS XML
|
||||
const feedUrl = `${HttpProtocol}${req.get('host')}${req.path}`;
|
||||
const feedUrl: string = `${HttpProtocol}${req.get("host")}${req.path}`;
|
||||
|
||||
let rssXml: string = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
|
|
@ -124,7 +148,7 @@ export const handleRSS = async (req: ExpressRequest, res: ExpressResponse): Prom
|
|||
<atom:link href="${feedUrl}" rel="self" type="application/rss+xml" />
|
||||
`;
|
||||
|
||||
items.forEach((item) => {
|
||||
items.forEach((item: (typeof items)[0]) => {
|
||||
rssXml += `
|
||||
<item>
|
||||
<title><![CDATA[${item.title}]]></title>
|
||||
|
|
@ -139,10 +163,10 @@ export const handleRSS = async (req: ExpressRequest, res: ExpressResponse): Prom
|
|||
</channel>
|
||||
</rss>`;
|
||||
|
||||
res.set('Content-Type', 'application/rss+xml');
|
||||
res.set("Content-Type", "application/rss+xml");
|
||||
res.send(rssXml);
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
res.status(500).send("Internal Server Error");
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -14,7 +14,9 @@ export interface StatusPageData {
|
|||
faviconUrl: string;
|
||||
}
|
||||
|
||||
export const getStatusPageData = async (
|
||||
export const getStatusPageData: (
|
||||
req: ExpressRequest,
|
||||
) => Promise<StatusPageData | null> = async (
|
||||
req: ExpressRequest,
|
||||
): Promise<StatusPageData | null> => {
|
||||
try {
|
||||
|
|
@ -36,7 +38,9 @@ export const getStatusPageData = async (
|
|||
req.hostname?.toString() || req.headers["host"]?.toString() || "";
|
||||
if (host) {
|
||||
statusPageIdOrDomain = host;
|
||||
logger.debug(`Found domain in request headers: ${statusPageIdOrDomain}`);
|
||||
logger.debug(
|
||||
`Found domain in request headers: ${statusPageIdOrDomain}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +51,8 @@ export const getStatusPageData = async (
|
|||
|
||||
let statusPageId: string;
|
||||
let title: string = "Status Page";
|
||||
let description: string = "Status Page lets you see real-time information about the status of our services.";
|
||||
let description: string =
|
||||
"Status Page lets you see real-time information about the status of our services.";
|
||||
|
||||
if (isPreview) {
|
||||
// For preview pages, use the extracted ID directly
|
||||
|
|
@ -55,7 +60,9 @@ export const getStatusPageData = async (
|
|||
// For preview, we might not have SEO data, so use defaults
|
||||
} else {
|
||||
// For live pages, call SEO API
|
||||
logger.debug(`Pinging the API with statusPageIdOrDomain: ${statusPageIdOrDomain}`);
|
||||
logger.debug(
|
||||
`Pinging the API with statusPageIdOrDomain: ${statusPageIdOrDomain}`,
|
||||
);
|
||||
const response: HTTPErrorResponse | HTTPResponse<JSONObject> =
|
||||
await API.get({
|
||||
url: URL.fromString(StatusPageApiClientUrl.toString()).addRoute(
|
||||
|
|
@ -91,4 +98,4 @@ export const getStatusPageData = async (
|
|||
logger.error(err);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue