Skip to content
This repository was archived by the owner on Mar 21, 2026. It is now read-only.

Commit ae4c61e

Browse files
Bug fixes and new apis: encode and deocde (#53)
2 parents 829d60e + 6552634 commit ae4c61e

3 files changed

Lines changed: 96 additions & 23 deletions

File tree

src/routes/api/v1/authed.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express, { Request, Response, NextFunction } from "express";
22
const router = express.Router();
33
import { getClicks, verifyKey } from "../../../functions.js";
44
import { rooms } from "../../ttt.js";
5+
import { time } from "console";
56

67
router.use(async (req: Request, res: Response, next: NextFunction) => {
78
const { auth } = req.headers;
@@ -64,4 +65,56 @@ router.get("/rooms", (req: Request, res: Response) => {
6465
}
6566
});
6667

68+
router.post("/encode", (req: Request, res: Response) => {
69+
let { text, times } = req.body;
70+
let counter = 0;
71+
72+
if (isNaN(Number(times))) {
73+
return res.status(400).json({
74+
error: "Times is NaN"
75+
});
76+
}
77+
78+
try{
79+
while(counter < times){
80+
text = btoa(text);
81+
counter++;
82+
}
83+
}
84+
catch{
85+
res.sendStatus(500);
86+
}
87+
88+
89+
res.status(200).json({
90+
output: String(text)
91+
});
92+
});
93+
94+
router.post("/decode", (req: Request, res: Response) => {
95+
let { text, times } = req.body;
96+
let counter = 0;
97+
98+
if (isNaN(Number(times))) {
99+
return res.status(400).json({
100+
error: "Times is NaN"
101+
});
102+
}
103+
104+
try{
105+
while(counter < times){
106+
text = atob(text);
107+
counter++;
108+
}
109+
}
110+
catch{
111+
res.sendStatus(500);
112+
}
113+
114+
115+
res.status(200).json({
116+
output: String(text)
117+
});
118+
});
119+
67120
export default router;

src/routes/api/v1/unauthed.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,47 +57,56 @@ router.post("/request", async (req: Request, res: Response) => {
5757
const { method, url, body, headers } = req.body;
5858
let resBody;
5959

60-
if(method && url){
61-
try{
60+
if (method && url) {
61+
try {
6262
let safeHeaders: Record<string, string> = {};
6363
if (headers && typeof headers === "object" && !Array.isArray(headers)) {
6464
for (const [key, value] of Object.entries(headers)) {
65-
if (
66-
typeof key === "string" &&
67-
typeof value !== "undefined" &&
68-
value !== null
69-
) {
65+
if (typeof key === "string" && value != null) {
7066
safeHeaders[key] = String(value);
7167
}
7268
}
7369
}
7470

71+
const outgoingBody =
72+
method === "GET"
73+
? undefined
74+
: typeof body === "string"
75+
? body
76+
: body !== undefined
77+
? JSON.stringify(body)
78+
: undefined;
79+
80+
const hasContentType = Object.keys(safeHeaders).some(
81+
(h) => h.toLowerCase() === "content-type"
82+
);
83+
if (outgoingBody && !hasContentType && typeof body !== "string") {
84+
safeHeaders["Content-Type"] = "application/json";
85+
}
86+
7587
const response = await fetch(url, {
7688
method: method,
7789
headers: safeHeaders,
78-
body: method == "GET" ? undefined : JSON.stringify(body)
90+
body: outgoingBody
7991
});
8092
const contenttype = await response.headers.get("content-type");
81-
if(contenttype?.includes("application/json")){
82-
resBody = await response.json()
83-
}
84-
else{
93+
if (contenttype?.includes("application/json")) {
94+
resBody = await response.json();
95+
} else {
8596
resBody = await response.text();
8697
}
8798
res.json({
8899
status: response.status,
89100
body: resBody,
90101
headers: Object.fromEntries(response.headers.entries())
91102
});
92-
}
93-
catch (error){
103+
} catch (error) {
94104
res.status(400).json({
95105
error: "There was an error while sending the request. Make sure that the url is well formatted"
96106
});
97107
console.error(error);
98108
}
99-
}
100-
else{
109+
} else {
101110
res.status(400).json({
102111
error: "method and url are required in body"
103112
});

views/projects/request.ejs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,30 @@
7777
}
7878
7979
sendBtn.addEventListener("click", async () => {
80-
if(url.value){
81-
headers = JSON.parse(headersInp.value);
82-
body = JSON.parse(bodyInp.value);
80+
if (url.value) {
81+
try {
82+
headers = JSON.parse(headersInp.value);
83+
} catch (e) {
84+
result.innerText = "Headers must be valid JSON";
85+
return;
86+
}
87+
88+
const rawBody = bodyInp.value;
89+
let bodyToSend;
90+
try {
91+
bodyToSend = JSON.parse(rawBody);
92+
} catch (e) {
93+
bodyToSend = rawBody;
94+
}
95+
8396
const response = await fetch("/api/v1/request", {
8497
method: "POST",
85-
headers: {
86-
"Content-Type": "application/json"
87-
},
98+
headers: { "Content-Type": "application/json" },
8899
body: JSON.stringify({
89100
method: method.value,
90101
url: url.value,
91102
headers: headers,
92-
body: body
103+
body: bodyToSend
93104
})
94105
});
95106
result.innerHTML = "";

0 commit comments

Comments
 (0)