index.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import ms from "ms";
  2. import {
  3. differenceInDays,
  4. differenceInHours,
  5. differenceInMonths
  6. } from "date-fns";
  7. export const addProtocol = (url: string): string => {
  8. const hasProtocol = /^\w+:\/\//.test(url);
  9. return hasProtocol ? url : `http://${url}`;
  10. };
  11. export const generateShortLink = (id: string, domain?: string): string => {
  12. const protocol =
  13. process.env.CUSTOM_DOMAIN_USE_HTTPS === "true" || !domain
  14. ? "https://"
  15. : "http://";
  16. return `${protocol}${domain || process.env.DEFAULT_DOMAIN}/${id}`;
  17. };
  18. export const isAdmin = (email: string): boolean =>
  19. process.env.ADMIN_EMAILS.split(",")
  20. .map(e => e.trim())
  21. .includes(email);
  22. export const getRedisKey = {
  23. link: (address: string, domain_id?: number, user_id?: number) =>
  24. `${address}-${domain_id || ""}-${user_id || ""}`,
  25. domain: (address: string) => `d-${address}`,
  26. host: (address: string) => `h-${address}`,
  27. user: (emailOrKey: string) => `u-${emailOrKey}`
  28. };
  29. // TODO: Add statsLimit
  30. export const getStatsLimit = (): number =>
  31. Number(process.env.DEFAULT_MAX_STATS_PER_LINK) || 100000000;
  32. export const getStatsCacheTime = (total?: number): number => {
  33. let durationInMs;
  34. switch (true) {
  35. case total <= 5000:
  36. durationInMs = ms("5 minutes");
  37. break;
  38. case total > 5000 && total < 20000:
  39. durationInMs = ms("10 minutes");
  40. break;
  41. case total < 40000:
  42. durationInMs = ms("15 minutes");
  43. break;
  44. case total > 40000:
  45. durationInMs = ms("30 minutes");
  46. break;
  47. default:
  48. durationInMs = ms("5 minutes");
  49. }
  50. return durationInMs / 1000;
  51. };
  52. export const statsObjectToArray = (obj: Stats) => {
  53. const objToArr = key =>
  54. Array.from(Object.keys(obj[key]))
  55. .map(name => ({
  56. name,
  57. value: obj[key][name]
  58. }))
  59. .sort((a, b) => b.value - a.value);
  60. return {
  61. browser: objToArr("browser"),
  62. os: objToArr("os"),
  63. country: objToArr("country"),
  64. referrer: objToArr("referrer")
  65. };
  66. };
  67. export const getDifferenceFunction = (
  68. type: "lastDay" | "lastWeek" | "lastMonth" | "allTime"
  69. ): Function => {
  70. if (type === "lastDay") return differenceInHours;
  71. if (type === "lastWeek") return differenceInDays;
  72. if (type === "lastMonth") return differenceInDays;
  73. if (type === "allTime") return differenceInMonths;
  74. throw new Error("Unknown type.");
  75. };
  76. export const getUTCDate = (dateString?: Date) => {
  77. const date = new Date(dateString || Date.now());
  78. return new Date(
  79. date.getUTCFullYear(),
  80. date.getUTCMonth(),
  81. date.getUTCDate(),
  82. date.getUTCHours()
  83. );
  84. };