@caldik/client 0.0.2 → 0.0.4

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 ADDED
@@ -0,0 +1,24 @@
1
+ # Caldik SDK
2
+
3
+ ## Caldik-hosted storefront
4
+
5
+ No credentials or store configuration are required:
6
+
7
+ ```ts
8
+ import { CaldikClient } from "@caldik/client"
9
+
10
+ const caldik = new CaldikClient()
11
+ const { products } = await caldik.products.list()
12
+ ```
13
+
14
+ ## External server
15
+
16
+ External integrations explicitly provide the store ID and a private `sk_` API
17
+ key:
18
+
19
+ ```ts
20
+ const caldik = new CaldikClient({
21
+ storeId: "STORE_...",
22
+ apiKey: process.env.CALDIK_PRIVATE_API_KEY,
23
+ })
24
+ ```
package/dist/index.d.mts CHANGED
@@ -175,19 +175,20 @@ interface CustomerLoginVerifyResponse {
175
175
  //#region src/internal/types.d.ts
176
176
  interface RequestOptions {
177
177
  edgeCache?: boolean;
178
+ /** Internal authorization policy. Protected hosted calls are the default. */
179
+ access?: "public" | "runtime";
178
180
  }
179
181
  //#endregion
180
182
  //#region src/internal/client.d.ts
181
- type CaldikTransport = "auto" | "browser" | "worker";
182
183
  type HttpClientOptions = {
183
184
  apiKey?: string;
184
185
  apiUrl?: string;
185
- transport?: CaldikTransport;
186
+ storeId?: string;
186
187
  };
187
188
  declare class CaldikHttpClient {
188
189
  private readonly apiKey?;
189
190
  private readonly apiUrl;
190
- private readonly transport;
191
+ private readonly storeId?;
191
192
  constructor(options?: HttpClientOptions);
192
193
  private baseUrl;
193
194
  private request;
@@ -250,13 +251,11 @@ declare class SubscriptionsApi {
250
251
  //#endregion
251
252
  //#region src/index.d.ts
252
253
  interface CaldikClientOptions {
253
- /** API key for explicit external/browser integrations. */
254
+ /** Store ID for external servers. Hosted Workers receive it automatically. */
255
+ storeId?: string;
256
+ /** Private API key for external server integrations. Never use in browsers. */
254
257
  apiKey?: string;
255
- /** @deprecated Use apiKey. Retained for backwards compatibility. */
256
- publicApiKey?: string;
257
258
  apiUrl?: string;
258
- /** Override automatic browser/Cloudflare Worker transport detection. */
259
- transport?: CaldikTransport;
260
259
  }
261
260
  declare class CaldikClient {
262
261
  readonly categories: CategoriesApi;
@@ -269,4 +268,4 @@ declare class CaldikClient {
269
268
  constructor(options?: CaldikClientOptions);
270
269
  }
271
270
  //#endregion
272
- export { CaldikClient, CaldikClientOptions, type CaldikTransport, CategoriesResponse, Category, CategoryDetails, CategoryProduct, CategoryResponse, CheckoutPayment, CommerceItem, CreateOrderInput, CreateSubscriptionInput, CustomerLoginResponse, CustomerLoginVerifyResponse, ManualCommerceItem, OrderResponse, Product, ProductCommerceItem, ProductResponse, ProductTag, ProductVariant, ProductsResponse, Review, ReviewsResponse, Store, StoreResponse, StorefrontOrder, SubscriptionResponse };
271
+ export { CaldikClient, CaldikClientOptions, CategoriesResponse, Category, CategoryDetails, CategoryProduct, CategoryResponse, CheckoutPayment, CommerceItem, CreateOrderInput, CreateSubscriptionInput, CustomerLoginResponse, CustomerLoginVerifyResponse, ManualCommerceItem, OrderResponse, Product, ProductCommerceItem, ProductResponse, ProductTag, ProductVariant, ProductsResponse, Review, ReviewsResponse, Store, StoreResponse, StorefrontOrder, SubscriptionResponse };
package/dist/index.mjs CHANGED
@@ -2,36 +2,45 @@
2
2
  const defaultApiUrl = typeof API_URL !== "undefined" ? API_URL : "https://api.caldik.com";
3
3
  //#endregion
4
4
  //#region src/internal/client.ts
5
+ const STORE_ID_PATTERN = /^STORE_[a-f0-9]{32}$/i;
5
6
  function isBrowserRuntime() {
6
7
  return typeof window !== "undefined" && typeof document !== "undefined";
7
8
  }
8
- function isCloudflareWorkerRuntime() {
9
- return typeof navigator !== "undefined" && navigator.userAgent === "Cloudflare-Workers";
9
+ function boundRuntimeStoreId() {
10
+ const storeId = globalThis.process?.env?.CALDIK_STORE_ID?.trim();
11
+ return storeId && STORE_ID_PATTERN.test(storeId) ? storeId : null;
10
12
  }
11
13
  var CaldikHttpClient = class {
12
14
  apiKey;
13
15
  apiUrl;
14
- transport;
16
+ storeId;
15
17
  constructor(options = {}) {
16
- this.apiKey = options.apiKey;
18
+ this.apiKey = options.apiKey?.trim() || void 0;
17
19
  this.apiUrl = options.apiUrl || defaultApiUrl;
18
- this.transport = options.transport || "auto";
20
+ this.storeId = options.storeId?.trim() || void 0;
21
+ if (this.apiKey && !this.apiKey.startsWith("sk_")) throw new Error("Caldik external integrations require a private sk_ API key");
22
+ if (this.storeId && !STORE_ID_PATTERN.test(this.storeId)) throw new Error("Caldik storeId is invalid");
23
+ if (isBrowserRuntime() && this.apiKey) throw new Error("Caldik private API keys cannot be used in browser code");
19
24
  }
20
25
  baseUrl() {
21
- if (this.apiKey) return `${this.apiUrl.replace(/\/+$/, "")}/v1/storefront`;
22
- if (this.transport === "browser" || this.transport === "auto" && isBrowserRuntime()) {
26
+ if (isBrowserRuntime()) {
23
27
  if (typeof location === "undefined") throw new Error("Caldik browser transport requires a browser origin");
24
28
  return new URL("/__caldik/api", location.origin).toString().replace(/\/+$/, "");
25
29
  }
26
- if (this.transport === "worker" || this.transport === "auto" && isCloudflareWorkerRuntime()) return `${this.apiUrl.replace(/\/+$/, "")}/v1/storefront`;
27
- throw new Error("Caldik authentication is unavailable in this environment. Provide apiKey for an external integration, or set transport to \"browser\" or \"worker\" when environment detection is unavailable.");
30
+ const boundStoreId = boundRuntimeStoreId();
31
+ if (boundStoreId && this.storeId && boundStoreId !== this.storeId) throw new Error("Caldik storeId does not match the hosted runtime");
32
+ const storeId = this.storeId || boundStoreId;
33
+ if (!storeId) throw new Error("Caldik authentication is unavailable. External servers must provide storeId and a private apiKey.");
34
+ if (!boundStoreId && !this.apiKey) throw new Error("Caldik authentication is unavailable. External servers must provide a private apiKey.");
35
+ return `${this.apiUrl.replace(/\/+$/, "")}/v1/stores/${encodeURIComponent(storeId)}`;
28
36
  }
29
37
  async request(method, path, input, params, options) {
30
38
  const url = new URL(`${this.baseUrl()}${path}`);
31
39
  if (params) for (const [key, value] of Object.entries(params)) url.searchParams.set(key, String(value));
32
40
  const headers = {};
33
- if (!isBrowserRuntime()) headers["User-Agent"] = "CaldikClient/0.0.1";
41
+ if (!isBrowserRuntime()) headers["User-Agent"] = "CaldikClient/0.0.2";
34
42
  if (this.apiKey) headers.Authorization = `Bearer ${this.apiKey}`;
43
+ else if (options?.access === "public") headers["X-Caldik-Public-Request"] = "1";
35
44
  if (input !== void 0) headers["Content-Type"] = "application/json";
36
45
  if (options?.edgeCache) headers["X-Cache-Policy"] = "public";
37
46
  const response = await fetch(url, {
@@ -63,10 +72,10 @@ var CategoriesApi = class {
63
72
  this.client = client;
64
73
  }
65
74
  list(limit) {
66
- return this.client.get("/categories", limit ? { limit } : void 0);
75
+ return this.client.get("/categories", limit ? { limit } : void 0, { access: "public" });
67
76
  }
68
77
  get(categoryId) {
69
- return this.client.get(`/categories/${categoryId}`);
78
+ return this.client.get(`/categories/${categoryId}`, void 0, { access: "public" });
70
79
  }
71
80
  };
72
81
  //#endregion
@@ -77,10 +86,10 @@ var ProductsApi = class {
77
86
  this.client = client;
78
87
  }
79
88
  list() {
80
- return this.client.get("/products");
89
+ return this.client.get("/products", void 0, { access: "public" });
81
90
  }
82
91
  get(productId) {
83
- return this.client.get(`/products/${productId}`);
92
+ return this.client.get(`/products/${productId}`, void 0, { access: "public" });
84
93
  }
85
94
  };
86
95
  //#endregion
@@ -91,7 +100,7 @@ var ReviewsApi = class {
91
100
  this.client = client;
92
101
  }
93
102
  list(limit) {
94
- return this.client.get("/reviews", limit ? { limit } : void 0);
103
+ return this.client.get("/reviews", limit ? { limit } : void 0, { access: "public" });
95
104
  }
96
105
  };
97
106
  //#endregion
@@ -102,7 +111,7 @@ var StoreApi = class {
102
111
  this.client = client;
103
112
  }
104
113
  get() {
105
- return this.client.get("");
114
+ return this.client.get("", void 0, { access: "public" });
106
115
  }
107
116
  };
108
117
  //#endregion
@@ -113,10 +122,10 @@ var CheckoutApi = class {
113
122
  this.client = client;
114
123
  }
115
124
  create(input) {
116
- return this.client.post("/checkout", input);
125
+ return this.client.post("/checkouts", input, { access: "public" });
117
126
  }
118
127
  payWithBalance(orderId) {
119
- return this.client.post(`/checkout/${encodeURIComponent(orderId)}/balance-payment`);
128
+ return this.client.post(`/checkouts/${encodeURIComponent(orderId)}/balance-payment`, void 0, { access: "public" });
120
129
  }
121
130
  };
122
131
  //#endregion
@@ -127,10 +136,10 @@ var CustomerApi = class {
127
136
  this.client = client;
128
137
  }
129
138
  requestLogin(email) {
130
- return this.client.post("/customer/login", { email });
139
+ return this.client.post("/customers/login", { email }, { access: "public" });
131
140
  }
132
141
  verifyLogin(code) {
133
- return this.client.post("/customer/login/verify", { code });
142
+ return this.client.post("/customers/login/verify", { code }, { access: "public" });
134
143
  }
135
144
  };
136
145
  //#endregion
@@ -141,7 +150,7 @@ var SubscriptionsApi = class {
141
150
  this.client = client;
142
151
  }
143
152
  create(input) {
144
- return this.client.post("/subscriptions", input);
153
+ return this.client.post("/subscriptions", input, { access: "public" });
145
154
  }
146
155
  };
147
156
  //#endregion
@@ -156,9 +165,9 @@ var CaldikClient = class {
156
165
  subscriptions;
157
166
  constructor(options = {}) {
158
167
  const client = new CaldikHttpClient({
159
- apiKey: options.apiKey ?? options.publicApiKey,
168
+ apiKey: options.apiKey,
160
169
  apiUrl: options.apiUrl,
161
- transport: options.transport
170
+ storeId: options.storeId
162
171
  });
163
172
  this.categories = new CategoriesApi(client);
164
173
  this.products = new ProductsApi(client);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caldik/client",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.mts",