@caldik/client 0.0.1 → 0.0.2

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/dist/index.d.mts CHANGED
@@ -5,8 +5,8 @@ interface Category {
5
5
  slug: string;
6
6
  description: string;
7
7
  price: string;
8
- logo: string;
9
- banner: string;
8
+ logo: string | null;
9
+ banner: string | null;
10
10
  }
11
11
  interface CategoriesResponse {
12
12
  success: boolean;
@@ -21,8 +21,8 @@ interface CategoryProduct {
21
21
  color: string;
22
22
  }[];
23
23
  price: string;
24
- logo: string;
25
- banner: string;
24
+ logo: string | null;
25
+ banner: string | null;
26
26
  }
27
27
  interface CategoryDetails {
28
28
  id: string;
@@ -56,7 +56,120 @@ interface ReviewsResponse {
56
56
  interface Product {
57
57
  id: string;
58
58
  name: string;
59
- price: number;
59
+ type: string;
60
+ slug: string;
61
+ description: string;
62
+ price: string;
63
+ logo: string | null;
64
+ banner: string | null;
65
+ videoUrl: string | null;
66
+ deliveryType: string;
67
+ minimumQuantity: number | null;
68
+ maximumQuantity: number | null;
69
+ variants: ProductVariant[];
70
+ tags: ProductTag[];
71
+ }
72
+ interface ProductVariant {
73
+ id: string;
74
+ name: string;
75
+ price: string;
76
+ compareAtPrice: string | null;
77
+ }
78
+ interface ProductTag {
79
+ label: string;
80
+ color: string;
81
+ }
82
+ interface ProductsResponse {
83
+ success: boolean;
84
+ products: Product[];
85
+ }
86
+ interface ProductResponse {
87
+ success: boolean;
88
+ product: Product;
89
+ }
90
+ interface ManualCommerceItem {
91
+ name: string;
92
+ description?: string;
93
+ quantity?: number;
94
+ unitAmount: string | number;
95
+ }
96
+ interface ProductCommerceItem {
97
+ productId: string;
98
+ productVariantId?: string;
99
+ quantity?: number;
100
+ }
101
+ type CommerceItem = ManualCommerceItem | ProductCommerceItem;
102
+ interface CreateOrderInput {
103
+ customerId?: string;
104
+ email?: string;
105
+ paymentMethod: "customer_balance" | "stripe" | "ovgc";
106
+ items: CommerceItem[];
107
+ successUrl?: string;
108
+ cancelUrl?: string;
109
+ }
110
+ interface StorefrontOrder {
111
+ id: string;
112
+ shortId: string;
113
+ displayId: string;
114
+ customerId: string | null;
115
+ paymentStatus: string;
116
+ fulfillmentStatus: string;
117
+ amount: string;
118
+ currency: string;
119
+ }
120
+ interface CheckoutPayment {
121
+ type: "customer_balance" | "stripe" | "ovgc";
122
+ checkoutUrl?: string | null;
123
+ checkoutSessionId?: string | null;
124
+ balanceAfter?: string;
125
+ }
126
+ interface OrderResponse {
127
+ success: boolean;
128
+ order: StorefrontOrder;
129
+ payment: CheckoutPayment | null;
130
+ }
131
+ interface CreateSubscriptionInput {
132
+ customerId?: string;
133
+ email?: string;
134
+ items: CommerceItem[];
135
+ billingInterval: "day" | "week" | "month" | "year";
136
+ billingIntervalCount?: number;
137
+ trialDays?: number;
138
+ successUrl: string;
139
+ cancelUrl: string;
140
+ }
141
+ interface SubscriptionResponse {
142
+ success: boolean;
143
+ subscription: {
144
+ id: string;
145
+ shortId: string;
146
+ displayId: string;
147
+ customerId: string;
148
+ status: string;
149
+ billingInterval: string;
150
+ billingIntervalCount: number;
151
+ amount: string;
152
+ currency: string;
153
+ };
154
+ payment: {
155
+ type: "stripe";
156
+ checkoutUrl: string;
157
+ checkoutSessionId: string;
158
+ };
159
+ }
160
+ interface CustomerLoginResponse {
161
+ success: boolean;
162
+ message: string;
163
+ }
164
+ interface CustomerLoginVerifyResponse {
165
+ success: boolean;
166
+ customer: {
167
+ id: string;
168
+ shortId: string;
169
+ displayId: string;
170
+ email: string;
171
+ status: string;
172
+ };
60
173
  }
61
174
  //#endregion
62
175
  //#region src/internal/types.d.ts
@@ -65,11 +178,21 @@ interface RequestOptions {
65
178
  }
66
179
  //#endregion
67
180
  //#region src/internal/client.d.ts
181
+ type CaldikTransport = "auto" | "browser" | "worker";
182
+ type HttpClientOptions = {
183
+ apiKey?: string;
184
+ apiUrl?: string;
185
+ transport?: CaldikTransport;
186
+ };
68
187
  declare class CaldikHttpClient {
69
- private publicKey?;
70
- private apiUrl;
71
- constructor(publicKey?: string | undefined, apiUrl?: string);
188
+ private readonly apiKey?;
189
+ private readonly apiUrl;
190
+ private readonly transport;
191
+ constructor(options?: HttpClientOptions);
192
+ private baseUrl;
193
+ private request;
72
194
  get<T>(path: string, params?: Record<string, string | number>, options?: RequestOptions): Promise<T>;
195
+ post<T>(path: string, input?: unknown, options?: RequestOptions): Promise<T>;
73
196
  }
74
197
  //#endregion
75
198
  //#region src/categories.d.ts
@@ -84,8 +207,8 @@ declare class CategoriesApi {
84
207
  declare class ProductsApi {
85
208
  private client;
86
209
  constructor(client: CaldikHttpClient);
87
- list(): Promise<Product[]>;
88
- get(productId: string): Promise<Product>;
210
+ list(): Promise<ProductsResponse>;
211
+ get(productId: string): Promise<ProductResponse>;
89
212
  }
90
213
  //#endregion
91
214
  //#region src/reviews.d.ts
@@ -102,17 +225,48 @@ declare class StoreApi {
102
225
  get(): Promise<StoreResponse>;
103
226
  }
104
227
  //#endregion
228
+ //#region src/checkout.d.ts
229
+ declare class CheckoutApi {
230
+ private client;
231
+ constructor(client: CaldikHttpClient);
232
+ create(input: CreateOrderInput): Promise<OrderResponse>;
233
+ payWithBalance(orderId: string): Promise<OrderResponse>;
234
+ }
235
+ //#endregion
236
+ //#region src/customer.d.ts
237
+ declare class CustomerApi {
238
+ private client;
239
+ constructor(client: CaldikHttpClient);
240
+ requestLogin(email: string): Promise<CustomerLoginResponse>;
241
+ verifyLogin(code: string): Promise<CustomerLoginVerifyResponse>;
242
+ }
243
+ //#endregion
244
+ //#region src/subscriptions.d.ts
245
+ declare class SubscriptionsApi {
246
+ private client;
247
+ constructor(client: CaldikHttpClient);
248
+ create(input: CreateSubscriptionInput): Promise<SubscriptionResponse>;
249
+ }
250
+ //#endregion
105
251
  //#region src/index.d.ts
106
252
  interface CaldikClientOptions {
253
+ /** API key for explicit external/browser integrations. */
254
+ apiKey?: string;
255
+ /** @deprecated Use apiKey. Retained for backwards compatibility. */
107
256
  publicApiKey?: string;
108
257
  apiUrl?: string;
258
+ /** Override automatic browser/Cloudflare Worker transport detection. */
259
+ transport?: CaldikTransport;
109
260
  }
110
261
  declare class CaldikClient {
111
262
  readonly categories: CategoriesApi;
112
263
  readonly products: ProductsApi;
113
264
  readonly reviews: ReviewsApi;
114
265
  readonly store: StoreApi;
266
+ readonly checkout: CheckoutApi;
267
+ readonly customer: CustomerApi;
268
+ readonly subscriptions: SubscriptionsApi;
115
269
  constructor(options?: CaldikClientOptions);
116
270
  }
117
271
  //#endregion
118
- export { CaldikClient, CaldikClientOptions };
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 };
package/dist/index.mjs CHANGED
@@ -2,29 +2,58 @@
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
+ function isBrowserRuntime() {
6
+ return typeof window !== "undefined" && typeof document !== "undefined";
7
+ }
8
+ function isCloudflareWorkerRuntime() {
9
+ return typeof navigator !== "undefined" && navigator.userAgent === "Cloudflare-Workers";
10
+ }
5
11
  var CaldikHttpClient = class {
6
- publicKey;
12
+ apiKey;
7
13
  apiUrl;
8
- constructor(publicKey, apiUrl = defaultApiUrl) {
9
- this.publicKey = publicKey;
10
- this.apiUrl = apiUrl;
14
+ transport;
15
+ constructor(options = {}) {
16
+ this.apiKey = options.apiKey;
17
+ this.apiUrl = options.apiUrl || defaultApiUrl;
18
+ this.transport = options.transport || "auto";
19
+ }
20
+ baseUrl() {
21
+ if (this.apiKey) return `${this.apiUrl.replace(/\/+$/, "")}/v1/storefront`;
22
+ if (this.transport === "browser" || this.transport === "auto" && isBrowserRuntime()) {
23
+ if (typeof location === "undefined") throw new Error("Caldik browser transport requires a browser origin");
24
+ return new URL("/__caldik/api", location.origin).toString().replace(/\/+$/, "");
25
+ }
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.");
11
28
  }
12
- async get(path, params, options) {
13
- const baseUrl = this.apiUrl.replace(/\/+$/, "");
14
- const url = new URL(`${baseUrl}/v1${path}`);
29
+ async request(method, path, input, params, options) {
30
+ const url = new URL(`${this.baseUrl()}${path}`);
15
31
  if (params) for (const [key, value] of Object.entries(params)) url.searchParams.set(key, String(value));
16
- const headers = {
17
- "User-Agent": "CaldikClient/0.0.1",
18
- ...this.publicKey && { Authorization: `Bearer ${this.publicKey}` }
19
- };
32
+ const headers = {};
33
+ if (!isBrowserRuntime()) headers["User-Agent"] = "CaldikClient/0.0.1";
34
+ if (this.apiKey) headers.Authorization = `Bearer ${this.apiKey}`;
35
+ if (input !== void 0) headers["Content-Type"] = "application/json";
20
36
  if (options?.edgeCache) headers["X-Cache-Policy"] = "public";
21
37
  const response = await fetch(url, {
38
+ method,
22
39
  headers,
23
- cache: "default"
40
+ ...input !== void 0 ? { body: JSON.stringify(input) } : {},
41
+ cache: "default",
42
+ credentials: "include"
24
43
  });
25
- if (!response.ok) throw new Error(response.statusText);
44
+ if (!response.ok) {
45
+ const error = await response.clone().json().catch(() => null);
46
+ const message = typeof error?.message === "string" && error.message ? error.message : `${response.status} ${response.statusText}`.trim();
47
+ throw new Error(message);
48
+ }
26
49
  return response.json();
27
50
  }
51
+ get(path, params, options) {
52
+ return this.request("GET", path, void 0, params, options);
53
+ }
54
+ post(path, input, options) {
55
+ return this.request("POST", path, input, void 0, options);
56
+ }
28
57
  };
29
58
  //#endregion
30
59
  //#region src/categories.ts
@@ -77,18 +106,67 @@ var StoreApi = class {
77
106
  }
78
107
  };
79
108
  //#endregion
109
+ //#region src/checkout.ts
110
+ var CheckoutApi = class {
111
+ client;
112
+ constructor(client) {
113
+ this.client = client;
114
+ }
115
+ create(input) {
116
+ return this.client.post("/checkout", input);
117
+ }
118
+ payWithBalance(orderId) {
119
+ return this.client.post(`/checkout/${encodeURIComponent(orderId)}/balance-payment`);
120
+ }
121
+ };
122
+ //#endregion
123
+ //#region src/customer.ts
124
+ var CustomerApi = class {
125
+ client;
126
+ constructor(client) {
127
+ this.client = client;
128
+ }
129
+ requestLogin(email) {
130
+ return this.client.post("/customer/login", { email });
131
+ }
132
+ verifyLogin(code) {
133
+ return this.client.post("/customer/login/verify", { code });
134
+ }
135
+ };
136
+ //#endregion
137
+ //#region src/subscriptions.ts
138
+ var SubscriptionsApi = class {
139
+ client;
140
+ constructor(client) {
141
+ this.client = client;
142
+ }
143
+ create(input) {
144
+ return this.client.post("/subscriptions", input);
145
+ }
146
+ };
147
+ //#endregion
80
148
  //#region src/index.ts
81
149
  var CaldikClient = class {
82
150
  categories;
83
151
  products;
84
152
  reviews;
85
153
  store;
154
+ checkout;
155
+ customer;
156
+ subscriptions;
86
157
  constructor(options = {}) {
87
- const client = new CaldikHttpClient(options.publicApiKey, options.apiUrl);
158
+ const client = new CaldikHttpClient({
159
+ apiKey: options.apiKey ?? options.publicApiKey,
160
+ apiUrl: options.apiUrl,
161
+ transport: options.transport
162
+ });
88
163
  this.categories = new CategoriesApi(client);
89
164
  this.products = new ProductsApi(client);
90
165
  this.reviews = new ReviewsApi(client);
91
166
  this.store = new StoreApi(client);
167
+ this.checkout = new CheckoutApi(client);
168
+ this.customer = new CustomerApi(client);
169
+ this.subscriptions = new SubscriptionsApi(client);
92
170
  }
93
171
  };
94
172
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caldik/client",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.mts",
@@ -17,7 +17,8 @@
17
17
  "access": "public"
18
18
  },
19
19
  "scripts": {
20
- "build": "tsdown"
20
+ "build": "tsdown",
21
+ "test": "bun test"
21
22
  },
22
23
  "devDependencies": {
23
24
  "tsdown": "^0.22.3",