Use static methods for responder and export the whole class
This commit is contained in:
parent
2ec6dd5476
commit
33964ca6d1
|
@ -1,5 +1,5 @@
|
|||
import { userReviewSchema, typeJson } from "../types";
|
||||
import { Logger, responder, data } from "../utils";
|
||||
import { Logger, Responder, data } from "../utils";
|
||||
import { Express, Request, Response } from "express";
|
||||
|
||||
export class PostListener {
|
||||
|
@ -24,7 +24,7 @@ export class PostListener {
|
|||
|
||||
res.writeHead(400, typeJson);
|
||||
res.write(
|
||||
responder.JsonError("recieved chunk contains invalid JSON"),
|
||||
Responder.JsonError("recieved chunk contains invalid JSON"),
|
||||
);
|
||||
res.end();
|
||||
return;
|
||||
|
@ -41,14 +41,14 @@ export class PostListener {
|
|||
data.appendReview(validUserReview);
|
||||
|
||||
res.writeHead(201, typeJson);
|
||||
res.write(responder.success("review was sent"));
|
||||
res.write(Responder.success("review was sent"));
|
||||
res.end();
|
||||
} catch (err) {
|
||||
this.logger.error("Failed to assign ID to review:", err);
|
||||
|
||||
res.writeHead(500, typeJson);
|
||||
res.write(
|
||||
responder.serverError("failed to assign ID to review"),
|
||||
Responder.serverError("failed to assign ID to review"),
|
||||
);
|
||||
res.end();
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ export class PostListener {
|
|||
this.logger.error(`${sender} <~ ${err}`);
|
||||
|
||||
res.writeHead(400, typeJson);
|
||||
res.write(responder.error(err));
|
||||
res.write(Responder.error(err));
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
import { ValidationError } from "yup";
|
||||
|
||||
class Responder {
|
||||
public success(message: string): string {
|
||||
export class Responder {
|
||||
public static success(message: string): string {
|
||||
return JSON.stringify({
|
||||
message: message,
|
||||
});
|
||||
}
|
||||
|
||||
public error(error: ValidationError): string {
|
||||
public static error(error: ValidationError): string {
|
||||
return JSON.stringify({
|
||||
error: {
|
||||
type: error.name,
|
||||
|
@ -16,7 +16,7 @@ class Responder {
|
|||
});
|
||||
}
|
||||
|
||||
public serverError(errorMessage: string): string {
|
||||
public static serverError(errorMessage: string): string {
|
||||
return JSON.stringify({
|
||||
error: {
|
||||
type: "InternalError",
|
||||
|
@ -25,7 +25,7 @@ class Responder {
|
|||
});
|
||||
}
|
||||
|
||||
public requestError(errorMessage: string): string {
|
||||
public static requestError(errorMessage: string): string {
|
||||
return JSON.stringify({
|
||||
error: {
|
||||
type: "RequestError",
|
||||
|
@ -34,7 +34,7 @@ class Responder {
|
|||
});
|
||||
}
|
||||
|
||||
public JsonError(errorMessage: string): string {
|
||||
public static JsonError(errorMessage: string): string {
|
||||
return JSON.stringify({
|
||||
error: {
|
||||
type: "JsonError",
|
||||
|
@ -43,5 +43,3 @@ class Responder {
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const responder = new Responder();
|
||||
|
|
Loading…
Reference in a new issue