🎨 Improve code readability by reformatting and updating function definitions and comments
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
/**
|
||||
* Challonge API v1 Service (DEPRECATED - REFERENCE ONLY)
|
||||
*
|
||||
* ⚠️ DEPRECATED: This service is maintained for reference purposes only.
|
||||
* Use challonge-v2.1.service.js for new development.
|
||||
*
|
||||
* Client for interacting with Challonge tournament platform API v1
|
||||
* Adapted from Discord bot for Vue 3 browser environment
|
||||
*/
|
||||
|
||||
import { API_CONFIG } from '../utilities/constants.js';
|
||||
|
||||
/**
|
||||
* Get the appropriate base URL based on environment
|
||||
* Development: Use Vite proxy to avoid CORS
|
||||
* Production: Use direct API (requires backend proxy or CORS handling)
|
||||
*/
|
||||
function getBaseURL() {
|
||||
// In development, use Vite proxy
|
||||
if (import.meta.env.DEV) {
|
||||
return '/api/challonge/v1/';
|
||||
}
|
||||
// In production, use direct API (will need backend proxy for CORS)
|
||||
return API_CONFIG.CHALLONGE_BASE_URL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Challonge API v1 client
|
||||
* @param {string} apiKey - Challonge API v1 key
|
||||
* @returns {Object} API client with methods
|
||||
*/
|
||||
export function createChallongeV1Client(apiKey) {
|
||||
const baseURL = getBaseURL();
|
||||
|
||||
/**
|
||||
* Make API request
|
||||
* @param {string} endpoint - API endpoint
|
||||
* @param {Object} options - Fetch options
|
||||
* @returns {Promise<Object>} Response data
|
||||
*/
|
||||
async function makeRequest(endpoint, options = {}) {
|
||||
const cleanEndpoint = endpoint.startsWith('/')
|
||||
? endpoint.slice(1)
|
||||
: endpoint;
|
||||
const url = new URL(`${baseURL}${cleanEndpoint}`, window.location.origin);
|
||||
url.searchParams.append('api_key', apiKey);
|
||||
|
||||
if (options.params) {
|
||||
Object.entries(options.params).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
url.searchParams.append(key, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const fetchOptions = {
|
||||
method: options.method || 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
...options.headers
|
||||
}
|
||||
};
|
||||
|
||||
if (options.body) {
|
||||
fetchOptions.body = JSON.stringify(options.body);
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url.toString(), fetchOptions);
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}));
|
||||
throw new Error(
|
||||
error.errors?.[0] || `HTTP ${response.status}: ${response.statusText}`
|
||||
);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('Challonge API v1 Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Tournament Methods
|
||||
const tournaments = {
|
||||
list: params => makeRequest('tournaments', { params }),
|
||||
get: (id, options = {}) =>
|
||||
makeRequest(`tournaments/${id}`, {
|
||||
params: {
|
||||
include_participants: options.includeParticipants ? 1 : 0,
|
||||
include_matches: options.includeMatches ? 1 : 0
|
||||
}
|
||||
}),
|
||||
create: data =>
|
||||
makeRequest('tournaments', {
|
||||
method: 'POST',
|
||||
body: { tournament: data }
|
||||
}),
|
||||
update: (id, data) =>
|
||||
makeRequest(`tournaments/${id}`, {
|
||||
method: 'PUT',
|
||||
body: { tournament: data }
|
||||
}),
|
||||
delete: id => makeRequest(`tournaments/${id}`, { method: 'DELETE' }),
|
||||
start: (id, options = {}) =>
|
||||
makeRequest(`tournaments/${id}/start`, {
|
||||
method: 'POST',
|
||||
params: options
|
||||
}),
|
||||
finalize: id =>
|
||||
makeRequest(`tournaments/${id}/finalize`, { method: 'POST' }),
|
||||
reset: id => makeRequest(`tournaments/${id}/reset`, { method: 'POST' })
|
||||
};
|
||||
|
||||
// Participant Methods
|
||||
const participants = {
|
||||
list: tournamentId =>
|
||||
makeRequest(`tournaments/${tournamentId}/participants`),
|
||||
add: (tournamentId, data) =>
|
||||
makeRequest(`tournaments/${tournamentId}/participants`, {
|
||||
method: 'POST',
|
||||
body: { participant: data }
|
||||
}),
|
||||
bulkAdd: (tournamentId, participants) =>
|
||||
makeRequest(`tournaments/${tournamentId}/participants/bulk_add`, {
|
||||
method: 'POST',
|
||||
body: { participants }
|
||||
}),
|
||||
update: (tournamentId, participantId, data) =>
|
||||
makeRequest(`tournaments/${tournamentId}/participants/${participantId}`, {
|
||||
method: 'PUT',
|
||||
body: { participant: data }
|
||||
}),
|
||||
delete: (tournamentId, participantId) =>
|
||||
makeRequest(`tournaments/${tournamentId}/participants/${participantId}`, {
|
||||
method: 'DELETE'
|
||||
}),
|
||||
checkIn: (tournamentId, participantId) =>
|
||||
makeRequest(
|
||||
`tournaments/${tournamentId}/participants/${participantId}/check_in`,
|
||||
{ method: 'POST' }
|
||||
),
|
||||
undoCheckIn: (tournamentId, participantId) =>
|
||||
makeRequest(
|
||||
`tournaments/${tournamentId}/participants/${participantId}/undo_check_in`,
|
||||
{ method: 'POST' }
|
||||
),
|
||||
randomize: tournamentId =>
|
||||
makeRequest(`tournaments/${tournamentId}/participants/randomize`, {
|
||||
method: 'POST'
|
||||
})
|
||||
};
|
||||
|
||||
// Match Methods
|
||||
const matches = {
|
||||
list: (tournamentId, params = {}) =>
|
||||
makeRequest(`tournaments/${tournamentId}/matches`, { params }),
|
||||
get: (tournamentId, matchId) =>
|
||||
makeRequest(`tournaments/${tournamentId}/matches/${matchId}`),
|
||||
update: (tournamentId, matchId, data) =>
|
||||
makeRequest(`tournaments/${tournamentId}/matches/${matchId}`, {
|
||||
method: 'PUT',
|
||||
body: { match: data }
|
||||
}),
|
||||
reopen: (tournamentId, matchId) =>
|
||||
makeRequest(`tournaments/${tournamentId}/matches/${matchId}/reopen`, {
|
||||
method: 'POST'
|
||||
}),
|
||||
markAsUnderway: (tournamentId, matchId) =>
|
||||
makeRequest(
|
||||
`tournaments/${tournamentId}/matches/${matchId}/mark_as_underway`,
|
||||
{ method: 'POST' }
|
||||
),
|
||||
unmarkAsUnderway: (tournamentId, matchId) =>
|
||||
makeRequest(
|
||||
`tournaments/${tournamentId}/matches/${matchId}/unmark_as_underway`,
|
||||
{ method: 'POST' }
|
||||
)
|
||||
};
|
||||
|
||||
return { tournaments, participants, matches };
|
||||
}
|
||||
|
||||
// Backwards compatibility export
|
||||
export const createChallongeClient = createChallongeV1Client;
|
||||
Reference in New Issue
Block a user