Skip to content

Commit

Permalink
Use Node Fetch instead of Got
Browse files Browse the repository at this point in the history
  • Loading branch information
therealsujitk committed Dec 11, 2023
1 parent d896180 commit 405f0bc
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 350 deletions.
47 changes: 15 additions & 32 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import express from 'express';
import got from 'got';
import http from 'http';
import https from 'https';
import NodeCache from 'node-cache';
import fetch from 'node-fetch';

const app = express();
const cache = new NodeCache();
Expand All @@ -20,18 +18,17 @@ async function createBadge(badge: Badge) {
const url = new URL(`http://img.shields.io/badge/${badge.key}-${badge.value}-${badge.color}?style=${badge.style}&logo=${badge.logo}`);

if (cache.has(url.toString())) {
return cache.get(url.toString());
return cache.get(url.toString()) as string;
}

const response = (await got(url)).body;
cache.set(url.toString(), response);
return response;
const response = await fetch(url);
const body = await response.text();
cache.set(url.toString(), body);
return body;
}

app.use(express.static(__dirname + '/frontend/build', {
index: false
}));
app.get('/*', (req, res) => {
app.use(express.static(__dirname + '/frontend/build', { index: false }));
app.get('/*', async (req, res) => {

if (!("app" in req.query)) {
return res.status(200).sendFile(__dirname + '/frontend/build/index.html');
Expand Down Expand Up @@ -68,28 +65,14 @@ app.get('/*', (req, res) => {
// 200 - 299 -> Successful Responses
// 100 - 199 -> Informational Responses

res.setHeader('Content-type', 'image/svg+xml');
res.status(200).send(await createBadge(badge));
}
createBadge(badge)
.then(badge => res.setHeader('Content-type', 'image/svg+xml').status(200).send(badge))
.catch(_ => res.status(500).send('Internal Server Error. Please open an issue at <a href="https://github.com/therealsujitk/vercel-badge/issues">vercel-badge/issues</a>.'));
};

try {
https.get("https://" + url, async (response) => {
var statusCode = response.statusCode;
await handleRequest(statusCode);
}).on('error', () => {
// This could mean the HTTPS site is not available so we check for HTTP
http.get("http://" + url, async (response) => {
var statusCode = response.statusCode;
await handleRequest(statusCode);
}).on('error', () => {
// Invalid Application Name
handleRequest(404);
});
});
} catch {
// An error was encountered for some unknown reason
res.status(500).send('Internal Server Error. Please open an issue at <a href="https://github.com/therealsujitk/vercel-badge/issues">vercel-badge/issues</a>.');
}
fetch(`http://${url}`)
.then(response => handleRequest(response.status))
.catch(_ => handleRequest());
});

export default app;
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
"license": "MIT",
"scripts": {
"preinstall": "cd frontend && yarn",
"dev": "ts-node -r dotenv/config bin/server.ts",
"dev": "ts-node bin/server.ts",
"build": "yarn run build:frontend && rm -rf dist && tsc -p .",
"build:frontend": "cd frontend && yarn run build",
"postbuild": "mkdir dist/frontend && cp -r frontend/build dist/frontend/build",
"start": "node dist/bin/server.js"
},
"dependencies": {
"express": "^4.18.2",
"got": "^11.8.2",
"node-cache": "^5.1.2"
"node-cache": "^5.1.2",
"node-fetch": "^2.7.0"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/got": "^9.6.12",
"@types/node": "^20.10.4",
"@types/node-fetch": "2.x",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
Expand Down
Loading

0 comments on commit 405f0bc

Please sign in to comment.