Add type safety to getSkipSegments

This commit is contained in:
Ajay Ramachandran
2020-12-29 14:16:11 -05:00
parent 432cc42cba
commit 29ef770759
4 changed files with 36 additions and 22 deletions

View File

@@ -1,19 +1,21 @@
import {config} from '../config';
import {Request} from 'express';
import { IPAddress } from '../types/segments.model';
export function getIP(req: Request): string {
export function getIP(req: Request): IPAddress {
if (config.behindProxy === true || config.behindProxy === "true") {
config.behindProxy = "X-Forwarded-For";
}
switch (config.behindProxy as string) {
case "X-Forwarded-For":
return req.headers['x-forwarded-for'] as string;
return req.headers['x-forwarded-for'] as IPAddress;
case "Cloudflare":
return req.headers['cf-connecting-ip'] as string;
return req.headers['cf-connecting-ip'] as IPAddress;
case "X-Real-IP":
return req.headers['x-real-ip'] as string;
return req.headers['x-real-ip'] as IPAddress;
default:
return req.connection.remoteAddress;
return req.connection.remoteAddress as IPAddress;
}
}
}