@betterstore/sdk 0.2.1 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @betterstore/sdk
2
2
 
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - route handlers added
8
+
3
9
  ## 0.2.1
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,3 +1,5 @@
1
+ import { NextRequest } from 'next/server';
2
+
1
3
  interface LineItem {
2
4
  quantity: number;
3
5
  productId?: string;
@@ -201,6 +203,17 @@ declare class Products {
201
203
  retrieve(productId: string): Promise<Product>;
202
204
  }
203
205
 
206
+ type NextjsRouteConfig = {
207
+ productionAllowedOrigins?: string[];
208
+ };
209
+ type BSClient = InstanceType<typeof BetterStore>;
210
+ declare function createNextJSHandler(betterStore: BSClient, config?: NextjsRouteConfig): {
211
+ GET(req: NextRequest): Promise<Response>;
212
+ POST(req: NextRequest): Promise<Response>;
213
+ PUT(req: NextRequest): Promise<Response>;
214
+ DELETE(req: NextRequest): Promise<Response>;
215
+ };
216
+
204
217
  declare class BetterStore {
205
218
  checkout: Checkout;
206
219
  products: Products;
@@ -208,4 +221,4 @@ declare class BetterStore {
208
221
  constructor(apiKey: string);
209
222
  }
210
223
 
211
- export { BetterStore as default };
224
+ export { createNextJSHandler, BetterStore as default };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { NextRequest } from 'next/server';
2
+
1
3
  interface LineItem {
2
4
  quantity: number;
3
5
  productId?: string;
@@ -201,6 +203,17 @@ declare class Products {
201
203
  retrieve(productId: string): Promise<Product>;
202
204
  }
203
205
 
206
+ type NextjsRouteConfig = {
207
+ productionAllowedOrigins?: string[];
208
+ };
209
+ type BSClient = InstanceType<typeof BetterStore>;
210
+ declare function createNextJSHandler(betterStore: BSClient, config?: NextjsRouteConfig): {
211
+ GET(req: NextRequest): Promise<Response>;
212
+ POST(req: NextRequest): Promise<Response>;
213
+ PUT(req: NextRequest): Promise<Response>;
214
+ DELETE(req: NextRequest): Promise<Response>;
215
+ };
216
+
204
217
  declare class BetterStore {
205
218
  checkout: Checkout;
206
219
  products: Products;
@@ -208,4 +221,4 @@ declare class BetterStore {
208
221
  constructor(apiKey: string);
209
222
  }
210
223
 
211
- export { BetterStore as default };
224
+ export { createNextJSHandler, BetterStore as default };
package/dist/index.js CHANGED
@@ -50,6 +50,7 @@ var __async = (__this, __arguments, generator) => {
50
50
  // src/index.ts
51
51
  var index_exports = {};
52
52
  __export(index_exports, {
53
+ createNextJSHandler: () => createNextJSHandler,
53
54
  default: () => index_default
54
55
  });
55
56
  module.exports = __toCommonJS(index_exports);
@@ -230,6 +231,224 @@ var Products = class {
230
231
  };
231
232
  var products_default = Products;
232
233
 
234
+ // src/route-handelers/next-js.ts
235
+ var defaultBetterStoreRoutes = {
236
+ checkout: {
237
+ GET: (req, betterStore) => __async(void 0, null, function* () {
238
+ const { searchParams } = new URL(req.url);
239
+ const checkoutId = searchParams.get("checkoutId");
240
+ if (!checkoutId) {
241
+ return new Response("Checkout ID is required", { status: 400 });
242
+ }
243
+ try {
244
+ const checkout = yield betterStore.checkout.retrieve(checkoutId);
245
+ return Response.json(checkout);
246
+ } catch (error) {
247
+ return new Response("Failed to fetch checkout", { status: 500 });
248
+ }
249
+ }),
250
+ POST: (req, betterStore) => __async(void 0, null, function* () {
251
+ try {
252
+ const body = yield req.json();
253
+ const checkout = yield betterStore.checkout.create(body);
254
+ return Response.json(checkout);
255
+ } catch (error) {
256
+ return new Response("Failed to create checkout", { status: 500 });
257
+ }
258
+ }),
259
+ PUT: (req, betterStore) => __async(void 0, null, function* () {
260
+ const { searchParams } = new URL(req.url);
261
+ const checkoutId = searchParams.get("checkoutId");
262
+ if (!checkoutId) {
263
+ return new Response("Checkout ID is required", { status: 400 });
264
+ }
265
+ try {
266
+ const body = yield req.json();
267
+ const checkout = yield betterStore.checkout.update(checkoutId, body);
268
+ return Response.json(checkout);
269
+ } catch (error) {
270
+ return new Response("Failed to update checkout", { status: 500 });
271
+ }
272
+ })
273
+ },
274
+ "checkout/shipping": {
275
+ GET: (req, betterStore) => __async(void 0, null, function* () {
276
+ const { searchParams } = new URL(req.url);
277
+ const checkoutId = searchParams.get("checkoutId");
278
+ if (!checkoutId) {
279
+ return new Response("Checkout ID is required", { status: 400 });
280
+ }
281
+ try {
282
+ const rates = yield betterStore.checkout.getShippingRates(checkoutId);
283
+ return Response.json(rates);
284
+ } catch (error) {
285
+ return new Response("Failed to get shipping rates", { status: 500 });
286
+ }
287
+ })
288
+ },
289
+ "checkout/payment": {
290
+ POST: (req, betterStore) => __async(void 0, null, function* () {
291
+ const { searchParams } = new URL(req.url);
292
+ const checkoutId = searchParams.get("checkoutId");
293
+ if (!checkoutId) {
294
+ return new Response("Checkout ID is required", { status: 400 });
295
+ }
296
+ try {
297
+ const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
298
+ return Response.json({ clientSecret: secret });
299
+ } catch (error) {
300
+ return new Response("Failed to generate payment secret", {
301
+ status: 500
302
+ });
303
+ }
304
+ })
305
+ },
306
+ customer: {
307
+ GET: (req, betterStore) => __async(void 0, null, function* () {
308
+ const { searchParams } = new URL(req.url);
309
+ const idOrEmail = searchParams.get("idOrEmail");
310
+ if (!idOrEmail) {
311
+ return new Response("Customer ID or email is required", {
312
+ status: 400
313
+ });
314
+ }
315
+ try {
316
+ const customer = yield betterStore.customer.retrieve(idOrEmail);
317
+ return Response.json(customer);
318
+ } catch (error) {
319
+ return new Response("Failed to fetch customer", { status: 500 });
320
+ }
321
+ }),
322
+ POST: (req, betterStore) => __async(void 0, null, function* () {
323
+ try {
324
+ const body = yield req.json();
325
+ const customer = yield betterStore.customer.create(body);
326
+ return Response.json(customer);
327
+ } catch (error) {
328
+ return new Response("Failed to create customer", { status: 500 });
329
+ }
330
+ }),
331
+ PUT: (req, betterStore) => __async(void 0, null, function* () {
332
+ const { searchParams } = new URL(req.url);
333
+ const customerId = searchParams.get("customerId");
334
+ if (!customerId) {
335
+ return new Response("Customer ID is required", { status: 400 });
336
+ }
337
+ try {
338
+ const body = yield req.json();
339
+ const customer = yield betterStore.customer.update(customerId, body);
340
+ return Response.json(customer);
341
+ } catch (error) {
342
+ return new Response("Failed to update customer", { status: 500 });
343
+ }
344
+ }),
345
+ DELETE: (req, betterStore) => __async(void 0, null, function* () {
346
+ const { searchParams } = new URL(req.url);
347
+ const customerId = searchParams.get("customerId");
348
+ if (!customerId) {
349
+ return new Response("Customer ID is required", { status: 400 });
350
+ }
351
+ try {
352
+ yield betterStore.customer.delete(customerId);
353
+ return new Response(null, { status: 204 });
354
+ } catch (error) {
355
+ return new Response("Failed to delete customer", { status: 500 });
356
+ }
357
+ })
358
+ },
359
+ product: {
360
+ GET: (req, betterStore) => __async(void 0, null, function* () {
361
+ const { searchParams } = new URL(req.url);
362
+ const productId = searchParams.get("productId");
363
+ try {
364
+ if (productId) {
365
+ const product = yield betterStore.products.retrieve(productId);
366
+ return Response.json(product);
367
+ } else {
368
+ const products = yield betterStore.products.list();
369
+ return Response.json(products);
370
+ }
371
+ } catch (error) {
372
+ return new Response("Failed to fetch products", { status: 500 });
373
+ }
374
+ })
375
+ }
376
+ };
377
+ function createNextJSHandler(betterStore, config = {}) {
378
+ const { productionAllowedOrigins = [] } = config;
379
+ const isProduction = process.env.NODE_ENV === "production";
380
+ function validateRequest(req) {
381
+ return __async(this, null, function* () {
382
+ if (isProduction && productionAllowedOrigins.length > 0) {
383
+ const origin = req.headers.get("origin");
384
+ if (!origin || !productionAllowedOrigins.includes(origin)) {
385
+ return new Response("Unauthorized", { status: 403 });
386
+ }
387
+ }
388
+ return null;
389
+ });
390
+ }
391
+ function getRouteFromPath(pathname) {
392
+ const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
393
+ const relevantPath = cleanPath.split("betterstore/")[1] || "";
394
+ return relevantPath;
395
+ }
396
+ return {
397
+ GET(req) {
398
+ return __async(this, null, function* () {
399
+ var _a2;
400
+ const validationError = yield validateRequest(req);
401
+ if (validationError) return validationError;
402
+ const route = getRouteFromPath(new URL(req.url).pathname);
403
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
404
+ if (!handler) {
405
+ return new Response(`Route not found: ${route}`, { status: 404 });
406
+ }
407
+ return handler(req, betterStore);
408
+ });
409
+ },
410
+ POST(req) {
411
+ return __async(this, null, function* () {
412
+ var _a2;
413
+ const validationError = yield validateRequest(req);
414
+ if (validationError) return validationError;
415
+ const route = getRouteFromPath(new URL(req.url).pathname);
416
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
417
+ if (!handler) {
418
+ return new Response(`Route not found: ${route}`, { status: 404 });
419
+ }
420
+ return handler(req, betterStore);
421
+ });
422
+ },
423
+ PUT(req) {
424
+ return __async(this, null, function* () {
425
+ var _a2;
426
+ const validationError = yield validateRequest(req);
427
+ if (validationError) return validationError;
428
+ const route = getRouteFromPath(new URL(req.url).pathname);
429
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
430
+ if (!handler) {
431
+ return new Response(`Route not found: ${route}`, { status: 404 });
432
+ }
433
+ return handler(req, betterStore);
434
+ });
435
+ },
436
+ DELETE(req) {
437
+ return __async(this, null, function* () {
438
+ var _a2;
439
+ const validationError = yield validateRequest(req);
440
+ if (validationError) return validationError;
441
+ const route = getRouteFromPath(new URL(req.url).pathname);
442
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
443
+ if (!handler) {
444
+ return new Response(`Route not found: ${route}`, { status: 404 });
445
+ }
446
+ return handler(req, betterStore);
447
+ });
448
+ }
449
+ };
450
+ }
451
+
233
452
  // src/index.ts
234
453
  var BetterStore = class {
235
454
  // private apiKey: string;
@@ -243,3 +462,7 @@ var BetterStore = class {
243
462
  }
244
463
  };
245
464
  var index_default = BetterStore;
465
+ // Annotate the CommonJS export names for ESM import in node:
466
+ 0 && (module.exports = {
467
+ createNextJSHandler
468
+ });
package/dist/index.mjs CHANGED
@@ -195,6 +195,224 @@ var Products = class {
195
195
  };
196
196
  var products_default = Products;
197
197
 
198
+ // src/route-handelers/next-js.ts
199
+ var defaultBetterStoreRoutes = {
200
+ checkout: {
201
+ GET: (req, betterStore) => __async(void 0, null, function* () {
202
+ const { searchParams } = new URL(req.url);
203
+ const checkoutId = searchParams.get("checkoutId");
204
+ if (!checkoutId) {
205
+ return new Response("Checkout ID is required", { status: 400 });
206
+ }
207
+ try {
208
+ const checkout = yield betterStore.checkout.retrieve(checkoutId);
209
+ return Response.json(checkout);
210
+ } catch (error) {
211
+ return new Response("Failed to fetch checkout", { status: 500 });
212
+ }
213
+ }),
214
+ POST: (req, betterStore) => __async(void 0, null, function* () {
215
+ try {
216
+ const body = yield req.json();
217
+ const checkout = yield betterStore.checkout.create(body);
218
+ return Response.json(checkout);
219
+ } catch (error) {
220
+ return new Response("Failed to create checkout", { status: 500 });
221
+ }
222
+ }),
223
+ PUT: (req, betterStore) => __async(void 0, null, function* () {
224
+ const { searchParams } = new URL(req.url);
225
+ const checkoutId = searchParams.get("checkoutId");
226
+ if (!checkoutId) {
227
+ return new Response("Checkout ID is required", { status: 400 });
228
+ }
229
+ try {
230
+ const body = yield req.json();
231
+ const checkout = yield betterStore.checkout.update(checkoutId, body);
232
+ return Response.json(checkout);
233
+ } catch (error) {
234
+ return new Response("Failed to update checkout", { status: 500 });
235
+ }
236
+ })
237
+ },
238
+ "checkout/shipping": {
239
+ GET: (req, betterStore) => __async(void 0, null, function* () {
240
+ const { searchParams } = new URL(req.url);
241
+ const checkoutId = searchParams.get("checkoutId");
242
+ if (!checkoutId) {
243
+ return new Response("Checkout ID is required", { status: 400 });
244
+ }
245
+ try {
246
+ const rates = yield betterStore.checkout.getShippingRates(checkoutId);
247
+ return Response.json(rates);
248
+ } catch (error) {
249
+ return new Response("Failed to get shipping rates", { status: 500 });
250
+ }
251
+ })
252
+ },
253
+ "checkout/payment": {
254
+ POST: (req, betterStore) => __async(void 0, null, function* () {
255
+ const { searchParams } = new URL(req.url);
256
+ const checkoutId = searchParams.get("checkoutId");
257
+ if (!checkoutId) {
258
+ return new Response("Checkout ID is required", { status: 400 });
259
+ }
260
+ try {
261
+ const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
262
+ return Response.json({ clientSecret: secret });
263
+ } catch (error) {
264
+ return new Response("Failed to generate payment secret", {
265
+ status: 500
266
+ });
267
+ }
268
+ })
269
+ },
270
+ customer: {
271
+ GET: (req, betterStore) => __async(void 0, null, function* () {
272
+ const { searchParams } = new URL(req.url);
273
+ const idOrEmail = searchParams.get("idOrEmail");
274
+ if (!idOrEmail) {
275
+ return new Response("Customer ID or email is required", {
276
+ status: 400
277
+ });
278
+ }
279
+ try {
280
+ const customer = yield betterStore.customer.retrieve(idOrEmail);
281
+ return Response.json(customer);
282
+ } catch (error) {
283
+ return new Response("Failed to fetch customer", { status: 500 });
284
+ }
285
+ }),
286
+ POST: (req, betterStore) => __async(void 0, null, function* () {
287
+ try {
288
+ const body = yield req.json();
289
+ const customer = yield betterStore.customer.create(body);
290
+ return Response.json(customer);
291
+ } catch (error) {
292
+ return new Response("Failed to create customer", { status: 500 });
293
+ }
294
+ }),
295
+ PUT: (req, betterStore) => __async(void 0, null, function* () {
296
+ const { searchParams } = new URL(req.url);
297
+ const customerId = searchParams.get("customerId");
298
+ if (!customerId) {
299
+ return new Response("Customer ID is required", { status: 400 });
300
+ }
301
+ try {
302
+ const body = yield req.json();
303
+ const customer = yield betterStore.customer.update(customerId, body);
304
+ return Response.json(customer);
305
+ } catch (error) {
306
+ return new Response("Failed to update customer", { status: 500 });
307
+ }
308
+ }),
309
+ DELETE: (req, betterStore) => __async(void 0, null, function* () {
310
+ const { searchParams } = new URL(req.url);
311
+ const customerId = searchParams.get("customerId");
312
+ if (!customerId) {
313
+ return new Response("Customer ID is required", { status: 400 });
314
+ }
315
+ try {
316
+ yield betterStore.customer.delete(customerId);
317
+ return new Response(null, { status: 204 });
318
+ } catch (error) {
319
+ return new Response("Failed to delete customer", { status: 500 });
320
+ }
321
+ })
322
+ },
323
+ product: {
324
+ GET: (req, betterStore) => __async(void 0, null, function* () {
325
+ const { searchParams } = new URL(req.url);
326
+ const productId = searchParams.get("productId");
327
+ try {
328
+ if (productId) {
329
+ const product = yield betterStore.products.retrieve(productId);
330
+ return Response.json(product);
331
+ } else {
332
+ const products = yield betterStore.products.list();
333
+ return Response.json(products);
334
+ }
335
+ } catch (error) {
336
+ return new Response("Failed to fetch products", { status: 500 });
337
+ }
338
+ })
339
+ }
340
+ };
341
+ function createNextJSHandler(betterStore, config = {}) {
342
+ const { productionAllowedOrigins = [] } = config;
343
+ const isProduction = process.env.NODE_ENV === "production";
344
+ function validateRequest(req) {
345
+ return __async(this, null, function* () {
346
+ if (isProduction && productionAllowedOrigins.length > 0) {
347
+ const origin = req.headers.get("origin");
348
+ if (!origin || !productionAllowedOrigins.includes(origin)) {
349
+ return new Response("Unauthorized", { status: 403 });
350
+ }
351
+ }
352
+ return null;
353
+ });
354
+ }
355
+ function getRouteFromPath(pathname) {
356
+ const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
357
+ const relevantPath = cleanPath.split("betterstore/")[1] || "";
358
+ return relevantPath;
359
+ }
360
+ return {
361
+ GET(req) {
362
+ return __async(this, null, function* () {
363
+ var _a2;
364
+ const validationError = yield validateRequest(req);
365
+ if (validationError) return validationError;
366
+ const route = getRouteFromPath(new URL(req.url).pathname);
367
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
368
+ if (!handler) {
369
+ return new Response(`Route not found: ${route}`, { status: 404 });
370
+ }
371
+ return handler(req, betterStore);
372
+ });
373
+ },
374
+ POST(req) {
375
+ return __async(this, null, function* () {
376
+ var _a2;
377
+ const validationError = yield validateRequest(req);
378
+ if (validationError) return validationError;
379
+ const route = getRouteFromPath(new URL(req.url).pathname);
380
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
381
+ if (!handler) {
382
+ return new Response(`Route not found: ${route}`, { status: 404 });
383
+ }
384
+ return handler(req, betterStore);
385
+ });
386
+ },
387
+ PUT(req) {
388
+ return __async(this, null, function* () {
389
+ var _a2;
390
+ const validationError = yield validateRequest(req);
391
+ if (validationError) return validationError;
392
+ const route = getRouteFromPath(new URL(req.url).pathname);
393
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
394
+ if (!handler) {
395
+ return new Response(`Route not found: ${route}`, { status: 404 });
396
+ }
397
+ return handler(req, betterStore);
398
+ });
399
+ },
400
+ DELETE(req) {
401
+ return __async(this, null, function* () {
402
+ var _a2;
403
+ const validationError = yield validateRequest(req);
404
+ if (validationError) return validationError;
405
+ const route = getRouteFromPath(new URL(req.url).pathname);
406
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
407
+ if (!handler) {
408
+ return new Response(`Route not found: ${route}`, { status: 404 });
409
+ }
410
+ return handler(req, betterStore);
411
+ });
412
+ }
413
+ };
414
+ }
415
+
198
416
  // src/index.ts
199
417
  var BetterStore = class {
200
418
  // private apiKey: string;
@@ -209,5 +427,6 @@ var BetterStore = class {
209
427
  };
210
428
  var index_default = BetterStore;
211
429
  export {
430
+ createNextJSHandler,
212
431
  index_default as default
213
432
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/sdk",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "E-commerce for Developers",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -20,6 +20,7 @@
20
20
  "devDependencies": {
21
21
  "@changesets/cli": "^2.28.1",
22
22
  "@types/axios": "^0.14.4",
23
+ "next": "^15.2.1",
23
24
  "prettier": "^3.5.3",
24
25
  "tsup": "^8.4.0",
25
26
  "typescript": "^5.8.2"