From 26ebb417f490c8fab287684ca80ba67ebf78c79f Mon Sep 17 00:00:00 2001 From: valadaptive Date: Thu, 7 Dec 2023 12:31:34 -0500 Subject: [PATCH] Rename stats API endpoints --- public/scripts/stats.js | 8 ++++---- server.js | 13 ++++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/public/scripts/stats.js b/public/scripts/stats.js index b8ff904ea..5a26a4905 100644 --- a/public/scripts/stats.js +++ b/public/scripts/stats.js @@ -172,7 +172,7 @@ async function characterStatsHandler(characters, this_chid) { * Fetches the character stats from the server. */ async function getStats() { - const response = await fetch('/getstats', { + const response = await fetch('/api/stats/get', { method: 'POST', headers: getRequestHeaders(), body: JSON.stringify({}), @@ -189,13 +189,13 @@ async function getStats() { /** * Asynchronously recreates the stats file from chat files. * - * Sends a POST request to the "/recreatestats" endpoint. If the request fails, + * Sends a POST request to the "/api/stats/recreate" endpoint. If the request fails, * it displays an error notification and throws an error. * * @throws {Error} If the request to recreate stats is unsuccessful. */ async function recreateStats() { - const response = await fetch('/recreatestats', { + const response = await fetch('/api/stats/recreate', { method: 'POST', headers: getRequestHeaders(), body: JSON.stringify({}), @@ -232,7 +232,7 @@ function calculateGenTime(gen_started, gen_finished) { * Sends a POST request to the server to update the statistics. */ async function updateStats() { - const response = await fetch('/updatestats', { + const response = await fetch('/api/stats/update', { method: 'POST', headers: getRequestHeaders(), body: JSON.stringify(charStats), diff --git a/server.js b/server.js index c872a1738..914eaa380 100644 --- a/server.js +++ b/server.js @@ -788,13 +788,11 @@ app.post('/getstatus', jsonParser, async function (request, response) { * @param {Object} response - The HTTP response object. * @returns {void} */ -app.post('/getstats', jsonParser, function (request, response) { +app.post('/api/stats/get', jsonParser, function (request, response) { response.send(JSON.stringify(statsHelpers.getCharStats())); }); /** - * Endpoint: POST /recreatestats - * * Triggers the recreation of statistics from chat files. * - If successful: returns a 200 OK status. * - On failure: returns a 500 Internal Server Error status. @@ -802,7 +800,7 @@ app.post('/getstats', jsonParser, function (request, response) { * @param {Object} request - Express request object. * @param {Object} response - Express response object. */ -app.post('/recreatestats', jsonParser, async function (request, response) { +app.post('/api/stats/recreate', jsonParser, async function (request, response) { try { await statsHelpers.recreateStats(DIRECTORIES.chats, DIRECTORIES.characters); return response.sendStatus(200); @@ -823,7 +821,7 @@ app.post('/recreatestats', jsonParser, async function (request, response) { * @returns {void} * */ -app.post('/updatestats', jsonParser, function (request, response) { +app.post('/api/stats/update', jsonParser, function (request, response) { if (!request.body) return response.sendStatus(400); statsHelpers.setCharStats(request.body); return response.sendStatus(200); @@ -2051,6 +2049,11 @@ redirect('/deleteworldinfo', '/api/worldinfo/delete'); redirect('/importworldinfo', '/api/worldinfo/import'); redirect('/editworldinfo', '/api/worldinfo/edit'); +// Redirect deprecated stats API endpoints +redirect('/getstats', '/api/stats/get'); +redirect('/recreatestats', '/api/stats/recreate'); +redirect('/updatestats', '/api/stats/update'); + // ** REST CLIENT ASYNC WRAPPERS ** /**