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
This commit is contained in:
@@ -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 };
|
||||
+5
-12
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
+14
-10
@@ -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 }),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user