Skip to content

Commit ce59cf1

Browse files
authored
feat: add new format xml (#486)
1 parent 7c1730e commit ce59cf1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

api/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ type UiConfig = {
5454
photoResize: number | string;
5555
};
5656

57+
/**
58+
* Generates an XML string representation of the provided data.
59+
*
60+
* @param {any} data - The data to be converted into XML format.
61+
* @returns {string} - A string containing the XML representation of the data.
62+
*/
63+
function generateXML(data: any): string {
64+
let xml = `<stats>\n`;
65+
for (const key in data) {
66+
xml += ` <${key}>${escapeHTML(data[key])}</${key}>\n`;
67+
}
68+
xml += `</stats>`;
69+
return xml;
70+
}
71+
5772
/**
5873
* Handles the generation card of a GitHub stats based on user data and specified options.
5974
*
@@ -119,7 +134,13 @@ async function readmeStats(req: any, res: any): Promise<any> {
119134
res.setHeader("Cache-Control", "s-maxage=7200, stale-while-revalidate");
120135

121136
if (uiConfig.Format === "json") {
137+
fetchStats.picture = `data:image/png;base64,${fetchStats.picture}`;
122138
res.json(fetchStats);
139+
} else if (uiConfig.Format === "xml") {
140+
fetchStats.picture = `data:image/png;base64,${fetchStats.picture}`;
141+
const xmlData = generateXML(fetchStats);
142+
res.setHeader("Content-Type", "application/xml");
143+
res.send(xmlData);
123144
} else if (uiConfig.Format === "png") {
124145
const svgString = await card(fetchStats, uiConfig);
125146
const resvg = new Resvg(svgString, { font: { defaultFontFamily: "Segoe UI" }});

0 commit comments

Comments
 (0)