Prettier
This commit is contained in:
parent
65382936cf
commit
550d862e09
|
@ -13,17 +13,25 @@ export class MessagesResponder extends ApiRoute {
|
|||
this.server.get(this.routeName, (req: ReviewRequest, 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) {
|
||||
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.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) {
|
||||
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.write(
|
||||
Responder.requestError(
|
||||
"skip parameter must be a number or undefined",
|
||||
),
|
||||
);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
@ -38,35 +46,40 @@ export class MessagesResponder extends ApiRoute {
|
|||
this.logger.log(`${Logger.emp(receiver)} <~ "${req.path}"`);
|
||||
});
|
||||
|
||||
this.server.get(`${this.routeName}/:id`, (req: IdRequest, res: Response) => {
|
||||
// "req.params.id" is the review ID that was receieved
|
||||
// "result" is the review that is identified by that ID, if any
|
||||
this.server.get(
|
||||
`${this.routeName}/:id`,
|
||||
(req: IdRequest, res: Response) => {
|
||||
// "req.params.id" is the review ID that was receieved
|
||||
// "result" is the review that is identified by that ID, if any
|
||||
|
||||
const receiver = req.headers["user-agent"];
|
||||
const result = data.getReviewById(req.params.id);
|
||||
let err = false;
|
||||
const receiver = req.headers["user-agent"];
|
||||
const result = data.getReviewById(req.params.id);
|
||||
let err = false;
|
||||
|
||||
if (req.params.id.length === 6) {
|
||||
if (Object.keys(result).length) {
|
||||
res.writeHead(200, typeJson);
|
||||
res.write(JSON.stringify(result));
|
||||
res.end();
|
||||
if (req.params.id.length === 6) {
|
||||
if (Object.keys(result).length) {
|
||||
res.writeHead(200, typeJson);
|
||||
res.write(JSON.stringify(result));
|
||||
res.end();
|
||||
} else {
|
||||
res.writeHead(404, typeJson);
|
||||
res.write(Responder.notFoundError("review not found"));
|
||||
res.end();
|
||||
err = true;
|
||||
}
|
||||
} else {
|
||||
res.writeHead(404, typeJson);
|
||||
res.write(Responder.notFoundError("review not found"));
|
||||
res.writeHead(400, typeJson);
|
||||
res.write(Responder.requestError("review id must be 6 characters"));
|
||||
res.end();
|
||||
err = true;
|
||||
}
|
||||
} else {
|
||||
res.writeHead(400, typeJson);
|
||||
res.write(Responder.requestError("review id must be 6 characters"));
|
||||
res.end();
|
||||
err = true;
|
||||
}
|
||||
|
||||
// If an error was returned to the client, mark their user agent red in the logs
|
||||
this.logger.log(`${err ? Logger.err(receiver) : Logger.emp(receiver)} <~ "${req.path}"`);
|
||||
});
|
||||
// If an error was returned to the client, mark their user agent red in the logs
|
||||
this.logger.log(
|
||||
`${err ? Logger.err(receiver) : Logger.emp(receiver)} <~ "${req.path}"`,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
this.complete();
|
||||
}
|
||||
|
|
|
@ -11,7 +11,10 @@ const rating = number()
|
|||
)
|
||||
.required();
|
||||
|
||||
export const typeJson = { "Access-Control-Allow-Origin": "*", "Content-Type": "application/json" };
|
||||
export const typeJson = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
export const userReviewSchema = object({
|
||||
username: string().min(2).max(30).required(),
|
||||
|
@ -40,4 +43,4 @@ export interface ReviewRequest extends Request {
|
|||
max: string;
|
||||
skip: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ export default class ApiRoute {
|
|||
public readonly server: Express;
|
||||
public readonly routeName: string;
|
||||
public readonly logger = new Logger("API");
|
||||
|
||||
|
||||
constructor(server: Express, route: string) {
|
||||
// API Routes are a list of routes, the beginning route should be first
|
||||
const mainRoute = `/api${route}`;
|
||||
|
@ -33,6 +33,6 @@ export default class ApiRoute {
|
|||
res.writeHead(400, typeJson);
|
||||
res.write(Responder.success("ok"));
|
||||
res.end();
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,8 +47,8 @@ export class Responder {
|
|||
return JSON.stringify({
|
||||
error: {
|
||||
type: "notFoundError",
|
||||
message: errorMessage
|
||||
}
|
||||
message: errorMessage,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue