Skip to content

Commit 4459ba6

Browse files
committed
feat: add shields.io dynamic badge json response
This commit adds the ability to set the return format of the `/api/status/up` cloud function. When this format is set to `shields` a dynamic shields.io badge json is returned.
1 parent a505538 commit 4459ba6

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

api/status/up.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,35 @@ const PATsWorking = async (fetcher, variables, retries = 0) => {
8888
}
8989
};
9090

91+
/**
92+
* Creates Json response that can be used for shields.io dynamic card generation.
93+
*
94+
* @param {*} up Whether the PATs are up or not.
95+
* @returns Dynamic shields.io JSON response object.
96+
*
97+
* @see https://shields.io/endpoint.
98+
*/
99+
const shieldsUptimeBadge = (up) => {
100+
const schemaVersion = 1;
101+
const isError = true;
102+
const label = "Public Instance";
103+
const message = up ? "up" : "down";
104+
const color = up ? "brightgreen" : "red";
105+
return {
106+
schemaVersion,
107+
label,
108+
message,
109+
color,
110+
isError,
111+
};
112+
};
113+
91114
/**
92115
* Cloud function that returns whether the PATs are still functional.
93116
*/
94-
export default async (_, res) => {
117+
export default async (req, res) => {
118+
const { type = "boolean" } = req.query;
119+
95120
res.setHeader("Content-Type", "application/json");
96121
try {
97122
// Add header to prevent abuse.
@@ -102,7 +127,14 @@ export default async (_, res) => {
102127
`max-age=0, s-maxage=${RATE_LIMIT_SECONDS}`,
103128
);
104129
}
105-
res.send(PATsValid);
130+
switch (type) {
131+
case "shields":
132+
res.send(shieldsUptimeBadge(PATsValid));
133+
break;
134+
default:
135+
res.send(PATsValid);
136+
break;
137+
}
106138
} catch (err) {
107139
// Return fail boolean if something went wrong.
108140
logger.error(err);

0 commit comments

Comments
 (0)