xonecas.io
4436
Sl

sesame-labs-coding-assignment / commit 6cdc4e1

Use a http status code library

- Provides more readability

Sean2023-01-17 6cdc4e10b78282c485fb7b15abc242a1d32fce61 parent 826e0f3

6 files changed

api/package-lock.json+16−0
@@ -11,9 +11,11 @@
1111 "dependencies": {
1212 "@koa/cors": "^4.0.0",
1313 "@prisma/client": "^4.8.1",
14+ "@types/http-status-codes": "^1.2.0",
1415 "@types/koa-session": "^5.10.6",
1516 "axios": "^1.2.2",
1617 "dotenv": "^16.0.3",
18+ "http-status-codes": "^2.2.0",
1719 "koa": "2.14.1",
1820 "koa-bodyparser": "4.3.0",
1921 "koa-json": "^2.0.2",
@@ -211,6 +213,15 @@
211213 "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.1.tgz",
212214 "integrity": "sha512-/K3ds8TRAfBvi5vfjuz8y6+GiAYBZ0x4tXv1Av6CWBWn0IlADc+ZX9pMq7oU0fNQPnBwIZl3rmeLp6SBApbxSQ=="
213215 },
216+ "node_modules/@types/http-status-codes": {
217+ "version": "1.2.0",
218+ "resolved": "https://registry.npmjs.org/@types/http-status-codes/-/http-status-codes-1.2.0.tgz",
219+ "integrity": "sha512-vjpjevMaxtrtdrrV/TQNIFT7mKL8nvIKG7G/LjMDZdVvqRxRg5SNfGkeuSaowVc0rbK8xDA2d/Etunyb5GyzzA==",
220+ "deprecated": "This is a stub types definition for http-status-codes (https://github.com/prettymuchbryce/node-http-status). http-status-codes provides its own type definitions, so you don\\'t need @types/http-status-codes installed!",
221+ "dependencies": {
222+ "http-status-codes": "*"
223+ }
224+ },
214225 "node_modules/@types/keygrip": {
215226 "version": "1.0.2",
216227 "resolved": "https://registry.npmjs.org/@types/keygrip/-/keygrip-1.0.2.tgz",
@@ -919,6 +930,11 @@
919930 "node": ">= 0.6"
920931 }
921932 },
933+ "node_modules/http-status-codes": {
934+ "version": "2.2.0",
935+ "resolved": "https://registry.npmjs.org/http-status-codes/-/http-status-codes-2.2.0.tgz",
936+ "integrity": "sha512-feERVo9iWxvnejp3SEfm/+oNG517npqL2/PIA8ORjyOZjGC7TwCRQsZylciLS64i6pJ0wRYz3rkXLRwbtFa8Ng=="
937+ },
922938 "node_modules/humanize-number": {
923939 "version": "0.0.2",
924940 "resolved": "https://registry.npmjs.org/humanize-number/-/humanize-number-0.0.2.tgz",
api/package.json+1−0
@@ -28,6 +28,7 @@
2828 "@types/koa-session": "^5.10.6",
2929 "axios": "^1.2.2",
3030 "dotenv": "^16.0.3",
31+ "http-status-codes": "^2.2.0",
3132 "koa": "2.14.1",
3233 "koa-bodyparser": "4.3.0",
3334 "koa-json": "^2.0.2",
api/src/authentication/index.ts+5−4
@@ -1,5 +1,6 @@
11 import Router from "koa-router";
22 import Koa from "koa";
3+import { StatusCodes } from "http-status-codes";
34
45 const router = new Router();
56
@@ -20,9 +21,9 @@ router.post(
2021 user = await ctx.prisma.user.create({
2122 data: { walletAddress: data.walletAddress },
2223 });
23- ctx.status = 201;
24+ ctx.status = StatusCodes.CREATED;
2425 } else {
25- ctx.status = 200;
26+ ctx.status = StatusCodes.OK;
2627 }
2728
2829 if (ctx.session) {
@@ -38,7 +39,7 @@ router.post(
3839 "/deauthenticate",
3940 async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
4041 ctx.session = null;
41- ctx.status = 200;
42+ ctx.status = StatusCodes.OK;
4243 await next();
4344 }
4445 );
@@ -52,7 +53,7 @@ router.get(
5253 where: { walletAddress: ctx.session.user.walletAddress },
5354 });
5455
55- ctx.status = 200;
56+ ctx.status = StatusCodes.OK;
5657 }
5758
5859 await next();
api/src/discord/index.ts+5−4
@@ -2,6 +2,7 @@ import Router from "koa-router";
22 import Koa from "koa";
33 import axios from "axios";
44 import { stringify } from "querystring";
5+import { StatusCodes } from "http-status-codes";
56
67 const router = new Router({ prefix: "/discord" });
78
@@ -46,7 +47,7 @@ router.get(
4647 data: { discordTokenId: discordToken.id },
4748 });
4849
49- ctx.status = 201;
50+ ctx.status = StatusCodes.CREATED;
5051 }
5152 ctx.redirect("/discord/join");
5253 await next();
@@ -55,7 +56,7 @@ router.get(
5556
5657 router.get("/join", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
5758 if (!ctx.session) {
58- ctx.status = 403;
59+ ctx.status = StatusCodes.FORBIDDEN;
5960 return await next();
6061 }
6162
@@ -102,7 +103,7 @@ router.get("/join", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
102103
103104 router.get("/verify", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
104105 if (!ctx.session) {
105- ctx.status = 403;
106+ ctx.status = StatusCodes.FORBIDDEN;
106107 return await next();
107108 }
108109
@@ -134,7 +135,7 @@ router.get("/verify", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
134135 include: { discordToken: true },
135136 data: { verified: true, coupon },
136137 });
137- ctx.status = 200;
138+ ctx.status = StatusCodes.OK;
138139
139140 await next();
140141 });
api/src/quest/index.ts+2−1
@@ -1,5 +1,6 @@
11 import Router from "koa-router";
22 import Koa from "koa";
3+import { StatusCodes } from "http-status-codes";
34
45 const router = new Router();
56
@@ -11,7 +12,7 @@ router.get("/quest", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
1112 coverImage: process.env.QUEST_COVER_IMAGE,
1213 };
1314
14- ctx.status = 200;
15+ ctx.status = StatusCodes.OK;
1516 await next();
1617 });
1718
api/src/routes.ts+2−1
@@ -3,6 +3,7 @@ import Router from "koa-router";
33 import authenticationRouter from "./authentication";
44 import discordRouter from "./discord";
55 import questRouter from "./quest";
6+import { StatusCodes } from "http-status-codes";
67
78 const router = new Router();
89
@@ -11,7 +12,7 @@ router.all(
1112 async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
1213 // Check if server is healthy and has connection to db:
1314 ctx.body = `Server is healthy. \n${await ctx.prisma.user.count()} records found in the db.`;
14- ctx.status = 200;
15+ ctx.status = StatusCodes.OK;
1516
1617 await next();
1718 }