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

@ -5,14 +5,7 @@ import {
userReviewSchema,
typeJson,
} from "../types";
import {
appendId,
bold,
checkFile,
emp,
Logger,
responder
} from "../utils";
import { appendId, bold, checkFile, emp, Logger, responder } from "../utils";
import { Express, Request, Response } from "express";
export class PostListener {
@ -37,22 +30,29 @@ export class PostListener {
this.logger.error(`${sender} <~ Recieved chunk was not valid JSON!`);
res.writeHead(400, typeJson);
res.write(responder.JsonError("recieved chunk contains invalid JSON"));
res.write(
responder.JsonError("recieved chunk contains invalid JSON"),
);
res.end();
return;
}
await userReviewSchema.validate(temp)
await userReviewSchema
.validate(temp)
.then((validUserReview) => {
req.on("end", () => {
this.logger.log(`${sender} ~>`, validUserReview);
serverReviewSchema.validate(appendId(validUserReview))
serverReviewSchema
.validate(appendId(validUserReview))
.then((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.write(responder.success("review was sent"));
@ -63,11 +63,12 @@ export class PostListener {
this.logger.error("Failed to assign ID to review:", err);
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();
});
})
});
})
.catch((err) => {
@ -78,6 +79,6 @@ export class PostListener {
res.end();
});
});
})
});
}
}

View file

@ -1,6 +1,6 @@
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({
username: string().min(2).max(30).required(),
@ -18,8 +18,8 @@ export const userReviewSchema = object({
});
export const serverReviewSchema = userReviewSchema.shape({
id: string().length(6).required()
})
id: string().length(6).required(),
});
export type UserSideReview = InferType<typeof userReviewSchema>;

View file

@ -1,7 +1,10 @@
import fs from "fs";
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 fullPath = dir + file;
let final: ServerSideReview[];
@ -30,6 +33,6 @@ export function appendId(userReview: UserSideReview): ServerSideReview {
username: userReview.username,
title: userReview.title,
content: userReview.content,
id: generateId()
id: generateId(),
};
}

View file

@ -9,7 +9,7 @@ export class Logger {
this._wrn = chalk.yellow("[WARN] ");
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 {

View file

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