Rename stats API endpoints

This commit is contained in:
valadaptive 2023-12-07 12:31:34 -05:00
parent d6f5e63d85
commit 26ebb417f4
2 changed files with 12 additions and 9 deletions

View File

@ -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),

View File

@ -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 **
/**