update api to use cleaner method of verifying query params

This commit is contained in:
Ean Milligan 2025-04-26 15:19:16 -04:00
parent e0e013bf61
commit 1240bff99c
10 changed files with 23 additions and 24 deletions

View File

@ -9,11 +9,12 @@ import {
import { generateApiDeleteEmail } from '../../commandUtils.ts';
import utils from '../../utils.ts';
import stdResp from '../stdResponses.ts';
import { verifyQueryHasParams } from '../utils.ts';
export const apiKeyDelete = async (query: Map<string, string>, apiUserid: bigint, apiUserEmail: string, apiUserDelCode: string): Promise<Response> => {
if (query.has('user') && (query.get('user') || '').length > 0 && query.has('email') && (query.get('email') || '').length > 0) {
if (verifyQueryHasParams(query, ['user', 'email'])) {
if (apiUserid === BigInt(query.get('user') || '0') && apiUserEmail === query.get('email')) {
if (query.has('code') && (query.get('code') || '').length > 0) {
if (verifyQueryHasParams(query, ['code'])) {
if ((query.get('code') || '') === apiUserDelCode) {
// User has recieved their delete code and we need to delete the account now
let erroredOut = false;

View File

@ -1,9 +1,10 @@
import dbClient from '../../db/client.ts';
import stdResp from '../stdResponses.ts';
import utils from '../../utils.ts';
import { verifyQueryHasParams } from '../utils.ts';
export const apiChannel = async (query: Map<string, string>, apiUserid: bigint): Promise<Response> => {
if (query.has('user') && (query.get('user') || '').length > 0) {
if (verifyQueryHasParams(query, ['user'])) {
if (apiUserid === BigInt(query.get('user') || '0')) {
// Flag to see if there is an error inside the catch
let erroredOut = false;

View File

@ -9,9 +9,10 @@ import {
import { generateApiKeyEmail } from '../../commandUtils.ts';
import utils from '../../utils.ts';
import stdResp from '../stdResponses.ts';
import { verifyQueryHasParams } from '../utils.ts';
export const apiKey = async (query: Map<string, string>): Promise<Response> => {
if (query.has('user') && (query.get('user') || '').length > 0 && query.has('email') && (query.get('email') || '').length > 0) {
if (verifyQueryHasParams(query, ['user', 'email'])) {
// Generate new secure key
const newKey = await nanoid(25);

View File

@ -6,9 +6,10 @@ import {
} from '../../../deps.ts';
import stdResp from '../stdResponses.ts';
import utils from '../../utils.ts';
import { verifyQueryHasParams } from '../utils.ts';
export const apiKeyAdmin = async (query: Map<string, string>, apiUserid: bigint): Promise<Response> => {
if (query.has('user') && (query.get('user') || '').length > 0 && query.has('a') && (query.get('a') || '').length > 0) {
if (verifyQueryHasParams(query, ['user', 'a'])) {
if (apiUserid === config.api.admin && apiUserid === BigInt(query.get('a') || '0')) {
// Generate new secure key
const newKey = await nanoid(25);

View File

@ -12,19 +12,13 @@ import { RollModifiers } from '../../mod.d.ts';
import utils from '../../utils.ts';
import { queueRoll } from '../../solver/rollQueue.ts';
import stdResp from '../stdResponses.ts';
import { verifyQueryHasParams } from '../utils.ts';
const apiWarning = `The following roll was conducted using my built in API. If someone in this channel did not request this roll, please report API abuse here: <${config.api.supportURL}>`;
export const apiRoll = async (query: Map<string, string>, apiUserid: bigint): Promise<Response> => {
// Make sure query contains all the needed parts
if (
query.has('rollstr') &&
(query.get('rollstr') || '').length > 0 &&
query.has('channel') &&
(query.get('channel') || '').length > 0 &&
query.has('user') &&
(query.get('user') || '').length > 0
) {
if (verifyQueryHasParams(query, ['user', 'channel', 'rollstr'])) {
if (query.has('n') && query.has('m')) {
// Alert API user that they shouldn't be doing this
return stdResp.BadRequest("Cannot have both 'n' and 'm'.");

View File

@ -1,9 +1,10 @@
import dbClient from '../../db/client.ts';
import stdResp from '../stdResponses.ts';
import utils from '../../utils.ts';
import { verifyQueryHasParams } from '../utils.ts';
export const apiChannelAdd = async (query: Map<string, string>, apiUserid: bigint): Promise<Response> => {
if (query.has('user') && (query.get('user') || '').length > 0 && query.has('channel') && (query.get('channel') || '').length > 0) {
if (verifyQueryHasParams(query, ['user', 'channel'])) {
if (apiUserid === BigInt(query.get('user') || '0')) {
// Flag to see if there is an error inside the catch
let erroredOut = false;

View File

@ -1,9 +1,10 @@
import dbClient from '../../db/client.ts';
import stdResp from '../stdResponses.ts';
import utils from '../../utils.ts';
import { verifyQueryHasParams } from '../utils.ts';
export const apiChannelManageActive = async (query: Map<string, string>, apiUserid: bigint, path: string): Promise<Response> => {
if (query.has('channel') && (query.get('channel') || '').length > 0 && query.has('user') && (query.get('user') || '').length > 0) {
if (verifyQueryHasParams(query, ['user', 'channel'])) {
if (apiUserid === BigInt(query.get('user') || '0')) {
// Flag to see if there is an error inside the catch
let value,

View File

@ -2,16 +2,10 @@ import config from '../../../config.ts';
import dbClient from '../../db/client.ts';
import stdResp from '../stdResponses.ts';
import utils from '../../utils.ts';
import { verifyQueryHasParams } from '../utils.ts';
export const apiChannelManageBan = async (query: Map<string, string>, apiUserid: bigint, path: string): Promise<Response> => {
if (
query.has('a') &&
(query.get('a') || '').length > 0 &&
query.has('channel') &&
(query.get('channel') || '').length > 0 &&
query.has('user') &&
(query.get('user') || '').length > 0
) {
if (verifyQueryHasParams(query, ['user', 'channel', 'a'])) {
if (apiUserid === config.api.admin && apiUserid === BigInt(query.get('a') || '0')) {
// Flag to see if there is an error inside the catch
let value,

View File

@ -2,9 +2,10 @@ import config from '../../../config.ts';
import dbClient from '../../db/client.ts';
import stdResp from '../stdResponses.ts';
import utils from '../../utils.ts';
import { verifyQueryHasParams } from '../utils.ts';
export const apiKeyManage = async (query: Map<string, string>, apiUserid: bigint, path: string): Promise<Response> => {
if (query.has('a') && (query.get('a') || '').length > 0 && query.has('user') && (query.get('user') || '').length > 0) {
if (verifyQueryHasParams(query, ['user', 'a'])) {
if (apiUserid === config.api.admin && apiUserid === BigInt(query.get('a') || '0')) {
// Flag to see if there is an error inside the catch
let key: string,

4
src/endpoints/utils.ts Normal file
View File

@ -0,0 +1,4 @@
export const verifyQueryHasParams = (query: Map<string, string>, desiredParams: Array<string>): boolean =>
desiredParams.every((param) => {
return query.has(param) && (query.get(param) || '').length > 0;
});