@betterstore/sdk 0.6.13 → 0.7.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 +14339 -222
- package/dist/index.d.ts +14339 -222
- package/dist/index.js +254 -537
- package/dist/index.mjs +252 -523
- package/package.json +4 -3
package/dist/index.mjs
CHANGED
|
@@ -1,606 +1,335 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
headers: {
|
|
1
|
+
// src/base-client.ts
|
|
2
|
+
var DEFAULT_BASE_URL = "https://v1.betterstore.io";
|
|
3
|
+
var BaseClient = class {
|
|
4
|
+
constructor(config) {
|
|
5
|
+
this.config = config;
|
|
6
|
+
this.headers = {
|
|
8
7
|
"Content-Type": "application/json",
|
|
9
|
-
Authorization: `Bearer ${
|
|
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;
|
|
19
|
-
});
|
|
8
|
+
Authorization: `Bearer ${this.config.secret}`
|
|
9
|
+
};
|
|
20
10
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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;
|
|
11
|
+
headers;
|
|
12
|
+
async get(path, params) {
|
|
13
|
+
const url = `${this.config.baseUrl}${path}`;
|
|
14
|
+
const searchParams = new URLSearchParams();
|
|
15
|
+
if (params) {
|
|
16
|
+
Object.entries(params).forEach(([key, val]) => {
|
|
17
|
+
if (val !== void 0) searchParams.set(key, val);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
console.log(`${url}?${searchParams.toString()}`);
|
|
21
|
+
const res = await fetch(`${url}?${searchParams.toString()}`, {
|
|
22
|
+
method: "GET",
|
|
23
|
+
headers: this.headers
|
|
24
|
+
});
|
|
25
|
+
return this.handleResponse(res);
|
|
49
26
|
}
|
|
50
|
-
async
|
|
51
|
-
const
|
|
52
|
-
"
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
27
|
+
async post(path, body) {
|
|
28
|
+
const res = await fetch(`${this.config.baseUrl}${path}`, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: this.headers,
|
|
31
|
+
body: body ? JSON.stringify(body) : void 0
|
|
32
|
+
});
|
|
33
|
+
return this.handleResponse(res);
|
|
56
34
|
}
|
|
57
|
-
async
|
|
58
|
-
const
|
|
59
|
-
"
|
|
60
|
-
|
|
61
|
-
);
|
|
62
|
-
return
|
|
35
|
+
async delete(path) {
|
|
36
|
+
const res = await fetch(`${this.config.baseUrl}${path}`, {
|
|
37
|
+
method: "DELETE",
|
|
38
|
+
headers: this.headers
|
|
39
|
+
});
|
|
40
|
+
return this.handleResponse(res);
|
|
63
41
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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;
|
|
42
|
+
async handleResponse(res) {
|
|
43
|
+
if (!res.ok) {
|
|
44
|
+
const error = await res.json().catch(() => ({ message: res.statusText, data: res.body }));
|
|
45
|
+
console.log(error);
|
|
46
|
+
throw new Error(error.message || `HTTP ${res.status}`);
|
|
81
47
|
}
|
|
82
|
-
return
|
|
48
|
+
return res.json();
|
|
83
49
|
}
|
|
84
50
|
};
|
|
85
|
-
var auth_default = Auth;
|
|
86
51
|
|
|
87
|
-
// src/checkout
|
|
88
|
-
var
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
52
|
+
// src/namespaces/client/client-checkout.ts
|
|
53
|
+
var ClientCheckoutNamespace = class {
|
|
54
|
+
constructor(client) {
|
|
55
|
+
this.client = client;
|
|
56
|
+
}
|
|
57
|
+
async get(id) {
|
|
58
|
+
return this.client.get(
|
|
59
|
+
`/api/client/checkout/get/${id}`
|
|
60
|
+
);
|
|
92
61
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const data = await this.apiClient.post(
|
|
98
|
-
"/checkout",
|
|
99
|
-
params
|
|
62
|
+
async update(id, data) {
|
|
63
|
+
return this.client.post(
|
|
64
|
+
`/api/client/checkout/update/${id}`,
|
|
65
|
+
data
|
|
100
66
|
);
|
|
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
67
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
async retrieve(checkoutId) {
|
|
110
|
-
const data = await this.apiClient.get(
|
|
111
|
-
`/checkout/${checkoutId}`
|
|
68
|
+
async getShippingRates(id) {
|
|
69
|
+
return this.client.get(
|
|
70
|
+
`/api/client/checkout/${id}/get-shipping-rates`
|
|
112
71
|
);
|
|
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
72
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
async update(checkoutId, params) {
|
|
123
|
-
const data = await this.apiClient.post(
|
|
124
|
-
`/checkout/${checkoutId}/update`,
|
|
125
|
-
params
|
|
73
|
+
async generatePaymentSecret(id) {
|
|
74
|
+
return this.client.post(
|
|
75
|
+
`/api/client/checkout/payment-secret/${id}`
|
|
126
76
|
);
|
|
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
77
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
async applyDiscountCode(checkoutId, discountCode) {
|
|
137
|
-
const data = await this.apiClient.post(
|
|
138
|
-
`/checkout/${checkoutId}/discounts/apply`,
|
|
139
|
-
{ code: discountCode }
|
|
78
|
+
async revalidateDiscounts(id) {
|
|
79
|
+
return this.client.get(
|
|
80
|
+
`/api/client/checkout/${id}/discounts/revalidate`
|
|
140
81
|
);
|
|
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
82
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const data = await this.apiClient.delete(
|
|
151
|
-
`/checkout/${checkoutId}/discounts/remove/${discountId}`
|
|
83
|
+
async applyDiscount(id, data) {
|
|
84
|
+
return this.client.post(
|
|
85
|
+
`/api/client/checkout/${id}/discounts/apply`,
|
|
86
|
+
data
|
|
152
87
|
);
|
|
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
88
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
async revalidateDiscounts(checkoutId) {
|
|
163
|
-
const data = await this.apiClient.get(
|
|
164
|
-
`/checkout/${checkoutId}/discounts/revalidate`
|
|
89
|
+
async removeDiscount(id, discountId) {
|
|
90
|
+
return this.client.delete(
|
|
91
|
+
`/api/client/checkout/${id}/discounts/remove/${discountId}`
|
|
165
92
|
);
|
|
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
93
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/namespaces/client/client-customers.ts
|
|
97
|
+
var ClientCustomersNamespace = class {
|
|
98
|
+
constructor(client) {
|
|
99
|
+
this.client = client;
|
|
100
|
+
}
|
|
101
|
+
async upsert(data) {
|
|
102
|
+
return this.client.post(
|
|
103
|
+
"/api/client/customers/upsert",
|
|
104
|
+
data
|
|
178
105
|
);
|
|
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
106
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
};
|
|
107
|
+
async update(id, data) {
|
|
108
|
+
return this.client.post(
|
|
109
|
+
`/api/client/customers/update/${id}`,
|
|
110
|
+
data
|
|
111
|
+
);
|
|
200
112
|
}
|
|
201
113
|
};
|
|
202
|
-
var checkout_default = Checkout;
|
|
203
114
|
|
|
204
|
-
// src/client/
|
|
205
|
-
var
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
115
|
+
// src/namespaces/client/client-utils.ts
|
|
116
|
+
var ClientUtilsNamespace = class {
|
|
117
|
+
constructor(client) {
|
|
118
|
+
this.client = client;
|
|
119
|
+
}
|
|
120
|
+
async getExchangeRates(currency) {
|
|
121
|
+
return this.client.get(`/api/client/utils/get-exchange-rates/${currency}`);
|
|
122
|
+
}
|
|
123
|
+
async autosuggestAddress(params) {
|
|
124
|
+
const { query, locale, latitude, longitude, countryCode } = params;
|
|
125
|
+
return this.client.get(`/api/client/utils/autosuggest-address`, {
|
|
126
|
+
query,
|
|
127
|
+
locale,
|
|
128
|
+
latitude,
|
|
129
|
+
longitude,
|
|
130
|
+
countryCode
|
|
131
|
+
});
|
|
209
132
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const apiClient = createApiClient(clientSecret, this.proxy);
|
|
215
|
-
const data = await apiClient.get(
|
|
216
|
-
`/checkout/${checkoutId}`
|
|
133
|
+
async lookupAddressById(id, params = {}) {
|
|
134
|
+
return this.client.get(
|
|
135
|
+
`/api/client/utils/lookup-address-by-id/${id}`,
|
|
136
|
+
params
|
|
217
137
|
);
|
|
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
138
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// src/client-factory.ts
|
|
142
|
+
function createBetterStoreClient(checkoutSecret, config) {
|
|
143
|
+
const baseClient = new BaseClient({
|
|
144
|
+
baseUrl: config?.proxy || DEFAULT_BASE_URL,
|
|
145
|
+
secret: checkoutSecret
|
|
146
|
+
});
|
|
147
|
+
return {
|
|
148
|
+
checkout: new ClientCheckoutNamespace(baseClient),
|
|
149
|
+
customers: new ClientCustomersNamespace(baseClient),
|
|
150
|
+
utils: new ClientUtilsNamespace(baseClient)
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/namespaces/auth.ts
|
|
155
|
+
var AuthNamespace = class {
|
|
156
|
+
constructor(client) {
|
|
157
|
+
this.client = client;
|
|
238
158
|
}
|
|
239
|
-
|
|
240
|
-
|
|
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;
|
|
159
|
+
async loginCustomerOtp(params) {
|
|
160
|
+
return this.client.post("/api/auth/login-customer-otp", params);
|
|
252
161
|
}
|
|
253
|
-
|
|
254
|
-
|
|
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;
|
|
162
|
+
async signupCustomerOtp(params) {
|
|
163
|
+
return this.client.post("/api/auth/signup-customer-otp", params);
|
|
265
164
|
}
|
|
266
|
-
|
|
267
|
-
|
|
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;
|
|
165
|
+
async verifyCustomerOtp(params) {
|
|
166
|
+
return this.client.post("/api/auth/verify-customer-otp", params);
|
|
279
167
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
168
|
+
async getCustomerSession(id) {
|
|
169
|
+
return this.client.get(`/api/auth/get-customer-session/${id}`);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// src/namespaces/checkout.ts
|
|
174
|
+
var CheckoutNamespace = class {
|
|
175
|
+
constructor(client) {
|
|
176
|
+
this.client = client;
|
|
177
|
+
}
|
|
178
|
+
async create(data) {
|
|
179
|
+
return this.client.post(
|
|
180
|
+
"/api/checkout/create",
|
|
181
|
+
data
|
|
287
182
|
);
|
|
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
183
|
}
|
|
295
|
-
|
|
296
|
-
|
|
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
|
-
};
|
|
184
|
+
async get(id) {
|
|
185
|
+
return this.client.get(`/api/checkout/get/${id}`);
|
|
309
186
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
const apiClient = createApiClient(clientSecret, this.proxy);
|
|
315
|
-
const data = await apiClient.post(
|
|
316
|
-
"/customer",
|
|
317
|
-
params
|
|
187
|
+
async update(id, data) {
|
|
188
|
+
return this.client.post(
|
|
189
|
+
`/api/checkout/update/${id}`,
|
|
190
|
+
data
|
|
318
191
|
);
|
|
319
|
-
if ("isError" in data && data.isError || !data || !("id" in data)) {
|
|
320
|
-
throw new Error("Failed to create customer");
|
|
321
|
-
}
|
|
322
|
-
return data;
|
|
323
192
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
async retrieveCustomer(clientSecret, idOrEmail) {
|
|
328
|
-
const apiClient = createApiClient(clientSecret, this.proxy);
|
|
329
|
-
const data = await apiClient.get(
|
|
330
|
-
`/customer/${idOrEmail}`
|
|
193
|
+
async getShippingRates(id) {
|
|
194
|
+
return this.client.get(
|
|
195
|
+
`/api/checkout/${id}/get-shipping-rates`
|
|
331
196
|
);
|
|
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
197
|
}
|
|
338
|
-
|
|
339
|
-
|
|
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
|
|
198
|
+
async generatePaymentSecret(id) {
|
|
199
|
+
return this.client.post(
|
|
200
|
+
`/api/checkout/payment-secret/${id}`
|
|
346
201
|
);
|
|
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
202
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
async getExchangeRate(clientSecret, baseCurrency, targetCurrency) {
|
|
357
|
-
const apiClient = createApiClient(clientSecret, this.proxy);
|
|
358
|
-
const data = await apiClient.get(
|
|
359
|
-
`/helpers/rates/${baseCurrency}`
|
|
203
|
+
async revalidateDiscounts(id) {
|
|
204
|
+
return this.client.get(
|
|
205
|
+
`/api/checkout/${id}/discounts/revalidate`
|
|
360
206
|
);
|
|
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
207
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
const { data } = await apiClient.get("/helpers/address/autosuggest", {
|
|
373
|
-
params
|
|
374
|
-
});
|
|
375
|
-
return data ?? [];
|
|
208
|
+
async applyDiscount(id, data) {
|
|
209
|
+
return this.client.post(
|
|
210
|
+
`/api/checkout/${id}/discounts/apply`,
|
|
211
|
+
data
|
|
212
|
+
);
|
|
376
213
|
}
|
|
377
|
-
async
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
});
|
|
382
|
-
return data;
|
|
214
|
+
async removeDiscount(id, discountId) {
|
|
215
|
+
return this.client.delete(
|
|
216
|
+
`/api/checkout/${id}/discounts/remove/${discountId}`
|
|
217
|
+
);
|
|
383
218
|
}
|
|
384
219
|
};
|
|
385
|
-
var client_default = Client;
|
|
386
220
|
|
|
387
|
-
// src/collections
|
|
388
|
-
var
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
this.apiClient = createApiClient(apiKey, proxy);
|
|
221
|
+
// src/namespaces/collections.ts
|
|
222
|
+
var CollectionsNamespace = class {
|
|
223
|
+
constructor(client) {
|
|
224
|
+
this.client = client;
|
|
392
225
|
}
|
|
393
226
|
async list(params) {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
return data.collections;
|
|
227
|
+
return this.client.post(
|
|
228
|
+
"/api/collections/find-many",
|
|
229
|
+
params
|
|
230
|
+
);
|
|
399
231
|
}
|
|
400
|
-
async
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
}
|
|
406
|
-
return data.collection;
|
|
232
|
+
async findFirst(params) {
|
|
233
|
+
return this.client.post(
|
|
234
|
+
"/api/collections/find-first",
|
|
235
|
+
params
|
|
236
|
+
);
|
|
407
237
|
}
|
|
408
238
|
};
|
|
409
|
-
var collections_default = Collections;
|
|
410
239
|
|
|
411
|
-
// src/
|
|
412
|
-
var
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
async create(params) {
|
|
421
|
-
const data = await this.apiClient.post(
|
|
422
|
-
"/customer",
|
|
423
|
-
params
|
|
240
|
+
// src/namespaces/customers.ts
|
|
241
|
+
var CustomersNamespace = class {
|
|
242
|
+
constructor(client) {
|
|
243
|
+
this.client = client;
|
|
244
|
+
}
|
|
245
|
+
async upsert(data) {
|
|
246
|
+
return this.client.post(
|
|
247
|
+
"/api/customers/upsert",
|
|
248
|
+
data
|
|
424
249
|
);
|
|
425
|
-
if ("isError" in data && data.isError || !data || !("id" in data)) {
|
|
426
|
-
throw new Error("Failed to create customer");
|
|
427
|
-
}
|
|
428
|
-
return data;
|
|
429
250
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
async retrieve(idOrEmail) {
|
|
434
|
-
const data = await this.apiClient.get(
|
|
435
|
-
`/customer/${idOrEmail}`
|
|
251
|
+
async findFirst(idOrEmail) {
|
|
252
|
+
return this.client.get(
|
|
253
|
+
`/api/customers/find-first/${idOrEmail}`
|
|
436
254
|
);
|
|
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
255
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
const data = await this.apiClient.put(
|
|
448
|
-
`/customer/${customerId}`,
|
|
449
|
-
params
|
|
256
|
+
async update(id, data) {
|
|
257
|
+
return this.client.post(
|
|
258
|
+
`/api/customers/update/${id}`,
|
|
259
|
+
data
|
|
450
260
|
);
|
|
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
261
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
async updateCustomerSubscription(stripeSubscriptionId, params) {
|
|
467
|
-
const data = await this.apiClient.put(
|
|
468
|
-
`/customer/subscription/${stripeSubscriptionId}`,
|
|
469
|
-
params
|
|
262
|
+
async delete(id) {
|
|
263
|
+
return this.client.delete(
|
|
264
|
+
`/api/customers/delete/${id}`
|
|
470
265
|
);
|
|
471
|
-
if ("isError" in data && data.isError || !data || !("id" in data)) {
|
|
472
|
-
return null;
|
|
473
|
-
}
|
|
474
|
-
return data;
|
|
475
266
|
}
|
|
476
267
|
};
|
|
477
|
-
var customer_default = Customer;
|
|
478
268
|
|
|
479
|
-
// src/
|
|
480
|
-
var
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
this.apiClient = createApiClient(apiKey, proxy);
|
|
269
|
+
// src/namespaces/products.ts
|
|
270
|
+
var ProductsNamespace = class {
|
|
271
|
+
constructor(client) {
|
|
272
|
+
this.client = client;
|
|
484
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* List products with optional filtering, sorting, and pagination
|
|
276
|
+
*/
|
|
485
277
|
async list(params) {
|
|
486
|
-
|
|
487
|
-
"/
|
|
488
|
-
params
|
|
278
|
+
return this.client.post(
|
|
279
|
+
"/api/products/find-many",
|
|
280
|
+
params
|
|
489
281
|
);
|
|
490
|
-
if (!data || "isError" in data && data.isError || !("discounts" in data)) {
|
|
491
|
-
return [];
|
|
492
|
-
}
|
|
493
|
-
return data.discounts;
|
|
494
282
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
283
|
+
/**
|
|
284
|
+
* Find the first product matching the criteria
|
|
285
|
+
*/
|
|
286
|
+
async findFirst(params) {
|
|
287
|
+
return this.client.post(
|
|
288
|
+
"/api/products/find-first",
|
|
498
289
|
params
|
|
499
290
|
);
|
|
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
291
|
}
|
|
506
292
|
};
|
|
507
|
-
var discounts_default = Discounts;
|
|
508
293
|
|
|
509
|
-
// src/
|
|
510
|
-
var
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
294
|
+
// src/namespaces/utils.ts
|
|
295
|
+
var UtilsNamespace = class {
|
|
296
|
+
constructor(client) {
|
|
297
|
+
this.client = client;
|
|
298
|
+
}
|
|
299
|
+
async getExchangeRates(currency) {
|
|
300
|
+
return this.client.get(`/api/utils/get-exchange-rates/${currency}`);
|
|
301
|
+
}
|
|
302
|
+
async autosuggestAddress(params) {
|
|
303
|
+
const { query, locale, latitude, longitude, countryCode } = params;
|
|
304
|
+
return this.client.get(`/api/utils/autosuggest-address`, {
|
|
305
|
+
query,
|
|
306
|
+
locale,
|
|
307
|
+
latitude,
|
|
308
|
+
longitude,
|
|
309
|
+
countryCode
|
|
310
|
+
});
|
|
524
311
|
}
|
|
525
|
-
async
|
|
526
|
-
|
|
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;
|
|
312
|
+
async lookupAddressById(id, params = {}) {
|
|
313
|
+
return this.client.get(`/api/utils/lookup-address-by-id/${id}`, params);
|
|
532
314
|
}
|
|
533
315
|
};
|
|
534
|
-
var products_default = Products;
|
|
535
316
|
|
|
536
|
-
// src/
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
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;
|
|
550
|
-
}
|
|
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
|
-
}
|
|
317
|
+
// src/main-factory.ts
|
|
318
|
+
function createBetterStore(secret, config) {
|
|
319
|
+
const baseClient = new BaseClient({
|
|
320
|
+
baseUrl: config?.proxy || DEFAULT_BASE_URL,
|
|
321
|
+
secret
|
|
322
|
+
});
|
|
588
323
|
return {
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
324
|
+
products: new ProductsNamespace(baseClient),
|
|
325
|
+
collections: new CollectionsNamespace(baseClient),
|
|
326
|
+
customers: new CustomersNamespace(baseClient),
|
|
327
|
+
auth: new AuthNamespace(baseClient),
|
|
328
|
+
checkout: new CheckoutNamespace(baseClient),
|
|
329
|
+
utils: new UtilsNamespace(baseClient)
|
|
595
330
|
};
|
|
596
331
|
}
|
|
597
|
-
function createStoreClient(config) {
|
|
598
|
-
return new client_default(config?.proxy);
|
|
599
|
-
}
|
|
600
332
|
export {
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
findAutomaticProductDiscount,
|
|
604
|
-
formatCurrency,
|
|
605
|
-
formatPrice
|
|
333
|
+
createBetterStore,
|
|
334
|
+
createBetterStoreClient
|
|
606
335
|
};
|