class ChallengeService { constructor(config) { this.baseUrl = config.API_BASE_URL; this.urlTail = "challenges"; } async postChallenge(username, password, opponent, text) { const response = await fetch(`${this.baseUrl}${this.urlTail}`, { method: "POST", headers: { "Content-Type": "application/json", "X-Username": username, "X-Password": password, }, body: JSON.stringify({ opponent: opponent, text: text, }), }); let body = null; try { body = await response.json(); } catch { console.error("Error in postChallenge"); } return { status: response.status, ok: response.ok, body: body, }; } async completeChallenge(username, password, challengeId, score, time, text, userWrittenText) { const response = await fetch(`${this.baseUrl}${this.urlTail}/${challengeId}/complete`, { method: "POST", headers: { "Content-Type": "application/json", "X-Username": username, "X-Password": password, }, body: JSON.stringify({ score: score, time: time, text: text, userWrittenText: userWrittenText, }), }); let body = null; try { body = await response.json(); } catch { console.error("Error in completeChallenge"); } return { status: response.status, ok: response.ok, body: body, }; } } window.ChallengeService = ChallengeService;