@betterstore/sdk 0.2.13 → 0.3.0

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.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - client added, syntax changed
8
+
3
9
  ## 0.2.13
4
10
 
5
11
  ### Patch Changes
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;
@@ -134,6 +132,37 @@ interface Customer$1 extends CustomerCreateParams {
134
132
  updatedAt: string;
135
133
  }
136
134
 
135
+ declare class Client {
136
+ /**
137
+ * Retrieve a checkout session by ID
138
+ */
139
+ retrieveCheckout(clientSecret: string, checkoutId: string): Promise<CheckoutSession>;
140
+ /**
141
+ * Update a checkout session
142
+ */
143
+ updateCheckout(clientSecret: string, checkoutId: string, params: CheckoutUpdateParams): Promise<CheckoutSession>;
144
+ /**
145
+ * Get shipping rates for a checkout session
146
+ */
147
+ getCheckoutShippingRates(clientSecret: string, checkoutId: string): Promise<ShippingRate[]>;
148
+ /**
149
+ * Generate payment secret for a checkout session
150
+ */
151
+ generateCheckoutsPaymentSecret(clientSecret: string, checkoutId: string): Promise<string>;
152
+ /**
153
+ * Create a new customer
154
+ */
155
+ createCustomer(clientSecret: string, params: CustomerCreateParams): Promise<Customer$1>;
156
+ /**
157
+ * Retrieve a customer by ID or email
158
+ */
159
+ retrieveCustomer(clientSecret: string, idOrEmail: string): Promise<Customer$1>;
160
+ /**
161
+ * Update a customer
162
+ */
163
+ updateCustomer(clientSecret: string, customerId: string, params: CustomerUpdateParams): Promise<Customer$1>;
164
+ }
165
+
137
166
  declare class Customer {
138
167
  private apiClient;
139
168
  constructor(apiKey: string);
@@ -206,23 +235,13 @@ declare class Products {
206
235
  retrieve(productId: string): Promise<Product>;
207
236
  }
208
237
 
209
- type NextjsRouteConfig = {
210
- apiKey?: string;
211
- productionAllowedOrigins?: string[];
212
- };
213
- type BSClient = InstanceType<typeof BetterStore>;
214
- declare function createNextJSHandler(betterStore: BSClient, config?: NextjsRouteConfig): {
215
- GET(req: NextRequest): Promise<Response>;
216
- POST(req: NextRequest): Promise<Response>;
217
- PUT(req: NextRequest): Promise<Response>;
218
- DELETE(req: NextRequest): Promise<Response>;
219
- };
220
-
221
- declare class BetterStore {
238
+ declare function betterStore(config: {
239
+ apiKey: string;
240
+ }): {
222
241
  checkout: Checkout;
223
242
  products: Products;
224
243
  customer: Customer;
225
- constructor(apiKey: string);
226
- }
244
+ };
245
+ declare function createStoreClient(): Client;
227
246
 
228
- export { type Address, type CheckoutCreateParams, type CheckoutSession, type CheckoutUpdateParams, type Customer$1 as Customer, type CustomerAddress, type CustomerCreateParams, type CustomerUpdateParams, type LineItem, type Product, type ProductOption, ProductStatus, type ProductVariant, type ShippingRate, type VariantOption, createNextJSHandler, BetterStore as default };
247
+ export { type Address, type CheckoutCreateParams, type CheckoutSession, type CheckoutUpdateParams, type Customer$1 as Customer, type CustomerAddress, type CustomerCreateParams, type CustomerUpdateParams, type LineItem, type Product, type ProductOption, ProductStatus, type ProductVariant, type ShippingRate, type VariantOption, createStoreClient, betterStore as default };
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;
@@ -134,6 +132,37 @@ interface Customer$1 extends CustomerCreateParams {
134
132
  updatedAt: string;
135
133
  }
136
134
 
135
+ declare class Client {
136
+ /**
137
+ * Retrieve a checkout session by ID
138
+ */
139
+ retrieveCheckout(clientSecret: string, checkoutId: string): Promise<CheckoutSession>;
140
+ /**
141
+ * Update a checkout session
142
+ */
143
+ updateCheckout(clientSecret: string, checkoutId: string, params: CheckoutUpdateParams): Promise<CheckoutSession>;
144
+ /**
145
+ * Get shipping rates for a checkout session
146
+ */
147
+ getCheckoutShippingRates(clientSecret: string, checkoutId: string): Promise<ShippingRate[]>;
148
+ /**
149
+ * Generate payment secret for a checkout session
150
+ */
151
+ generateCheckoutsPaymentSecret(clientSecret: string, checkoutId: string): Promise<string>;
152
+ /**
153
+ * Create a new customer
154
+ */
155
+ createCustomer(clientSecret: string, params: CustomerCreateParams): Promise<Customer$1>;
156
+ /**
157
+ * Retrieve a customer by ID or email
158
+ */
159
+ retrieveCustomer(clientSecret: string, idOrEmail: string): Promise<Customer$1>;
160
+ /**
161
+ * Update a customer
162
+ */
163
+ updateCustomer(clientSecret: string, customerId: string, params: CustomerUpdateParams): Promise<Customer$1>;
164
+ }
165
+
137
166
  declare class Customer {
138
167
  private apiClient;
139
168
  constructor(apiKey: string);
@@ -206,23 +235,13 @@ declare class Products {
206
235
  retrieve(productId: string): Promise<Product>;
207
236
  }
208
237
 
209
- type NextjsRouteConfig = {
210
- apiKey?: string;
211
- productionAllowedOrigins?: string[];
212
- };
213
- type BSClient = InstanceType<typeof BetterStore>;
214
- declare function createNextJSHandler(betterStore: BSClient, config?: NextjsRouteConfig): {
215
- GET(req: NextRequest): Promise<Response>;
216
- POST(req: NextRequest): Promise<Response>;
217
- PUT(req: NextRequest): Promise<Response>;
218
- DELETE(req: NextRequest): Promise<Response>;
219
- };
220
-
221
- declare class BetterStore {
238
+ declare function betterStore(config: {
239
+ apiKey: string;
240
+ }): {
222
241
  checkout: Checkout;
223
242
  products: Products;
224
243
  customer: Customer;
225
- constructor(apiKey: string);
226
- }
244
+ };
245
+ declare function createStoreClient(): Client;
227
246
 
228
- export { type Address, type CheckoutCreateParams, type CheckoutSession, type CheckoutUpdateParams, type Customer$1 as Customer, type CustomerAddress, type CustomerCreateParams, type CustomerUpdateParams, type LineItem, type Product, type ProductOption, ProductStatus, type ProductVariant, type ShippingRate, type VariantOption, createNextJSHandler, BetterStore as default };
247
+ export { type Address, type CheckoutCreateParams, type CheckoutSession, type CheckoutUpdateParams, type Customer$1 as Customer, type CustomerAddress, type CustomerCreateParams, type CustomerUpdateParams, type LineItem, type Product, type ProductOption, ProductStatus, type ProductVariant, type ShippingRate, type VariantOption, createStoreClient, betterStore as default };
package/dist/index.js CHANGED
@@ -51,8 +51,8 @@ var __async = (__this, __arguments, generator) => {
51
51
  var index_exports = {};
52
52
  __export(index_exports, {
53
53
  ProductStatus: () => ProductStatus,
54
- createNextJSHandler: () => createNextJSHandler,
55
- default: () => index_default
54
+ createStoreClient: () => createStoreClient,
55
+ default: () => betterStore
56
56
  });
57
57
  module.exports = __toCommonJS(index_exports);
58
58
 
@@ -165,6 +165,96 @@ var Checkout = class {
165
165
  };
166
166
  var checkout_default = Checkout;
167
167
 
168
+ // src/client/index.ts
169
+ var Client = class {
170
+ /**
171
+ * Retrieve a checkout session by ID
172
+ */
173
+ retrieveCheckout(clientSecret, checkoutId) {
174
+ return __async(this, null, function* () {
175
+ const apiClient = createApiClient(clientSecret);
176
+ const data = yield apiClient.get(
177
+ `/checkout/${checkoutId}`
178
+ );
179
+ return data;
180
+ });
181
+ }
182
+ /**
183
+ * Update a checkout session
184
+ */
185
+ updateCheckout(clientSecret, checkoutId, params) {
186
+ return __async(this, null, function* () {
187
+ const apiClient = createApiClient(clientSecret);
188
+ const data = yield apiClient.put(
189
+ `/checkout/${checkoutId}`,
190
+ params
191
+ );
192
+ return data;
193
+ });
194
+ }
195
+ /**
196
+ * Get shipping rates for a checkout session
197
+ */
198
+ getCheckoutShippingRates(clientSecret, checkoutId) {
199
+ return __async(this, null, function* () {
200
+ const apiClient = createApiClient(clientSecret);
201
+ const data = yield apiClient.get(
202
+ `/checkout/shipping/${checkoutId}`
203
+ );
204
+ return data;
205
+ });
206
+ }
207
+ /**
208
+ * Generate payment secret for a checkout session
209
+ */
210
+ generateCheckoutsPaymentSecret(clientSecret, checkoutId) {
211
+ return __async(this, null, function* () {
212
+ const apiClient = createApiClient(clientSecret);
213
+ const data = yield apiClient.post(
214
+ `/checkout/payment/${checkoutId}`
215
+ );
216
+ return data;
217
+ });
218
+ }
219
+ /**
220
+ * Create a new customer
221
+ */
222
+ createCustomer(clientSecret, params) {
223
+ return __async(this, null, function* () {
224
+ const apiClient = createApiClient(clientSecret);
225
+ const data = yield apiClient.post("/customers", params);
226
+ return data;
227
+ });
228
+ }
229
+ /**
230
+ * Retrieve a customer by ID or email
231
+ */
232
+ retrieveCustomer(clientSecret, idOrEmail) {
233
+ return __async(this, null, function* () {
234
+ const apiClient = createApiClient(clientSecret);
235
+ const data = yield apiClient.get(`/customers/${idOrEmail}`);
236
+ if (!data) {
237
+ throw new Error("Customer not found");
238
+ }
239
+ return data;
240
+ });
241
+ }
242
+ /**
243
+ * Update a customer
244
+ */
245
+ updateCustomer(clientSecret, customerId, params) {
246
+ return __async(this, null, function* () {
247
+ const apiClient = createApiClient(clientSecret);
248
+ const data = yield apiClient.put(
249
+ `/customers/${customerId}`,
250
+ params
251
+ );
252
+ return data;
253
+ });
254
+ }
255
+ };
256
+ var client_default = Client;
257
+
168
258
  // src/customer/index.ts
169
259
  var Customer = class {
170
260
  constructor(apiKey) {
@@ -239,304 +329,6 @@ var Products = class {
239
329
  };
240
330
  var products_default = Products;
241
331
 
242
- // src/proxies/next-js.ts
243
- var defaultBetterStoreRoutes = {
244
- checkout: {
245
- GET: (req, betterStore) => __async(void 0, null, function* () {
246
- const { searchParams } = new URL(req.url);
247
- const checkoutId = searchParams.get("checkoutId");
248
- if (!checkoutId) {
249
- return new Response("Checkout ID is required", { status: 400 });
250
- }
251
- try {
252
- const checkout = yield betterStore.checkout.retrieve(checkoutId);
253
- return Response.json(checkout);
254
- } catch (error) {
255
- return new Response("Failed to fetch checkout", { status: 500 });
256
- }
257
- }),
258
- POST: (req, betterStore) => __async(void 0, null, function* () {
259
- try {
260
- const body = yield req.json();
261
- const checkout = yield betterStore.checkout.create(body);
262
- return Response.json(checkout);
263
- } catch (error) {
264
- return new Response("Failed to create checkout", { status: 500 });
265
- }
266
- }),
267
- PUT: (req, betterStore) => __async(void 0, null, function* () {
268
- const { searchParams } = new URL(req.url);
269
- const checkoutId = searchParams.get("checkoutId");
270
- if (!checkoutId) {
271
- return new Response("Checkout ID is required", { status: 400 });
272
- }
273
- try {
274
- const body = yield req.json();
275
- const checkout = yield betterStore.checkout.update(checkoutId, body);
276
- return Response.json(checkout);
277
- } catch (error) {
278
- return new Response("Failed to update checkout", { status: 500 });
279
- }
280
- })
281
- },
282
- "checkout/shipping": {
283
- GET: (req, betterStore) => __async(void 0, null, function* () {
284
- const { searchParams } = new URL(req.url);
285
- const checkoutId = searchParams.get("checkoutId");
286
- if (!checkoutId) {
287
- return new Response("Checkout ID is required", { status: 400 });
288
- }
289
- try {
290
- const rates = yield betterStore.checkout.getShippingRates(checkoutId);
291
- return Response.json(rates);
292
- } catch (error) {
293
- return new Response("Failed to get shipping rates", { status: 500 });
294
- }
295
- })
296
- },
297
- "checkout/payment": {
298
- POST: (req, betterStore) => __async(void 0, null, function* () {
299
- const { searchParams } = new URL(req.url);
300
- const checkoutId = searchParams.get("checkoutId");
301
- if (!checkoutId) {
302
- return new Response("Checkout ID is required", { status: 400 });
303
- }
304
- try {
305
- const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
306
- return Response.json({ clientSecret: secret });
307
- } catch (error) {
308
- return new Response("Failed to generate payment secret", {
309
- status: 500
310
- });
311
- }
312
- })
313
- },
314
- customer: {
315
- GET: (req, betterStore) => __async(void 0, null, function* () {
316
- const { searchParams } = new URL(req.url);
317
- const idOrEmail = searchParams.get("idOrEmail");
318
- if (!idOrEmail) {
319
- return new Response("Customer ID or email is required", {
320
- status: 400
321
- });
322
- }
323
- try {
324
- const customer = yield betterStore.customer.retrieve(idOrEmail);
325
- return Response.json(customer);
326
- } catch (error) {
327
- return new Response("Failed to fetch customer", { status: 500 });
328
- }
329
- }),
330
- POST: (req, betterStore) => __async(void 0, null, function* () {
331
- try {
332
- const body = yield req.json();
333
- const customer = yield betterStore.customer.create(body);
334
- return Response.json(customer);
335
- } catch (error) {
336
- return new Response("Failed to create customer", { status: 500 });
337
- }
338
- }),
339
- PUT: (req, betterStore) => __async(void 0, null, function* () {
340
- const { searchParams } = new URL(req.url);
341
- const customerId = searchParams.get("customerId");
342
- if (!customerId) {
343
- return new Response("Customer ID is required", { status: 400 });
344
- }
345
- try {
346
- const body = yield req.json();
347
- const customer = yield betterStore.customer.update(customerId, body);
348
- return Response.json(customer);
349
- } catch (error) {
350
- return new Response("Failed to update customer", { status: 500 });
351
- }
352
- }),
353
- DELETE: (req, betterStore) => __async(void 0, null, function* () {
354
- const { searchParams } = new URL(req.url);
355
- const customerId = searchParams.get("customerId");
356
- if (!customerId) {
357
- return new Response("Customer ID is required", { status: 400 });
358
- }
359
- try {
360
- yield betterStore.customer.delete(customerId);
361
- return new Response(null, { status: 204 });
362
- } catch (error) {
363
- return new Response("Failed to delete customer", { status: 500 });
364
- }
365
- })
366
- },
367
- product: {
368
- GET: (req, betterStore) => __async(void 0, null, function* () {
369
- const { searchParams } = new URL(req.url);
370
- const productId = searchParams.get("productId");
371
- try {
372
- if (productId) {
373
- const product = yield betterStore.products.retrieve(productId);
374
- return Response.json(product);
375
- } else {
376
- const products = yield betterStore.products.list();
377
- return Response.json(products);
378
- }
379
- } catch (error) {
380
- return new Response("Failed to fetch products", { status: 500 });
381
- }
382
- })
383
- }
384
- };
385
- function addCORSHeaders(response, origin, allowedOrigins) {
386
- if (origin && allowedOrigins.includes(origin)) {
387
- response.headers.set("Access-Control-Allow-Origin", origin);
388
- }
389
- response.headers.set(
390
- "Access-Control-Allow-Methods",
391
- "GET, POST, PUT, DELETE, OPTIONS"
392
- );
393
- response.headers.set(
394
- "Access-Control-Allow-Headers",
395
- "Content-Type, Authorization"
396
- );
397
- return response;
398
- }
399
- function createNextJSHandler(betterStore, config = {}) {
400
- const { apiKey, productionAllowedOrigins = [] } = config;
401
- const isProduction = process.env.NODE_ENV === "production";
402
- function validateRequest(req) {
403
- return __async(this, null, function* () {
404
- if (apiKey) {
405
- const authHeader = req.headers.get("Authorization");
406
- const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
407
- if (!providedKey || providedKey !== apiKey) {
408
- return new Response("Unauthorized", {
409
- status: 401,
410
- headers: { "WWW-Authenticate": "Bearer" }
411
- });
412
- }
413
- }
414
- const origin = req.headers.get("origin");
415
- if (isProduction && productionAllowedOrigins.length > 0) {
416
- if (!origin || !productionAllowedOrigins.includes(origin)) {
417
- return new Response("Unauthorized", { status: 403 });
418
- }
419
- }
420
- return null;
421
- });
422
- }
423
- function getRouteFromPath(pathname) {
424
- const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
425
- const relevantPath = cleanPath.split("betterstore/")[1] || "";
426
- return relevantPath;
427
- }
428
- return {
429
- GET(req) {
430
- return __async(this, null, function* () {
431
- var _a2;
432
- const validationError = yield validateRequest(req);
433
- if (validationError)
434
- return addCORSHeaders(
435
- validationError,
436
- req.headers.get("origin"),
437
- productionAllowedOrigins
438
- );
439
- const route = getRouteFromPath(new URL(req.url).pathname);
440
- const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
441
- if (!handler) {
442
- return addCORSHeaders(
443
- new Response(`Route not found: ${route}`, { status: 404 }),
444
- req.headers.get("origin"),
445
- productionAllowedOrigins
446
- );
447
- }
448
- const response = yield handler(req, betterStore);
449
- return addCORSHeaders(
450
- response,
451
- req.headers.get("origin"),
452
- productionAllowedOrigins
453
- );
454
- });
455
- },
456
- POST(req) {
457
- return __async(this, null, function* () {
458
- var _a2;
459
- const validationError = yield validateRequest(req);
460
- if (validationError)
461
- return addCORSHeaders(
462
- validationError,
463
- req.headers.get("origin"),
464
- productionAllowedOrigins
465
- );
466
- const route = getRouteFromPath(new URL(req.url).pathname);
467
- const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
468
- if (!handler) {
469
- return addCORSHeaders(
470
- new Response(`Route not found: ${route}`, { status: 404 }),
471
- req.headers.get("origin"),
472
- productionAllowedOrigins
473
- );
474
- }
475
- const response = yield handler(req, betterStore);
476
- return addCORSHeaders(
477
- response,
478
- req.headers.get("origin"),
479
- productionAllowedOrigins
480
- );
481
- });
482
- },
483
- PUT(req) {
484
- return __async(this, null, function* () {
485
- var _a2;
486
- const validationError = yield validateRequest(req);
487
- if (validationError)
488
- return addCORSHeaders(
489
- validationError,
490
- req.headers.get("origin"),
491
- productionAllowedOrigins
492
- );
493
- const route = getRouteFromPath(new URL(req.url).pathname);
494
- const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
495
- if (!handler) {
496
- return addCORSHeaders(
497
- new Response(`Route not found: ${route}`, { status: 404 }),
498
- req.headers.get("origin"),
499
- productionAllowedOrigins
500
- );
501
- }
502
- const response = yield handler(req, betterStore);
503
- return addCORSHeaders(
504
- response,
505
- req.headers.get("origin"),
506
- productionAllowedOrigins
507
- );
508
- });
509
- },
510
- DELETE(req) {
511
- return __async(this, null, function* () {
512
- var _a2;
513
- const validationError = yield validateRequest(req);
514
- if (validationError)
515
- return addCORSHeaders(
516
- validationError,
517
- req.headers.get("origin"),
518
- productionAllowedOrigins
519
- );
520
- const route = getRouteFromPath(new URL(req.url).pathname);
521
- const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
522
- if (!handler) {
523
- return addCORSHeaders(
524
- new Response(`Route not found: ${route}`, { status: 404 }),
525
- req.headers.get("origin"),
526
- productionAllowedOrigins
527
- );
528
- }
529
- const response = yield handler(req, betterStore);
530
- return addCORSHeaders(
531
- response,
532
- req.headers.get("origin"),
533
- productionAllowedOrigins
534
- );
535
- });
536
- }
537
- };
538
- }
539
-
540
332
  // src/products/types.ts
541
333
  var ProductStatus = /* @__PURE__ */ ((ProductStatus2) => {
542
334
  ProductStatus2["DRAFT"] = "DRAFT";
@@ -546,20 +338,21 @@ var ProductStatus = /* @__PURE__ */ ((ProductStatus2) => {
546
338
  })(ProductStatus || {});
547
339
 
548
340
  // src/index.ts
549
- var BetterStore = class {
550
- // private apiKey: string;
551
- constructor(apiKey) {
552
- if (!apiKey) {
553
- throw new Error("API key is required.");
554
- }
555
- this.checkout = new checkout_default(apiKey);
556
- this.products = new products_default(apiKey);
557
- this.customer = new customer_default(apiKey);
341
+ function betterStore(config) {
342
+ if (!config.apiKey) {
343
+ throw new Error("API key is required.");
558
344
  }
559
- };
560
- var index_default = BetterStore;
345
+ return {
346
+ checkout: new checkout_default(config.apiKey),
347
+ products: new products_default(config.apiKey),
348
+ customer: new customer_default(config.apiKey)
349
+ };
350
+ }
351
+ function createStoreClient() {
352
+ return new client_default();
353
+ }
561
354
  // Annotate the CommonJS export names for ESM import in node:
562
355
  0 && (module.exports = {
563
356
  ProductStatus,
564
- createNextJSHandler
357
+ createStoreClient
565
358
  });
package/dist/index.mjs CHANGED
@@ -128,6 +128,96 @@ var Checkout = class {
128
128
  };
129
129
  var checkout_default = Checkout;
130
130
 
131
+ // src/client/index.ts
132
+ var Client = class {
133
+ /**
134
+ * Retrieve a checkout session by ID
135
+ */
136
+ retrieveCheckout(clientSecret, checkoutId) {
137
+ return __async(this, null, function* () {
138
+ const apiClient = createApiClient(clientSecret);
139
+ const data = yield apiClient.get(
140
+ `/checkout/${checkoutId}`
141
+ );
142
+ return data;
143
+ });
144
+ }
145
+ /**
146
+ * Update a checkout session
147
+ */
148
+ updateCheckout(clientSecret, checkoutId, params) {
149
+ return __async(this, null, function* () {
150
+ const apiClient = createApiClient(clientSecret);
151
+ const data = yield apiClient.put(
152
+ `/checkout/${checkoutId}`,
153
+ params
154
+ );
155
+ return data;
156
+ });
157
+ }
158
+ /**
159
+ * Get shipping rates for a checkout session
160
+ */
161
+ getCheckoutShippingRates(clientSecret, checkoutId) {
162
+ return __async(this, null, function* () {
163
+ const apiClient = createApiClient(clientSecret);
164
+ const data = yield apiClient.get(
165
+ `/checkout/shipping/${checkoutId}`
166
+ );
167
+ return data;
168
+ });
169
+ }
170
+ /**
171
+ * Generate payment secret for a checkout session
172
+ */
173
+ generateCheckoutsPaymentSecret(clientSecret, checkoutId) {
174
+ return __async(this, null, function* () {
175
+ const apiClient = createApiClient(clientSecret);
176
+ const data = yield apiClient.post(
177
+ `/checkout/payment/${checkoutId}`
178
+ );
179
+ return data;
180
+ });
181
+ }
182
+ /**
183
+ * Create a new customer
184
+ */
185
+ createCustomer(clientSecret, params) {
186
+ return __async(this, null, function* () {
187
+ const apiClient = createApiClient(clientSecret);
188
+ const data = yield apiClient.post("/customers", params);
189
+ return data;
190
+ });
191
+ }
192
+ /**
193
+ * Retrieve a customer by ID or email
194
+ */
195
+ retrieveCustomer(clientSecret, idOrEmail) {
196
+ return __async(this, null, function* () {
197
+ const apiClient = createApiClient(clientSecret);
198
+ const data = yield apiClient.get(`/customers/${idOrEmail}`);
199
+ if (!data) {
200
+ throw new Error("Customer not found");
201
+ }
202
+ return data;
203
+ });
204
+ }
205
+ /**
206
+ * Update a customer
207
+ */
208
+ updateCustomer(clientSecret, customerId, params) {
209
+ return __async(this, null, function* () {
210
+ const apiClient = createApiClient(clientSecret);
211
+ const data = yield apiClient.put(
212
+ `/customers/${customerId}`,
213
+ params
214
+ );
215
+ return data;
216
+ });
217
+ }
218
+ };
219
+ var client_default = Client;
220
+
131
221
  // src/customer/index.ts
132
222
  var Customer = class {
133
223
  constructor(apiKey) {
@@ -202,304 +292,6 @@ var Products = class {
202
292
  };
203
293
  var products_default = Products;
204
294
 
205
- // src/proxies/next-js.ts
206
- var defaultBetterStoreRoutes = {
207
- checkout: {
208
- GET: (req, betterStore) => __async(void 0, null, function* () {
209
- const { searchParams } = new URL(req.url);
210
- const checkoutId = searchParams.get("checkoutId");
211
- if (!checkoutId) {
212
- return new Response("Checkout ID is required", { status: 400 });
213
- }
214
- try {
215
- const checkout = yield betterStore.checkout.retrieve(checkoutId);
216
- return Response.json(checkout);
217
- } catch (error) {
218
- return new Response("Failed to fetch checkout", { status: 500 });
219
- }
220
- }),
221
- POST: (req, betterStore) => __async(void 0, null, function* () {
222
- try {
223
- const body = yield req.json();
224
- const checkout = yield betterStore.checkout.create(body);
225
- return Response.json(checkout);
226
- } catch (error) {
227
- return new Response("Failed to create checkout", { status: 500 });
228
- }
229
- }),
230
- PUT: (req, betterStore) => __async(void 0, null, function* () {
231
- const { searchParams } = new URL(req.url);
232
- const checkoutId = searchParams.get("checkoutId");
233
- if (!checkoutId) {
234
- return new Response("Checkout ID is required", { status: 400 });
235
- }
236
- try {
237
- const body = yield req.json();
238
- const checkout = yield betterStore.checkout.update(checkoutId, body);
239
- return Response.json(checkout);
240
- } catch (error) {
241
- return new Response("Failed to update checkout", { status: 500 });
242
- }
243
- })
244
- },
245
- "checkout/shipping": {
246
- GET: (req, betterStore) => __async(void 0, null, function* () {
247
- const { searchParams } = new URL(req.url);
248
- const checkoutId = searchParams.get("checkoutId");
249
- if (!checkoutId) {
250
- return new Response("Checkout ID is required", { status: 400 });
251
- }
252
- try {
253
- const rates = yield betterStore.checkout.getShippingRates(checkoutId);
254
- return Response.json(rates);
255
- } catch (error) {
256
- return new Response("Failed to get shipping rates", { status: 500 });
257
- }
258
- })
259
- },
260
- "checkout/payment": {
261
- POST: (req, betterStore) => __async(void 0, null, function* () {
262
- const { searchParams } = new URL(req.url);
263
- const checkoutId = searchParams.get("checkoutId");
264
- if (!checkoutId) {
265
- return new Response("Checkout ID is required", { status: 400 });
266
- }
267
- try {
268
- const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
269
- return Response.json({ clientSecret: secret });
270
- } catch (error) {
271
- return new Response("Failed to generate payment secret", {
272
- status: 500
273
- });
274
- }
275
- })
276
- },
277
- customer: {
278
- GET: (req, betterStore) => __async(void 0, null, function* () {
279
- const { searchParams } = new URL(req.url);
280
- const idOrEmail = searchParams.get("idOrEmail");
281
- if (!idOrEmail) {
282
- return new Response("Customer ID or email is required", {
283
- status: 400
284
- });
285
- }
286
- try {
287
- const customer = yield betterStore.customer.retrieve(idOrEmail);
288
- return Response.json(customer);
289
- } catch (error) {
290
- return new Response("Failed to fetch customer", { status: 500 });
291
- }
292
- }),
293
- POST: (req, betterStore) => __async(void 0, null, function* () {
294
- try {
295
- const body = yield req.json();
296
- const customer = yield betterStore.customer.create(body);
297
- return Response.json(customer);
298
- } catch (error) {
299
- return new Response("Failed to create customer", { status: 500 });
300
- }
301
- }),
302
- PUT: (req, betterStore) => __async(void 0, null, function* () {
303
- const { searchParams } = new URL(req.url);
304
- const customerId = searchParams.get("customerId");
305
- if (!customerId) {
306
- return new Response("Customer ID is required", { status: 400 });
307
- }
308
- try {
309
- const body = yield req.json();
310
- const customer = yield betterStore.customer.update(customerId, body);
311
- return Response.json(customer);
312
- } catch (error) {
313
- return new Response("Failed to update customer", { status: 500 });
314
- }
315
- }),
316
- DELETE: (req, betterStore) => __async(void 0, null, function* () {
317
- const { searchParams } = new URL(req.url);
318
- const customerId = searchParams.get("customerId");
319
- if (!customerId) {
320
- return new Response("Customer ID is required", { status: 400 });
321
- }
322
- try {
323
- yield betterStore.customer.delete(customerId);
324
- return new Response(null, { status: 204 });
325
- } catch (error) {
326
- return new Response("Failed to delete customer", { status: 500 });
327
- }
328
- })
329
- },
330
- product: {
331
- GET: (req, betterStore) => __async(void 0, null, function* () {
332
- const { searchParams } = new URL(req.url);
333
- const productId = searchParams.get("productId");
334
- try {
335
- if (productId) {
336
- const product = yield betterStore.products.retrieve(productId);
337
- return Response.json(product);
338
- } else {
339
- const products = yield betterStore.products.list();
340
- return Response.json(products);
341
- }
342
- } catch (error) {
343
- return new Response("Failed to fetch products", { status: 500 });
344
- }
345
- })
346
- }
347
- };
348
- function addCORSHeaders(response, origin, allowedOrigins) {
349
- if (origin && allowedOrigins.includes(origin)) {
350
- response.headers.set("Access-Control-Allow-Origin", origin);
351
- }
352
- response.headers.set(
353
- "Access-Control-Allow-Methods",
354
- "GET, POST, PUT, DELETE, OPTIONS"
355
- );
356
- response.headers.set(
357
- "Access-Control-Allow-Headers",
358
- "Content-Type, Authorization"
359
- );
360
- return response;
361
- }
362
- function createNextJSHandler(betterStore, config = {}) {
363
- const { apiKey, productionAllowedOrigins = [] } = config;
364
- const isProduction = process.env.NODE_ENV === "production";
365
- function validateRequest(req) {
366
- return __async(this, null, function* () {
367
- if (apiKey) {
368
- const authHeader = req.headers.get("Authorization");
369
- const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
370
- if (!providedKey || providedKey !== apiKey) {
371
- return new Response("Unauthorized", {
372
- status: 401,
373
- headers: { "WWW-Authenticate": "Bearer" }
374
- });
375
- }
376
- }
377
- const origin = req.headers.get("origin");
378
- if (isProduction && productionAllowedOrigins.length > 0) {
379
- if (!origin || !productionAllowedOrigins.includes(origin)) {
380
- return new Response("Unauthorized", { status: 403 });
381
- }
382
- }
383
- return null;
384
- });
385
- }
386
- function getRouteFromPath(pathname) {
387
- const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
388
- const relevantPath = cleanPath.split("betterstore/")[1] || "";
389
- return relevantPath;
390
- }
391
- return {
392
- GET(req) {
393
- return __async(this, null, function* () {
394
- var _a2;
395
- const validationError = yield validateRequest(req);
396
- if (validationError)
397
- return addCORSHeaders(
398
- validationError,
399
- req.headers.get("origin"),
400
- productionAllowedOrigins
401
- );
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 addCORSHeaders(
406
- new Response(`Route not found: ${route}`, { status: 404 }),
407
- req.headers.get("origin"),
408
- productionAllowedOrigins
409
- );
410
- }
411
- const response = yield handler(req, betterStore);
412
- return addCORSHeaders(
413
- response,
414
- req.headers.get("origin"),
415
- productionAllowedOrigins
416
- );
417
- });
418
- },
419
- POST(req) {
420
- return __async(this, null, function* () {
421
- var _a2;
422
- const validationError = yield validateRequest(req);
423
- if (validationError)
424
- return addCORSHeaders(
425
- validationError,
426
- req.headers.get("origin"),
427
- productionAllowedOrigins
428
- );
429
- const route = getRouteFromPath(new URL(req.url).pathname);
430
- const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
431
- if (!handler) {
432
- return addCORSHeaders(
433
- new Response(`Route not found: ${route}`, { status: 404 }),
434
- req.headers.get("origin"),
435
- productionAllowedOrigins
436
- );
437
- }
438
- const response = yield handler(req, betterStore);
439
- return addCORSHeaders(
440
- response,
441
- req.headers.get("origin"),
442
- productionAllowedOrigins
443
- );
444
- });
445
- },
446
- PUT(req) {
447
- return __async(this, null, function* () {
448
- var _a2;
449
- const validationError = yield validateRequest(req);
450
- if (validationError)
451
- return addCORSHeaders(
452
- validationError,
453
- req.headers.get("origin"),
454
- productionAllowedOrigins
455
- );
456
- const route = getRouteFromPath(new URL(req.url).pathname);
457
- const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
458
- if (!handler) {
459
- return addCORSHeaders(
460
- new Response(`Route not found: ${route}`, { status: 404 }),
461
- req.headers.get("origin"),
462
- productionAllowedOrigins
463
- );
464
- }
465
- const response = yield handler(req, betterStore);
466
- return addCORSHeaders(
467
- response,
468
- req.headers.get("origin"),
469
- productionAllowedOrigins
470
- );
471
- });
472
- },
473
- DELETE(req) {
474
- return __async(this, null, function* () {
475
- var _a2;
476
- const validationError = yield validateRequest(req);
477
- if (validationError)
478
- return addCORSHeaders(
479
- validationError,
480
- req.headers.get("origin"),
481
- productionAllowedOrigins
482
- );
483
- const route = getRouteFromPath(new URL(req.url).pathname);
484
- const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
485
- if (!handler) {
486
- return addCORSHeaders(
487
- new Response(`Route not found: ${route}`, { status: 404 }),
488
- req.headers.get("origin"),
489
- productionAllowedOrigins
490
- );
491
- }
492
- const response = yield handler(req, betterStore);
493
- return addCORSHeaders(
494
- response,
495
- req.headers.get("origin"),
496
- productionAllowedOrigins
497
- );
498
- });
499
- }
500
- };
501
- }
502
-
503
295
  // src/products/types.ts
504
296
  var ProductStatus = /* @__PURE__ */ ((ProductStatus2) => {
505
297
  ProductStatus2["DRAFT"] = "DRAFT";
@@ -509,20 +301,21 @@ var ProductStatus = /* @__PURE__ */ ((ProductStatus2) => {
509
301
  })(ProductStatus || {});
510
302
 
511
303
  // src/index.ts
512
- var BetterStore = class {
513
- // private apiKey: string;
514
- constructor(apiKey) {
515
- if (!apiKey) {
516
- throw new Error("API key is required.");
517
- }
518
- this.checkout = new checkout_default(apiKey);
519
- this.products = new products_default(apiKey);
520
- this.customer = new customer_default(apiKey);
304
+ function betterStore(config) {
305
+ if (!config.apiKey) {
306
+ throw new Error("API key is required.");
521
307
  }
522
- };
523
- var index_default = BetterStore;
308
+ return {
309
+ checkout: new checkout_default(config.apiKey),
310
+ products: new products_default(config.apiKey),
311
+ customer: new customer_default(config.apiKey)
312
+ };
313
+ }
314
+ function createStoreClient() {
315
+ return new client_default();
316
+ }
524
317
  export {
525
318
  ProductStatus,
526
- createNextJSHandler,
527
- index_default as default
319
+ createStoreClient,
320
+ betterStore as default
528
321
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/sdk",
3
- "version": "0.2.13",
3
+ "version": "0.3.0",
4
4
  "description": "E-commerce for Developers",
5
5
  "private": false,
6
6
  "publishConfig": {