commerce-kit 0.0.40 → 0.1.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/README.md CHANGED
@@ -1,75 +1,143 @@
1
1
  # commerce-kit
2
2
 
3
- `commerce-kit` is a simple TypeScript library designed specifically for e-commerce applications built with Next.js. It provides a range of utilities to interact with products, categories, and orders, seamlessly integrating with Stripe for payment processing.
3
+ TypeScript SDK for building e-commerce apps. Works with Stripe and YNS APIs through a clean, unified interface. Built for Next.js, but plays nice with whatever you're using.
4
4
 
5
5
  Built by [Your Next Store](https://yournextstore.com).
6
6
 
7
7
  ## Features
8
8
 
9
- - **Product Browsing**: Easily fetch and display products.
10
- - **Category Management**: Manage and retrieve product categories.
11
- - **Order Handling**: Create and manage customer orders.
12
- - **Cart Operations**: Add products to cart and retrieve cart details.
13
- - **Stripe Integration**: Built-in support for payment processing using Stripe.
9
+ - **Multi-provider**: Switch between Stripe and YNS without changing code
10
+ - **GraphQL support**: Field selection for YNS, REST for Stripe
11
+ - **Type-safe**: Full TypeScript with provider-specific types
12
+ - **Tree-shakeable**: Only bundle what you use
13
+ - **Zero config**: Works out of the box, customize when needed
14
14
 
15
- ## Installation
16
-
17
- Install the package via npm:
15
+ ## Install
18
16
 
19
17
  ```bash
20
18
  npm install commerce-kit
21
19
  ```
22
20
 
23
- ## Usage
24
-
25
- `commerce-kit` is intended for use with Next.js applications. Here's a simple example of how to use it to fetch and display products:
21
+ ## Quick start
26
22
 
27
23
  ```tsx
28
24
  import * as Commerce from "commerce-kit";
29
25
  import { formatMoney } from "commerce-kit/currencies";
30
- import Image from "next/image";
31
- import Link from "next/link";
32
26
 
27
+ // Configure once
28
+ Commerce.configure({
29
+ provider: "stripe", // or "yns"
30
+ stripe: { secretKey: process.env.STRIPE_SECRET_KEY }
31
+ });
32
+
33
+ // Use everywhere
33
34
  export async function ProductList() {
34
- const products = await Commerce.productBrowse({ first: 6 });
35
+ const products = await Commerce.product.browse({ first: 6 });
35
36
 
36
37
  return (
37
- <ul>
38
- {products.map((product) => (
39
- <li key={product.id}>
40
- <Link href={`/product/${product.metadata.slug}`}>
41
- <article>
42
- {product.images[0] && (
43
- <Image src={product.images[0]} width={300} height={300} alt={product.name} />
44
- )}
45
- <h2>{product.name}</h2>
46
- {product.default_price.unit_amount && (
47
- <p>
48
- {formatMoney({
49
- amount: product.default_price.unit_amount,
50
- currency: product.default_price.currency,
51
- locale: "en-US",
52
- })}
53
- </p>
54
- )}
55
- </article>
56
- </Link>
57
- </li>
38
+ <div>
39
+ {products.data.map((product) => (
40
+ <div key={product.id}>
41
+ <h2>{product.name}</h2>
42
+ <p>{formatMoney({ amount: product.price, currency: product.currency })}</p>
43
+ </div>
58
44
  ))}
59
- </ul>
45
+ </div>
60
46
  );
61
47
  }
62
48
  ```
63
49
 
64
- ## Debugging
50
+ ## API
51
+
52
+ ### Products
53
+ ```tsx
54
+ // Browse with filters
55
+ const products = await Commerce.product.browse({
56
+ first: 10,
57
+ category: "electronics",
58
+ fields: ["id", "name", "price"] // GraphQL field selection (YNS only)
59
+ });
60
+
61
+ // Get single product
62
+ const product = await Commerce.product.get({ slug: "awesome-laptop" });
63
+
64
+ // Search
65
+ const results = await Commerce.product.search({ query: "macbook" });
66
+ ```
67
+
68
+ ### Cart
69
+ ```tsx
70
+ // Add to cart
71
+ const cart = await Commerce.cart.add({
72
+ variantId: "variant_123",
73
+ quantity: 2
74
+ });
75
+
76
+ // Update quantity
77
+ await Commerce.cart.update({
78
+ cartId: cart.cartId,
79
+ variantId: "variant_123",
80
+ quantity: 3
81
+ });
82
+
83
+ // Get cart
84
+ const cartData = await Commerce.cart.get({ cartId: cart.cartId });
85
+ ```
86
+
87
+ ### Provider switching
88
+ ```tsx
89
+ // Override provider per call
90
+ const stripeProducts = await Commerce.product.browse({
91
+ first: 10,
92
+ _provider: "stripe"
93
+ });
94
+
95
+ // Or use scoped instances
96
+ const yns = Commerce.withProvider("yns");
97
+ const stripe = Commerce.withProvider("stripe");
98
+
99
+ const ynsProducts = await yns.product.browse({ first: 10 });
100
+ const stripeProducts = await stripe.product.browse({ first: 10 });
101
+ ```
102
+
103
+ ## Providers
104
+
105
+ ### YNS
106
+ ```tsx
107
+ Commerce.configure({
108
+ provider: "yns",
109
+ yns: {
110
+ endpoint: "https://api.yournextstore.com",
111
+ token: process.env.YNS_API_TOKEN
112
+ }
113
+ });
114
+ ```
115
+
116
+ ### Stripe
117
+ ```tsx
118
+ Commerce.configure({
119
+ provider: "stripe",
120
+ stripe: {
121
+ secretKey: process.env.STRIPE_SECRET_KEY,
122
+ tagPrefix: "my-store" // optional
123
+ }
124
+ });
125
+ ```
126
+
127
+ ## Migration
128
+
129
+ Old way (still works):
130
+ ```tsx
131
+ const products = await Commerce.productBrowse({ first: 6 });
132
+ ```
65
133
 
66
- This library uses a custom logger to output debug information. To control the debug output, use the `LOG_LEVEL` environment variable. The following levels are supported:
134
+ New way (recommended):
135
+ ```tsx
136
+ const products = await Commerce.product.browse({ first: 6 });
137
+ ```
67
138
 
68
- - **ERROR** Critical issue for a specific request that needs immediate attention.
69
- - **WARN** – Something that should be reviewed eventually.
70
- - **LOG** – Details on regular operations.
71
- - **DEBUG** – Debug information, including `time` and `timeEnd` function outputs.
139
+ Same data, better DX, provider flexibility, GraphQL field selection.
72
140
 
73
141
  ## License
74
142
 
75
- This project is licensed under the AGPL Version 3 license – see the LICENSE.md file for details.
143
+ AGPL-3.0 – see LICENSE.md
@@ -4,7 +4,7 @@ type Money = {
4
4
  };
5
5
  declare const getStripeAmountFromDecimal: ({ amount: major, currency }: Money) => number;
6
6
  declare const getDecimalFromStripeAmount: ({ amount: minor, currency }: Money) => number;
7
- declare const formatMoney: ({ amount: minor, currency, locale, }: Money & {
7
+ declare const formatMoney: ({ amount: minor, currency, locale }: Money & {
8
8
  locale?: string;
9
9
  }) => string;
10
10