class MessageService { constructor(config) { this.baseUrl = config.API_BASE_URL; this.urlTail = "messages"; } async getMessages(username, password) { const response = await fetch(`${this.baseUrl}${this.urlTail}`, { method: "GET", headers: { "X-Username": username, "X-Password": password, }, }); let body = null; try { body = await response.json(); } catch { console.error("Error in getMessages"); } return { status: response.status, ok: response.ok, body: body, }; } async postMessage(username, password, recipient, type, 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({ recipient: recipient, type: type, text: text, }), }); let body = null; try { body = await response.json(); } catch { console.error("Error in postMessage"); } return { status: response.status, ok: response.ok, body: body, }; } async markMessageAsRead(username, password, messageId) { const response = await fetch(`${this.baseUrl}${this.urlTail}/${messageId}/read`, { method: "PATCH", headers: { "X-Username": username, "X-Password": password, }, }); let body = null; try { body = await response.json(); } catch { console.error("Error in markMessageAsRead"); } return { status: response.status, ok: response.ok, body: body, }; } async markAllMessagesAsRead(username, password) { const response = await fetch(`${this.baseUrl}${this.urlTail}/read`, { method: "PATCH", headers: { "X-Username": username, "X-Password": password, }, }); let body = null; try { body = await response.json(); } catch { console.error("Error in markAllMessagesAsRead"); } return { status: response.status, ok: response.ok, body: body, }; } } window.MessageService = MessageService;