Skip to content

Commit

Permalink
refactor some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Lou1sVuong committed Oct 30, 2024
1 parent 494eff4 commit 33baa14
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
"author": "",
"license": "ISC",
"dependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^5.0.0",
"@types/swagger-ui-express": "^4.1.7",
"cors": "^2.8.5",
"express": "^4.21.1",
"nodemon": "^3.1.7",
"swagger-autogen": "^2.23.7",
Expand Down
64 changes: 24 additions & 40 deletions src/routes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,30 @@ postRouter.use(bodyParser.json()); // to use body object in requests
*/

postRouter.post("/", async (req, res) => {
const url = "http://3.107.186.138/answer";
const keywords = {
"LiNEAR": "https://linearprotocol.org",
const url = "http://3.107.186.138/answer";
try {
console.log(req.body);

const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(req.body),
});

const data = await response.text();

const result = {
formattedText: data,
keywords: [],
};
try {
console.log(req.body);

const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(req.body),
});

const data = await response.text();
function formatMarkdown(response, keywords) {
const foundKeywords = [];

for (let key in keywords) {
const regex = new RegExp(`\\b${key}\\b`, 'gi');
if (regex.test(response)) {
foundKeywords.push({
keyword: key,
link: keywords[key]
});
response = response.replace(regex, `[${key}](${keywords[key]})`);
}
}

return {
formattedText: response,
keywords: foundKeywords
};
}
res.send(JSON.stringify(formatMarkdown(data, keywords), null, 2));
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).send("An error occurred.");
}

res.send(result);
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).send("An error occurred.");
}
});

/**
Expand Down Expand Up @@ -208,4 +192,4 @@ postRouter.post("/", async (req, res) => {
// }
// });

export default postRouter;
export default postRouter;

0 comments on commit 33baa14

Please sign in to comment.