From 9ada6e101621bc5d8c78ccb0386246065d1a2afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=CA=9C=C9=AA=C9=B4=E1=B4=87=C9=AA=20N=E1=B4=8F=E1=B4=9C?= =?UTF-8?q?=E1=B4=A2=E1=B4=87=C9=B4?= <157171073+Shineii86@users.noreply.github.com> Date: Thu, 25 Jun 2026 00:54:18 +0530 Subject: [PATCH] v1.4.0: Add creator attribution middleware --- CHANGELOG.md | 1 + src/middleware/creatorInfo.js | 72 +++++++++++++++++++++++++++++++++++ src/routes/apiRoutes.js | 8 ++++ 3 files changed, 81 insertions(+) create mode 100644 src/middleware/creatorInfo.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 5490832..f17ceb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## v1.4.0 +- Added creator attribution middleware — injects author info into every response - Fixed Vercel deployment: replaced legacy `builds`/`routes` config with modern `functions` config - Added `api/index.js` serverless entry point (Vercel requires this for proper function detection) - Security headers: removed deprecated `X-XSS-Protection`, added `Strict-Transport-Security` (HSTS) diff --git a/src/middleware/creatorInfo.js b/src/middleware/creatorInfo.js new file mode 100644 index 0000000..03e4619 --- /dev/null +++ b/src/middleware/creatorInfo.js @@ -0,0 +1,72 @@ +/* + * ======= • ======= • ======= • ======= • =======• ======= + * MiruroAPI — creatorInfo.js + * Repository: https://github.com/Shineii86/MiruroAPI + * + * @description + * Express middleware that injects creator attribution metadata + * into every JSON API response. Overrides res.json() to append + * author details and a server-side timestamp, unless the + * response already contains creator information. + * + * @exports + * addCreatorInfo — Express middleware function + * creatorInfo — Static creator metadata object + * + * @author Shinei Nouzen + * @license MIT + * ======= • ======= • ======= • ======= • =======• ======= + */ + +// ══════════════════════════════════════════════════════════════ +// CREATOR METADATA +// ══════════════════════════════════════════════════════════════ + +// NOTE: Injected into every response unless `data.creator` already exists +const creatorInfo = { + creator: "Shinei Nouzen", + github: "https://github.com/Shineii86", + telegram: "https://telegram.me/Shineii86", + message: "Build with ❤️ by Shinei Nouzen", +}; + +// ══════════════════════════════════════════════════════════════ +// MIDDLEWARE +// ══════════════════════════════════════════════════════════════ + +/** + * Express middleware that enriches every JSON response with + * creator attribution and an IN-formatted timestamp. + * + * @param {import('express').Request} req - Express request object + * @param {import('express').Response} res - Express response object + * @param {import('express').NextFunction} next - Passes control forward + * @returns {void} + * + * @example + * app.use(addCreatorInfo); + * res.json({ anime: [...] }); + * // Response includes creator, github, telegram, message, and timestamp + */ +const addCreatorInfo = (req, res, next) => { + // NOTE: Preserve the original json method so we can wrap it + const originalJson = res.json.bind(res); + + // NOTE: Monkey-patch res.json to inject creator info + res.json = (data) => { + // NOTE: Only inject if data is a plain object and missing creator field + if (typeof data === "object" && data !== null && !data.creator) { + data = { + ...data, + ...creatorInfo, + timestamp: new Date().toLocaleString("en-IN", { timeZone: "Asia/Kolkata", hour12: true }), + }; + } + return originalJson(data); + }; + + next(); +}; + +module.exports = { addCreatorInfo, creatorInfo }; +// ══════════════════════════════════════════════════════════════ END: creatorInfo.js diff --git a/src/routes/apiRoutes.js b/src/routes/apiRoutes.js index e5b6583..17624c0 100644 --- a/src/routes/apiRoutes.js +++ b/src/routes/apiRoutes.js @@ -21,6 +21,7 @@ const express = require("express"); const anilist = require("../helpers/anilist"); const pipe = require("../helpers/pipe"); const { getCached, setCache } = require("../helpers/cache"); +const { addCreatorInfo } = require("../middleware/creatorInfo"); // ══════════════════════════════════════════════════════════════ // ROUTE REGISTRATION @@ -39,6 +40,13 @@ const { getCached, setCache } = require("../helpers/cache"); const createApiRoutes = (jsonResponse, jsonError) => { const router = express.Router(); + // ══════════════════════════════════════════════════════════════ + // CREATOR ATTRIBUTION (must be before all routes) + // ══════════════════════════════════════════════════════════════ + + // ---- FEATURE: Creator attribution middleware — injects author info into every response ---- + router.use(addCreatorInfo); + // ══════════════════════════════════════════════════════════════ // REQUEST COUNTER (must be before all routes) // ══════════════════════════════════════════════════════════════