@caldik/client 0.0.5 → 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 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.d.mts CHANGED
@@ -1,3 +1,15 @@
1
+ //#region src/payment-providers.generated.d.ts
2
+ declare const paymentProviderIds: readonly ["customer_balance", "stripe", "ovgc"];
3
+ declare const checkoutPaymentProviderIds: readonly ["customer_balance", "stripe", "ovgc"];
4
+ declare const subscriptionPaymentProviderIds: readonly ["stripe"];
5
+ type PaymentProviderId = typeof paymentProviderIds[number];
6
+ type CheckoutPaymentProviderId = typeof checkoutPaymentProviderIds[number];
7
+ type SubscriptionPaymentProviderId = typeof subscriptionPaymentProviderIds[number];
8
+ //#endregion
9
+ //#region src/store-events.generated.d.ts
10
+ declare const storeWebhookEventNames: readonly ["store.updated", "store.suspended", "store.restored", "domain.created", "domain.updated", "domain.deleted", "member.invited", "member.updated", "member.removed", "member.invite_revoked", "product.created", "product.updated", "product.deleted", "category.created", "category.updated", "category.deleted", "discount.created", "discount.updated", "discount.deleted", "customer.created", "customer.updated", "order.created", "order.updated", "subscription.created", "subscription.updated"];
11
+ type StoreWebhookEventName = typeof storeWebhookEventNames[number];
12
+ //#endregion
1
13
  //#region src/types.d.ts
2
14
  interface StorefrontMetadata {
3
15
  id: string;
@@ -134,7 +146,7 @@ type CommerceItem = ManualCommerceItem | ProductCommerceItem;
134
146
  interface CreateOrderInput {
135
147
  customerId?: string;
136
148
  email?: string;
137
- paymentMethod: "customer_balance" | "stripe" | "ovgc";
149
+ paymentMethod: CheckoutPaymentProviderId;
138
150
  items: CommerceItem[];
139
151
  successUrl?: string;
140
152
  cancelUrl?: string;
@@ -150,7 +162,7 @@ interface StorefrontOrder {
150
162
  currency: string;
151
163
  }
152
164
  interface CheckoutPayment {
153
- type: "customer_balance" | "stripe" | "ovgc";
165
+ type: CheckoutPaymentProviderId;
154
166
  checkoutUrl?: string | null;
155
167
  checkoutSessionId?: string | null;
156
168
  balanceAfter?: string;
@@ -163,6 +175,7 @@ interface OrderResponse {
163
175
  interface CreateSubscriptionInput {
164
176
  customerId?: string;
165
177
  email?: string;
178
+ paymentMethod?: SubscriptionPaymentProviderId;
166
179
  items: CommerceItem[];
167
180
  billingInterval: "day" | "week" | "month" | "year";
168
181
  billingIntervalCount?: number;
@@ -184,11 +197,33 @@ interface SubscriptionResponse {
184
197
  currency: string;
185
198
  };
186
199
  payment: {
187
- type: "stripe";
200
+ type: SubscriptionPaymentProviderId;
188
201
  checkoutUrl: string;
189
202
  checkoutSessionId: string;
190
203
  };
191
204
  }
205
+ interface StoreWebhookEventDefinition {
206
+ name: StoreWebhookEventName;
207
+ resource: string;
208
+ action: string;
209
+ description: string;
210
+ }
211
+ interface StoreWebhookEventsResponse {
212
+ success: boolean;
213
+ apiVersion: "v1";
214
+ catalogVersion: string;
215
+ events: StoreWebhookEventDefinition[];
216
+ }
217
+ interface StoreWebhookEvent<TObject extends Record<string, unknown> = Record<string, unknown>> {
218
+ id: string;
219
+ type: StoreWebhookEventName;
220
+ createdAt: number;
221
+ storeId: string;
222
+ data: {
223
+ object: TObject;
224
+ previousAttributes?: Record<string, unknown>;
225
+ };
226
+ }
192
227
  interface CustomerLoginResponse {
193
228
  success: boolean;
194
229
  message: string;
@@ -281,9 +316,17 @@ declare class SubscriptionsApi {
281
316
  create(input: CreateSubscriptionInput): Promise<SubscriptionResponse>;
282
317
  }
283
318
  //#endregion
319
+ //#region src/webhooks.d.ts
320
+ declare class WebhooksApi {
321
+ private client;
322
+ constructor(client: CaldikHttpClient);
323
+ /** Lists the canonical events accepted by store webhook subscriptions. */
324
+ events(): Promise<StoreWebhookEventsResponse>;
325
+ }
326
+ //#endregion
284
327
  //#region src/index.d.ts
285
328
  interface CaldikClientOptions {
286
- /** Store ID for external servers. Hosted Workers receive it automatically. */
329
+ /** Store ID override for external servers or local browser development. */
287
330
  storeId?: string;
288
331
  /** Private API key for external server integrations. Never use in browsers. */
289
332
  apiKey?: string;
@@ -297,7 +340,8 @@ declare class CaldikClient {
297
340
  readonly checkout: CheckoutApi;
298
341
  readonly customer: CustomerApi;
299
342
  readonly subscriptions: SubscriptionsApi;
343
+ readonly webhooks: WebhooksApi;
300
344
  constructor(options?: CaldikClientOptions);
301
345
  }
302
346
  //#endregion
303
- export { CaldikClient, CaldikClientOptions, CategoriesResponse, Category, CategoryDetails, CategoryMetadata, CategoryProduct, CategoryResponse, CheckoutPayment, CommerceItem, CreateOrderInput, CreateSubscriptionInput, CustomerLoginResponse, CustomerLoginVerifyResponse, ManualCommerceItem, OrderResponse, Product, ProductCommerceItem, ProductCustomField, ProductCustomFieldInputType, ProductCustomFieldOption, ProductMetadata, ProductResponse, ProductTag, ProductVariant, ProductsResponse, Review, ReviewsResponse, Store, StoreResponse, StorefrontMetadata, StorefrontOrder, SubscriptionResponse };
347
+ export { CaldikClient, CaldikClientOptions, CategoriesResponse, Category, CategoryDetails, CategoryMetadata, CategoryProduct, CategoryResponse, CheckoutPayment, CheckoutPaymentProviderId, CommerceItem, CreateOrderInput, CreateSubscriptionInput, CustomerLoginResponse, CustomerLoginVerifyResponse, ManualCommerceItem, OrderResponse, PaymentProviderId, Product, ProductCommerceItem, ProductCustomField, ProductCustomFieldInputType, ProductCustomFieldOption, ProductMetadata, ProductResponse, ProductTag, ProductVariant, ProductsResponse, Review, ReviewsResponse, Store, StoreResponse, StoreWebhookEvent, StoreWebhookEventDefinition, StoreWebhookEventName, StoreWebhookEventsResponse, StorefrontMetadata, StorefrontOrder, SubscriptionPaymentProviderId, SubscriptionResponse, checkoutPaymentProviderIds, paymentProviderIds, storeWebhookEventNames, subscriptionPaymentProviderIds };
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
  }
@@ -10,6 +11,10 @@ function boundRuntimeStoreId() {
10
11
  const storeId = globalThis.process?.env?.CALDIK_STORE_ID?.trim();
11
12
  return storeId && STORE_ID_PATTERN.test(storeId) ? storeId : null;
12
13
  }
14
+ function browserRuntimeStoreId() {
15
+ const storeId = document.documentElement?.getAttribute(STOREFRONT_STORE_ID_ATTRIBUTE)?.trim();
16
+ return storeId && STORE_ID_PATTERN.test(storeId) ? storeId : null;
17
+ }
13
18
  var CaldikHttpClient = class {
14
19
  apiKey;
15
20
  apiUrl;
@@ -17,17 +22,17 @@ var CaldikHttpClient = class {
17
22
  constructor(options = {}) {
18
23
  this.apiKey = options.apiKey?.trim() || void 0;
19
24
  this.apiUrl = options.apiUrl || defaultApiUrl;
20
- this.storeId = options.storeId?.trim() || void 0;
25
+ const configuredStoreId = options.storeId?.trim() || void 0;
21
26
  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");
27
+ if (configuredStoreId && !STORE_ID_PATTERN.test(configuredStoreId)) throw new Error("Caldik storeId is invalid");
23
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);
24
30
  }
25
31
  baseUrl() {
26
32
  if (isBrowserRuntime()) {
27
- if (typeof location === "undefined") throw new Error("Caldik browser transport requires a browser origin");
28
- const storeReference = location.hostname.trim().toLowerCase().replace(/\.$/, "");
29
- if (!storeReference) throw new Error("Caldik browser transport requires a storefront hostname");
30
- return `${this.apiUrl.replace(/\/+$/, "")}/v1/stores/${encodeURIComponent(storeReference)}`;
33
+ const storeId = this.storeId;
34
+ if (!storeId) throw new Error("Caldik storefront context is unavailable. Hosted storefronts receive it automatically; local or externally hosted browsers must provide storeId.");
35
+ return `${this.apiUrl.replace(/\/+$/, "")}/v1/stores/${encodeURIComponent(storeId)}`;
31
36
  }
32
37
  const boundStoreId = boundRuntimeStoreId();
33
38
  if (boundStoreId && this.storeId && boundStoreId !== this.storeId) throw new Error("Caldik storeId does not match the hosted runtime");
@@ -156,6 +161,60 @@ var SubscriptionsApi = class {
156
161
  }
157
162
  };
158
163
  //#endregion
164
+ //#region src/webhooks.ts
165
+ var WebhooksApi = class {
166
+ client;
167
+ constructor(client) {
168
+ this.client = client;
169
+ }
170
+ /** Lists the canonical events accepted by store webhook subscriptions. */
171
+ events() {
172
+ return this.client.get("/webhooks/events");
173
+ }
174
+ };
175
+ //#endregion
176
+ //#region src/payment-providers.generated.ts
177
+ const paymentProviderIds = [
178
+ "customer_balance",
179
+ "stripe",
180
+ "ovgc"
181
+ ];
182
+ const checkoutPaymentProviderIds = [
183
+ "customer_balance",
184
+ "stripe",
185
+ "ovgc"
186
+ ];
187
+ const subscriptionPaymentProviderIds = ["stripe"];
188
+ //#endregion
189
+ //#region src/store-events.generated.ts
190
+ const storeWebhookEventNames = [
191
+ "store.updated",
192
+ "store.suspended",
193
+ "store.restored",
194
+ "domain.created",
195
+ "domain.updated",
196
+ "domain.deleted",
197
+ "member.invited",
198
+ "member.updated",
199
+ "member.removed",
200
+ "member.invite_revoked",
201
+ "product.created",
202
+ "product.updated",
203
+ "product.deleted",
204
+ "category.created",
205
+ "category.updated",
206
+ "category.deleted",
207
+ "discount.created",
208
+ "discount.updated",
209
+ "discount.deleted",
210
+ "customer.created",
211
+ "customer.updated",
212
+ "order.created",
213
+ "order.updated",
214
+ "subscription.created",
215
+ "subscription.updated"
216
+ ];
217
+ //#endregion
159
218
  //#region src/index.ts
160
219
  var CaldikClient = class {
161
220
  categories;
@@ -165,6 +224,7 @@ var CaldikClient = class {
165
224
  checkout;
166
225
  customer;
167
226
  subscriptions;
227
+ webhooks;
168
228
  constructor(options = {}) {
169
229
  const client = new CaldikHttpClient({
170
230
  apiKey: options.apiKey,
@@ -178,7 +238,8 @@ var CaldikClient = class {
178
238
  this.checkout = new CheckoutApi(client);
179
239
  this.customer = new CustomerApi(client);
180
240
  this.subscriptions = new SubscriptionsApi(client);
241
+ this.webhooks = new WebhooksApi(client);
181
242
  }
182
243
  };
183
244
  //#endregion
184
- export { CaldikClient };
245
+ export { CaldikClient, checkoutPaymentProviderIds, paymentProviderIds, storeWebhookEventNames, subscriptionPaymentProviderIds };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caldik/client",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.mts",
@@ -18,6 +18,7 @@
18
18
  },
19
19
  "scripts": {
20
20
  "build": "tsdown",
21
+ "sync:api-contracts": "bun scripts/sync-api-contracts.ts",
21
22
  "test": "bun test"
22
23
  },
23
24
  "devDependencies": {