@caldik/client 0.0.6 → 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
@@ -22,9 +22,3 @@ const caldik = new CaldikClient({
22
22
  apiKey: process.env.CALDIK_PRIVATE_API_KEY,
23
23
  })
24
24
  ```
25
-
26
- ## Canonical contracts
27
-
28
- ```ts
29
- const { events, catalogVersion } = await caldik.webhooks.events()
30
- ```
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. Hosted Workers receive it automatically. */
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
@@ -1,8 +1,32 @@
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;
29
+ const STOREFRONT_STORE_ID_ATTRIBUTE = "data-caldik-store-id";
6
30
  function isBrowserRuntime() {
7
31
  return typeof window !== "undefined" && typeof document !== "undefined";
8
32
  }
@@ -11,19 +35,15 @@ function boundRuntimeStoreId() {
11
35
  return storeId && STORE_ID_PATTERN.test(storeId) ? storeId : null;
12
36
  }
13
37
  function browserRuntimeStoreId() {
14
- const cookies = typeof document.cookie === "string" ? document.cookie : "";
15
- for (const item of cookies.split(";")) {
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;
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]";
27
47
  }
28
48
  var CaldikHttpClient = class {
29
49
  apiKey;
@@ -31,15 +51,16 @@ var CaldikHttpClient = class {
31
51
  storeId;
32
52
  constructor(options = {}) {
33
53
  this.apiKey = options.apiKey?.trim() || void 0;
34
- this.apiUrl = options.apiUrl || defaultApiUrl;
35
- this.storeId = options.storeId?.trim() || void 0;
54
+ this.apiUrl = options.apiUrl || defaultApiUrl(isBrowserRuntime());
55
+ const configuredStoreId = options.storeId?.trim() || void 0;
36
56
  if (this.apiKey && !this.apiKey.startsWith("sk_")) throw new Error("Caldik external integrations require a private sk_ API key");
37
- if (this.storeId && !STORE_ID_PATTERN.test(this.storeId)) throw new Error("Caldik storeId is invalid");
57
+ if (configuredStoreId && !STORE_ID_PATTERN.test(configuredStoreId)) throw new Error("Caldik storeId is invalid");
38
58
  if (isBrowserRuntime() && this.apiKey) throw new Error("Caldik private API keys cannot be used in browser code");
59
+ this.storeId = configuredStoreId || (isBrowserRuntime() ? browserRuntimeStoreId() || void 0 : void 0);
39
60
  }
40
61
  baseUrl() {
41
62
  if (isBrowserRuntime()) {
42
- const storeId = this.storeId || browserRuntimeStoreId();
63
+ const storeId = this.storeId;
43
64
  if (!storeId) throw new Error("Caldik storefront context is unavailable. Hosted storefronts receive it automatically; local or externally hosted browsers must provide storeId.");
44
65
  return `${this.apiUrl.replace(/\/+$/, "")}/v1/stores/${encodeURIComponent(storeId)}`;
45
66
  }
@@ -56,7 +77,7 @@ var CaldikHttpClient = class {
56
77
  const headers = {};
57
78
  if (!isBrowserRuntime()) headers["User-Agent"] = "CaldikClient/0.0.2";
58
79
  if (this.apiKey) headers.Authorization = `Bearer ${this.apiKey}`;
59
- 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";
60
81
  if (input !== void 0) headers["Content-Type"] = "application/json";
61
82
  if (options?.edgeCache) headers["X-Cache-Policy"] = "public";
62
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.6",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.mts",