Better syntax

This commit is contained in:
Akif9748 2022-04-03 21:22:06 +03:00
parent 1e80da99a8
commit 6beeddd17e

View file

@ -1,12 +1,8 @@
const { User, Message, Thread } = require("../../classes"); const { User, Message, Thread } = require("../../classes");
const db = require("quick.db"); const db = require("quick.db");
const { Router } = require("express") const { Router } = require("express")
const app = Router(); const app = Router();
class ApiResponse { class ApiResponse {
constructor(status, result) { constructor(status, result) {
this.status = status; this.status = status;
@ -16,30 +12,38 @@ class ApiResponse {
const { request, response } = require("express"); const { request, response } = require("express");
/**
* AUTH TYPE:
headers:
{
username: "Username for client",
password: "Password of selected username for client"
}
*/
/**
* REQUEST TYPE:
* GET /api/action/id
*
* @example message action:
* GET /api/message/0
*
*/
/** /**
* For intellisense * For intellisense
* @param {request} req * @param {request} req
* @param {response} res * @param {response} res
*/ */
app.use((req, res, next) => {
app.get("/:action/:id", (req, res) => {
const error = (status, error) => const error = (status, error) =>
res.status(status).json(new ApiResponse(status, { error })) res.status(status).json(new ApiResponse(status, { error }))
/**
* AUTH TYPE:
headers:
{
username: "Username for client",
password: "Password of selected username for client"
}
*/
const { username = null, password = null } = req.headers; const { username = null, password = null } = req.headers;
if (!username || !password) if (!username || !password)
@ -52,40 +56,43 @@ app.get("/:action/:id", (req, res) => {
if (user.key !== password) if (user.key !== password)
return error(401, 'Incorrect Password!') return error(401, 'Incorrect Password!')
next();
})
app.get("/message/:id", (req, res) => {
const error = (status, error) =>
res.status(status).json(new ApiResponse(status, { error }));
/** const { id = null } = req.params;
* REQUEST TYPE: if (!id) return error(400, "Missing id in query")
* GET /api/action/id const message = new Message().getId(id);
*
* @example message action:
* GET /api/message/0
*
*/
const { action } = req.params;
if (action === "message") { if (!message || message.deleted) return error(404, "We have not got any message declared as this id.");
const { id = null } = req.params; res.status(200).json(new ApiResponse(200, message));
if (!id) return error(400, "Missing id in query")
const message = new Message().getId(id);
if (!message || message.deleted) return error(404, "We have not got any message declared as this id.");
res.status(200).json(new ApiResponse(200, message));
} else if (action === "user") {
const { id = null } = req.params;
if (!id) return error(400, "Missing id in query")
const member = new User().getId(id);
if (!member || member.deleted) return error(404, "We have not got any user declared as this id.");
res.status(200).json(new ApiResponse(200, member)); })
}
else app.get("/user/:id", (req, res) => {
return error(400, "Missing/undefined param in action section: " + action);
const error = (status, error) =>
res.status(status).json(new ApiResponse(status, { error }))
const { id = null } = req.params;
if (!id) return error(400, "Missing id in query")
const member = new User().getId(id);
if (!member || member.deleted) return error(404, "We have not got any user declared as this id.");
res.status(200).json(new ApiResponse(200, member));
}); });
app.all("*", (req, res) => res.status(400).json(new ApiResponse(400, { error: "Bad request" })));
module.exports = app; module.exports = app;