Rename character API endpoints

Precursor to moving the character API into its own module
This commit is contained in:
valadaptive 2023-12-04 06:51:45 -05:00
parent 45730d4766
commit b689b8bd30
4 changed files with 29 additions and 29 deletions

View File

@ -1190,7 +1190,7 @@ export function getEntitiesList({ doFilter } = {}) {
} }
export async function getOneCharacter(avatarUrl) { export async function getOneCharacter(avatarUrl) {
const response = await fetch('/getonecharacter', { const response = await fetch('/api/characters/get', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify({ body: JSON.stringify({
@ -1214,7 +1214,7 @@ export async function getOneCharacter(avatarUrl) {
} }
async function getCharacters() { async function getCharacters() {
var response = await fetch('/getcharacters', { var response = await fetch('/api/characters/all', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify({ body: JSON.stringify({
@ -1270,7 +1270,7 @@ async function replaceCurrentChat() {
await clearChat(); await clearChat();
chat.length = 0; chat.length = 0;
const chatsResponse = await fetch('/getallchatsofcharacter', { const chatsResponse = await fetch('/api/characters/chats', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify({ avatar_url: characters[this_chid].avatar }), body: JSON.stringify({ avatar_url: characters[this_chid].avatar }),
@ -4166,7 +4166,7 @@ async function DupeChar() {
} }
const body = { avatar_url: characters[this_chid].avatar }; const body = { avatar_url: characters[this_chid].avatar };
const response = await fetch('/dupecharacter', { const response = await fetch('/api/characters/duplicate', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify(body), body: JSON.stringify(body),
@ -4793,7 +4793,7 @@ async function renameCharacter() {
if (newValue && newValue !== characters[this_chid].name) { if (newValue && newValue !== characters[this_chid].name) {
const body = JSON.stringify({ avatar_url: oldAvatar, new_name: newValue }); const body = JSON.stringify({ avatar_url: oldAvatar, new_name: newValue });
const response = await fetch('/renamecharacter', { const response = await fetch('/api/characters/rename', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body, body,
@ -5883,7 +5883,7 @@ export async function getChatsFromFiles(data, isGroupChat) {
async function getPastCharacterChats() { async function getPastCharacterChats() {
if (!characters[this_chid]) return; if (!characters[this_chid]) return;
const response = await fetch('/getallchatsofcharacter', { const response = await fetch('/api/characters/chats', {
method: 'POST', method: 'POST',
body: JSON.stringify({ avatar_url: characters[this_chid].avatar }), body: JSON.stringify({ avatar_url: characters[this_chid].avatar }),
headers: getRequestHeaders(), headers: getRequestHeaders(),
@ -6841,7 +6841,7 @@ async function createOrEditCharacter(e) {
if ($('#form_create').attr('actiontype') == 'createcharacter') { if ($('#form_create').attr('actiontype') == 'createcharacter') {
if ($('#character_name_pole').val().length > 0) { if ($('#character_name_pole').val().length > 0) {
//if the character name text area isn't empty (only posible when creating a new character) //if the character name text area isn't empty (only posible when creating a new character)
let url = '/createcharacter'; let url = '/api/characters/create';
if (crop_data != undefined) { if (crop_data != undefined) {
url += `?crop=${encodeURIComponent(JSON.stringify(crop_data))}`; url += `?crop=${encodeURIComponent(JSON.stringify(crop_data))}`;
@ -6923,7 +6923,7 @@ async function createOrEditCharacter(e) {
toastr.error('Name is required'); toastr.error('Name is required');
} }
} else { } else {
let url = '/editcharacter'; let url = '/api/characters/edit';
if (crop_data != undefined) { if (crop_data != undefined) {
url += `?crop=${encodeURIComponent(JSON.stringify(crop_data))}`; url += `?crop=${encodeURIComponent(JSON.stringify(crop_data))}`;
@ -7468,7 +7468,7 @@ async function importCharacter(file) {
const data = await jQuery.ajax({ const data = await jQuery.ajax({
type: 'POST', type: 'POST',
url: '/importcharacter', url: '/api/characters/import',
data: formData, data: formData,
async: true, async: true,
cache: false, cache: false,
@ -7576,7 +7576,7 @@ export async function handleDeleteCharacter(popup_type, this_chid, delete_chats)
const msg = { avatar_url: avatar, delete_chats: delete_chats }; const msg = { avatar_url: avatar, delete_chats: delete_chats };
const response = await fetch('/deletecharacter', { const response = await fetch('/api/characters/delete', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify(msg), body: JSON.stringify(msg),
@ -8990,7 +8990,7 @@ jQuery(async function () {
await createOrEditCharacter(); await createOrEditCharacter();
const body = { format, avatar_url: characters[this_chid].avatar }; const body = { format, avatar_url: characters[this_chid].avatar };
const response = await fetch('/exportcharacter', { const response = await fetch('/api/characters/export', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify(body), body: JSON.stringify(body),

View File

@ -53,7 +53,7 @@ class CharacterContextMenu {
static duplicate = async (characterId) => { static duplicate = async (characterId) => {
const character = CharacterContextMenu.#getCharacter(characterId); const character = CharacterContextMenu.#getCharacter(characterId);
return fetch('/dupecharacter', { return fetch('/api/characters/duplicate', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify({ avatar_url: character.avatar }), body: JSON.stringify({ avatar_url: character.avatar }),
@ -81,7 +81,7 @@ class CharacterContextMenu {
}, },
}; };
return fetch('/v2/editcharacterattribute', { return fetch('/api/characters/merge-attributes', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify(data), body: JSON.stringify(data),
@ -115,7 +115,7 @@ class CharacterContextMenu {
static delete = async (characterId, deleteChats = false) => { static delete = async (characterId, deleteChats = false) => {
const character = CharacterContextMenu.#getCharacter(characterId); const character = CharacterContextMenu.#getCharacter(characterId);
return fetch('/deletecharacter', { return fetch('/api/characters/delete', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify({ avatar_url: character.avatar, delete_chats: deleteChats }), body: JSON.stringify({ avatar_url: character.avatar, delete_chats: deleteChats }),
@ -124,7 +124,7 @@ class CharacterContextMenu {
if (response.ok) { if (response.ok) {
deleteCharacter(character.name, character.avatar).then(() => { deleteCharacter(character.name, character.avatar).then(() => {
if (deleteChats) { if (deleteChats) {
fetch('/getallchatsofcharacter', { fetch('/api/characters/chats', {
method: 'POST', method: 'POST',
body: JSON.stringify({ avatar_url: character.avatar }), body: JSON.stringify({ avatar_url: character.avatar }),
headers: getRequestHeaders(), headers: getRequestHeaders(),

View File

@ -42,7 +42,7 @@ async function getExistingChatNames() {
const data = await getGroupPastChats(selected_group); const data = await getGroupPastChats(selected_group);
return data.map(x => x.file_name); return data.map(x => x.file_name);
} else { } else {
const response = await fetch('/getallchatsofcharacter', { const response = await fetch('/api/characters/chats', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify({ avatar_url: characters[this_chid].avatar }), body: JSON.stringify({ avatar_url: characters[this_chid].avatar }),

View File

@ -1027,7 +1027,7 @@ function charaFormatData(data) {
return char; return char;
} }
app.post('/createcharacter', urlencodedParser, async function (request, response) { app.post('/api/characters/create', urlencodedParser, async function (request, response) {
if (!request.body) return response.sendStatus(400); if (!request.body) return response.sendStatus(400);
request.body.ch_name = sanitize(request.body.ch_name); request.body.ch_name = sanitize(request.body.ch_name);
@ -1073,7 +1073,7 @@ app.post('/renamechat', jsonParser, async function (request, response) {
return response.send({ ok: true }); return response.send({ ok: true });
}); });
app.post('/renamecharacter', jsonParser, async function (request, response) { app.post('/api/characters/rename', jsonParser, async function (request, response) {
if (!request.body.avatar_url || !request.body.new_name) { if (!request.body.avatar_url || !request.body.new_name) {
return response.sendStatus(400); return response.sendStatus(400);
} }
@ -1119,7 +1119,7 @@ app.post('/renamecharacter', jsonParser, async function (request, response) {
} }
}); });
app.post('/editcharacter', urlencodedParser, async function (request, response) { app.post('/api/characters/edit', urlencodedParser, async function (request, response) {
if (!request.body) { if (!request.body) {
console.error('Error: no response body detected'); console.error('Error: no response body detected');
response.status(400).send('Error: no response body detected'); response.status(400).send('Error: no response body detected');
@ -1166,7 +1166,7 @@ app.post('/editcharacter', urlencodedParser, async function (request, response)
* @param {Object} response - The HTTP response object. * @param {Object} response - The HTTP response object.
* @returns {void} * @returns {void}
*/ */
app.post('/editcharacterattribute', jsonParser, async function (request, response) { app.post('/api/characters/edit-attribute', jsonParser, async function (request, response) {
console.log(request.body); console.log(request.body);
if (!request.body) { if (!request.body) {
console.error('Error: no response body detected'); console.error('Error: no response body detected');
@ -1212,7 +1212,7 @@ app.post('/editcharacterattribute', jsonParser, async function (request, respons
* *
* @returns {void} * @returns {void}
* */ * */
app.post('/v2/editcharacterattribute', jsonParser, async function (request, response) { app.post('/api/characters/merge-attributes', jsonParser, async function (request, response) {
const update = request.body; const update = request.body;
const avatarPath = path.join(charactersPath, update.avatar); const avatarPath = path.join(charactersPath, update.avatar);
@ -1240,7 +1240,7 @@ app.post('/v2/editcharacterattribute', jsonParser, async function (request, resp
} }
}); });
app.post('/deletecharacter', jsonParser, async function (request, response) { app.post('/api/characters/delete', jsonParser, async function (request, response) {
if (!request.body || !request.body.avatar_url) { if (!request.body || !request.body.avatar_url) {
return response.sendStatus(400); return response.sendStatus(400);
} }
@ -1413,7 +1413,7 @@ const processCharacter = async (item, i) => {
/** /**
* HTTP POST endpoint for the "/getcharacters" route. * HTTP POST endpoint for the "/api/characters/all" route.
* *
* This endpoint is responsible for reading character files from the `charactersPath` directory, * This endpoint is responsible for reading character files from the `charactersPath` directory,
* parsing character data, calculating stats for each character and responding with the data. * parsing character data, calculating stats for each character and responding with the data.
@ -1426,7 +1426,7 @@ const processCharacter = async (item, i) => {
* @param {object} response The HTTP response object. * @param {object} response The HTTP response object.
* @return {undefined} Does not return a value. * @return {undefined} Does not return a value.
*/ */
app.post('/getcharacters', jsonParser, function (request, response) { app.post('/api/characters/all', jsonParser, function (request, response) {
fs.readdir(charactersPath, async (err, files) => { fs.readdir(charactersPath, async (err, files) => {
if (err) { if (err) {
console.error(err); console.error(err);
@ -1449,7 +1449,7 @@ app.post('/getcharacters', jsonParser, function (request, response) {
}); });
}); });
app.post('/getonecharacter', jsonParser, async function (request, response) { app.post('/api/characters/get', jsonParser, async function (request, response) {
if (!request.body) return response.sendStatus(400); if (!request.body) return response.sendStatus(400);
const item = request.body.avatar_url; const item = request.body.avatar_url;
const filePath = path.join(charactersPath, item); const filePath = path.join(charactersPath, item);
@ -1916,7 +1916,7 @@ function getImages(path) {
.sort(Intl.Collator().compare); .sort(Intl.Collator().compare);
} }
app.post('/getallchatsofcharacter', jsonParser, async function (request, response) { app.post('/api/characters/chats', jsonParser, async function (request, response) {
if (!request.body) return response.sendStatus(400); if (!request.body) return response.sendStatus(400);
const characterDirectory = (request.body.avatar_url).replace('.png', ''); const characterDirectory = (request.body.avatar_url).replace('.png', '');
@ -1993,7 +1993,7 @@ function getPngName(file) {
return file; return file;
} }
app.post('/importcharacter', urlencodedParser, async function (request, response) { app.post('/api/characters/import', urlencodedParser, async function (request, response) {
if (!request.body || request.file === undefined) return response.sendStatus(400); if (!request.body || request.file === undefined) return response.sendStatus(400);
@ -2137,7 +2137,7 @@ app.post('/importcharacter', urlencodedParser, async function (request, response
} }
}); });
app.post('/dupecharacter', jsonParser, async function (request, response) { app.post('/api/characters/duplicate', jsonParser, async function (request, response) {
try { try {
if (!request.body.avatar_url) { if (!request.body.avatar_url) {
console.log('avatar URL not found in request body'); console.log('avatar URL not found in request body');
@ -2252,7 +2252,7 @@ app.post('/exportchat', jsonParser, async function (request, response) {
} }
}); });
app.post('/exportcharacter', jsonParser, async function (request, response) { app.post('/api/characters/export', jsonParser, async function (request, response) {
if (!request.body.format || !request.body.avatar_url) { if (!request.body.format || !request.body.avatar_url) {
return response.sendStatus(400); return response.sendStatus(400);
} }