Added messages to most api calls

This commit is contained in:
Ean Milligan (Bastion) 2022-06-21 21:48:07 -04:00
parent 93375585c1
commit cc45794497
10 changed files with 29 additions and 27 deletions

View File

@ -110,7 +110,7 @@ const start = async (): Promise<void> => {
break;
default:
// Alert API user that they messed up
requestEvent.respondWith(stdResp.NotFound(''));
requestEvent.respondWith(stdResp.NotFound('Auth Get'));
break;
}
break;
@ -122,7 +122,7 @@ const start = async (): Promise<void> => {
break;
default:
// Alert API user that they messed up
requestEvent.respondWith(stdResp.NotFound(''));
requestEvent.respondWith(stdResp.NotFound('Auth Post'));
break;
}
break;
@ -152,7 +152,7 @@ const start = async (): Promise<void> => {
break;
default:
// Alert API user that they messed up
requestEvent.respondWith(stdResp.NotFound(''));
requestEvent.respondWith(stdResp.NotFound('Auth Put'));
break;
}
break;
@ -164,13 +164,13 @@ const start = async (): Promise<void> => {
break;
default:
// Alert API user that they messed up
requestEvent.respondWith(stdResp.NotFound(''));
requestEvent.respondWith(stdResp.NotFound('Auth Del'));
break;
}
break;
default:
// Alert API user that they messed up
requestEvent.respondWith(stdResp.MethodNotAllowed(''));
requestEvent.respondWith(stdResp.MethodNotAllowed('Auth'));
break;
}
@ -190,13 +190,13 @@ const start = async (): Promise<void> => {
break;
default:
// Alert API user that they messed up
requestEvent.respondWith(stdResp.NotFound(''));
requestEvent.respondWith(stdResp.NotFound('NoAuth Get'));
break;
}
break;
default:
// Alert API user that they messed up
requestEvent.respondWith(stdResp.MethodNotAllowed(''));
requestEvent.respondWith(stdResp.MethodNotAllowed('NoAuth'));
break;
}
}

View File

@ -22,7 +22,7 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map<s
await dbClient.execute('DELETE FROM allowed_channels WHERE userid = ?', [apiUserid]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(stdResp.InternalServerError('Channel Clean Failed'));
requestEvent.respondWith(stdResp.InternalServerError('Channel Clean Failed.'));
erroredOut = true;
});
if (erroredOut) {
@ -31,19 +31,19 @@ export const apiKeyDelete = async (requestEvent: Deno.RequestEvent, query: Map<s
await dbClient.execute('DELETE FROM all_keys WHERE userid = ?', [apiUserid]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(stdResp.InternalServerError('Delete Key Failed'));
requestEvent.respondWith(stdResp.InternalServerError('Delete Key Failed.'));
erroredOut = true;
});
if (erroredOut) {
return;
} else {
// Send OK as response to indicate key deletion was successful
requestEvent.respondWith(stdResp.OK(''));
requestEvent.respondWith(stdResp.OK('You have been removed from the DB, Goodbye.'));
return;
}
} else {
// Alert API user that they shouldn't be doing this
requestEvent.respondWith(stdResp.Forbidden(''));
requestEvent.respondWith(stdResp.Forbidden('Invalid Delete Code.'));
}
} else {
// User does not have their delete code yet, so we need to generate one and email it to them

View File

@ -15,7 +15,7 @@ export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map<str
// Get all channels userid has authorized
const dbAllowedChannelQuery = await dbClient.query('SELECT * FROM allowed_channels WHERE userid = ?', [apiUserid]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(stdResp.InternalServerError(''));
requestEvent.respondWith(stdResp.InternalServerError('Failed to get channels.'));
erroredOut = true;
});

View File

@ -24,7 +24,7 @@ export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map<string,
await dbClient.execute('INSERT INTO all_keys(userid,apiKey,email) values(?,?,?)', [BigInt(query.get('user') || '0'), newKey, (query.get('email') || '').toLowerCase()]).catch(
(e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(stdResp.InternalServerError(''));
requestEvent.respondWith(stdResp.InternalServerError('Failed to store key.'));
erroredOut = true;
},
);

View File

@ -21,7 +21,7 @@ export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map<st
// Insert new key/user pair into the db
await dbClient.execute('INSERT INTO all_keys(userid,apiKey) values(?,?)', [apiUserid, newKey]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(stdResp.InternalServerError(''));
requestEvent.respondWith(stdResp.InternalServerError('Failed to store key.'));
erroredOut = true;
});

View File

@ -21,7 +21,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
) {
if (query.has('n') && query.has('m')) {
// Alert API user that they shouldn't be doing this
requestEvent.respondWith(stdResp.BadRequest(''));
requestEvent.respondWith(stdResp.BadRequest('Cannot have both \'n\' and \'m\'.'));
return;
}
@ -57,7 +57,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
if (rollCmd.length === 0) {
// Alert API user that they messed up
requestEvent.respondWith(stdResp.BadRequest(''));
requestEvent.respondWith(stdResp.BadRequest('rollCmd is required.'));
// Always log API rolls for abuse detection
dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'EmptyInput', null]).catch((e) => {
@ -68,7 +68,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
if (query.has('o') && (query.get('o')?.toLowerCase() !== 'd' && query.get('o')?.toLowerCase() !== 'a')) {
// Alert API user that they messed up
requestEvent.respondWith(stdResp.BadRequest(''));
requestEvent.respondWith(stdResp.BadRequest('Order must be set to \'a\' or \'d\'.'));
// Always log API rolls for abuse detection
dbClient.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'BadOrder', null]).catch((e) => {
@ -107,7 +107,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
} catch (err) {
// Handle any errors we missed
log(LT.ERROR, `Unhandled Error: ${JSON.stringify(err)}`);
requestEvent.respondWith(stdResp.InternalServerError(''));
requestEvent.respondWith(stdResp.InternalServerError('Something went wrong.'));
}
} else {
// Alert API user that they messed up

View File

@ -15,7 +15,7 @@ export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<
// Insert new user/channel pair into the db
await dbClient.execute('INSERT INTO allowed_channels(userid,channelid) values(?,?)', [apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(stdResp.InternalServerError(''));
requestEvent.respondWith(stdResp.InternalServerError('Failed to store channel.'));
erroredOut = true;
});
@ -24,7 +24,7 @@ export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<
return;
} else {
// Send OK to indicate modification was successful
requestEvent.respondWith(stdResp.OK(''));
requestEvent.respondWith(stdResp.OK('Successfully added channel.'));
return;
}
} else {

View File

@ -22,7 +22,7 @@ export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, qu
// Update the requested entry
await dbClient.execute('UPDATE allowed_channels SET active = ? WHERE userid = ? AND channelid = ?', [value, apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(stdResp.InternalServerError(''));
requestEvent.respondWith(stdResp.InternalServerError('Failed to update channel.'));
erroredOut = true;
});
@ -31,7 +31,7 @@ export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, qu
return;
} else {
// Send API key as response
requestEvent.respondWith(stdResp.OK(''));
requestEvent.respondWith(stdResp.OK(`Successfully active to ${value}.`));
return;
}
} else {

View File

@ -26,7 +26,7 @@ export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query
// Execute the DB modification
await dbClient.execute('UPDATE allowed_channels SET banned = ? WHERE userid = ? AND channelid = ?', [value, apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(stdResp.InternalServerError(''));
requestEvent.respondWith(stdResp.InternalServerError('Failed to update channel.'));
erroredOut = true;
});
@ -35,7 +35,7 @@ export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query
return;
} else {
// Send OK to indicate modification was successful
requestEvent.respondWith(stdResp.OK(''));
requestEvent.respondWith(stdResp.OK(`Successfully active to ${value}.`));
return;
}
} else {

View File

@ -11,7 +11,9 @@ export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<s
if ((query.has('a') && ((query.get('a') || '').length > 0)) && (query.has('user') && ((query.get('user') || '').length > 0))) {
if (apiUserid === config.api.admin && apiUserid === BigInt(query.get('a') || '0')) {
// Flag to see if there is an error inside the catch
let key, value, erroredOut = false;
let key: string,
value: number,
erroredOut = false;
// Determine key to edit
if (path.toLowerCase().indexOf('ban') > 0) {
@ -30,7 +32,7 @@ export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<s
// Execute the DB modification
await dbClient.execute('UPDATE all_keys SET ?? = ? WHERE userid = ?', [key, value, apiUserid]).catch((e) => {
log(LT.ERROR, `Failed to insert into database: ${JSON.stringify(e)}`);
requestEvent.respondWith(stdResp.InternalServerError(''));
requestEvent.respondWith(stdResp.InternalServerError(`Failed to ${key} to ${value}.`));
erroredOut = true;
});
@ -39,7 +41,7 @@ export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<s
return;
} else {
// Send OK as response to indicate modification was successful
requestEvent.respondWith(stdResp.OK(''));
requestEvent.respondWith(stdResp.OK(`Successfully ${key} to ${value}.`));
return;
}
} else {