@seip/blue-bird 0.7.4 → 0.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/frontend/src/http/api.js +14 -9
- package/package.json +1 -1
package/frontend/src/http/api.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
const
|
|
1
|
+
const defaultPort = (import.meta.env.PORT || "3000").replace(
|
|
2
|
+
/^["']|["']$/g,
|
|
3
|
+
"",
|
|
4
|
+
);
|
|
2
5
|
const host = (import.meta.env.HOST || "localhost").replace(/^["']|["']$/g, "");
|
|
3
6
|
|
|
4
7
|
/**
|
|
@@ -7,18 +10,20 @@ const host = (import.meta.env.HOST || "localhost").replace(/^["']|["']$/g, "");
|
|
|
7
10
|
* @param {string} [path] - API path relative to the server root (e.g. "api/users").
|
|
8
11
|
* @param {URL} [requestUrl] - Current request URL (pass `Astro.url` from the page). Required in production.
|
|
9
12
|
* @returns {string} Absolute URL ready for fetch.
|
|
13
|
+
* @example apiUrl('api/', Astro.url)
|
|
10
14
|
*/
|
|
11
15
|
export function apiUrl(path = "", requestUrl) {
|
|
12
|
-
if (import.meta.env.DEV) {
|
|
13
|
-
return `http://${host}:${port}/${path}`;
|
|
14
|
-
}
|
|
15
16
|
if (!requestUrl) {
|
|
16
|
-
throw new Error(
|
|
17
|
+
throw new Error(
|
|
18
|
+
"apiUrl: requestUrl is required in production (pass Astro.url)",
|
|
19
|
+
);
|
|
17
20
|
}
|
|
18
21
|
const url = new URL(`/${path}`, requestUrl);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
const runtimePort =
|
|
23
|
+
(typeof process !== "undefined" && process.env && process.env.PORT) ||
|
|
24
|
+
defaultPort;
|
|
25
|
+
url.hostname = "127.0.0.1";
|
|
26
|
+
url.port = runtimePort;
|
|
27
|
+
url.protocol = "http:";
|
|
23
28
|
return url.href;
|
|
24
29
|
}
|
package/package.json
CHANGED