@versacommerce/versacommerce-js-sdk 0.5.0 → 0.5.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/README.md
CHANGED
|
@@ -2,45 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
Frontend JavaScript SDK for VersaCommerce shops that allows you to access products, collections, pages, blogs, navigation, and carts from any website.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
```
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
});
|
|
7
|
+
```bash
|
|
8
|
+
npm install @versacommerce/versacommerce-js-sdk
|
|
9
|
+
```
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
client.findProduct('xyz1').then((product) => {
|
|
14
|
-
console.log(product.title); // => 'Ein Beispielprodukt'
|
|
15
|
-
console.log(product.price); // => 5.95
|
|
16
|
-
product.images.forEach((image) => {
|
|
17
|
-
console.log(image.resize(50, 50));
|
|
18
|
-
// => 'http://img.versacommerce.io/...'
|
|
19
|
-
});
|
|
20
|
-
});
|
|
11
|
+
Or via script tag:
|
|
21
12
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
collection.products.forEach((product) => {
|
|
26
|
-
console.log(product.featuredImage.resize(50, 50));
|
|
27
|
-
// => 'http://img.versacommerce.io/...'
|
|
28
|
-
});
|
|
29
|
-
});
|
|
13
|
+
```html
|
|
14
|
+
<script src="https://unpkg.com/@versacommerce/versacommerce-js-sdk"></script>
|
|
15
|
+
```
|
|
30
16
|
|
|
31
|
-
|
|
32
|
-
// retrieved from the local cache instead of firing another network request.
|
|
33
|
-
client.findProduct('abc1').then((product) => {
|
|
34
|
-
console.log(product.title); // => 'Ein Beispielprodukt'
|
|
35
|
-
});
|
|
17
|
+
## Quick Start
|
|
36
18
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const product = values[0];
|
|
40
|
-
const cart = values[1];
|
|
19
|
+
```javascript
|
|
20
|
+
import VersaCommerce from '@versacommerce/versacommerce-js-sdk';
|
|
41
21
|
|
|
42
|
-
|
|
22
|
+
const client = VersaCommerce.createClient({
|
|
23
|
+
shopName: 'my-shop' // subdomain of your shop
|
|
43
24
|
});
|
|
25
|
+
|
|
26
|
+
// Fetch a product
|
|
27
|
+
const product = await client.findProduct(123);
|
|
28
|
+
console.log(product.title, product.price);
|
|
29
|
+
|
|
30
|
+
// Add to cart
|
|
31
|
+
const cart = await client.getCart();
|
|
32
|
+
cart.addItem(product);
|
|
44
33
|
```
|
|
45
34
|
|
|
46
35
|
## API Reference
|
|
@@ -48,9 +37,8 @@ Promise.all([client.findProduct('xyz1'), client.getCart()]).then((values) => {
|
|
|
48
37
|
### Shop
|
|
49
38
|
|
|
50
39
|
```javascript
|
|
51
|
-
// Get shop metadata
|
|
52
40
|
client.getShop().then((shop) => {
|
|
53
|
-
console.log(shop.name); // => "
|
|
41
|
+
console.log(shop.name); // => "My Shop"
|
|
54
42
|
console.log(shop.currency); // => "EUR"
|
|
55
43
|
console.log(shop.productsCount); // => 15
|
|
56
44
|
console.log(shop.collectionsCount); // => 9
|
|
@@ -61,30 +49,43 @@ client.getShop().then((shop) => {
|
|
|
61
49
|
|
|
62
50
|
```javascript
|
|
63
51
|
// Find by ID
|
|
64
|
-
client.findProduct(206301).then((product) => {
|
|
52
|
+
client.findProduct(206301).then((product) => {
|
|
53
|
+
console.log(product.title);
|
|
54
|
+
console.log(product.price);
|
|
55
|
+
product.images.forEach((image) => {
|
|
56
|
+
console.log(image.resize(50, 50));
|
|
57
|
+
});
|
|
58
|
+
});
|
|
65
59
|
|
|
66
60
|
// Find by handle (URL slug)
|
|
67
|
-
client.findProductByHandle('
|
|
68
|
-
console.log(product.title);
|
|
61
|
+
client.findProductByHandle('my-product').then((product) => {
|
|
62
|
+
console.log(product.title);
|
|
69
63
|
});
|
|
70
64
|
```
|
|
71
65
|
|
|
72
66
|
### Collections
|
|
73
67
|
|
|
74
68
|
```javascript
|
|
75
|
-
// Find by ID
|
|
76
|
-
client.findCollection(20053).then((collection) => { ... });
|
|
77
|
-
|
|
78
|
-
// Find by handle
|
|
79
|
-
client.findCollectionByHandle('sale').then((collection) => { ... });
|
|
80
|
-
|
|
81
69
|
// Get all collections
|
|
82
70
|
client.getCollections().then((collections) => {
|
|
83
71
|
collections.forEach((c) => console.log(c.title));
|
|
84
72
|
});
|
|
73
|
+
|
|
74
|
+
// Find by ID
|
|
75
|
+
client.findCollection(20053).then((collection) => {
|
|
76
|
+
console.log(collection.title);
|
|
77
|
+
collection.products.forEach((product) => {
|
|
78
|
+
console.log(product.title);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Find by handle
|
|
83
|
+
client.findCollectionByHandle('sale').then((collection) => {
|
|
84
|
+
console.log(collection.title);
|
|
85
|
+
});
|
|
85
86
|
```
|
|
86
87
|
|
|
87
|
-
### Pages
|
|
88
|
+
### Pages
|
|
88
89
|
|
|
89
90
|
```javascript
|
|
90
91
|
// Get all pages
|
|
@@ -94,8 +95,8 @@ client.getPages().then((pages) => {
|
|
|
94
95
|
|
|
95
96
|
// Find page by handle
|
|
96
97
|
client.findPage('impressum').then((page) => {
|
|
97
|
-
console.log(page.title);
|
|
98
|
-
console.log(page.content); //
|
|
98
|
+
console.log(page.title);
|
|
99
|
+
console.log(page.content); // HTML content
|
|
99
100
|
});
|
|
100
101
|
```
|
|
101
102
|
|
|
@@ -108,16 +109,16 @@ client.getBlogs().then((blogs) => {
|
|
|
108
109
|
});
|
|
109
110
|
|
|
110
111
|
// Find blog by handle
|
|
111
|
-
client.findBlog('
|
|
112
|
-
console.log(blog.title);
|
|
113
|
-
console.log(blog.articlesCount);
|
|
112
|
+
client.findBlog('news').then((blog) => {
|
|
113
|
+
console.log(blog.title);
|
|
114
|
+
console.log(blog.articlesCount);
|
|
114
115
|
});
|
|
115
116
|
|
|
116
117
|
// Find article
|
|
117
|
-
client.findArticle('
|
|
118
|
-
console.log(article.title);
|
|
119
|
-
console.log(article.author);
|
|
120
|
-
console.log(article.publishedAt);
|
|
118
|
+
client.findArticle('news', 'my-article').then((article) => {
|
|
119
|
+
console.log(article.title);
|
|
120
|
+
console.log(article.author);
|
|
121
|
+
console.log(article.publishedAt);
|
|
121
122
|
});
|
|
122
123
|
```
|
|
123
124
|
|
|
@@ -130,8 +131,8 @@ client.getLinklists().then((linklists) => {
|
|
|
130
131
|
});
|
|
131
132
|
|
|
132
133
|
// Find navigation by handle
|
|
133
|
-
client.findLinklist('
|
|
134
|
-
console.log(nav.title);
|
|
134
|
+
client.findLinklist('main-menu').then((nav) => {
|
|
135
|
+
console.log(nav.title);
|
|
135
136
|
nav.rootLinks.forEach((link) => {
|
|
136
137
|
console.log(link.title, link.url);
|
|
137
138
|
// Nested sub-navigation
|
|
@@ -145,173 +146,33 @@ client.findLinklist('haupt-navigation').then((nav) => {
|
|
|
145
146
|
### Cart
|
|
146
147
|
|
|
147
148
|
```javascript
|
|
149
|
+
// Get cart
|
|
148
150
|
client.getCart().then((cart) => {
|
|
149
151
|
console.log(cart.totalPrice);
|
|
150
152
|
cart.items.forEach((item) => console.log(item.title));
|
|
151
153
|
});
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
## Tech Stack
|
|
155
|
-
|
|
156
|
-
| Technology | Version | Purpose |
|
|
157
|
-
| -------------- | ------- | --------------------- |
|
|
158
|
-
| Node.js | 22 LTS | Runtime |
|
|
159
|
-
| Vite | 6.x | Build tool |
|
|
160
|
-
| Vitest | 3.x | Testing framework |
|
|
161
|
-
| ESLint | 9.x | Linting (Flat Config) |
|
|
162
|
-
| Prettier | 3.x | Code formatting |
|
|
163
|
-
| GitHub Actions | - | CI/CD |
|
|
164
|
-
| npm | - | Package registry |
|
|
165
154
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
```bash
|
|
172
|
-
# Install Node.js via nvm (recommended)
|
|
173
|
-
nvm install 22
|
|
174
|
-
nvm use 22
|
|
175
|
-
|
|
176
|
-
# Or use the .nvmrc file
|
|
177
|
-
nvm use
|
|
155
|
+
// Add item to cart
|
|
156
|
+
const product = await client.findProduct(123);
|
|
157
|
+
const cart = await client.getCart();
|
|
158
|
+
cart.addItem(product);
|
|
178
159
|
```
|
|
179
160
|
|
|
180
|
-
##
|
|
161
|
+
## Caching
|
|
181
162
|
|
|
182
|
-
|
|
183
|
-
npm install
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
## Development
|
|
187
|
-
|
|
188
|
-
```bash
|
|
189
|
-
# Start development server
|
|
190
|
-
npm run dev
|
|
191
|
-
|
|
192
|
-
# Run tests in watch mode
|
|
193
|
-
npm run test:watch
|
|
194
|
-
|
|
195
|
-
# Check linting
|
|
196
|
-
npm run lint
|
|
197
|
-
|
|
198
|
-
# Format code
|
|
199
|
-
npm run format
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
## Testing
|
|
203
|
-
|
|
204
|
-
```bash
|
|
205
|
-
# Run all tests
|
|
206
|
-
npm run test
|
|
207
|
-
|
|
208
|
-
# Run tests with coverage
|
|
209
|
-
npm run test:coverage
|
|
210
|
-
|
|
211
|
-
# Run tests in browser mode
|
|
212
|
-
npm run test:browser
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### Test Results
|
|
216
|
-
|
|
217
|
-
- **329 tests** across 39 test files
|
|
218
|
-
- Unit tests for all core modules, models, and utilities
|
|
219
|
-
- Integration tests for Store and Cart functionality
|
|
220
|
-
|
|
221
|
-
## Building
|
|
222
|
-
|
|
223
|
-
Creates a production build in `dist/`:
|
|
224
|
-
|
|
225
|
-
```bash
|
|
226
|
-
npm run build
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
### Build Output
|
|
230
|
-
|
|
231
|
-
| File | Size | Gzipped |
|
|
232
|
-
| ------------------------------ | -------- | -------- |
|
|
233
|
-
| `versacommerce-js-sdk.umd.cjs` | 20.89 kB | 5.84 kB |
|
|
234
|
-
| `versacommerce-js-sdk.es.js` | 62.24 kB | 11.89 kB |
|
|
235
|
-
|
|
236
|
-
Both UMD (for `<script>` tags) and ES modules are generated with source maps.
|
|
237
|
-
|
|
238
|
-
## Usage
|
|
239
|
-
|
|
240
|
-
### Via npm
|
|
241
|
-
|
|
242
|
-
```bash
|
|
243
|
-
npm install @versacommerce/versacommerce-js-sdk
|
|
244
|
-
```
|
|
163
|
+
The SDK automatically caches fetched resources. Subsequent requests for the same resource return the cached version:
|
|
245
164
|
|
|
246
165
|
```javascript
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
const client = VersaCommerce.createClient({ shopName: 'my-shop' });
|
|
250
|
-
```
|
|
166
|
+
// First call fetches from server
|
|
167
|
+
await client.findProduct(123);
|
|
251
168
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
For direct browser usage, include the UMD build from the `dist/` folder:
|
|
255
|
-
|
|
256
|
-
```html
|
|
257
|
-
<script src="path/to/versacommerce-js-sdk.umd.cjs"></script>
|
|
258
|
-
<script>
|
|
259
|
-
const client = VersaCommerce.createClient({ shopName: 'my-shop' });
|
|
260
|
-
</script>
|
|
169
|
+
// Second call returns cached version (no network request)
|
|
170
|
+
await client.findProduct(123);
|
|
261
171
|
```
|
|
262
172
|
|
|
263
|
-
##
|
|
264
|
-
|
|
265
|
-
The project uses **GitHub Actions** for continuous integration:
|
|
266
|
-
|
|
267
|
-
- **CI Pipeline** (`.github/workflows/ci.yml`): Runs on every push and PR
|
|
268
|
-
- Linting with ESLint
|
|
269
|
-
- Format check with Prettier
|
|
270
|
-
- All tests with coverage
|
|
271
|
-
- Production build
|
|
272
|
-
|
|
273
|
-
- **Publish Pipeline** (`.github/workflows/publish.yml`): Runs on release
|
|
274
|
-
- Publishes to npm
|
|
173
|
+
## Contributing
|
|
275
174
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
| Script | Description |
|
|
279
|
-
| ----------------------- | ------------------------------ |
|
|
280
|
-
| `npm run dev` | Start Vite dev server |
|
|
281
|
-
| `npm run build` | Production build |
|
|
282
|
-
| `npm run test` | Run tests once |
|
|
283
|
-
| `npm run test:watch` | Run tests in watch mode |
|
|
284
|
-
| `npm run test:coverage` | Run tests with coverage report |
|
|
285
|
-
| `npm run test:browser` | Run tests in browser mode |
|
|
286
|
-
| `npm run lint` | Check code with ESLint |
|
|
287
|
-
| `npm run lint:fix` | Fix ESLint issues |
|
|
288
|
-
| `npm run format` | Format code with Prettier |
|
|
289
|
-
| `npm run format:check` | Check formatting |
|
|
290
|
-
|
|
291
|
-
## Project Structure
|
|
292
|
-
|
|
293
|
-
```
|
|
294
|
-
versacommerce-js-sdk/
|
|
295
|
-
├── lib/ # Source code
|
|
296
|
-
│ ├── core/ # Core modules (Client, Adapter, Store, etc.)
|
|
297
|
-
│ ├── models/ # Data models (Product, Cart, Variant, etc.)
|
|
298
|
-
│ ├── utils/ # Utility functions
|
|
299
|
-
│ ├── versacommerce.js # Main entry point
|
|
300
|
-
│ ├── exports.js # Browser global export
|
|
301
|
-
│ └── version.js # Version number
|
|
302
|
-
├── test/ # Test files
|
|
303
|
-
│ ├── unit/ # Unit tests
|
|
304
|
-
│ ├── integration/ # Integration tests
|
|
305
|
-
│ ├── support/ # Test helpers
|
|
306
|
-
│ ├── fixtures/ # Test data
|
|
307
|
-
│ └── setup.js # Vitest setup
|
|
308
|
-
├── dist/ # Build output (generated)
|
|
309
|
-
├── .github/workflows/ # GitHub Actions
|
|
310
|
-
├── vite.config.js # Vite configuration
|
|
311
|
-
├── vitest.config.js # Vitest configuration
|
|
312
|
-
├── eslint.config.js # ESLint 9 flat config
|
|
313
|
-
└── .prettierrc # Prettier configuration
|
|
314
|
-
```
|
|
175
|
+
See [DEVELOPMENT.md](DEVELOPMENT.md) for development setup and guidelines.
|
|
315
176
|
|
|
316
177
|
## License
|
|
317
178
|
|