Consistency and cleanup

master
Dessa Simpson 2020-08-02 22:06:28 -07:00
parent 567b2bba27
commit 34a9c0d0d1
4 changed files with 22 additions and 19 deletions

View File

@ -2,12 +2,10 @@ import * as config from "./config";
import * as requests from "./requests";
import * as twitch from "./twitch";
import { URLSearchParams } from "url";
import { QueryResult } from "pg";
import express from "express";
import session from "express-session";
import pgSessionStore from "connect-pg-simple";
import fetch, { Response as FetchResponse } from "node-fetch";
import * as eta from "eta";
import db from "./db";
import errorHandler from "./errors";
@ -26,13 +24,13 @@ app.use(session({
// API
app.get("/api/getRequests", async (request, response) => {
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
requests.getRequests(requestCount).then((val: QueryResult) => response.send(val))
requests.getRequests(requestCount).then((val: Array<any>) => response.send(val))
.catch((e: any) => errorHandler(request,response,e));
});
app.get("/api/getAllRequests", async (request, response) => {
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
requests.getAllRequests(requestCount).then((val: QueryResult) => response.send(val))
requests.getAllRequests(requestCount).then((val: Array<any>) => response.send(val))
.catch((e: any) => errorHandler(request,response,e));
});
@ -92,7 +90,10 @@ app.post("/api/updateRequestScore", async (request, response) => {
}
var url = request.body.url as string;
var scoreDiff = parseInt(request.body.scoreDiff as string, 10);
requests.updateRequestScore(url,scoreDiff).then((val: string) => response.send(val))
requests.updateRequestScore(url,scoreDiff).then((val: [number,string]) => {
response.status(val[0]);
response.send(val[1]);
})
.catch((e: any) => errorHandler(request,response,e));
});
@ -104,7 +105,10 @@ app.post("/api/deleteRequest", async (request, response) => {
return
}
var url = request.body.url as string;
requests.deleteRequest(url).then((val: string) => response.send(val))
requests.deleteRequest(url).then((val: [number,string]) => {
response.status(val[0]);
response.send(val[1]);
})
.catch((e: any) => errorHandler(request,response,e));
});
@ -151,9 +155,9 @@ app.get("/", async (request, response) => {
});
// Logout
app.get ("/logout", async (request, response) => request.session!.destroy((err) => response.redirect(307, '/')));
app.get ("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/')));
const server = app.listen(config.port, () => {
app.listen(config.port, () => {
console.log(`Listening on port ${config.port}`);
});

View File

@ -1,4 +1,4 @@
const { Pool } = require('pg');
import { Pool } from 'pg';
const db = new Pool();

View File

@ -40,7 +40,7 @@ const addRequestQuery = {
text: "INSERT INTO requests (url,requester) VALUES ($1,$2)"
}
export async function addRequest(url: string, requester: string) {
export async function addRequest(url: string, requester: string): Promise<[number,string]> {
var validUrl = false;
for (var regex of validUrlRegexes) {
if (regex.test(url)) {
@ -56,7 +56,7 @@ export async function addRequest(url: string, requester: string) {
}
var query = Object.assign(addRequestQuery, { values: [url,requester] });
return db.query(query)
.then((result: pg.QueryResult) => [201,"Song request added."]);
.then(() => [201,"Song request added."] as [number,string]);
};
// updateRequestState
@ -70,7 +70,7 @@ const updateRequestStateQuery = {
text: "UPDATE requests SET state = $2 WHERE url = $1"
}
export async function updateRequestState(url: string, state: string) {
export async function updateRequestState(url: string, state: string): Promise<[number,string]> {
var query = Object.assign(checkValidStateQuery, { values: [state] });
var result = await db.query(query);
if (result.rowCount < 1) {
@ -79,7 +79,7 @@ export async function updateRequestState(url: string, state: string) {
var query = Object.assign(updateRequestStateQuery, { values: [url,state] });
return db.query(query)
.then((result: pg.QueryResult) => [200,"Song request state updated."]);
.then(() => [200,"Song request state updated."] as [number,string]);
};
// updateRequestScore
@ -88,10 +88,10 @@ const updateRequestScoreQuery = {
text: "UPDATE requests SET score = score + $2 WHERE url = $1"
}
export async function updateRequestScore(url: string, scoreDiff: number) {
export async function updateRequestScore(url: string, scoreDiff: number): Promise<[number,string]> {
var query = Object.assign(updateRequestScoreQuery, { values: [url,scoreDiff] });
return db.query(query)
.then((result: pg.QueryResult) => "Song request score updated.");
.then(() => [200,"Song request score updated."] as [number,string]);
};
// deleteRequest
@ -100,9 +100,8 @@ const deleteRequestQuery = {
text: "DELETE FROM requests WHERE url = $1"
}
export async function deleteRequest(url: string) {
export async function deleteRequest(url: string): Promise<[number,string]> {
var query = Object.assign(deleteRequestQuery, { values: [url] });
return db.query(query)
.then((result: pg.QueryResult) => "Song request deleted.");
.then(() => [200,"Song request deleted."] as [number,string]);
};

View File

@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2017",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,