forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserAgent.ts
More file actions
36 lines (32 loc) · 1.02 KB
/
userAgent.ts
File metadata and controls
36 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { Request, Response, NextFunction } from "express";
import { lines } from "../utils/getResponses";
// Type of middleware and handler
export type Handler = (
req: Request,
res: Response,
next: NextFunction
) => Promise<void> | void;
/**
*
* @param userAgent The user agent of the requester
* @returns A boolean that is true of the user agent provided is from curl / wget / httpie
*/
const isTerminal: (userAgent: string | undefined) => boolean = (userAgent) => {
if (userAgent === undefined) return false;
if (/curl|wget|httpie/i.test(userAgent)) return true;
return false;
};
export const userAgentMiddleware: Handler = (req, res, next) => {
/**
* Get the user agent from the request
* Determine if the user agent is from curl / wget / httpie
* If true then proceed using the next function
* Else return with message
*/
const userAgent = req.headers["user-agent"];
if (!isTerminal(userAgent)) {
res.send(lines.notFound);
return;
}
next();
};