@caldik/client 0.0.7 → 0.0.8

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/README.md CHANGED
@@ -21,4 +21,4 @@ const caldik = new CaldikClient({
21
21
  storeId: "STORE_...",
22
22
  apiKey: process.env.CALDIK_PRIVATE_API_KEY,
23
23
  })
24
- ```
24
+ ```
package/dist/index.mjs CHANGED
@@ -1,5 +1,28 @@
1
+ //#region src/internal/environment.ts
2
+ function firstValue(...values) {
3
+ for (const value of values) {
4
+ const normalized = value?.trim();
5
+ if (normalized) return normalized;
6
+ }
7
+ }
8
+ function importMetaEnvironment() {
9
+ return import.meta.env;
10
+ }
11
+ /** Public context exposed by Next/Vinext, Vite, or Astro during `caldik dev`. */
12
+ function browserBuildStoreId() {
13
+ const environment = importMetaEnvironment();
14
+ return firstValue(typeof process === "undefined" ? void 0 : process.env.NEXT_PUBLIC_CALDIK_STORE_ID, environment?.VITE_CALDIK_STORE_ID, environment?.PUBLIC_CALDIK_STORE_ID);
15
+ }
16
+ function environmentApiUrl(browser) {
17
+ const environment = importMetaEnvironment();
18
+ if (browser) return firstValue(typeof process === "undefined" ? void 0 : process.env.NEXT_PUBLIC_CALDIK_API_URL, environment?.VITE_CALDIK_API_URL, environment?.PUBLIC_CALDIK_API_URL);
19
+ return firstValue(typeof process === "undefined" ? void 0 : process.env.CALDIK_API_URL);
20
+ }
21
+ //#endregion
1
22
  //#region src/config.ts
2
- const defaultApiUrl = typeof API_URL !== "undefined" ? API_URL : "https://api.caldik.com";
23
+ function defaultApiUrl(browser) {
24
+ return environmentApiUrl(browser) || (typeof API_URL !== "undefined" ? API_URL : "https://api.caldik.com");
25
+ }
3
26
  //#endregion
4
27
  //#region src/internal/client.ts
5
28
  const STORE_ID_PATTERN = /^STORE_[a-f0-9]{32}$/i;
@@ -12,8 +35,15 @@ function boundRuntimeStoreId() {
12
35
  return storeId && STORE_ID_PATTERN.test(storeId) ? storeId : null;
13
36
  }
14
37
  function browserRuntimeStoreId() {
15
- const storeId = document.documentElement?.getAttribute(STOREFRONT_STORE_ID_ATTRIBUTE)?.trim();
16
- return storeId && STORE_ID_PATTERN.test(storeId) ? storeId : null;
38
+ const documentStoreId = document.documentElement?.getAttribute(STOREFRONT_STORE_ID_ATTRIBUTE)?.trim();
39
+ if (documentStoreId && STORE_ID_PATTERN.test(documentStoreId)) return documentStoreId;
40
+ const buildStoreId = browserBuildStoreId();
41
+ return buildStoreId && STORE_ID_PATTERN.test(buildStoreId) ? buildStoreId : null;
42
+ }
43
+ function isLocalBrowserRuntime() {
44
+ if (!isBrowserRuntime() || typeof location === "undefined") return false;
45
+ const hostname = location.hostname.toLowerCase();
46
+ return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "::1" || hostname === "[::1]";
17
47
  }
18
48
  var CaldikHttpClient = class {
19
49
  apiKey;
@@ -21,7 +51,7 @@ var CaldikHttpClient = class {
21
51
  storeId;
22
52
  constructor(options = {}) {
23
53
  this.apiKey = options.apiKey?.trim() || void 0;
24
- this.apiUrl = options.apiUrl || defaultApiUrl;
54
+ this.apiUrl = options.apiUrl || defaultApiUrl(isBrowserRuntime());
25
55
  const configuredStoreId = options.storeId?.trim() || void 0;
26
56
  if (this.apiKey && !this.apiKey.startsWith("sk_")) throw new Error("Caldik external integrations require a private sk_ API key");
27
57
  if (configuredStoreId && !STORE_ID_PATTERN.test(configuredStoreId)) throw new Error("Caldik storeId is invalid");
@@ -47,7 +77,7 @@ var CaldikHttpClient = class {
47
77
  const headers = {};
48
78
  if (!isBrowserRuntime()) headers["User-Agent"] = "CaldikClient/0.0.2";
49
79
  if (this.apiKey) headers.Authorization = `Bearer ${this.apiKey}`;
50
- else if (options?.access === "public" && !isBrowserRuntime()) headers["X-Caldik-Public-Request"] = "1";
80
+ else if (options?.access === "public" && (!isBrowserRuntime() || isLocalBrowserRuntime())) headers["X-Caldik-Public-Request"] = "1";
51
81
  if (input !== void 0) headers["Content-Type"] = "application/json";
52
82
  if (options?.edgeCache) headers["X-Cache-Policy"] = "public";
53
83
  const response = await fetch(url, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caldik/client",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.mts",