bring back axios

This commit is contained in:
McMistrzYT 2024-02-08 18:16:16 +01:00
parent 894e9f67f5
commit 698fc4a7b5
6 changed files with 97 additions and 43 deletions

View File

@ -68,7 +68,7 @@ async function Initialize() {
Initialize();
// ! FESTIVAL-SPECIFIC STUFF
import axios from "axios";
//import axios from "axios";
axios.defaults.validateStatus = () => true;
axios.defaults.headers.common["X-Do-Not-Redirect"] = "true";
//axios.defaults.validateStatus = () => true;
//axios.defaults.headers.common["X-Do-Not-Redirect"] = "true";

View File

@ -1,3 +1,4 @@
import axios from "axios";
import { Err } from "./Logger";
import { red } from "colorette";
import { FULL_SERVER_ROOT } from "./Constants";
@ -11,31 +12,15 @@ let LastContentDownloadDate: Date = new Date(0); // set it to 1970 as default cu
GenerateFortnitePages(null);
export async function GenerateFortnitePages(ForUser: User | null): Promise<{ Success: boolean, FNPages: { [key: string]: unknown } | null }> {
let status;
let data;
if (FullFortnitePages === null || Date.now() > LastContentDownloadDate.getTime() + 30 * 60 * 1000) {
const response = await fetch("https://fortnitecontent-website-prod07.ol.epicgames.com/content/api/pages/fortnite-game");
status = response.status;
data = await response.json();
} else {
status = 200;
data = FullFortnitePages;
}
/*const { status, data } = // check if 30 minutes have passed since last content update. if so, get a new copy of pages, if not, fuck off
const { status, data } = // check if 30 minutes have passed since last content update. if so, get a new copy of pages, if not, fuck off
FullFortnitePages === null || Date.now() > LastContentDownloadDate.getTime() + 30 * 60 * 1000 ?
await fetch("https://fortnitecontent-website-prod07.ol.epicgames.com/content/api/pages/fortnite-game") :
FullFortnitePages;*/
await axios.get("https://fortnitecontent-website-prod07.ol.epicgames.com/content/api/pages/fortnite-game") :
{ status: 200, data: FullFortnitePages };
let OGSparks;
if (OriginalSparks === null || Date.now() > LastContentDownloadDate.getTime() + 30 * 60 * 1000) {
const response = await fetch("https://fortnitecontent-website-prod07.ol.epicgames.com/content/api/pages/fortnite-game/spark-tracks");
OGSparks = {status: 200, data: await response.json()}
} else {
OGSparks = OriginalSparks
}
const OGSparks =
OriginalSparks === null || Date.now() > LastContentDownloadDate.getTime() + 30 * 60 * 1000 ?
await axios.get("https://fortnitecontent-website-prod07.ol.epicgames.com/content/api/pages/fortnite-game/spark-tracks") :
{ status: 200, data: OriginalSparks };
FullFortnitePages = {
...data,

View File

@ -1,3 +1,4 @@
import axios from "axios";
import jwt from "jsonwebtoken";
import qs from "querystring";
import j from "joi";
@ -15,14 +16,11 @@ const App = Router();
// ? hacky, if you want, make it less hacky
async function QuickRevokeToken(res: Response, Token: string) {
await fetch("https://discord.com/api/oauth2/token/revoke", {
method: "POST",
await axios.post("https://discord.com/api/oauth2/token/revoke", qs.stringify({ token: Token, token_type_hint: "access_token" }), {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": `Basic ${Buffer.from(`${DISCORD_CLIENT_ID}:${DISCORD_CLIENT_SECRET}`).toString("base64")}`
},
body: qs.stringify({ token: Token, token_type_hint: "access_token" })
}
})
return res;
}
@ -38,36 +36,33 @@ App.get("/discord",
//const Discord = await axios.post(`https://discord.com/api/oauth2/token`, qs.stringify({ grant_type: "authorization_code", code: req.query.code as string, redirect_uri: `${FULL_SERVER_ROOT}/api/discord` }), { auth: { username: DISCORD_CLIENT_ID!, password: DISCORD_CLIENT_SECRET! } });
const Discord = await fetch(
const Discord = await axios.post(
"https://discord.com/api/oauth2/token",
qs.stringify({ grant_type: "authorization_code", code: req.query.code as string, redirect_uri: `${FULL_SERVER_ROOT}/api/discord` }),
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": `Basic ${Buffer.from(`${DISCORD_CLIENT_ID}:${DISCORD_CLIENT_SECRET}`).toString("base64")}`
},
body: qs.stringify({ grant_type: "authorization_code", code: req.query.code as string, redirect_uri: `${FULL_SERVER_ROOT}/api/discord` })
}
}
)
if (Discord.status !== 200)
return res.status(500).send("Failed to request OAuth token from Discord's services.");
const DiscordData = await Discord.json() as any; // :waaaaa:
const DiscordData = Discord.data; // :waaaaa:
if (!DiscordData.scope.includes("identify"))
return (await QuickRevokeToken(res, DiscordData.access_token)).status(400).send("Missing identify scope. Please check if your OAuth link is correctly set up!");
const UserData = await fetch("https://discord.com/api/v10/users/@me", {
method: "GET",
const UserData = await axios.get("https://discord.com/api/v10/users/@me", {
headers: {
"Authorization": `${DiscordData.token_type} ${DiscordData.access_token}`
}
})
const UserDataBody = await UserData.json() as any;
const UserDataBody = UserData.data;
if (UserData.status !== 200)
return (await QuickRevokeToken(res, DiscordData.access_token)).status(500).send("Failed to request user data from Discord's services.");

View File

@ -2,6 +2,7 @@ import { Router } from "express";
import { FullFortnitePages, GenerateFortnitePages } from "../Modules/FNUtil";
import { IS_DEBUG } from "../Modules/Constants";
import { RequireAuthentication } from "../Modules/Middleware";
import axios from "axios";
const App = Router();
@ -41,11 +42,11 @@ App.get("/content/api/pages/fortnite-game/:Section", RequireAuthentication(), as
if (!CachedSection)
return res.status(404).send("funny section not found haha kill me");
const ContentFromServer = await fetch(`https://fortnitecontent-website-prod07.ol.epicgames.com/content/api/pages/fortnite-game/${CachedSection._title}`)
const ContentFromServer = await axios.get(`https://fortnitecontent-website-prod07.ol.epicgames.com/content/api/pages/fortnite-game/${CachedSection._title}`)
if (ContentFromServer.status !== 200)
return res.status(404).json({ error: IS_DEBUG ? await ContentFromServer.text() : "Fortnite server returned an error." });
return res.status(404).json({ error: IS_DEBUG ? ContentFromServer.data : "Fortnite server returned an error." });
res.json(await ContentFromServer.json());
res.json(ContentFromServer.data);
})
export default {

View File

@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"@types/node-cron": "^3.0.11",
"axios": "^1.6.7",
"better-sqlite3": "^9.3.0",
"colorette": "^2.0.20",
"cookie-parser": "^1.4.6",
@ -546,6 +547,21 @@
"resolved": "https://registry.npmjs.org/async/-/async-3.2.5.tgz",
"integrity": "sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg=="
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"node_modules/axios": {
"version": "1.6.7",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz",
"integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==",
"dependencies": {
"follow-redirects": "^1.15.4",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/balanced-match": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
@ -915,6 +931,17 @@
"resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.20.tgz",
"integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w=="
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/content-disposition": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
@ -1032,6 +1059,14 @@
"node": ">=4.0.0"
}
},
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
@ -1280,6 +1315,25 @@
"which": "bin/which"
}
},
"node_modules/follow-redirects": {
"version": "1.15.5",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/foreground-child": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-3.1.1.tgz",
@ -1295,6 +1349,19 @@
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/forwarded": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
@ -1960,6 +2027,11 @@
"node": ">= 0.10"
}
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/pump": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz",

View File

@ -42,6 +42,7 @@
},
"dependencies": {
"@types/node-cron": "^3.0.11",
"axios": "^1.6.7",
"better-sqlite3": "^9.3.0",
"colorette": "^2.0.20",
"cookie-parser": "^1.4.6",