mirror of
https://github.com/Shineii86/MiruroAPI.git
synced 2026-10-22 10:15:10 +00:00
v1.4.0: Add creator attribution middleware
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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)
|
||||||
// ══════════════════════════════════════════════════════════════
|
// ══════════════════════════════════════════════════════════════
|
||||||
|
|||||||
Reference in New Issue
Block a user