aboutsummaryrefslogtreecommitdiff
path: root/src/config
diff options
context:
space:
mode:
authorRitesh Ghosh <[email protected]>2024-10-06 01:13:23 +0530
committerGitHub <[email protected]>2024-10-06 01:13:23 +0530
commit46f688ac12a99b8fb145b0745dd4cc6babff1e1e (patch)
tree9560dd057822069a2162ef01a1118f3ac05e6d07 /src/config
parent55810ccf2372209f9b46c96ac12811e5a05f7961 (diff)
downloadaniwatch-api-46f688ac12a99b8fb145b0745dd4cc6babff1e1e.tar.xz
aniwatch-api-46f688ac12a99b8fb145b0745dd4cc6babff1e1e.zip
Aniwatch API Version 2 (#66)
BREAKING CHANGE: * chore: remove files that are not necessary for api v2 * test: update existing tests to use pkg * feat: organized aniwatch api envs and add more info about them * feat: update tsconfig to include strict noUnsed params * feat(api homepage): revamp api home page * feat: update wani kuni image * feat: add dot img * feat: use hono cors * feat: use hono rate limiter * build: remove unnecessary deps, add ones needed and update description * feat: add hianime routes and their handlers * feat: update vercel deployment file * docs: update logo and scraper docs, add envs section * feat: update main server file * feat: update peronal deployments caution section
Diffstat (limited to 'src/config')
-rw-r--r--src/config/axios.ts21
-rw-r--r--src/config/cors.ts17
-rw-r--r--src/config/errorHandler.ts11
-rw-r--r--src/config/notFoundHandler.ts8
-rw-r--r--src/config/ratelimit.ts24
5 files changed, 19 insertions, 62 deletions
diff --git a/src/config/axios.ts b/src/config/axios.ts
deleted file mode 100644
index 6292e9f..0000000
--- a/src/config/axios.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-import axios, { AxiosError, type AxiosRequestConfig } from "axios";
-import {
- SRC_BASE_URL,
- ACCEPT_HEADER,
- USER_AGENT_HEADER,
- ACCEPT_ENCODING_HEADER,
-} from "../utils/constants.js";
-
-const clientConfig: AxiosRequestConfig = {
- timeout: 10000,
- baseURL: SRC_BASE_URL,
- headers: {
- Accept: ACCEPT_HEADER,
- "User-Agent": USER_AGENT_HEADER,
- "Accept-Encoding": ACCEPT_ENCODING_HEADER,
- },
-};
-
-const client = axios.create(clientConfig);
-
-export { client, AxiosError };
diff --git a/src/config/cors.ts b/src/config/cors.ts
index 8089522..6d6e138 100644
--- a/src/config/cors.ts
+++ b/src/config/cors.ts
@@ -1,24 +1,17 @@
-import cors from "cors";
import { config } from "dotenv";
+import { cors } from "hono/cors";
config();
-const allowedOrigins = process.env.CORS_ALLOWED_ORIGINS
- ? process.env.CORS_ALLOWED_ORIGINS.split(",")
+const allowedOrigins = process.env.ANIWATCH_API_CORS_ALLOWED_ORIGINS
+ ? process.env.ANIWATCH_API_CORS_ALLOWED_ORIGINS.split(",")
: ["http://localhost:4000", "*"];
const corsConfig = cors({
- origin: function (origin, callback) {
- if (!origin || allowedOrigins.includes(origin)) {
- callback(null, true);
- } else {
- callback(new Error("Not allowed by CORS"));
- }
- },
- methods: ["GET"],
+ allowMethods: ["GET"],
maxAge: 600,
credentials: true,
- optionsSuccessStatus: 200,
+ origin: allowedOrigins,
});
export default corsConfig;
diff --git a/src/config/errorHandler.ts b/src/config/errorHandler.ts
deleted file mode 100644
index 560f141..0000000
--- a/src/config/errorHandler.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-import type { ErrorRequestHandler } from "express";
-
-const errorHandler: ErrorRequestHandler = (error, req, res, next) => {
- const status = error?.status || 500;
- res.status(status).json({
- status,
- message: error?.message || "Something Went Wrong",
- });
-};
-
-export default errorHandler;
diff --git a/src/config/notFoundHandler.ts b/src/config/notFoundHandler.ts
deleted file mode 100644
index a372e8e..0000000
--- a/src/config/notFoundHandler.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-import type { RequestHandler } from "express";
-import createHttpError from "http-errors";
-
-const notFoundHandler: RequestHandler = (req, res, next) => {
- return next(createHttpError.NotFound());
-};
-
-export default notFoundHandler;
diff --git a/src/config/ratelimit.ts b/src/config/ratelimit.ts
index c5add3f..afd6e54 100644
--- a/src/config/ratelimit.ts
+++ b/src/config/ratelimit.ts
@@ -1,17 +1,21 @@
import { config } from "dotenv";
-import createHttpError from "http-errors";
-import { rateLimit } from "express-rate-limit";
+import { rateLimiter } from "hono-rate-limiter";
+import { getConnInfo } from "@hono/node-server/conninfo";
config();
-export const ratelimit = rateLimit({
- windowMs: Number(process.env.WINDOWMS) || 30 * 60 * 1000,
- limit: Number(process.env.MAX) || 6,
- legacyHeaders: true,
+export const ratelimit = rateLimiter({
+ windowMs: Number(process.env.ANIWATCH_API_WINDOW_MS) || 30 * 60 * 1000,
+ limit: Number(process.env.ANIWATCH_API_MAX_REQS) || 6,
standardHeaders: "draft-7",
- handler: function (_, __, next) {
- next(
- createHttpError.TooManyRequests("Too many API requests, try again later")
- );
+ keyGenerator(c) {
+ const { remote } = getConnInfo(c);
+ const key =
+ `${String(remote.addressType)}_` +
+ `${String(remote.address)}:${String(remote.port)}`;
+
+ return key;
},
+ handler: (c) =>
+ c.json({ status: 429, message: "Too Many Requests 😵" }, { status: 429 }),
});