This commit is contained in:
powermaker450 2024-08-21 01:36:33 -04:00
parent b1bdc83876
commit fd4bc44842
5 changed files with 31 additions and 27 deletions

View file

@ -1,18 +1,11 @@
import fs from "fs"; import fs from "fs";
import { import {
serverReviewSchema, serverReviewSchema,
ServerSideReview, ServerSideReview,
userReviewSchema, userReviewSchema,
typeJson, typeJson,
} from "../types"; } from "../types";
import { import { appendId, bold, checkFile, emp, Logger, responder } from "../utils";
appendId,
bold,
checkFile,
emp,
Logger,
responder
} from "../utils";
import { Express, Request, Response } from "express"; import { Express, Request, Response } from "express";
export class PostListener { export class PostListener {
@ -37,22 +30,29 @@ export class PostListener {
this.logger.error(`${sender} <~ Recieved chunk was not valid JSON!`); this.logger.error(`${sender} <~ Recieved chunk was not valid JSON!`);
res.writeHead(400, typeJson); res.writeHead(400, typeJson);
res.write(responder.JsonError("recieved chunk contains invalid JSON")); res.write(
responder.JsonError("recieved chunk contains invalid JSON"),
);
res.end(); res.end();
return; return;
} }
await userReviewSchema.validate(temp) await userReviewSchema
.validate(temp)
.then((validUserReview) => { .then((validUserReview) => {
req.on("end", () => { req.on("end", () => {
this.logger.log(`${sender} ~>`, validUserReview); this.logger.log(`${sender} ~>`, validUserReview);
serverReviewSchema.validate(appendId(validUserReview)) serverReviewSchema
.validate(appendId(validUserReview))
.then((validServerReview) => { .then((validServerReview) => {
data.push(validServerReview); data.push(validServerReview);
fs.writeFileSync("./persist/data.json", JSON.stringify(data, null, 2)); fs.writeFileSync(
"./persist/data.json",
JSON.stringify(data, null, 2),
);
res.writeHead(201, typeJson); res.writeHead(201, typeJson);
res.write(responder.success("review was sent")); res.write(responder.success("review was sent"));
@ -63,11 +63,12 @@ export class PostListener {
this.logger.error("Failed to assign ID to review:", err); this.logger.error("Failed to assign ID to review:", err);
res.writeHead(500, typeJson); res.writeHead(500, typeJson);
res.write(responder.serverError("failed to assign ID to review")); res.write(
responder.serverError("failed to assign ID to review"),
);
res.end(); res.end();
}); });
});
})
}) })
.catch((err) => { .catch((err) => {
@ -78,6 +79,6 @@ export class PostListener {
res.end(); res.end();
}); });
}); });
}) });
} }
} }

View file

@ -1,6 +1,6 @@
import { object, string, number, InferType } from "yup"; import { object, string, number, InferType } from "yup";
export const typeJson = {"Content-Type": "application/json"}; export const typeJson = { "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(),
@ -18,8 +18,8 @@ export const userReviewSchema = object({
}); });
export const serverReviewSchema = userReviewSchema.shape({ export const serverReviewSchema = userReviewSchema.shape({
id: string().length(6).required() id: string().length(6).required(),
}) });
export type UserSideReview = InferType<typeof userReviewSchema>; export type UserSideReview = InferType<typeof userReviewSchema>;

View file

@ -1,7 +1,10 @@
import fs from "fs"; import fs from "fs";
import { serverReviewSchema, ServerSideReview, UserSideReview } from "../types"; import { serverReviewSchema, ServerSideReview, UserSideReview } from "../types";
export function checkFile(file: string, encoding: fs.EncodingOption): ServerSideReview[] { export function checkFile(
file: string,
encoding: fs.EncodingOption,
): ServerSideReview[] {
const dir = "./persist/"; const dir = "./persist/";
const fullPath = dir + file; const fullPath = dir + file;
let final: ServerSideReview[]; let final: ServerSideReview[];
@ -30,6 +33,6 @@ export function appendId(userReview: UserSideReview): ServerSideReview {
username: userReview.username, username: userReview.username,
title: userReview.title, title: userReview.title,
content: userReview.content, content: userReview.content,
id: generateId() id: generateId(),
}; };
} }

View file

@ -9,7 +9,7 @@ export class Logger {
this._wrn = chalk.yellow("[WARN] "); this._wrn = chalk.yellow("[WARN] ");
this._err = chalk.red("[ERROR] "); this._err = chalk.red("[ERROR] ");
this._main = chalk.bold.gray(`[Simple Review Server | ${emp(origin ?? "Anonymous")}] `); this._main = chalk.bold.gray(`[SRS | ${emp(origin ?? "Anonymous")}] `);
} }
public log(text: any, args?: any): void { public log(text: any, args?: any): void {

View file

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