Skip to content

Commit

Permalink
Merge pull request #13 from PRATHAM1ST/master
Browse files Browse the repository at this point in the history
Added Middleware and Handler
  • Loading branch information
PRATHAM1ST authored Sep 23, 2023
2 parents 6279bf6 + 9755126 commit 51dc349
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 26 deletions.
8 changes: 3 additions & 5 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require('dotenv').config()
import express, { Express, Request, Response } from "express";
import membersRoute from "./routes/members";
import cors from "cors";
import morgan from "morgan";
import ResponseHandler from "./components/response";

const corsOptions = {
origin: "*",
Expand All @@ -15,11 +17,7 @@ app.use(cors());
app.use(morgan("dev"));

app.get("/", (req: Request, res: Response) => {
return res.status(200).send({
status: true,
data: "Hello World",
message: "Success",
});
return ResponseHandler.success(req, res, "Hello World", "Success");
});

app.use("/members", membersRoute);
Expand Down
20 changes: 20 additions & 0 deletions components/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Request, Response } from "express";

class ResponseHandler {
static success(req: Request, res: Response, data: any, message: string) {
return res.status(200).json({
status: true,
data,
message,
});
}

static error(req: Request, res: Response, message: string) {
return res.status(500).json({
status: false,
message,
});
}
}

export default ResponseHandler;
37 changes: 17 additions & 20 deletions controllers/members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios, { AxiosResponse } from "axios";
import { parseHTML } from "linkedom";
import membersList from "../data/members-list-old.json";
import membersListLatest from "../data/members-list.json";
import ResponseHandler from "../components/response";

interface Member {
login: string;
Expand Down Expand Up @@ -91,31 +92,27 @@ export async function PATCH(req: Request, res: Response) {
membersListArray.map(async (member) => updateSingleMember(member))
);

return res.status(200).json({
status: true,
data: membersListArray,
message: "Members list updated",
});
} catch (err) {
return res.status(500).json({
status: false,
message: err,
});
return ResponseHandler.success(
req,
res,
membersListArray,
"Members list updated"
);
} catch (err: any) {
return ResponseHandler.error(req, res, err);
}
}

export async function GET(req: Request, res: Response) {
try {
return res.status(200).json({
status: true,
data: membersListLatest,
message: "Members list successfully retrieved",
});
} catch (err) {
return res.status(500).json({
status: false,
message: err,
});
return ResponseHandler.success(
req,
res,
membersListLatest,
"Members list"
);
} catch (err: any) {
return ResponseHandler.error(req, res, err);
}
}

Expand Down
25 changes: 25 additions & 0 deletions middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NextFunction, Request, Response } from "express";

function auth(req: Request, res: Response, next: NextFunction) {
try {
const token = req.header("Authorization");
if (!token) throw Error("No token, authorization denied");

const [user, password] = token?.split(" ");
if (!user || !password) throw Error("No token, authorization denied");
if (
user !== process.env.USERNAME?.toLowerCase() ||
password !== process.env.PASSWORD?.toLowerCase()
)
throw Error("Invalid token, authorization denied");

next();
} catch (err) {
return res.status(500).json({
status: false,
message: err,
});
}
}

export default auth;
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"axios": "^1.5.0",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"linkedom": "^0.15.3",
"morgan": "^1.10.0",
Expand Down
3 changes: 2 additions & 1 deletion routes/members.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import express from "express";
import controllers from "../controllers/members";
import auth from "../middleware/auth";

const router = express.Router();

router.get("/", controllers.GET);
router.patch("/", controllers.PATCH);
router.patch("/", auth, controllers.PATCH);

export default router;

0 comments on commit 51dc349

Please sign in to comment.