Change URLs to use the JS URL constructor consistently across codebase.

Fixes #281
This commit is contained in:
zkldi
2021-08-13 03:20:14 +01:00
parent 89b45703d5
commit 02ee2ef002
4 changed files with 37 additions and 16 deletions
@@ -3,6 +3,8 @@ import { HasOwnProperty } from "utils/misc";
import { IIDXDans } from "lib/constants/classes";
import { ClassHandler } from "../../../framework/user-game-stats/types";
import { ServerConfig } from "lib/setup/config";
import { CreateURLWithParams } from "utils/url";
import { profile } from "console";
export async function CreateArcIIDXClassHandler(
profileID: string,
@@ -16,14 +18,15 @@ export async function CreateArcIIDXClassHandler(
// SP and DP dans are located in the same place,
// fetch once, then return a function that traverses this data.
try {
const res = await fetch(
`${ServerConfig.ARC_API_URL}/api/v1/iidx/27/profiles?_id=${profileID}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
const url = CreateURLWithParams(`${ServerConfig.ARC_API_URL}/api/v1/iidx/27/profiles`, {
_id: profileID,
});
const res = await fetch(url.href, {
headers: {
Authorization: `Bearer ${token}`,
},
});
json = await res.json();
} catch (e) {
@@ -5,6 +5,7 @@ import { GetKaiTypeClientCredentials, KaiTypeToBaseURL } from "./utils";
import nodeFetch from "utils/fetch";
import p from "prudence";
import { KaiAuthDocument } from "tachi-common";
import { CreateURLWithParams } from "utils/url";
const REAUTH_SCHEMA = {
access_token: "string",
@@ -35,11 +36,14 @@ export function CreateKaiReauthFunction(
return async () => {
let res;
try {
res = await fetch(
`${KaiTypeToBaseURL(kaiType)}/oauth/token?refresh_token=${
authDoc.refreshToken
}&grant_type=refresh_token&client_secret=${CLIENT_SECRET}&client_id=${CLIENT_ID}`
);
const url = CreateURLWithParams(`${KaiTypeToBaseURL(kaiType)}/oauth/token`, {
refresh_token: authDoc.refreshToken,
grant_type: "refresh_token",
client_secret: CLIENT_SECRET,
client_id: CLIENT_ID,
});
res = await fetch(url.href);
} catch (err) {
logger.error(`Unexpected error while fetching reauth?`, { res, err });
throw new ScoreImportFatalError(
+8 -3
View File
@@ -7,6 +7,7 @@ import { FormatUserDoc } from "utils/user";
import nodeFetch from "utils/fetch";
import { Random20Hex } from "utils/misc";
import { ServerConfig } from "lib/setup/config";
import { CreateURLWithParams } from "utils/url";
const logger = CreateLogCtx(__filename);
@@ -105,9 +106,13 @@ export async function ValidateCaptcha(
remoteAddr: string | undefined,
fetch = nodeFetch
) {
const r = await fetch(
`https://www.google.com/recaptcha/api/siteverify?secret=${ServerConfig.CAPTCHA_SECRET_KEY}&response=${recaptcha}&remoteip=${remoteAddr}`
);
const url = CreateURLWithParams(`https://www.google.com/recaptcha/api/siteverify`, {
secret: ServerConfig.CAPTCHA_SECRET_KEY,
response: recaptcha,
remoteip: remoteAddr ?? "",
});
const r = await fetch(url.href);
if (r.status !== 200) {
logger.verbose(`Failed GCaptcha response ${r.status}, ${r.body}`);
+9
View File
@@ -0,0 +1,9 @@
export function CreateURLWithParams(url: string, params: Record<string, string>) {
const u = new URL(url);
for (const key in params) {
u.searchParams.set(key, params[key]);
}
return u;
}