@caldik/client 0.0.5 → 0.0.6

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,3 +22,9 @@ 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
@@ -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,6 +316,14 @@ 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
329
  /** Store ID for external servers. Hosted Workers receive it automatically. */
@@ -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
@@ -10,6 +10,21 @@ function boundRuntimeStoreId() {
10
10
  const storeId = globalThis.process?.env?.CALDIK_STORE_ID?.trim();
11
11
  return storeId && STORE_ID_PATTERN.test(storeId) ? storeId : null;
12
12
  }
13
+ 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;
27
+ }
13
28
  var CaldikHttpClient = class {
14
29
  apiKey;
15
30
  apiUrl;
@@ -24,10 +39,9 @@ var CaldikHttpClient = class {
24
39
  }
25
40
  baseUrl() {
26
41
  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)}`;
42
+ const storeId = this.storeId || browserRuntimeStoreId();
43
+ if (!storeId) throw new Error("Caldik storefront context is unavailable. Hosted storefronts receive it automatically; local or externally hosted browsers must provide storeId.");
44
+ return `${this.apiUrl.replace(/\/+$/, "")}/v1/stores/${encodeURIComponent(storeId)}`;
31
45
  }
32
46
  const boundStoreId = boundRuntimeStoreId();
33
47
  if (boundStoreId && this.storeId && boundStoreId !== this.storeId) throw new Error("Caldik storeId does not match the hosted runtime");
@@ -156,6 +170,60 @@ var SubscriptionsApi = class {
156
170
  }
157
171
  };
158
172
  //#endregion
173
+ //#region src/webhooks.ts
174
+ var WebhooksApi = class {
175
+ client;
176
+ constructor(client) {
177
+ this.client = client;
178
+ }
179
+ /** Lists the canonical events accepted by store webhook subscriptions. */
180
+ events() {
181
+ return this.client.get("/webhooks/events");
182
+ }
183
+ };
184
+ //#endregion
185
+ //#region src/payment-providers.generated.ts
186
+ const paymentProviderIds = [
187
+ "customer_balance",
188
+ "stripe",
189
+ "ovgc"
190
+ ];
191
+ const checkoutPaymentProviderIds = [
192
+ "customer_balance",
193
+ "stripe",
194
+ "ovgc"
195
+ ];
196
+ const subscriptionPaymentProviderIds = ["stripe"];
197
+ //#endregion
198
+ //#region src/store-events.generated.ts
199
+ const storeWebhookEventNames = [
200
+ "store.updated",
201
+ "store.suspended",
202
+ "store.restored",
203
+ "domain.created",
204
+ "domain.updated",
205
+ "domain.deleted",
206
+ "member.invited",
207
+ "member.updated",
208
+ "member.removed",
209
+ "member.invite_revoked",
210
+ "product.created",
211
+ "product.updated",
212
+ "product.deleted",
213
+ "category.created",
214
+ "category.updated",
215
+ "category.deleted",
216
+ "discount.created",
217
+ "discount.updated",
218
+ "discount.deleted",
219
+ "customer.created",
220
+ "customer.updated",
221
+ "order.created",
222
+ "order.updated",
223
+ "subscription.created",
224
+ "subscription.updated"
225
+ ];
226
+ //#endregion
159
227
  //#region src/index.ts
160
228
  var CaldikClient = class {
161
229
  categories;
@@ -165,6 +233,7 @@ var CaldikClient = class {
165
233
  checkout;
166
234
  customer;
167
235
  subscriptions;
236
+ webhooks;
168
237
  constructor(options = {}) {
169
238
  const client = new CaldikHttpClient({
170
239
  apiKey: options.apiKey,
@@ -178,7 +247,8 @@ var CaldikClient = class {
178
247
  this.checkout = new CheckoutApi(client);
179
248
  this.customer = new CustomerApi(client);
180
249
  this.subscriptions = new SubscriptionsApi(client);
250
+ this.webhooks = new WebhooksApi(client);
181
251
  }
182
252
  };
183
253
  //#endregion
184
- export { CaldikClient };
254
+ 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.6",
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": {