v1.4.0: Add creator attribution middleware

This commit is contained in:
Sʜɪɴᴇɪ Nᴏᴜᴢᴇɴ
2026-06-25 00:54:18 +05:30
parent 3a28c4d182
commit 9ada6e1016
3 changed files with 81 additions and 0 deletions
+1
View File
@@ -1,6 +1,7 @@
# Changelog # Changelog
## v1.4.0 ## 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 - 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) - 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) - Security headers: removed deprecated `X-XSS-Protection`, added `Strict-Transport-Security` (HSTS)
+72
View File
@@ -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
+8
View File
@@ -21,6 +21,7 @@ const express = require("express");
const anilist = require("../helpers/anilist"); const anilist = require("../helpers/anilist");
const pipe = require("../helpers/pipe"); const pipe = require("../helpers/pipe");
const { getCached, setCache } = require("../helpers/cache"); const { getCached, setCache } = require("../helpers/cache");
const { addCreatorInfo } = require("../middleware/creatorInfo");
// ══════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════
// ROUTE REGISTRATION // ROUTE REGISTRATION
@@ -39,6 +40,13 @@ const { getCached, setCache } = require("../helpers/cache");
const createApiRoutes = (jsonResponse, jsonError) => { const createApiRoutes = (jsonResponse, jsonError) => {
const router = express.Router(); 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) // REQUEST COUNTER (must be before all routes)
// ══════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════