/
kashmov
/
api-library-lr2
Обзор
Документация
Войти
/
kashmov
/
api-library-lr2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/middleware/fields.js
22 строки
711 B
kashmov
upload files
25 сен 2025, 10:32
25 сен 2025, 10:32
2890114
Код
Авторство
О чём код?
export default function fields(allowed, defaultFields) { const allowedSet = new Set(allowed); const defaultSet = new Set(defaultFields); return function fieldsMiddleware(req, res, next) { const inc = (req.query.include || '').trim(); if (!inc) { req.selectedFields = [...defaultSet]; return next(); } const parts = inc.split(',').map(s => s.trim()).filter(Boolean); const chosen = []; for (const p of parts) { if (!allowedSet.has(p)) { return res.status(400).json({ error: `Unknown field in include: ${p}` }); } chosen.push(p); } req.selectedFields = chosen.length ? chosen : [...defaultSet]; next(); }; }