server/src/middleware/apiKey-handler.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 38
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 18
mnd 4
bc 4
fnc 1
dl 0
loc 38
ccs 10
cts 10
cp 1
rs 10
bpm 4
cpm 5
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A apiKey-handler.js ➔ apiKeyHandler 0 27 5
1
// eslint-disable-next-line no-unused-vars
2
import express from "express";
3
import apiKeyModel from "../models/api-key.js";
4
5
/**
6
 *
7
 * @param {express.Request} req
8
 * @param {express.Response} res
9
 * @param {express.NextFunction} next
10
 */
11
async function apiKeyHandler(req, res, next) {
12 100
    if (req.path === "/admin/feed") {
13 2
        return next();
14
    }
15
16 98
    const apiKey = req.headers['x-api-key'];
17
18 98
    const apiKeyString = Array.isArray(apiKey) ? apiKey[0] : apiKey;
19
20 98
    if (!apiKeyString) {
21 1
        return res.status(401).json({
22
            success: false,
23
            message: 'API key is required.'
24
        });
25
    }
26
27 97
    const isValidKey = await apiKeyModel.checkOne(apiKeyString);
28
29 97
    if (!isValidKey) {
30 1
        return res.status(401).json({
31
            success: false,
32
            message: 'Invalid or missing API key. Access denied.'
33
        });
34
    }
35
36 96
    return next();
37
}
38
39
export default apiKeyHandler;
40