From 34a9c0d0d1a549fd315d527b1c5a683286333f54 Mon Sep 17 00:00:00 2001 From: Dessa Simpson Date: Sun, 2 Aug 2020 22:06:28 -0700 Subject: [PATCH] Consistency and cleanup --- src/app.ts | 20 ++++++++++++-------- src/db.ts | 2 +- src/requests.ts | 17 ++++++++--------- tsconfig.json | 2 +- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/app.ts b/src/app.ts index f223415..866d170 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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) => 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) => 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}`); }); diff --git a/src/db.ts b/src/db.ts index 493eb6d..b9d564a 100644 --- a/src/db.ts +++ b/src/db.ts @@ -1,4 +1,4 @@ -const { Pool } = require('pg'); +import { Pool } from 'pg'; const db = new Pool(); diff --git a/src/requests.ts b/src/requests.ts index f1bf10a..fbc4754 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -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]); }; - diff --git a/tsconfig.json b/tsconfig.json index fb88b50..f66b180 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es5", + "target": "es2017", "module": "commonjs", "strict": true, "esModuleInterop": true,