@caldik/client 0.0.6 → 0.0.7
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 +1 -7
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +7 -16
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -326,7 +326,7 @@ declare class WebhooksApi {
|
|
|
326
326
|
//#endregion
|
|
327
327
|
//#region src/index.d.ts
|
|
328
328
|
interface CaldikClientOptions {
|
|
329
|
-
/** Store ID for external servers
|
|
329
|
+
/** Store ID override for external servers or local browser development. */
|
|
330
330
|
storeId?: string;
|
|
331
331
|
/** Private API key for external server integrations. Never use in browsers. */
|
|
332
332
|
apiKey?: string;
|
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@ const defaultApiUrl = typeof API_URL !== "undefined" ? API_URL : "https://api.ca
|
|
|
3
3
|
//#endregion
|
|
4
4
|
//#region src/internal/client.ts
|
|
5
5
|
const STORE_ID_PATTERN = /^STORE_[a-f0-9]{32}$/i;
|
|
6
|
+
const STOREFRONT_STORE_ID_ATTRIBUTE = "data-caldik-store-id";
|
|
6
7
|
function isBrowserRuntime() {
|
|
7
8
|
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
8
9
|
}
|
|
@@ -11,19 +12,8 @@ function boundRuntimeStoreId() {
|
|
|
11
12
|
return storeId && STORE_ID_PATTERN.test(storeId) ? storeId : null;
|
|
12
13
|
}
|
|
13
14
|
function browserRuntimeStoreId() {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
const separator = item.indexOf("=");
|
|
17
|
-
if (separator < 1) continue;
|
|
18
|
-
if (item.slice(0, separator).trim() !== "__Host-caldik-store-id") continue;
|
|
19
|
-
try {
|
|
20
|
-
const value = decodeURIComponent(item.slice(separator + 1).trim());
|
|
21
|
-
return STORE_ID_PATTERN.test(value) ? value : null;
|
|
22
|
-
} catch {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return null;
|
|
15
|
+
const storeId = document.documentElement?.getAttribute(STOREFRONT_STORE_ID_ATTRIBUTE)?.trim();
|
|
16
|
+
return storeId && STORE_ID_PATTERN.test(storeId) ? storeId : null;
|
|
27
17
|
}
|
|
28
18
|
var CaldikHttpClient = class {
|
|
29
19
|
apiKey;
|
|
@@ -32,14 +22,15 @@ var CaldikHttpClient = class {
|
|
|
32
22
|
constructor(options = {}) {
|
|
33
23
|
this.apiKey = options.apiKey?.trim() || void 0;
|
|
34
24
|
this.apiUrl = options.apiUrl || defaultApiUrl;
|
|
35
|
-
|
|
25
|
+
const configuredStoreId = options.storeId?.trim() || void 0;
|
|
36
26
|
if (this.apiKey && !this.apiKey.startsWith("sk_")) throw new Error("Caldik external integrations require a private sk_ API key");
|
|
37
|
-
if (
|
|
27
|
+
if (configuredStoreId && !STORE_ID_PATTERN.test(configuredStoreId)) throw new Error("Caldik storeId is invalid");
|
|
38
28
|
if (isBrowserRuntime() && this.apiKey) throw new Error("Caldik private API keys cannot be used in browser code");
|
|
29
|
+
this.storeId = configuredStoreId || (isBrowserRuntime() ? browserRuntimeStoreId() || void 0 : void 0);
|
|
39
30
|
}
|
|
40
31
|
baseUrl() {
|
|
41
32
|
if (isBrowserRuntime()) {
|
|
42
|
-
const storeId = this.storeId
|
|
33
|
+
const storeId = this.storeId;
|
|
43
34
|
if (!storeId) throw new Error("Caldik storefront context is unavailable. Hosted storefronts receive it automatically; local or externally hosted browsers must provide storeId.");
|
|
44
35
|
return `${this.apiUrl.replace(/\/+$/, "")}/v1/stores/${encodeURIComponent(storeId)}`;
|
|
45
36
|
}
|