@betterstore/sdk 0.2.8 → 0.2.10
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/.prettierignore +2 -2
- package/CHANGELOG.md +12 -0
- package/LICENSE +21 -21
- package/dist/index.d.mts +6 -20
- package/dist/index.d.ts +6 -20
- package/dist/index.js +8 -305
- package/dist/index.mjs +8 -304
- package/package.json +3 -3
package/.prettierignore
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
node_modules
|
|
2
|
-
dist
|
|
1
|
+
node_modules
|
|
2
|
+
dist
|
|
3
3
|
pnpm-lock.yaml
|
package/CHANGELOG.md
CHANGED
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Better Store
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Better Store
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { NextRequest } from 'next/server';
|
|
2
|
-
|
|
3
1
|
interface LineItem {
|
|
4
2
|
quantity: number;
|
|
5
3
|
productId?: string;
|
|
@@ -81,9 +79,9 @@ declare class Checkout {
|
|
|
81
79
|
*/
|
|
82
80
|
create(params: CheckoutCreateParams): Promise<CheckoutSession>;
|
|
83
81
|
/**
|
|
84
|
-
* Retrieve a checkout session by ID
|
|
82
|
+
* Retrieve a checkout session by ID
|
|
85
83
|
*/
|
|
86
|
-
retrieve(
|
|
84
|
+
retrieve(checkoutId: string): Promise<CheckoutSession>;
|
|
87
85
|
/**
|
|
88
86
|
* Update a checkout session
|
|
89
87
|
*/
|
|
@@ -204,21 +202,9 @@ declare class Products {
|
|
|
204
202
|
}
|
|
205
203
|
|
|
206
204
|
declare function getCheckoutEmbedProps(betterStore: BetterStore): {
|
|
207
|
-
retrieveCheckout:
|
|
208
|
-
updateCheckout:
|
|
209
|
-
getShippingRates:
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
type NextjsRouteConfig = {
|
|
213
|
-
apiKey?: string;
|
|
214
|
-
productionAllowedOrigins?: string[];
|
|
215
|
-
};
|
|
216
|
-
type BSClient = InstanceType<typeof BetterStore>;
|
|
217
|
-
declare function createNextJSHandler(betterStore: BSClient, config?: NextjsRouteConfig): {
|
|
218
|
-
GET(req: NextRequest): Promise<Response>;
|
|
219
|
-
POST(req: NextRequest): Promise<Response>;
|
|
220
|
-
PUT(req: NextRequest): Promise<Response>;
|
|
221
|
-
DELETE(req: NextRequest): Promise<Response>;
|
|
205
|
+
retrieveCheckout: InstanceType<typeof BetterStore>["checkout"]["retrieve"];
|
|
206
|
+
updateCheckout: InstanceType<typeof BetterStore>["checkout"]["update"];
|
|
207
|
+
getShippingRates: InstanceType<typeof BetterStore>["checkout"]["getShippingRates"];
|
|
222
208
|
};
|
|
223
209
|
|
|
224
210
|
declare class BetterStore {
|
|
@@ -228,4 +214,4 @@ declare class BetterStore {
|
|
|
228
214
|
constructor(apiKey: string);
|
|
229
215
|
}
|
|
230
216
|
|
|
231
|
-
export {
|
|
217
|
+
export { BetterStore as default, getCheckoutEmbedProps };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { NextRequest } from 'next/server';
|
|
2
|
-
|
|
3
1
|
interface LineItem {
|
|
4
2
|
quantity: number;
|
|
5
3
|
productId?: string;
|
|
@@ -81,9 +79,9 @@ declare class Checkout {
|
|
|
81
79
|
*/
|
|
82
80
|
create(params: CheckoutCreateParams): Promise<CheckoutSession>;
|
|
83
81
|
/**
|
|
84
|
-
* Retrieve a checkout session by ID
|
|
82
|
+
* Retrieve a checkout session by ID
|
|
85
83
|
*/
|
|
86
|
-
retrieve(
|
|
84
|
+
retrieve(checkoutId: string): Promise<CheckoutSession>;
|
|
87
85
|
/**
|
|
88
86
|
* Update a checkout session
|
|
89
87
|
*/
|
|
@@ -204,21 +202,9 @@ declare class Products {
|
|
|
204
202
|
}
|
|
205
203
|
|
|
206
204
|
declare function getCheckoutEmbedProps(betterStore: BetterStore): {
|
|
207
|
-
retrieveCheckout:
|
|
208
|
-
updateCheckout:
|
|
209
|
-
getShippingRates:
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
type NextjsRouteConfig = {
|
|
213
|
-
apiKey?: string;
|
|
214
|
-
productionAllowedOrigins?: string[];
|
|
215
|
-
};
|
|
216
|
-
type BSClient = InstanceType<typeof BetterStore>;
|
|
217
|
-
declare function createNextJSHandler(betterStore: BSClient, config?: NextjsRouteConfig): {
|
|
218
|
-
GET(req: NextRequest): Promise<Response>;
|
|
219
|
-
POST(req: NextRequest): Promise<Response>;
|
|
220
|
-
PUT(req: NextRequest): Promise<Response>;
|
|
221
|
-
DELETE(req: NextRequest): Promise<Response>;
|
|
205
|
+
retrieveCheckout: InstanceType<typeof BetterStore>["checkout"]["retrieve"];
|
|
206
|
+
updateCheckout: InstanceType<typeof BetterStore>["checkout"]["update"];
|
|
207
|
+
getShippingRates: InstanceType<typeof BetterStore>["checkout"]["getShippingRates"];
|
|
222
208
|
};
|
|
223
209
|
|
|
224
210
|
declare class BetterStore {
|
|
@@ -228,4 +214,4 @@ declare class BetterStore {
|
|
|
228
214
|
constructor(apiKey: string);
|
|
229
215
|
}
|
|
230
216
|
|
|
231
|
-
export {
|
|
217
|
+
export { BetterStore as default, getCheckoutEmbedProps };
|
package/dist/index.js
CHANGED
|
@@ -50,7 +50,6 @@ var __async = (__this, __arguments, generator) => {
|
|
|
50
50
|
// src/index.ts
|
|
51
51
|
var index_exports = {};
|
|
52
52
|
__export(index_exports, {
|
|
53
|
-
createNextJSHandler: () => createNextJSHandler,
|
|
54
53
|
default: () => index_default,
|
|
55
54
|
getCheckoutEmbedProps: () => getCheckoutEmbedProps
|
|
56
55
|
});
|
|
@@ -58,13 +57,16 @@ module.exports = __toCommonJS(index_exports);
|
|
|
58
57
|
|
|
59
58
|
// src/utils/axios.ts
|
|
60
59
|
var import_axios = __toESM(require("axios"));
|
|
61
|
-
var API_BASE_URL = "https://api.betterstore.io/
|
|
60
|
+
var API_BASE_URL = "https://api.betterstore.io/v1";
|
|
62
61
|
var createApiClient = (apiKey) => {
|
|
63
62
|
const client = import_axios.default.create({
|
|
64
63
|
baseURL: API_BASE_URL,
|
|
65
64
|
headers: {
|
|
66
65
|
"Content-Type": "application/json",
|
|
67
|
-
Authorization: `Bearer ${apiKey}
|
|
66
|
+
Authorization: `Bearer ${apiKey}`,
|
|
67
|
+
"Access-Control-Allow-Origin": "*",
|
|
68
|
+
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
|
|
69
|
+
"Access-Control-Allow-Headers": "Content-Type, Authorization"
|
|
68
70
|
}
|
|
69
71
|
});
|
|
70
72
|
client.interceptors.response.use(
|
|
@@ -113,12 +115,12 @@ var Checkout = class {
|
|
|
113
115
|
});
|
|
114
116
|
}
|
|
115
117
|
/**
|
|
116
|
-
* Retrieve a checkout session by ID
|
|
118
|
+
* Retrieve a checkout session by ID
|
|
117
119
|
*/
|
|
118
|
-
retrieve(
|
|
120
|
+
retrieve(checkoutId) {
|
|
119
121
|
return __async(this, null, function* () {
|
|
120
122
|
const data = yield this.apiClient.get(
|
|
121
|
-
`/checkout/${
|
|
123
|
+
`/checkout/${checkoutId}`
|
|
122
124
|
);
|
|
123
125
|
return data;
|
|
124
126
|
});
|
|
@@ -241,304 +243,6 @@ function getCheckoutEmbedProps(betterStore) {
|
|
|
241
243
|
};
|
|
242
244
|
}
|
|
243
245
|
|
|
244
|
-
// src/route-handelers/next-js.ts
|
|
245
|
-
var defaultBetterStoreRoutes = {
|
|
246
|
-
checkout: {
|
|
247
|
-
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
248
|
-
const { searchParams } = new URL(req.url);
|
|
249
|
-
const checkoutId = searchParams.get("checkoutId");
|
|
250
|
-
if (!checkoutId) {
|
|
251
|
-
return new Response("Checkout ID is required", { status: 400 });
|
|
252
|
-
}
|
|
253
|
-
try {
|
|
254
|
-
const checkout = yield betterStore.checkout.retrieve(checkoutId);
|
|
255
|
-
return Response.json(checkout);
|
|
256
|
-
} catch (error) {
|
|
257
|
-
return new Response("Failed to fetch checkout", { status: 500 });
|
|
258
|
-
}
|
|
259
|
-
}),
|
|
260
|
-
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
261
|
-
try {
|
|
262
|
-
const body = yield req.json();
|
|
263
|
-
const checkout = yield betterStore.checkout.create(body);
|
|
264
|
-
return Response.json(checkout);
|
|
265
|
-
} catch (error) {
|
|
266
|
-
return new Response("Failed to create checkout", { status: 500 });
|
|
267
|
-
}
|
|
268
|
-
}),
|
|
269
|
-
PUT: (req, betterStore) => __async(void 0, null, function* () {
|
|
270
|
-
const { searchParams } = new URL(req.url);
|
|
271
|
-
const checkoutId = searchParams.get("checkoutId");
|
|
272
|
-
if (!checkoutId) {
|
|
273
|
-
return new Response("Checkout ID is required", { status: 400 });
|
|
274
|
-
}
|
|
275
|
-
try {
|
|
276
|
-
const body = yield req.json();
|
|
277
|
-
const checkout = yield betterStore.checkout.update(checkoutId, body);
|
|
278
|
-
return Response.json(checkout);
|
|
279
|
-
} catch (error) {
|
|
280
|
-
return new Response("Failed to update checkout", { status: 500 });
|
|
281
|
-
}
|
|
282
|
-
})
|
|
283
|
-
},
|
|
284
|
-
"checkout/shipping": {
|
|
285
|
-
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
286
|
-
const { searchParams } = new URL(req.url);
|
|
287
|
-
const checkoutId = searchParams.get("checkoutId");
|
|
288
|
-
if (!checkoutId) {
|
|
289
|
-
return new Response("Checkout ID is required", { status: 400 });
|
|
290
|
-
}
|
|
291
|
-
try {
|
|
292
|
-
const rates = yield betterStore.checkout.getShippingRates(checkoutId);
|
|
293
|
-
return Response.json(rates);
|
|
294
|
-
} catch (error) {
|
|
295
|
-
return new Response("Failed to get shipping rates", { status: 500 });
|
|
296
|
-
}
|
|
297
|
-
})
|
|
298
|
-
},
|
|
299
|
-
"checkout/payment": {
|
|
300
|
-
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
301
|
-
const { searchParams } = new URL(req.url);
|
|
302
|
-
const checkoutId = searchParams.get("checkoutId");
|
|
303
|
-
if (!checkoutId) {
|
|
304
|
-
return new Response("Checkout ID is required", { status: 400 });
|
|
305
|
-
}
|
|
306
|
-
try {
|
|
307
|
-
const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
|
|
308
|
-
return Response.json({ clientSecret: secret });
|
|
309
|
-
} catch (error) {
|
|
310
|
-
return new Response("Failed to generate payment secret", {
|
|
311
|
-
status: 500
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
})
|
|
315
|
-
},
|
|
316
|
-
customer: {
|
|
317
|
-
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
318
|
-
const { searchParams } = new URL(req.url);
|
|
319
|
-
const idOrEmail = searchParams.get("idOrEmail");
|
|
320
|
-
if (!idOrEmail) {
|
|
321
|
-
return new Response("Customer ID or email is required", {
|
|
322
|
-
status: 400
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
try {
|
|
326
|
-
const customer = yield betterStore.customer.retrieve(idOrEmail);
|
|
327
|
-
return Response.json(customer);
|
|
328
|
-
} catch (error) {
|
|
329
|
-
return new Response("Failed to fetch customer", { status: 500 });
|
|
330
|
-
}
|
|
331
|
-
}),
|
|
332
|
-
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
333
|
-
try {
|
|
334
|
-
const body = yield req.json();
|
|
335
|
-
const customer = yield betterStore.customer.create(body);
|
|
336
|
-
return Response.json(customer);
|
|
337
|
-
} catch (error) {
|
|
338
|
-
return new Response("Failed to create customer", { status: 500 });
|
|
339
|
-
}
|
|
340
|
-
}),
|
|
341
|
-
PUT: (req, betterStore) => __async(void 0, null, function* () {
|
|
342
|
-
const { searchParams } = new URL(req.url);
|
|
343
|
-
const customerId = searchParams.get("customerId");
|
|
344
|
-
if (!customerId) {
|
|
345
|
-
return new Response("Customer ID is required", { status: 400 });
|
|
346
|
-
}
|
|
347
|
-
try {
|
|
348
|
-
const body = yield req.json();
|
|
349
|
-
const customer = yield betterStore.customer.update(customerId, body);
|
|
350
|
-
return Response.json(customer);
|
|
351
|
-
} catch (error) {
|
|
352
|
-
return new Response("Failed to update customer", { status: 500 });
|
|
353
|
-
}
|
|
354
|
-
}),
|
|
355
|
-
DELETE: (req, betterStore) => __async(void 0, null, function* () {
|
|
356
|
-
const { searchParams } = new URL(req.url);
|
|
357
|
-
const customerId = searchParams.get("customerId");
|
|
358
|
-
if (!customerId) {
|
|
359
|
-
return new Response("Customer ID is required", { status: 400 });
|
|
360
|
-
}
|
|
361
|
-
try {
|
|
362
|
-
yield betterStore.customer.delete(customerId);
|
|
363
|
-
return new Response(null, { status: 204 });
|
|
364
|
-
} catch (error) {
|
|
365
|
-
return new Response("Failed to delete customer", { status: 500 });
|
|
366
|
-
}
|
|
367
|
-
})
|
|
368
|
-
},
|
|
369
|
-
product: {
|
|
370
|
-
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
371
|
-
const { searchParams } = new URL(req.url);
|
|
372
|
-
const productId = searchParams.get("productId");
|
|
373
|
-
try {
|
|
374
|
-
if (productId) {
|
|
375
|
-
const product = yield betterStore.products.retrieve(productId);
|
|
376
|
-
return Response.json(product);
|
|
377
|
-
} else {
|
|
378
|
-
const products = yield betterStore.products.list();
|
|
379
|
-
return Response.json(products);
|
|
380
|
-
}
|
|
381
|
-
} catch (error) {
|
|
382
|
-
return new Response("Failed to fetch products", { status: 500 });
|
|
383
|
-
}
|
|
384
|
-
})
|
|
385
|
-
}
|
|
386
|
-
};
|
|
387
|
-
function addCORSHeaders(response, origin, allowedOrigins) {
|
|
388
|
-
if (origin && allowedOrigins.includes(origin)) {
|
|
389
|
-
response.headers.set("Access-Control-Allow-Origin", origin);
|
|
390
|
-
}
|
|
391
|
-
response.headers.set(
|
|
392
|
-
"Access-Control-Allow-Methods",
|
|
393
|
-
"GET, POST, PUT, DELETE, OPTIONS"
|
|
394
|
-
);
|
|
395
|
-
response.headers.set(
|
|
396
|
-
"Access-Control-Allow-Headers",
|
|
397
|
-
"Content-Type, Authorization"
|
|
398
|
-
);
|
|
399
|
-
return response;
|
|
400
|
-
}
|
|
401
|
-
function createNextJSHandler(betterStore, config = {}) {
|
|
402
|
-
const { apiKey, productionAllowedOrigins = [] } = config;
|
|
403
|
-
const isProduction = process.env.NODE_ENV === "production";
|
|
404
|
-
function validateRequest(req) {
|
|
405
|
-
return __async(this, null, function* () {
|
|
406
|
-
if (apiKey) {
|
|
407
|
-
const authHeader = req.headers.get("Authorization");
|
|
408
|
-
const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
|
|
409
|
-
if (!providedKey || providedKey !== apiKey) {
|
|
410
|
-
return new Response("Unauthorized", {
|
|
411
|
-
status: 401,
|
|
412
|
-
headers: { "WWW-Authenticate": "Bearer" }
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
const origin = req.headers.get("origin");
|
|
417
|
-
if (isProduction && productionAllowedOrigins.length > 0) {
|
|
418
|
-
if (!origin || !productionAllowedOrigins.includes(origin)) {
|
|
419
|
-
return new Response("Unauthorized", { status: 403 });
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
return null;
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
|
-
function getRouteFromPath(pathname) {
|
|
426
|
-
const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
|
|
427
|
-
const relevantPath = cleanPath.split("betterstore/")[1] || "";
|
|
428
|
-
return relevantPath;
|
|
429
|
-
}
|
|
430
|
-
return {
|
|
431
|
-
GET(req) {
|
|
432
|
-
return __async(this, null, function* () {
|
|
433
|
-
var _a2;
|
|
434
|
-
const validationError = yield validateRequest(req);
|
|
435
|
-
if (validationError)
|
|
436
|
-
return addCORSHeaders(
|
|
437
|
-
validationError,
|
|
438
|
-
req.headers.get("origin"),
|
|
439
|
-
productionAllowedOrigins
|
|
440
|
-
);
|
|
441
|
-
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
442
|
-
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
|
|
443
|
-
if (!handler) {
|
|
444
|
-
return addCORSHeaders(
|
|
445
|
-
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
446
|
-
req.headers.get("origin"),
|
|
447
|
-
productionAllowedOrigins
|
|
448
|
-
);
|
|
449
|
-
}
|
|
450
|
-
const response = yield handler(req, betterStore);
|
|
451
|
-
return addCORSHeaders(
|
|
452
|
-
response,
|
|
453
|
-
req.headers.get("origin"),
|
|
454
|
-
productionAllowedOrigins
|
|
455
|
-
);
|
|
456
|
-
});
|
|
457
|
-
},
|
|
458
|
-
POST(req) {
|
|
459
|
-
return __async(this, null, function* () {
|
|
460
|
-
var _a2;
|
|
461
|
-
const validationError = yield validateRequest(req);
|
|
462
|
-
if (validationError)
|
|
463
|
-
return addCORSHeaders(
|
|
464
|
-
validationError,
|
|
465
|
-
req.headers.get("origin"),
|
|
466
|
-
productionAllowedOrigins
|
|
467
|
-
);
|
|
468
|
-
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
469
|
-
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
|
|
470
|
-
if (!handler) {
|
|
471
|
-
return addCORSHeaders(
|
|
472
|
-
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
473
|
-
req.headers.get("origin"),
|
|
474
|
-
productionAllowedOrigins
|
|
475
|
-
);
|
|
476
|
-
}
|
|
477
|
-
const response = yield handler(req, betterStore);
|
|
478
|
-
return addCORSHeaders(
|
|
479
|
-
response,
|
|
480
|
-
req.headers.get("origin"),
|
|
481
|
-
productionAllowedOrigins
|
|
482
|
-
);
|
|
483
|
-
});
|
|
484
|
-
},
|
|
485
|
-
PUT(req) {
|
|
486
|
-
return __async(this, null, function* () {
|
|
487
|
-
var _a2;
|
|
488
|
-
const validationError = yield validateRequest(req);
|
|
489
|
-
if (validationError)
|
|
490
|
-
return addCORSHeaders(
|
|
491
|
-
validationError,
|
|
492
|
-
req.headers.get("origin"),
|
|
493
|
-
productionAllowedOrigins
|
|
494
|
-
);
|
|
495
|
-
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
496
|
-
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
|
|
497
|
-
if (!handler) {
|
|
498
|
-
return addCORSHeaders(
|
|
499
|
-
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
500
|
-
req.headers.get("origin"),
|
|
501
|
-
productionAllowedOrigins
|
|
502
|
-
);
|
|
503
|
-
}
|
|
504
|
-
const response = yield handler(req, betterStore);
|
|
505
|
-
return addCORSHeaders(
|
|
506
|
-
response,
|
|
507
|
-
req.headers.get("origin"),
|
|
508
|
-
productionAllowedOrigins
|
|
509
|
-
);
|
|
510
|
-
});
|
|
511
|
-
},
|
|
512
|
-
DELETE(req) {
|
|
513
|
-
return __async(this, null, function* () {
|
|
514
|
-
var _a2;
|
|
515
|
-
const validationError = yield validateRequest(req);
|
|
516
|
-
if (validationError)
|
|
517
|
-
return addCORSHeaders(
|
|
518
|
-
validationError,
|
|
519
|
-
req.headers.get("origin"),
|
|
520
|
-
productionAllowedOrigins
|
|
521
|
-
);
|
|
522
|
-
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
523
|
-
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
|
|
524
|
-
if (!handler) {
|
|
525
|
-
return addCORSHeaders(
|
|
526
|
-
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
527
|
-
req.headers.get("origin"),
|
|
528
|
-
productionAllowedOrigins
|
|
529
|
-
);
|
|
530
|
-
}
|
|
531
|
-
const response = yield handler(req, betterStore);
|
|
532
|
-
return addCORSHeaders(
|
|
533
|
-
response,
|
|
534
|
-
req.headers.get("origin"),
|
|
535
|
-
productionAllowedOrigins
|
|
536
|
-
);
|
|
537
|
-
});
|
|
538
|
-
}
|
|
539
|
-
};
|
|
540
|
-
}
|
|
541
|
-
|
|
542
246
|
// src/index.ts
|
|
543
247
|
var BetterStore = class {
|
|
544
248
|
// private apiKey: string;
|
|
@@ -554,6 +258,5 @@ var BetterStore = class {
|
|
|
554
258
|
var index_default = BetterStore;
|
|
555
259
|
// Annotate the CommonJS export names for ESM import in node:
|
|
556
260
|
0 && (module.exports = {
|
|
557
|
-
createNextJSHandler,
|
|
558
261
|
getCheckoutEmbedProps
|
|
559
262
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -21,13 +21,16 @@ var __async = (__this, __arguments, generator) => {
|
|
|
21
21
|
|
|
22
22
|
// src/utils/axios.ts
|
|
23
23
|
import axios from "axios";
|
|
24
|
-
var API_BASE_URL = "https://api.betterstore.io/
|
|
24
|
+
var API_BASE_URL = "https://api.betterstore.io/v1";
|
|
25
25
|
var createApiClient = (apiKey) => {
|
|
26
26
|
const client = axios.create({
|
|
27
27
|
baseURL: API_BASE_URL,
|
|
28
28
|
headers: {
|
|
29
29
|
"Content-Type": "application/json",
|
|
30
|
-
Authorization: `Bearer ${apiKey}
|
|
30
|
+
Authorization: `Bearer ${apiKey}`,
|
|
31
|
+
"Access-Control-Allow-Origin": "*",
|
|
32
|
+
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
|
|
33
|
+
"Access-Control-Allow-Headers": "Content-Type, Authorization"
|
|
31
34
|
}
|
|
32
35
|
});
|
|
33
36
|
client.interceptors.response.use(
|
|
@@ -76,12 +79,12 @@ var Checkout = class {
|
|
|
76
79
|
});
|
|
77
80
|
}
|
|
78
81
|
/**
|
|
79
|
-
* Retrieve a checkout session by ID
|
|
82
|
+
* Retrieve a checkout session by ID
|
|
80
83
|
*/
|
|
81
|
-
retrieve(
|
|
84
|
+
retrieve(checkoutId) {
|
|
82
85
|
return __async(this, null, function* () {
|
|
83
86
|
const data = yield this.apiClient.get(
|
|
84
|
-
`/checkout/${
|
|
87
|
+
`/checkout/${checkoutId}`
|
|
85
88
|
);
|
|
86
89
|
return data;
|
|
87
90
|
});
|
|
@@ -204,304 +207,6 @@ function getCheckoutEmbedProps(betterStore) {
|
|
|
204
207
|
};
|
|
205
208
|
}
|
|
206
209
|
|
|
207
|
-
// src/route-handelers/next-js.ts
|
|
208
|
-
var defaultBetterStoreRoutes = {
|
|
209
|
-
checkout: {
|
|
210
|
-
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
211
|
-
const { searchParams } = new URL(req.url);
|
|
212
|
-
const checkoutId = searchParams.get("checkoutId");
|
|
213
|
-
if (!checkoutId) {
|
|
214
|
-
return new Response("Checkout ID is required", { status: 400 });
|
|
215
|
-
}
|
|
216
|
-
try {
|
|
217
|
-
const checkout = yield betterStore.checkout.retrieve(checkoutId);
|
|
218
|
-
return Response.json(checkout);
|
|
219
|
-
} catch (error) {
|
|
220
|
-
return new Response("Failed to fetch checkout", { status: 500 });
|
|
221
|
-
}
|
|
222
|
-
}),
|
|
223
|
-
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
224
|
-
try {
|
|
225
|
-
const body = yield req.json();
|
|
226
|
-
const checkout = yield betterStore.checkout.create(body);
|
|
227
|
-
return Response.json(checkout);
|
|
228
|
-
} catch (error) {
|
|
229
|
-
return new Response("Failed to create checkout", { status: 500 });
|
|
230
|
-
}
|
|
231
|
-
}),
|
|
232
|
-
PUT: (req, betterStore) => __async(void 0, null, function* () {
|
|
233
|
-
const { searchParams } = new URL(req.url);
|
|
234
|
-
const checkoutId = searchParams.get("checkoutId");
|
|
235
|
-
if (!checkoutId) {
|
|
236
|
-
return new Response("Checkout ID is required", { status: 400 });
|
|
237
|
-
}
|
|
238
|
-
try {
|
|
239
|
-
const body = yield req.json();
|
|
240
|
-
const checkout = yield betterStore.checkout.update(checkoutId, body);
|
|
241
|
-
return Response.json(checkout);
|
|
242
|
-
} catch (error) {
|
|
243
|
-
return new Response("Failed to update checkout", { status: 500 });
|
|
244
|
-
}
|
|
245
|
-
})
|
|
246
|
-
},
|
|
247
|
-
"checkout/shipping": {
|
|
248
|
-
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
249
|
-
const { searchParams } = new URL(req.url);
|
|
250
|
-
const checkoutId = searchParams.get("checkoutId");
|
|
251
|
-
if (!checkoutId) {
|
|
252
|
-
return new Response("Checkout ID is required", { status: 400 });
|
|
253
|
-
}
|
|
254
|
-
try {
|
|
255
|
-
const rates = yield betterStore.checkout.getShippingRates(checkoutId);
|
|
256
|
-
return Response.json(rates);
|
|
257
|
-
} catch (error) {
|
|
258
|
-
return new Response("Failed to get shipping rates", { status: 500 });
|
|
259
|
-
}
|
|
260
|
-
})
|
|
261
|
-
},
|
|
262
|
-
"checkout/payment": {
|
|
263
|
-
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
264
|
-
const { searchParams } = new URL(req.url);
|
|
265
|
-
const checkoutId = searchParams.get("checkoutId");
|
|
266
|
-
if (!checkoutId) {
|
|
267
|
-
return new Response("Checkout ID is required", { status: 400 });
|
|
268
|
-
}
|
|
269
|
-
try {
|
|
270
|
-
const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
|
|
271
|
-
return Response.json({ clientSecret: secret });
|
|
272
|
-
} catch (error) {
|
|
273
|
-
return new Response("Failed to generate payment secret", {
|
|
274
|
-
status: 500
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
})
|
|
278
|
-
},
|
|
279
|
-
customer: {
|
|
280
|
-
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
281
|
-
const { searchParams } = new URL(req.url);
|
|
282
|
-
const idOrEmail = searchParams.get("idOrEmail");
|
|
283
|
-
if (!idOrEmail) {
|
|
284
|
-
return new Response("Customer ID or email is required", {
|
|
285
|
-
status: 400
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
try {
|
|
289
|
-
const customer = yield betterStore.customer.retrieve(idOrEmail);
|
|
290
|
-
return Response.json(customer);
|
|
291
|
-
} catch (error) {
|
|
292
|
-
return new Response("Failed to fetch customer", { status: 500 });
|
|
293
|
-
}
|
|
294
|
-
}),
|
|
295
|
-
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
296
|
-
try {
|
|
297
|
-
const body = yield req.json();
|
|
298
|
-
const customer = yield betterStore.customer.create(body);
|
|
299
|
-
return Response.json(customer);
|
|
300
|
-
} catch (error) {
|
|
301
|
-
return new Response("Failed to create customer", { status: 500 });
|
|
302
|
-
}
|
|
303
|
-
}),
|
|
304
|
-
PUT: (req, betterStore) => __async(void 0, null, function* () {
|
|
305
|
-
const { searchParams } = new URL(req.url);
|
|
306
|
-
const customerId = searchParams.get("customerId");
|
|
307
|
-
if (!customerId) {
|
|
308
|
-
return new Response("Customer ID is required", { status: 400 });
|
|
309
|
-
}
|
|
310
|
-
try {
|
|
311
|
-
const body = yield req.json();
|
|
312
|
-
const customer = yield betterStore.customer.update(customerId, body);
|
|
313
|
-
return Response.json(customer);
|
|
314
|
-
} catch (error) {
|
|
315
|
-
return new Response("Failed to update customer", { status: 500 });
|
|
316
|
-
}
|
|
317
|
-
}),
|
|
318
|
-
DELETE: (req, betterStore) => __async(void 0, null, function* () {
|
|
319
|
-
const { searchParams } = new URL(req.url);
|
|
320
|
-
const customerId = searchParams.get("customerId");
|
|
321
|
-
if (!customerId) {
|
|
322
|
-
return new Response("Customer ID is required", { status: 400 });
|
|
323
|
-
}
|
|
324
|
-
try {
|
|
325
|
-
yield betterStore.customer.delete(customerId);
|
|
326
|
-
return new Response(null, { status: 204 });
|
|
327
|
-
} catch (error) {
|
|
328
|
-
return new Response("Failed to delete customer", { status: 500 });
|
|
329
|
-
}
|
|
330
|
-
})
|
|
331
|
-
},
|
|
332
|
-
product: {
|
|
333
|
-
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
334
|
-
const { searchParams } = new URL(req.url);
|
|
335
|
-
const productId = searchParams.get("productId");
|
|
336
|
-
try {
|
|
337
|
-
if (productId) {
|
|
338
|
-
const product = yield betterStore.products.retrieve(productId);
|
|
339
|
-
return Response.json(product);
|
|
340
|
-
} else {
|
|
341
|
-
const products = yield betterStore.products.list();
|
|
342
|
-
return Response.json(products);
|
|
343
|
-
}
|
|
344
|
-
} catch (error) {
|
|
345
|
-
return new Response("Failed to fetch products", { status: 500 });
|
|
346
|
-
}
|
|
347
|
-
})
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
function addCORSHeaders(response, origin, allowedOrigins) {
|
|
351
|
-
if (origin && allowedOrigins.includes(origin)) {
|
|
352
|
-
response.headers.set("Access-Control-Allow-Origin", origin);
|
|
353
|
-
}
|
|
354
|
-
response.headers.set(
|
|
355
|
-
"Access-Control-Allow-Methods",
|
|
356
|
-
"GET, POST, PUT, DELETE, OPTIONS"
|
|
357
|
-
);
|
|
358
|
-
response.headers.set(
|
|
359
|
-
"Access-Control-Allow-Headers",
|
|
360
|
-
"Content-Type, Authorization"
|
|
361
|
-
);
|
|
362
|
-
return response;
|
|
363
|
-
}
|
|
364
|
-
function createNextJSHandler(betterStore, config = {}) {
|
|
365
|
-
const { apiKey, productionAllowedOrigins = [] } = config;
|
|
366
|
-
const isProduction = process.env.NODE_ENV === "production";
|
|
367
|
-
function validateRequest(req) {
|
|
368
|
-
return __async(this, null, function* () {
|
|
369
|
-
if (apiKey) {
|
|
370
|
-
const authHeader = req.headers.get("Authorization");
|
|
371
|
-
const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
|
|
372
|
-
if (!providedKey || providedKey !== apiKey) {
|
|
373
|
-
return new Response("Unauthorized", {
|
|
374
|
-
status: 401,
|
|
375
|
-
headers: { "WWW-Authenticate": "Bearer" }
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
const origin = req.headers.get("origin");
|
|
380
|
-
if (isProduction && productionAllowedOrigins.length > 0) {
|
|
381
|
-
if (!origin || !productionAllowedOrigins.includes(origin)) {
|
|
382
|
-
return new Response("Unauthorized", { status: 403 });
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
return null;
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
function getRouteFromPath(pathname) {
|
|
389
|
-
const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
|
|
390
|
-
const relevantPath = cleanPath.split("betterstore/")[1] || "";
|
|
391
|
-
return relevantPath;
|
|
392
|
-
}
|
|
393
|
-
return {
|
|
394
|
-
GET(req) {
|
|
395
|
-
return __async(this, null, function* () {
|
|
396
|
-
var _a2;
|
|
397
|
-
const validationError = yield validateRequest(req);
|
|
398
|
-
if (validationError)
|
|
399
|
-
return addCORSHeaders(
|
|
400
|
-
validationError,
|
|
401
|
-
req.headers.get("origin"),
|
|
402
|
-
productionAllowedOrigins
|
|
403
|
-
);
|
|
404
|
-
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
405
|
-
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
|
|
406
|
-
if (!handler) {
|
|
407
|
-
return addCORSHeaders(
|
|
408
|
-
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
409
|
-
req.headers.get("origin"),
|
|
410
|
-
productionAllowedOrigins
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
const response = yield handler(req, betterStore);
|
|
414
|
-
return addCORSHeaders(
|
|
415
|
-
response,
|
|
416
|
-
req.headers.get("origin"),
|
|
417
|
-
productionAllowedOrigins
|
|
418
|
-
);
|
|
419
|
-
});
|
|
420
|
-
},
|
|
421
|
-
POST(req) {
|
|
422
|
-
return __async(this, null, function* () {
|
|
423
|
-
var _a2;
|
|
424
|
-
const validationError = yield validateRequest(req);
|
|
425
|
-
if (validationError)
|
|
426
|
-
return addCORSHeaders(
|
|
427
|
-
validationError,
|
|
428
|
-
req.headers.get("origin"),
|
|
429
|
-
productionAllowedOrigins
|
|
430
|
-
);
|
|
431
|
-
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
432
|
-
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
|
|
433
|
-
if (!handler) {
|
|
434
|
-
return addCORSHeaders(
|
|
435
|
-
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
436
|
-
req.headers.get("origin"),
|
|
437
|
-
productionAllowedOrigins
|
|
438
|
-
);
|
|
439
|
-
}
|
|
440
|
-
const response = yield handler(req, betterStore);
|
|
441
|
-
return addCORSHeaders(
|
|
442
|
-
response,
|
|
443
|
-
req.headers.get("origin"),
|
|
444
|
-
productionAllowedOrigins
|
|
445
|
-
);
|
|
446
|
-
});
|
|
447
|
-
},
|
|
448
|
-
PUT(req) {
|
|
449
|
-
return __async(this, null, function* () {
|
|
450
|
-
var _a2;
|
|
451
|
-
const validationError = yield validateRequest(req);
|
|
452
|
-
if (validationError)
|
|
453
|
-
return addCORSHeaders(
|
|
454
|
-
validationError,
|
|
455
|
-
req.headers.get("origin"),
|
|
456
|
-
productionAllowedOrigins
|
|
457
|
-
);
|
|
458
|
-
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
459
|
-
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
|
|
460
|
-
if (!handler) {
|
|
461
|
-
return addCORSHeaders(
|
|
462
|
-
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
463
|
-
req.headers.get("origin"),
|
|
464
|
-
productionAllowedOrigins
|
|
465
|
-
);
|
|
466
|
-
}
|
|
467
|
-
const response = yield handler(req, betterStore);
|
|
468
|
-
return addCORSHeaders(
|
|
469
|
-
response,
|
|
470
|
-
req.headers.get("origin"),
|
|
471
|
-
productionAllowedOrigins
|
|
472
|
-
);
|
|
473
|
-
});
|
|
474
|
-
},
|
|
475
|
-
DELETE(req) {
|
|
476
|
-
return __async(this, null, function* () {
|
|
477
|
-
var _a2;
|
|
478
|
-
const validationError = yield validateRequest(req);
|
|
479
|
-
if (validationError)
|
|
480
|
-
return addCORSHeaders(
|
|
481
|
-
validationError,
|
|
482
|
-
req.headers.get("origin"),
|
|
483
|
-
productionAllowedOrigins
|
|
484
|
-
);
|
|
485
|
-
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
486
|
-
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
|
|
487
|
-
if (!handler) {
|
|
488
|
-
return addCORSHeaders(
|
|
489
|
-
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
490
|
-
req.headers.get("origin"),
|
|
491
|
-
productionAllowedOrigins
|
|
492
|
-
);
|
|
493
|
-
}
|
|
494
|
-
const response = yield handler(req, betterStore);
|
|
495
|
-
return addCORSHeaders(
|
|
496
|
-
response,
|
|
497
|
-
req.headers.get("origin"),
|
|
498
|
-
productionAllowedOrigins
|
|
499
|
-
);
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
};
|
|
503
|
-
}
|
|
504
|
-
|
|
505
210
|
// src/index.ts
|
|
506
211
|
var BetterStore = class {
|
|
507
212
|
// private apiKey: string;
|
|
@@ -516,7 +221,6 @@ var BetterStore = class {
|
|
|
516
221
|
};
|
|
517
222
|
var index_default = BetterStore;
|
|
518
223
|
export {
|
|
519
|
-
createNextJSHandler,
|
|
520
224
|
index_default as default,
|
|
521
225
|
getCheckoutEmbedProps
|
|
522
226
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@betterstore/sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.10",
|
|
4
4
|
"description": "E-commerce for Developers",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"axios": "^1.8.2"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"
|
|
32
|
+
"prep": "changeset && changeset version && git add .",
|
|
33
33
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
34
34
|
"lint": "tsc",
|
|
35
35
|
"format:check": "prettier --check --ignore-path .prettierignore .",
|
|
36
36
|
"format": "prettier --write --ignore-path .prettierignore .",
|
|
37
37
|
"ci": "pnpm run lint && pnpm run format:check && pnpm run build",
|
|
38
|
-
"release": "pnpm run ci &&
|
|
38
|
+
"release": "pnpm run ci && pnpm publish -r --no-git-checks"
|
|
39
39
|
}
|
|
40
40
|
}
|