@betterstore/sdk 0.6.13 → 0.7.1

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.mjs CHANGED
@@ -1,606 +1,332 @@
1
- // src/_utils/axios.ts
2
- import axios from "axios";
3
- var API_BASE_URL = "https://v1.betterstore.io";
4
- var createApiClient = (apiKey, proxy) => {
5
- const client = axios.create({
6
- baseURL: proxy ?? API_BASE_URL,
7
- headers: {
8
- "Content-Type": "application/json",
9
- Authorization: `Bearer ${apiKey}`
10
- }
11
- });
12
- if (process.env.NODE_ENV === "development") {
13
- client.interceptors.request.use((config) => {
14
- console.log("Request method:", config.method);
15
- console.log("Request URL:", config.url);
16
- console.log("Request headers:", config.headers);
17
- console.log("Request body:", config.data);
18
- return config;
1
+ // src/base-client.ts
2
+ var BaseClient = class {
3
+ constructor(config) {
4
+ this.config = config;
5
+ }
6
+ async get(path, params) {
7
+ const url = new URL(path, this.config.baseUrl);
8
+ if (params) {
9
+ Object.entries(params).forEach(([key, val]) => {
10
+ if (val !== void 0) url.searchParams.set(key, String(val));
11
+ });
12
+ }
13
+ const res = await fetch(url.toString(), {
14
+ method: "GET",
15
+ headers: this.config.headers
19
16
  });
17
+ return this.handleResponse(res);
20
18
  }
21
- client.interceptors.response.use(
22
- (response) => response.data,
23
- (error) => {
24
- const apiError = {
25
- isError: true,
26
- status: error.response?.status ?? 500,
27
- message: error.response?.data?.error || error.message || "Unknown error",
28
- code: error.response?.data?.code,
29
- details: error.response?.data
30
- };
31
- throw apiError;
32
- }
33
- );
34
- return client;
35
- };
36
-
37
- // src/auth/providers/otp.ts
38
- var OTP = class {
39
- apiClient;
40
- constructor(apiClient) {
41
- this.apiClient = apiClient;
42
- }
43
- async signup(params) {
44
- const data = await this.apiClient.post(
45
- "/auth/otp/signup",
46
- params
47
- );
48
- return data;
49
- }
50
- async login(params) {
51
- const data = await this.apiClient.post(
52
- "/auth/otp/login",
53
- params
54
- );
55
- return data;
19
+ async post(path, body) {
20
+ const res = await fetch(`${this.config.baseUrl}${path}`, {
21
+ method: "POST",
22
+ headers: this.config.headers,
23
+ body: body ? JSON.stringify(body) : void 0
24
+ });
25
+ return this.handleResponse(res);
56
26
  }
57
- async verify(params) {
58
- const data = await this.apiClient.post(
59
- "/auth/otp/verify",
60
- params
61
- );
62
- return data;
27
+ async delete(path) {
28
+ const res = await fetch(`${this.config.baseUrl}${path}`, {
29
+ method: "DELETE",
30
+ headers: this.config.headers
31
+ });
32
+ return this.handleResponse(res);
63
33
  }
64
- };
65
-
66
- // src/auth/index.ts
67
- var Auth = class {
68
- apiClient;
69
- otp;
70
- constructor(apiKey, proxy) {
71
- this.apiClient = createApiClient(apiKey, proxy);
72
- this.otp = new OTP(this.apiClient);
73
- }
74
- async retrieveSession(id) {
75
- const data = await this.apiClient.get(
76
- `/auth/session/${id}`
77
- );
78
- if ("isError" in data && data.isError || !data || !("token" in data)) {
79
- console.error(`Customer session with id ${id} not found`);
80
- return null;
34
+ async handleResponse(res) {
35
+ if (!res.ok) {
36
+ const error = await res.json().catch(() => ({ message: res.statusText }));
37
+ throw new Error(error.message || `HTTP ${res.status}`);
81
38
  }
82
- return data;
39
+ return res.json();
83
40
  }
84
41
  };
85
- var auth_default = Auth;
86
42
 
87
- // src/checkout/index.ts
88
- var Checkout = class {
89
- apiClient;
90
- constructor(apiKey, proxy) {
91
- this.apiClient = createApiClient(apiKey, proxy);
43
+ // src/namespaces/client/client-checkout.ts
44
+ var ClientCheckoutNamespace = class {
45
+ constructor(client) {
46
+ this.client = client;
47
+ }
48
+ async get(id) {
49
+ return this.client.get(
50
+ `/api/client/checkout/get/${id}`
51
+ );
92
52
  }
93
- /**
94
- * Create a new checkout session
95
- */
96
- async create(params) {
97
- const data = await this.apiClient.post(
98
- "/checkout",
99
- params
53
+ async update(id, data) {
54
+ return this.client.post(
55
+ `/api/client/checkout/update/${id}`,
56
+ data
100
57
  );
101
- if ("isError" in data && data.isError || !data || !("id" in data)) {
102
- throw new Error("Failed to create checkout session");
103
- }
104
- return data;
105
58
  }
106
- /**
107
- * Retrieve a checkout session by ID
108
- */
109
- async retrieve(checkoutId) {
110
- const data = await this.apiClient.get(
111
- `/checkout/${checkoutId}`
59
+ async getShippingRates(id) {
60
+ return this.client.get(
61
+ `/api/client/checkout/${id}/get-shipping-rates`
112
62
  );
113
- if ("isError" in data && data.isError || !data || !("id" in data)) {
114
- console.error(`Checkout session with id ${checkoutId} not found`);
115
- return null;
116
- }
117
- return data;
118
63
  }
119
- /**
120
- * Update a checkout session
121
- */
122
- async update(checkoutId, params) {
123
- const data = await this.apiClient.post(
124
- `/checkout/${checkoutId}/update`,
125
- params
64
+ async generatePaymentSecret(id) {
65
+ return this.client.post(
66
+ `/api/client/checkout/payment-secret/${id}`
126
67
  );
127
- if ("isError" in data && data.isError || !data || !("id" in data)) {
128
- console.error(`Checkout session with id ${checkoutId} not found`);
129
- return null;
130
- }
131
- return data;
132
68
  }
133
- /**
134
- * Apply a discount code to a checkout session
135
- */
136
- async applyDiscountCode(checkoutId, discountCode) {
137
- const data = await this.apiClient.post(
138
- `/checkout/${checkoutId}/discounts/apply`,
139
- { code: discountCode }
69
+ async revalidateDiscounts(id) {
70
+ return this.client.get(
71
+ `/api/client/checkout/${id}/discounts/revalidate`
140
72
  );
141
- if ("isError" in data && data.isError || !data || !("id" in data)) {
142
- throw new Error("Failed to apply discount code");
143
- }
144
- return data;
145
73
  }
146
- /**
147
- * Remove a discount from a checkout session
148
- */
149
- async removeDiscount(checkoutId, discountId) {
150
- const data = await this.apiClient.delete(
151
- `/checkout/${checkoutId}/discounts/remove/${discountId}`
74
+ async applyDiscount(id, data) {
75
+ return this.client.post(
76
+ `/api/client/checkout/${id}/discounts/apply`,
77
+ data
152
78
  );
153
- if ("isError" in data && data.isError || !data || !("id" in data)) {
154
- console.error(`Checkout session with id ${checkoutId} not found`);
155
- return null;
156
- }
157
- return data;
158
79
  }
159
- /**
160
- * Revalidate a checkout session
161
- */
162
- async revalidateDiscounts(checkoutId) {
163
- const data = await this.apiClient.get(
164
- `/checkout/${checkoutId}/discounts/revalidate`
80
+ async removeDiscount(id, discountId) {
81
+ return this.client.delete(
82
+ `/api/client/checkout/${id}/discounts/remove/${discountId}`
165
83
  );
166
- if ("isError" in data && data.isError || !data || !("id" in data)) {
167
- console.error(`Checkout session with id ${checkoutId} not found`);
168
- return null;
169
- }
170
- return data;
171
84
  }
172
- /**
173
- * Get shipping rates for a checkout session
174
- */
175
- async getShippingRates(checkoutId) {
176
- const data = await this.apiClient.get(
177
- `/checkout/${checkoutId}/shipping/rates`
85
+ };
86
+
87
+ // src/namespaces/client/client-customers.ts
88
+ var ClientCustomersNamespace = class {
89
+ constructor(client) {
90
+ this.client = client;
91
+ }
92
+ async upsert(data) {
93
+ return this.client.post(
94
+ "/api/client/customers/upsert",
95
+ data
178
96
  );
179
- console.log("SHIPPING RATES DATA: ", data);
180
- if ("isError" in data && data.isError || !data) {
181
- console.error("Failed to get shipping rates: ", data);
182
- return {};
183
- }
184
- return data;
185
97
  }
186
- /**
187
- * Generate payment secret for a checkout session
188
- */
189
- async generatePaymentSecret(checkoutId) {
190
- const data = await this.apiClient.post(`
191
- /checkout/${checkoutId}/payment`);
192
- if ("isError" in data && data.isError || !data || !("paymentSecret" in data)) {
193
- throw new Error("Failed to generate payment secret");
194
- }
195
- return {
196
- paymentSecret: data.paymentSecret,
197
- publicKey: data.publicKey,
198
- checkoutSession: data.checkoutSession
199
- };
98
+ async update(id, data) {
99
+ return this.client.post(
100
+ `/api/client/customers/update/${id}`,
101
+ data
102
+ );
200
103
  }
201
104
  };
202
- var checkout_default = Checkout;
203
105
 
204
- // src/client/index.ts
205
- var Client = class {
206
- proxy;
207
- constructor(proxy) {
208
- this.proxy = proxy;
106
+ // src/namespaces/client/client-utils.ts
107
+ var ClientUtilsNamespace = class {
108
+ constructor(client) {
109
+ this.client = client;
110
+ }
111
+ async getExchangeRates(currency) {
112
+ return this.client.get(`/api/client/utils/get-exchange-rates/${currency}`);
113
+ }
114
+ async autosuggestAddress(params) {
115
+ const { query, locale, latitude, longitude, countryCode } = params;
116
+ return this.client.get(`/api/client/utils/autosuggest-address`, {
117
+ query,
118
+ locale,
119
+ latitude,
120
+ longitude,
121
+ countryCode
122
+ });
209
123
  }
210
- /**
211
- * Retrieve a checkout session by ID
212
- */
213
- async retrieveCheckout(clientSecret, checkoutId) {
214
- const apiClient = createApiClient(clientSecret, this.proxy);
215
- const data = await apiClient.get(
216
- `/checkout/${checkoutId}`
124
+ async lookupAddressById(id, params = {}) {
125
+ return this.client.get(
126
+ `/api/client/utils/lookup-address-by-id/${id}`,
127
+ params
217
128
  );
218
- if ("isError" in data && data.isError || !data || !("id" in data)) {
219
- console.error(`Checkout session with id ${checkoutId} not found`);
220
- return null;
221
- }
222
- return data;
223
129
  }
224
- /**
225
- * Update a checkout session
226
- */
227
- async updateCheckout(clientSecret, checkoutId, params) {
228
- const apiClient = createApiClient(clientSecret, this.proxy);
229
- const data = await apiClient.post(
230
- `/checkout/${checkoutId}/update`,
231
- { ...params }
232
- );
233
- if ("isError" in data && data.isError || !data || !("id" in data)) {
234
- console.error(`Checkout session with id ${checkoutId} not found`);
235
- return null;
130
+ };
131
+
132
+ // src/client-factory.ts
133
+ function createBetterStoreClient(clientSecret, config) {
134
+ const baseClient = new BaseClient({
135
+ baseUrl: config?.proxy || "https://api.betterstore.com",
136
+ headers: {
137
+ Authorization: `Bearer ${clientSecret}`,
138
+ "Content-Type": "application/json"
236
139
  }
237
- return data;
140
+ });
141
+ return {
142
+ checkout: new ClientCheckoutNamespace(baseClient),
143
+ customers: new ClientCustomersNamespace(baseClient),
144
+ utils: new ClientUtilsNamespace(baseClient)
145
+ };
146
+ }
147
+
148
+ // src/namespaces/auth.ts
149
+ var AuthNamespace = class {
150
+ constructor(client) {
151
+ this.client = client;
238
152
  }
239
- /**
240
- * Apply a discount code to a checkout session
241
- */
242
- async applyDiscountCode(clientSecret, checkoutId, discountCode) {
243
- const apiClient = createApiClient(clientSecret, this.proxy);
244
- const data = await apiClient.post(
245
- `/checkout/${checkoutId}/discounts/apply`,
246
- { code: discountCode }
247
- );
248
- if ("isError" in data && data.isError || !data || !("id" in data)) {
249
- throw new Error("Failed to apply discount code");
250
- }
251
- return data;
153
+ async loginCustomerOtp(params) {
154
+ return this.client.post("/api/auth/login-customer-otp", params);
252
155
  }
253
- /**
254
- * Remove a discount code from a checkout session
255
- */
256
- async removeDiscount(clientSecret, checkoutId, discountId) {
257
- const apiClient = createApiClient(clientSecret, this.proxy);
258
- const data = await apiClient.delete(
259
- `/checkout/${checkoutId}/discounts/remove/${discountId}`
260
- );
261
- if ("isError" in data && data.isError || !data || !("id" in data)) {
262
- throw new Error("Failed to remove discount code");
263
- }
264
- return data;
156
+ async signupCustomerOtp(params) {
157
+ return this.client.post("/api/auth/signup-customer-otp", params);
265
158
  }
266
- /**
267
- * Revalidate a checkout session
268
- */
269
- async revalidateDiscounts(clientSecret, checkoutId) {
270
- const apiClient = createApiClient(clientSecret, this.proxy);
271
- const data = await apiClient.get(
272
- `/checkout/${checkoutId}/discounts/revalidate`
273
- );
274
- if ("isError" in data && data.isError || !data || !("id" in data)) {
275
- console.error(`Checkout session with id ${checkoutId} not found`);
276
- return null;
277
- }
278
- return data;
159
+ async verifyCustomerOtp(params) {
160
+ return this.client.post("/api/auth/verify-customer-otp", params);
279
161
  }
280
- /**
281
- * Get shipping rates for a checkout session
282
- */
283
- async getCheckoutShippingRates(clientSecret, checkoutId) {
284
- const apiClient = createApiClient(clientSecret, this.proxy);
285
- const data = await apiClient.get(
286
- `/checkout/${checkoutId}/shipping/rates`
162
+ async getCustomerSession(id) {
163
+ return this.client.get(`/api/auth/get-customer-session/${id}`);
164
+ }
165
+ };
166
+
167
+ // src/namespaces/checkout.ts
168
+ var CheckoutNamespace = class {
169
+ constructor(client) {
170
+ this.client = client;
171
+ }
172
+ async create(data) {
173
+ return this.client.post(
174
+ "/api/checkout/create",
175
+ data
287
176
  );
288
- console.log("SHIPPING RATES DATA: ", data);
289
- if ("isError" in data && data.isError || !data) {
290
- console.error("Failed to get shipping rates: ", data);
291
- return {};
292
- }
293
- return data;
294
177
  }
295
- /**
296
- * Generate payment secret for a checkout session
297
- */
298
- async generateCheckoutPaymentSecret(clientSecret, checkoutId) {
299
- const apiClient = createApiClient(clientSecret, this.proxy);
300
- const data = await apiClient.post(`/checkout/${checkoutId}/payment`);
301
- if ("isError" in data && data.isError || !data || !("paymentSecret" in data)) {
302
- throw new Error("Failed to generate payment secret");
303
- }
304
- return {
305
- paymentSecret: data.paymentSecret,
306
- publicKey: data.publicKey,
307
- checkoutSession: data.checkoutSession
308
- };
178
+ async get(id) {
179
+ return this.client.get(`/api/checkout/get/${id}`);
309
180
  }
310
- /**
311
- * Create a new customer
312
- */
313
- async createCustomer(clientSecret, params) {
314
- const apiClient = createApiClient(clientSecret, this.proxy);
315
- const data = await apiClient.post(
316
- "/customer",
317
- params
181
+ async update(id, data) {
182
+ return this.client.post(
183
+ `/api/checkout/update/${id}`,
184
+ data
318
185
  );
319
- if ("isError" in data && data.isError || !data || !("id" in data)) {
320
- throw new Error("Failed to create customer");
321
- }
322
- return data;
323
186
  }
324
- /**
325
- * Retrieve a customer by ID or email
326
- */
327
- async retrieveCustomer(clientSecret, idOrEmail) {
328
- const apiClient = createApiClient(clientSecret, this.proxy);
329
- const data = await apiClient.get(
330
- `/customer/${idOrEmail}`
187
+ async getShippingRates(id) {
188
+ return this.client.get(
189
+ `/api/checkout/${id}/get-shipping-rates`
331
190
  );
332
- if ("isError" in data && data.isError || !data || !("id" in data)) {
333
- console.error(`Customer with id or email ${idOrEmail} not found`);
334
- return null;
335
- }
336
- return data;
337
191
  }
338
- /**
339
- * Update a customer
340
- */
341
- async updateCustomer(clientSecret, customerId, params) {
342
- const apiClient = createApiClient(clientSecret, this.proxy);
343
- const data = await apiClient.put(
344
- `/customer/${customerId}`,
345
- params
192
+ async generatePaymentSecret(id) {
193
+ return this.client.post(
194
+ `/api/checkout/payment-secret/${id}`
346
195
  );
347
- if ("isError" in data && data.isError || !data || !("id" in data)) {
348
- console.error(`Customer with id ${customerId} not found`);
349
- return null;
350
- }
351
- return data;
352
196
  }
353
- /**
354
- * Get exchange rate for a currency
355
- */
356
- async getExchangeRate(clientSecret, baseCurrency, targetCurrency) {
357
- const apiClient = createApiClient(clientSecret, this.proxy);
358
- const data = await apiClient.get(
359
- `/helpers/rates/${baseCurrency}`
197
+ async revalidateDiscounts(id) {
198
+ return this.client.get(
199
+ `/api/checkout/${id}/discounts/revalidate`
360
200
  );
361
- const rate = data.rates[targetCurrency];
362
- if (!rate) {
363
- throw new Error("Could not get exchange rate for target currency");
364
- }
365
- return rate;
366
201
  }
367
- /**
368
- * Get autosuggest address results
369
- */
370
- async getAutosuggestAddressResults(clientSecret, params) {
371
- const apiClient = createApiClient(clientSecret, this.proxy);
372
- const { data } = await apiClient.get("/helpers/address/autosuggest", {
373
- params
374
- });
375
- return data ?? [];
202
+ async applyDiscount(id, data) {
203
+ return this.client.post(
204
+ `/api/checkout/${id}/discounts/apply`,
205
+ data
206
+ );
376
207
  }
377
- async lookupAddress(clientSecret, params) {
378
- const apiClient = createApiClient(clientSecret, this.proxy);
379
- const { data } = await apiClient.get("/helpers/address/lookup", {
380
- params
381
- });
382
- return data;
208
+ async removeDiscount(id, discountId) {
209
+ return this.client.delete(
210
+ `/api/checkout/${id}/discounts/remove/${discountId}`
211
+ );
383
212
  }
384
213
  };
385
- var client_default = Client;
386
214
 
387
- // src/collections/index.ts
388
- var Collections = class {
389
- apiClient;
390
- constructor(apiKey, proxy) {
391
- this.apiClient = createApiClient(apiKey, proxy);
215
+ // src/namespaces/collections.ts
216
+ var CollectionsNamespace = class {
217
+ constructor(client) {
218
+ this.client = client;
392
219
  }
393
220
  async list(params) {
394
- const data = await this.apiClient.post(`/collections`, params ?? {});
395
- if (!data || "isError" in data && data.isError || !("collections" in data)) {
396
- return [];
397
- }
398
- return data.collections;
221
+ return this.client.post(
222
+ "/api/collections/find-many",
223
+ params
224
+ );
399
225
  }
400
- async retrieve(params) {
401
- const data = await this.apiClient.post(`/collections/retrieve`, params);
402
- if ("isError" in data && data.isError || !data || !("collection" in data)) {
403
- console.error(`Collection not found`);
404
- return null;
405
- }
406
- return data.collection;
226
+ async findFirst(params) {
227
+ return this.client.post(
228
+ "/api/collections/find-first",
229
+ params
230
+ );
407
231
  }
408
232
  };
409
- var collections_default = Collections;
410
233
 
411
- // src/customer/index.ts
412
- var Customer = class {
413
- apiClient;
414
- constructor(apiKey, proxy) {
415
- this.apiClient = createApiClient(apiKey, proxy);
416
- }
417
- /**
418
- * Create a new customer
419
- */
420
- async create(params) {
421
- const data = await this.apiClient.post(
422
- "/customer",
423
- params
234
+ // src/namespaces/customers.ts
235
+ var CustomersNamespace = class {
236
+ constructor(client) {
237
+ this.client = client;
238
+ }
239
+ async upsert(data) {
240
+ return this.client.post(
241
+ "/api/customers/upsert",
242
+ data
424
243
  );
425
- if ("isError" in data && data.isError || !data || !("id" in data)) {
426
- throw new Error("Failed to create customer");
427
- }
428
- return data;
429
244
  }
430
- /**
431
- * Retrieve a customer by ID or email
432
- */
433
- async retrieve(idOrEmail) {
434
- const data = await this.apiClient.get(
435
- `/customer/${idOrEmail}`
245
+ async findFirst(idOrEmail) {
246
+ return this.client.get(
247
+ `/api/customers/find-first/${idOrEmail}`
436
248
  );
437
- if ("isError" in data && data.isError || !data || !("id" in data)) {
438
- console.error(`Customer with id or email ${idOrEmail} not found`);
439
- return null;
440
- }
441
- return data;
442
249
  }
443
- /**
444
- * Update a customer
445
- */
446
- async update(customerId, params) {
447
- const data = await this.apiClient.put(
448
- `/customer/${customerId}`,
449
- params
250
+ async update(id, data) {
251
+ return this.client.post(
252
+ `/api/customers/update/${id}`,
253
+ data
450
254
  );
451
- if ("isError" in data && data.isError || !data || !("id" in data)) {
452
- console.error(`Customer with id ${customerId} not found`);
453
- return null;
454
- }
455
- return data;
456
- }
457
- /**
458
- * Delete a customer
459
- */
460
- async delete(customerId) {
461
- await this.apiClient.delete(`/customer/${customerId}`);
462
255
  }
463
- /**
464
- * Update a customer subscription
465
- */
466
- async updateCustomerSubscription(stripeSubscriptionId, params) {
467
- const data = await this.apiClient.put(
468
- `/customer/subscription/${stripeSubscriptionId}`,
469
- params
256
+ async delete(id) {
257
+ return this.client.delete(
258
+ `/api/customers/delete/${id}`
470
259
  );
471
- if ("isError" in data && data.isError || !data || !("id" in data)) {
472
- return null;
473
- }
474
- return data;
475
260
  }
476
261
  };
477
- var customer_default = Customer;
478
262
 
479
- // src/discounts/index.ts
480
- var Discounts = class {
481
- apiClient;
482
- constructor(apiKey, proxy) {
483
- this.apiClient = createApiClient(apiKey, proxy);
263
+ // src/namespaces/products.ts
264
+ var ProductsNamespace = class {
265
+ constructor(client) {
266
+ this.client = client;
484
267
  }
268
+ /**
269
+ * List products with optional filtering, sorting, and pagination
270
+ */
485
271
  async list(params) {
486
- const data = await this.apiClient.post(
487
- "/discounts",
488
- params ?? {}
272
+ return this.client.post(
273
+ "/api/products/find-many",
274
+ params
489
275
  );
490
- if (!data || "isError" in data && data.isError || !("discounts" in data)) {
491
- return [];
492
- }
493
- return data.discounts;
494
276
  }
495
- async retrieve(params) {
496
- const data = await this.apiClient.post(
497
- `/discounts/retrieve`,
277
+ /**
278
+ * Find the first product matching the criteria
279
+ */
280
+ async findFirst(params) {
281
+ return this.client.post(
282
+ "/api/products/find-first",
498
283
  params
499
284
  );
500
- if ("isError" in data && data.isError || !data || !("discount" in data)) {
501
- console.error(`Discount not found`);
502
- return null;
503
- }
504
- return data.discount;
505
285
  }
506
286
  };
507
- var discounts_default = Discounts;
508
287
 
509
- // src/products/index.ts
510
- var Products = class {
511
- apiClient;
512
- constructor(apiKey, proxy) {
513
- this.apiClient = createApiClient(apiKey, proxy);
514
- }
515
- async list(params) {
516
- const data = await this.apiClient.post(
517
- "/products",
518
- params ?? {}
519
- );
520
- if (!data || "isError" in data && data.isError || !("products" in data)) {
521
- return [];
522
- }
523
- return data.products;
288
+ // src/namespaces/utils.ts
289
+ var UtilsNamespace = class {
290
+ constructor(client) {
291
+ this.client = client;
292
+ }
293
+ async getExchangeRates(currency) {
294
+ return this.client.get(`/api/utils/get-exchange-rates/${currency}`);
295
+ }
296
+ async autosuggestAddress(params) {
297
+ const { query, locale, latitude, longitude, countryCode } = params;
298
+ return this.client.get(`/api/utils/autosuggest-address`, {
299
+ query,
300
+ locale,
301
+ latitude,
302
+ longitude,
303
+ countryCode
304
+ });
524
305
  }
525
- async retrieve(params) {
526
- const data = await this.apiClient.post("/products/retrieve", params);
527
- if ("isError" in data && data.isError || !data || !("product" in data)) {
528
- console.error(`Product not found`);
529
- return null;
530
- }
531
- return data.product;
306
+ async lookupAddressById(id, params = {}) {
307
+ return this.client.get(`/api/utils/lookup-address-by-id/${id}`, params);
532
308
  }
533
309
  };
534
- var products_default = Products;
535
310
 
536
- // src/types-and-methods.ts
537
- import { formatCurrency, formatPrice } from "@betterstore/bridge";
538
-
539
- // src/methods/cart-discounts.ts
540
- function getHighestDiscount(productId, priceInCents, discounts) {
541
- const applicableDiscounts = discounts.filter(
542
- (discount) => discount.allowedProductIDs.includes(productId)
543
- );
544
- const bestDiscount = applicableDiscounts.length > 1 ? applicableDiscounts.reduce((bestSoFar, currentDiscount) => {
545
- let currentDiscountValueInCents = currentDiscount.value;
546
- if (currentDiscount.valueType === "PERCENTAGE") {
547
- currentDiscountValueInCents = currentDiscount.value / 100 * priceInCents;
548
- } else if (currentDiscount.valueType === "FREE") {
549
- currentDiscountValueInCents = priceInCents;
311
+ // src/main-factory.ts
312
+ function createBetterStore(secret, config) {
313
+ const baseClient = new BaseClient({
314
+ baseUrl: config?.proxy || "https://v1.betterstore.io",
315
+ headers: {
316
+ Authorization: `Bearer ${secret}`,
317
+ "Content-Type": "application/json"
550
318
  }
551
- return (bestSoFar?.value ?? 0) > currentDiscountValueInCents ? bestSoFar : currentDiscount;
552
- }, applicableDiscounts[0]) : applicableDiscounts[0];
553
- return bestDiscount;
554
- }
555
- function calculateDiscountAmount(originalPrice, discount) {
556
- let discountValueInCents = discount?.value;
557
- const isFreeDiscount = discount?.valueType === "FREE";
558
- if (discount?.valueType === "PERCENTAGE") {
559
- discountValueInCents = discount.value / 100 * originalPrice;
560
- } else if (discount?.valueType === "FREE") {
561
- discountValueInCents = originalPrice;
562
- }
563
- const finalPrice = discountValueInCents ? Math.max(originalPrice - discountValueInCents, 0) : originalPrice;
564
- const isDiscounted = discountValueInCents === 0;
565
- return {
566
- finalPrice,
567
- discountAmount: discountValueInCents ?? 0,
568
- originalPrice,
569
- isFree: isFreeDiscount,
570
- isDiscounted
571
- };
572
- }
573
- function findAutomaticProductDiscount({
574
- productId,
575
- priceInCents,
576
- discounts
577
- }) {
578
- const discount = getHighestDiscount(productId, priceInCents, discounts);
579
- const result = calculateDiscountAmount(priceInCents, discount);
580
- return { ...result, discount };
581
- }
582
-
583
- // src/index.ts
584
- function createBetterStore(config) {
585
- if (!config.apiKey) {
586
- throw new Error("API key is required.");
587
- }
319
+ });
588
320
  return {
589
- checkout: new checkout_default(config.apiKey, config.proxy),
590
- customer: new customer_default(config.apiKey, config.proxy),
591
- discounts: new discounts_default(config.apiKey, config.proxy),
592
- collections: new collections_default(config.apiKey, config.proxy),
593
- products: new products_default(config.apiKey, config.proxy),
594
- auth: new auth_default(config.apiKey, config.proxy)
321
+ products: new ProductsNamespace(baseClient),
322
+ collections: new CollectionsNamespace(baseClient),
323
+ customers: new CustomersNamespace(baseClient),
324
+ auth: new AuthNamespace(baseClient),
325
+ checkout: new CheckoutNamespace(baseClient),
326
+ utils: new UtilsNamespace(baseClient)
595
327
  };
596
328
  }
597
- function createStoreClient(config) {
598
- return new client_default(config?.proxy);
599
- }
600
329
  export {
601
- createStoreClient,
602
- createBetterStore as default,
603
- findAutomaticProductDiscount,
604
- formatCurrency,
605
- formatPrice
330
+ createBetterStore,
331
+ createBetterStoreClient
606
332
  };