Skip to content

Commit

Permalink
chore: follow all Biome established rules
Browse files Browse the repository at this point in the history
  • Loading branch information
craigzour committed Aug 11, 2024
1 parent 5bbdeb6 commit d15e32f
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 84 deletions.
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import dotenv from "dotenv";

dotenv.config();

export const SERVER_PORT = process.env.SERVER_PORT ? Number(process.env.SERVER_PORT) : 3001;
export const SERVER_PORT = process.env.SERVER_PORT
? Number(process.env.SERVER_PORT)
: 3001;
4 changes: 1 addition & 3 deletions src/lib/vault/getSubmission.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
async function main(formId: string, submissionId: string) {
export function getSubmission(formId: string, submissionId: string) {
return {
formId,
submissionId,
};
}

export default main;
10 changes: 6 additions & 4 deletions src/middleware/authentication/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { NextFunction, Request, Response } from "express";
import type { NextFunction, Request, Response } from "express";

async function main(request: Request, response: Response, next: NextFunction) {
export function authenticationMiddleware(
_request: Request,
_response: Response,
next: NextFunction,
) {
next();
}

export default main;
11 changes: 7 additions & 4 deletions src/middleware/globalErrorHandler/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Request, Response, NextFunction } from "express";
import type { Request, Response, NextFunction } from "express";

function main(error: Error, _request: Request, response: Response, _next: NextFunction) {
export function globalErrorHandlerMiddleware(
error: Error,
_request: Request,
response: Response,
_next: NextFunction,
) {
console.error(JSON.stringify(error, Object.getOwnPropertyNames(error)));
response.sendStatus(500);
}

export default main;
10 changes: 6 additions & 4 deletions src/middleware/rateLimiter/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { NextFunction, Request, Response } from "express";
import type { NextFunction, Request, Response } from "express";

async function main(request: Request, response: Response, next: NextFunction) {
export function rateLimiterMiddleware(
_request: Request,
_response: Response,
next: NextFunction,
) {
next();
}

export default main;
6 changes: 2 additions & 4 deletions src/middleware/routeNotFound/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Request, Response } from "express";
import type { Request, Response } from "express";

function main(_request: Request, response: Response) {
export function routeNotFoundMiddleware(_request: Request, response: Response) {
response.sendStatus(404);
}

export default main;
12 changes: 5 additions & 7 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Router } from "express";
import formsRouter from "./routes/forms/router";
import statusRouter from "./routes/status/router";
import routeNotFoundMiddleware from "./middleware/routeNotFound/middleware";
import globalErrorHandlerMiddleware from "./middleware/globalErrorHandler/middleware";
import { formsRouter } from "./routes/forms/router";
import { statusRouter } from "./routes/status/router";
import { routeNotFoundMiddleware } from "./middleware/routeNotFound/middleware";
import { globalErrorHandlerMiddleware } from "./middleware/globalErrorHandler/middleware";

const router = Router();
export const router = Router();

router
.use("/forms", formsRouter)
Expand All @@ -13,5 +13,3 @@ router
.use(routeNotFoundMiddleware)
// 500: Catches all unhandled errors from non async functions and errors passed through next() in async functions
.use(globalErrorHandlerMiddleware);

export default router;
12 changes: 5 additions & 7 deletions src/routes/forms/router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Router } from "express";
import submissionRouter from "./submission/router";
import authenticationMiddleware from "../../middleware/authentication/middleware";
import rateLimiterMiddleware from "../../middleware/rateLimiter/middleware";
import { submissionRouter } from "./submission/router";
import { authenticationMiddleware } from "../../middleware/authentication/middleware";
import { rateLimiterMiddleware } from "../../middleware/rateLimiter/middleware";

const router = Router();
export const formsRouter = Router();

router
formsRouter
.use(authenticationMiddleware)
.use(rateLimiterMiddleware)
.use("/:formId/submission", submissionRouter);

export default router;
8 changes: 3 additions & 5 deletions src/routes/forms/submission/downloaded/router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Request, Response, Router } from "express";
import { type Request, type Response, Router } from "express";

const router = Router({
export const downloadedRouter = Router({
mergeParams: true,
});

router.get("/", async (request: Request, response: Response) => {
downloadedRouter.get("/", (request: Request, response: Response) => {
response.json({
formId: request.params.formId,
});
});

export default router;
8 changes: 3 additions & 5 deletions src/routes/forms/submission/new/router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Request, Response, Router } from "express";
import { type Request, type Response, Router } from "express";

const router = Router({
export const newRouter = Router({
mergeParams: true,
});

router.get("/", async (request: Request, response: Response) => {
newRouter.get("/", (request: Request, response: Response) => {
response.json({
formId: request.params.formId,
});
});

export default router;
12 changes: 5 additions & 7 deletions src/routes/forms/submission/router.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Router } from "express";
import newRouter from "./new/router";
import downloadedRouter from "./downloaded/router";
import submissionIdRouter from "./submissionId/router";
import { newRouter } from "./new/router";
import { downloadedRouter } from "./downloaded/router";
import { submissionIdRouter } from "./submissionId/router";

const router = Router({
export const submissionRouter = Router({
mergeParams: true,
});

router
submissionRouter
.use("/new", newRouter)
.use("/downloaded", downloadedRouter)
.use("/:submissionId", submissionIdRouter);

export default router;
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Request, Response, Router } from "express";
import { type Request, type Response, Router } from "express";

const router = Router({
export const confirmationCodeRouter = Router({
mergeParams: true,
});

router.put("/", async (request: Request, response: Response) => {
confirmationCodeRouter.put("/", (request: Request, response: Response) => {
response.json({
formId: request.params.formId,
submissionId: request.params.submissionId,
confirmationCode: request.params.confirmationCode,
});
});

export default router;
8 changes: 3 additions & 5 deletions src/routes/forms/submission/submissionId/confirm/router.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Router } from "express";
import confirmationCodeRouter from "./confirmationCode/router";
import { confirmationCodeRouter } from "./confirmationCode/router";

const router = Router({
export const confirmRouter = Router({
mergeParams: true,
});

router.use("/:confirmationCode", confirmationCodeRouter);

export default router;
confirmRouter.use("/:confirmationCode", confirmationCodeRouter);
16 changes: 7 additions & 9 deletions src/routes/forms/submission/submissionId/router.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Request, Response, Router } from "express";
import confirmRouter from "./confirm/router";
import getSubmission from "../../../../lib/vault/getSubmission";
import { type Request, type Response, Router } from "express";
import { confirmRouter } from "./confirm/router";
import { getSubmission } from "../../../../lib/vault/getSubmission";

const router = Router({
export const submissionIdRouter = Router({
mergeParams: true,
});

router.get("/", async (request: Request, response: Response) => {
submissionIdRouter.get("/", (request: Request, response: Response) => {
const formId = request.params.formId;
const submissionId = request.params.submissionId;
const submission = await getSubmission(formId, submissionId);
const submission = getSubmission(formId, submissionId);
response.json(submission);
});

router.use("/confirm", confirmRouter);

export default router;
submissionIdRouter.use("/confirm", confirmRouter);
8 changes: 3 additions & 5 deletions src/routes/status/router.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Response, Router } from "express";
import { type Response, Router } from "express";

const router = Router();
export const statusRouter = Router();

router.get("/", (_, response: Response) => {
statusRouter.get("/", (_, response: Response) => {
response.json({
status: "running",
});
});

export default router;
6 changes: 3 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import express, { Express } from "express";
import express, { type Express } from "express";
import { SERVER_PORT } from "./config";
import router from "./router";
import { router } from "./router";

const server: Express = express();

server.use("/", router);

server.listen(SERVER_PORT, () => {
console.log(`>>> API server listening on port ${SERVER_PORT} <<<`);
console.info(`>>> API server listening on port ${SERVER_PORT} <<<`);
});
8 changes: 4 additions & 4 deletions test/routes/forms/submission/router.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { vi, describe, it, expect, beforeAll } from "vitest";
import request from "supertest";
import express from "express";
import router from "../../../../src/routes/forms/router";
import { formsRouter } from "../../../../src/routes/forms/router";

vi.mock("../../../../src/middleware/authentication/middleware", () => ({
default: (_req, _res, next) => next(),
authenticationMiddleware: (_req, _res, next) => next(),
}));

vi.mock("../../../../src/middleware/rateLimiter/middleware", () => ({
default: (_req, _res, next) => next(),
rateLimiterMiddleware: (_req, _res, next) => next(),
}));

describe("routes/forms/submission", () => {
let app: express.Express;

beforeAll(() => {
app = express();
app.use("/", router);
app.use("/", formsRouter);
});

it("GET /new", async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/routes/status/router.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { describe, it, expect, beforeAll } from "vitest";
import request from "supertest";
import express from "express";
import router from "../../../src/routes/status/router";
import { statusRouter } from "../../../src/routes/status/router";

describe("routes/status", () => {
let app: express.Express;

beforeAll(() => {
app = express();
app.use("/", router);
app.use("/", statusRouter);
});

it("GET /status", async () => {
Expand Down

0 comments on commit d15e32f

Please sign in to comment.