]> Untitled Git - lemmy-ui.git/blob - src/shared/env.ts
Adding a few more 0.18.0 API changes. (#1324)
[lemmy-ui.git] / src / shared / env.ts
1 import { isBrowser } from "./utils";
2
3 const testHost = "0.0.0.0:8536";
4
5 function getInternalHost() {
6   return !isBrowser()
7     ? process.env.LEMMY_UI_LEMMY_INTERNAL_HOST ?? testHost
8     : testHost; // used for local dev
9 }
10
11 export function getExternalHost() {
12   return isBrowser()
13     ? `${window.location.hostname}${
14         ["1234", "1235"].includes(window.location.port)
15           ? ":8536"
16           : window.location.port == ""
17           ? ""
18           : `:${window.location.port}`
19       }`
20     : process.env.LEMMY_UI_LEMMY_EXTERNAL_HOST || testHost;
21 }
22
23 function getSecure() {
24   return (
25     isBrowser()
26       ? window.location.protocol.includes("https")
27       : process.env.LEMMY_UI_HTTPS === "true"
28   )
29     ? "s"
30     : "";
31 }
32
33 function getHost() {
34   return isBrowser() ? getExternalHost() : getInternalHost();
35 }
36
37 function getBaseLocal(s = "") {
38   return `http${s}://${getHost()}`;
39 }
40
41 export function getHttpBaseInternal() {
42   return getBaseLocal(); // Don't use secure here
43 }
44
45 export function getHttpBaseExternal() {
46   return `http${getSecure()}://${getExternalHost()}`;
47 }
48
49 export function getHttpBase() {
50   return getBaseLocal(getSecure());
51 }
52
53 export function isHttps() {
54   return getSecure() === "s";
55 }
56
57 console.log(`httpbase: ${getHttpBase()}`);
58 console.log(`isHttps: ${isHttps()}`);
59
60 // This is for html tags, don't include port
61 export function httpExternalPath(path: string) {
62   return `http${getSecure()}://${getExternalHost().replace(
63     /:\d+/g,
64     ""
65   )}${path}`;
66 }