mirror of
https://github.com/Pandipipas/scoreko-dev.git
synced 2026-06-06 03:32:06 +00:00
fix(challonge): send Authorization-Type headers for v2 oauth tokens
This commit is contained in:
+40
-13
@@ -202,35 +202,62 @@ const exchangeOAuthCodeForToken = async (
|
|||||||
return token;
|
return token;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const parseJsonResponse = async (response: Response): Promise<unknown> => {
|
||||||
|
const rawBody = await response.text();
|
||||||
|
if (!rawBody) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(rawBody) as unknown;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const requestChallonge = async (path: string, token: string): Promise<unknown> => {
|
const requestChallonge = async (path: string, token: string): Promise<unknown> => {
|
||||||
const response = await fetch(`${CHALLONGE_API_BASE}${path}`, {
|
const requestUrl = `${CHALLONGE_API_BASE}${path}`;
|
||||||
|
|
||||||
|
const v2Response = await fetch(requestUrl, {
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/vnd.api+json',
|
'Content-Type': 'application/vnd.api+json',
|
||||||
|
'Authorization-Type': 'v2',
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const rawBody = await response.text();
|
const v2Payload = await parseJsonResponse(v2Response);
|
||||||
let payload: unknown = null;
|
|
||||||
if (rawBody) {
|
if (v2Response.ok) {
|
||||||
try {
|
return v2Payload;
|
||||||
payload = JSON.parse(rawBody) as unknown;
|
|
||||||
} catch {
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Challonge responded with ${response.status} ${response.statusText}`.trim());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fallback for personal API keys pasted manually (v1 auth style).
|
||||||
|
if (v2Response.status === 401) {
|
||||||
|
const v1Response = await fetch(requestUrl, {
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/json',
|
||||||
|
'Content-Type': 'application/vnd.api+json',
|
||||||
|
'Authorization-Type': 'v1',
|
||||||
|
Authorization: token,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const v1Payload = await parseJsonResponse(v1Response);
|
||||||
|
if (v1Response.ok) {
|
||||||
|
return v1Payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
const maybeError = v2Payload as { errors?: { detail?: string }; error?: string } | null;
|
||||||
const maybeError = payload as { errors?: { detail?: string }; error?: string } | null;
|
if (!v2Response.ok) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
maybeError?.errors?.detail || maybeError?.error || `Challonge responded with ${response.status} ${response.statusText}`.trim(),
|
maybeError?.errors?.detail || maybeError?.error || `Challonge responded with ${v2Response.status} ${v2Response.statusText}`.trim(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return payload;
|
return v2Payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeTournamentSlug = (value: string): string => {
|
const normalizeTournamentSlug = (value: string): string => {
|
||||||
|
|||||||
Reference in New Issue
Block a user