commerce-kit 0.0.41 → 0.2.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,234 @@
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
+ - **Class-based**: Industry standard pattern like Stripe, OpenAI, AWS SDK
11
+ - **Zero config**: Works out of the box with environment variables
12
+ - **Type-safe**: Full TypeScript with provider-specific types
13
+ - **GraphQL support**: Field selection for YNS, REST for Stripe
14
+ - **Multi-instance**: Perfect for testing and multi-tenant apps
14
15
 
15
- ## Installation
16
-
17
- Install the package via npm:
16
+ ## Install
18
17
 
19
18
  ```bash
20
19
  npm install commerce-kit
21
20
  ```
22
21
 
23
- ## Usage
22
+ ## Quick start
24
23
 
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:
24
+ **Zero config** - works automatically with environment variables:
26
25
 
27
26
  ```tsx
28
- import * as Commerce from "commerce-kit";
27
+ // Set environment variables:
28
+ // STRIPE_SECRET_KEY=sk_test_...
29
+ // or YNS_ENDPOINT=https://api.yournextstore.com and YNS_TOKEN=token_...
30
+
31
+ import commerce from "commerce-kit";
29
32
  import { formatMoney } from "commerce-kit/currencies";
30
- import Image from "next/image";
31
- import Link from "next/link";
32
33
 
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
+ **Explicit configuration**:
51
+
52
+ ```tsx
53
+ import { Commerce } from "commerce-kit";
54
+
55
+ // Create your own instance with explicit config
56
+ const commerce = new Commerce({
57
+ provider: "stripe",
58
+ stripe: { secretKey: "sk_test_..." }
59
+ });
60
+
61
+ const products = await commerce.product.browse({ first: 6 });
62
+ ```
63
+
64
+ ## Usage Patterns
65
+
66
+ ### 1. Default Instance (Recommended)
67
+
68
+ ```tsx
69
+ import commerce from "commerce-kit";
70
+
71
+ // Auto-detects from STRIPE_SECRET_KEY or YNS_ENDPOINT/YNS_TOKEN
72
+ const products = await commerce.product.browse({ first: 10 });
73
+ const cart = await commerce.cart.add({ variantId: "var_123", quantity: 1 });
74
+ ```
75
+
76
+ ### 2. Commerce Class
77
+
78
+ ```tsx
79
+ import { Commerce } from "commerce-kit";
80
+
81
+ // Zero-config constructor
82
+ const commerce = new Commerce();
83
+
84
+ // Or explicit configuration
85
+ const commerce = new Commerce({
86
+ provider: "stripe",
87
+ stripe: { secretKey: "sk_test_..." }
88
+ });
89
+
90
+ const products = await commerce.product.browse({ first: 10 });
91
+ ```
92
+
93
+ ### 3. Provider-Specific Classes
94
+
95
+ ```tsx
96
+ import { StripeCommerce, YNSCommerce } from "commerce-kit";
97
+
98
+ // Stripe-specific client
99
+ const stripe = new StripeCommerce({
100
+ secretKey: "sk_test_..."
101
+ });
102
+
103
+ // YNS-specific client with GraphQL support
104
+ const yns = new YNSCommerce({
105
+ endpoint: "https://api.yournextstore.com",
106
+ token: "token_..."
107
+ });
108
+
109
+ const stripeProducts = await stripe.product.browse({ first: 10 });
110
+ const ynsProducts = await yns.product.browse({
111
+ first: 10,
112
+ fields: ["id", "name", "price"] // GraphQL field selection
113
+ });
114
+ ```
115
+
116
+ ## API Reference
117
+
118
+ ### Products
119
+
120
+ ```tsx
121
+ import { Commerce } from "commerce-kit";
122
+ const commerce = new Commerce();
123
+
124
+ // Browse with filters
125
+ const products = await commerce.product.browse({
126
+ first: 10,
127
+ category: "electronics",
128
+ fields: ["id", "name", "price"] // GraphQL field selection (YNS only)
129
+ });
130
+
131
+ // Get single product
132
+ const product = await commerce.product.get({ slug: "awesome-laptop" });
133
+
134
+ // Search (YNS only)
135
+ const results = await commerce.product.search({ query: "macbook" });
136
+ ```
137
+
138
+ ### Cart
139
+
140
+ ```tsx
141
+ import { Commerce } from "commerce-kit";
142
+ const commerce = new Commerce();
143
+
144
+ // Add to cart
145
+ const result = await commerce.cart.add({
146
+ variantId: "variant_123",
147
+ quantity: 2
148
+ });
149
+
150
+ // Update quantity
151
+ await commerce.cart.update({
152
+ cartId: result.cartId,
153
+ variantId: "variant_123",
154
+ quantity: 3
155
+ });
156
+
157
+ // Get cart
158
+ const cartData = await commerce.cart.get({ cartId: result.cartId });
159
+ ```
160
+
161
+ ### Orders (YNS only)
162
+
163
+ ```tsx
164
+ import { YNSCommerce } from "commerce-kit";
165
+
166
+ const yns = new YNSCommerce({
167
+ endpoint: process.env.YNS_ENDPOINT,
168
+ token: process.env.YNS_TOKEN
169
+ });
170
+
171
+ // List orders
172
+ const orders = await yns.order.list({ first: 10 });
173
+
174
+ // Get single order
175
+ const order = await yns.order.get({ id: "order_123" });
176
+ ```
177
+
178
+ ## Environment Variables
179
+
180
+ Set these environment variables for automatic configuration:
181
+
182
+ ```bash
183
+ # For Stripe
184
+ STRIPE_SECRET_KEY=sk_test_...
185
+ STRIPE_TAG_PREFIX=my-store # optional
186
+
187
+ # For YNS
188
+ YNS_ENDPOINT=https://api.yournextstore.com
189
+ YNS_TOKEN=token_...
190
+
191
+ # Optional: explicitly choose provider
192
+ COMMERCE_PROVIDER=stripe # or "yns"
193
+ ```
194
+
195
+ ## Multi-Tenant & Testing
196
+
197
+ Perfect for multi-tenant applications and testing:
198
+
199
+ ```tsx
200
+ // Multi-tenant
201
+ class TenantService {
202
+ getCommerce(tenantId: string) {
203
+ const config = this.getTenantConfig(tenantId);
204
+ return new Commerce(config);
205
+ }
206
+ }
207
+
208
+ // Testing
209
+ describe("Product Service", () => {
210
+ it("should fetch products", async () => {
211
+ const commerce = new Commerce({
212
+ provider: "stripe",
213
+ stripe: { secretKey: "sk_test_mock" }
214
+ });
215
+
216
+ const products = await commerce.product.browse();
217
+ expect(products).toBeDefined();
218
+ });
219
+ });
220
+ ```
221
+
222
+ ## Why Class-Based?
65
223
 
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:
224
+ Following industry standards from Stripe, OpenAI, and AWS SDK:
67
225
 
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.
226
+ - **Explicit initialization** - Clear where configuration happens
227
+ - **Multiple instances** - Multi-tenant and testing support
228
+ - **Type safety** - Better TypeScript integration
229
+ - **No global state** - Each instance is isolated
230
+ - ✅ **Familiar pattern** - Same as `new Stripe()`, `new OpenAI()`
72
231
 
73
232
  ## License
74
233
 
75
- This project is licensed under the AGPL Version 3 license – see the LICENSE.md file for details.
234
+ AGPL-3.0 – see LICENSE.md