Compare commits

..

No commits in common. "550d862e0926b47c2b08a99e2a5efa35581d3961" and "9843459d22d6ea5592fdafacc609b4b4735f2072" have entirely different histories.

5 changed files with 34 additions and 86 deletions

View file

@ -1,6 +1,6 @@
import { Express, Response } from "express"; import { Express, Request, Response } from "express";
import { Logger, Responder, data } from "../utils"; import { Logger, Responder, data } from "../utils";
import { IdRequest, ReviewRequest, typeJson } from "../types"; import { IdRequest, typeJson } from "../types";
import ApiRoute from "../utils/ApiRoute"; import ApiRoute from "../utils/ApiRoute";
export class MessagesResponder extends ApiRoute { export class MessagesResponder extends ApiRoute {
@ -10,34 +10,9 @@ export class MessagesResponder extends ApiRoute {
} }
public async start(): Promise<void> { public async start(): Promise<void> {
this.server.get(this.routeName, (req: ReviewRequest, res: Response) => { this.server.get(this.routeName, (req: Request, res: Response) => {
// This is a mouthful. :sigh:
// If the request parameter "max" is present and it isn't a number
if ((req.query.max && !+req.query.max) || +req.query.max < 1) {
res.writeHead(400, typeJson);
res.write(
Responder.requestError(
"max parameter must be a positive non-zero number or undefined",
),
);
res.end();
return;
}
// Same check as the above one, but for skip
if ((req.query.skip && !+req.query.skip) || +req.query.skip < 1) {
res.writeHead(400, typeJson);
res.write(
Responder.requestError(
"skip parameter must be a number or undefined",
),
);
res.end();
return;
}
const receiver = req.headers["user-agent"]; const receiver = req.headers["user-agent"];
const result = data.getReviews(+req.query.max, +req.query.skip); const result = data.getReviews();
res.writeHead(200, typeJson); res.writeHead(200, typeJson);
res.write(JSON.stringify(result)); res.write(JSON.stringify(result));
@ -46,9 +21,7 @@ export class MessagesResponder extends ApiRoute {
this.logger.log(`${Logger.emp(receiver)} <~ "${req.path}"`); this.logger.log(`${Logger.emp(receiver)} <~ "${req.path}"`);
}); });
this.server.get( this.server.get(`${this.routeName}/:id`, (req: IdRequest, res: Response) => {
`${this.routeName}/:id`,
(req: IdRequest, res: Response) => {
// "req.params.id" is the review ID that was receieved // "req.params.id" is the review ID that was receieved
// "result" is the review that is identified by that ID, if any // "result" is the review that is identified by that ID, if any
@ -75,11 +48,8 @@ export class MessagesResponder extends ApiRoute {
} }
// If an error was returned to the client, mark their user agent red in the logs // If an error was returned to the client, mark their user agent red in the logs
this.logger.log( this.logger.log(`${err ? Logger.err(receiver) : Logger.emp(receiver)} <~ "${req.path}"`);
`${err ? Logger.err(receiver) : Logger.emp(receiver)} <~ "${req.path}"`, });
);
},
);
this.complete(); this.complete();
} }

View file

@ -11,10 +11,7 @@ const rating = number()
) )
.required(); .required();
export const typeJson = { export const typeJson = { "Access-Control-Allow-Origin": "*", "Content-Type": "application/json" };
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
};
export const userReviewSchema = object({ export const userReviewSchema = object({
username: string().min(2).max(30).required(), username: string().min(2).max(30).required(),
@ -37,10 +34,3 @@ export interface IdRequest extends Request {
id: string; id: string;
}; };
} }
export interface ReviewRequest extends Request {
query: {
max: string;
skip: string;
};
}

View file

@ -33,6 +33,6 @@ export default class ApiRoute {
res.writeHead(400, typeJson); res.writeHead(400, typeJson);
res.write(Responder.success("ok")); res.write(Responder.success("ok"));
res.end(); res.end();
}); })
} }
} }

View file

@ -6,7 +6,6 @@ import {
UserSideReview, UserSideReview,
} from "../types"; } from "../types";
import { toServerReview, checkFile, stripId } from "./functions"; import { toServerReview, checkFile, stripId } from "./functions";
import { Logger } from "./logger";
export class ReviewData { export class ReviewData {
public data: ServerSideReview[]; public data: ServerSideReview[];
@ -32,23 +31,12 @@ export class ReviewData {
}); });
} }
public getReviews(max?: number, skip?: number): ServerSideReview[] { public getReviews(): ServerSideReview[] {
let final: ServerSideReview[] = []; let final: ServerSideReview[] = [];
// If "max" isn't provided, is Infinity, or is greater than the amount of reviews
const sendAllReviews = !max || max === Infinity || max > this.data.length;
const useList = skip ? this.data.slice(skip, this.data.length) : this.data;
if (sendAllReviews) { for (const review of this.data) {
for (const review of useList) {
final.push(review); final.push(review);
} }
} else {
for (const review of useList) {
while (final.length !== max) {
final.push(review);
}
}
}
return final; return final;
} }

View file

@ -47,8 +47,8 @@ export class Responder {
return JSON.stringify({ return JSON.stringify({
error: { error: {
type: "notFoundError", type: "notFoundError",
message: errorMessage, message: errorMessage
}, }
}); });
} }
} }