@versacommerce/versacommerce-js-sdk 0.5.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/LICENSE +21 -0
- package/README.md +318 -0
- package/dist/versacommerce-js-sdk.es.js +2680 -0
- package/dist/versacommerce-js-sdk.es.js.map +1 -0
- package/dist/versacommerce-js-sdk.umd.cjs +2 -0
- package/dist/versacommerce-js-sdk.umd.cjs.map +1 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 VersaCommerce
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# VersaCommerce JavaScript SDK
|
|
2
|
+
|
|
3
|
+
Frontend JavaScript SDK for VersaCommerce shops that allows you to access products, collections, pages, blogs, navigation, and carts from any website.
|
|
4
|
+
|
|
5
|
+
## Example Usage
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
const client = VersaCommerce.createClient({
|
|
9
|
+
shopName: 'mein-test-shop' // subdomain of your shop
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// fetch a single product
|
|
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
|
+
});
|
|
21
|
+
|
|
22
|
+
// fetch a single collection
|
|
23
|
+
client.findCollection('xyz1').then((collection) => {
|
|
24
|
+
console.log(collection.title); // => 'Eine Beispielgruppe'
|
|
25
|
+
collection.products.forEach((product) => {
|
|
26
|
+
console.log(product.featuredImage.resize(50, 50));
|
|
27
|
+
// => 'http://img.versacommerce.io/...'
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// fetch the same product again, but this time the product will be
|
|
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
|
+
});
|
|
36
|
+
|
|
37
|
+
// find a product and add to cart
|
|
38
|
+
Promise.all([client.findProduct('xyz1'), client.getCart()]).then((values) => {
|
|
39
|
+
const product = values[0];
|
|
40
|
+
const cart = values[1];
|
|
41
|
+
|
|
42
|
+
cart.addItem(product);
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## API Reference
|
|
47
|
+
|
|
48
|
+
### Shop
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
// Get shop metadata
|
|
52
|
+
client.getShop().then((shop) => {
|
|
53
|
+
console.log(shop.name); // => "Björn's Einkaufswelt"
|
|
54
|
+
console.log(shop.currency); // => "EUR"
|
|
55
|
+
console.log(shop.productsCount); // => 15
|
|
56
|
+
console.log(shop.collectionsCount); // => 9
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Products
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
// Find by ID
|
|
64
|
+
client.findProduct(206301).then((product) => { ... });
|
|
65
|
+
|
|
66
|
+
// Find by handle (URL slug)
|
|
67
|
+
client.findProductByHandle('ball-in-vielen-farben').then((product) => {
|
|
68
|
+
console.log(product.title); // => "Ball"
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Collections
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
// Find by ID
|
|
76
|
+
client.findCollection(20053).then((collection) => { ... });
|
|
77
|
+
|
|
78
|
+
// Find by handle
|
|
79
|
+
client.findCollectionByHandle('sale').then((collection) => { ... });
|
|
80
|
+
|
|
81
|
+
// Get all collections
|
|
82
|
+
client.getCollections().then((collections) => {
|
|
83
|
+
collections.forEach((c) => console.log(c.title));
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Pages (CMS)
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
// Get all pages
|
|
91
|
+
client.getPages().then((pages) => {
|
|
92
|
+
pages.forEach((page) => console.log(page.handle, page.title));
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Find page by handle
|
|
96
|
+
client.findPage('impressum').then((page) => {
|
|
97
|
+
console.log(page.title); // => "Impressum"
|
|
98
|
+
console.log(page.content); // => "<h2>Verantwortlich</h2>..."
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Blogs & Articles
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
// Get all blogs
|
|
106
|
+
client.getBlogs().then((blogs) => {
|
|
107
|
+
blogs.forEach((blog) => console.log(blog.title));
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Find blog by handle
|
|
111
|
+
client.findBlog('mein-blog').then((blog) => {
|
|
112
|
+
console.log(blog.title); // => "Mein Blog"
|
|
113
|
+
console.log(blog.articlesCount); // => 5
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Find article
|
|
117
|
+
client.findArticle('mein-blog', 'mein-artikel').then((article) => {
|
|
118
|
+
console.log(article.title); // => "Mein Artikel"
|
|
119
|
+
console.log(article.author); // => "Max Mustermann"
|
|
120
|
+
console.log(article.publishedAt); // => "2025-01-01T12:00:00+01:00"
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Linklists (Navigation)
|
|
125
|
+
|
|
126
|
+
```javascript
|
|
127
|
+
// Get all navigation menus
|
|
128
|
+
client.getLinklists().then((linklists) => {
|
|
129
|
+
linklists.forEach((nav) => console.log(nav.handle, nav.title));
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Find navigation by handle
|
|
133
|
+
client.findLinklist('haupt-navigation').then((nav) => {
|
|
134
|
+
console.log(nav.title); // => "Haupt-Navigation"
|
|
135
|
+
nav.rootLinks.forEach((link) => {
|
|
136
|
+
console.log(link.title, link.url);
|
|
137
|
+
// Nested sub-navigation
|
|
138
|
+
link.children.forEach((child) => {
|
|
139
|
+
console.log(' -', child.title, child.url);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Cart
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
client.getCart().then((cart) => {
|
|
149
|
+
console.log(cart.totalPrice);
|
|
150
|
+
cart.items.forEach((item) => console.log(item.title));
|
|
151
|
+
});
|
|
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
|
+
|
|
166
|
+
## Prerequisites
|
|
167
|
+
|
|
168
|
+
- **Node.js 22** or higher (LTS recommended)
|
|
169
|
+
- **npm** (comes with Node.js)
|
|
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
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Installation
|
|
181
|
+
|
|
182
|
+
```bash
|
|
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
|
+
```
|
|
245
|
+
|
|
246
|
+
```javascript
|
|
247
|
+
import VersaCommerce from '@versacommerce/versacommerce-js-sdk';
|
|
248
|
+
|
|
249
|
+
const client = VersaCommerce.createClient({ shopName: 'my-shop' });
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Via Script Tag (UMD)
|
|
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>
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## CI/CD
|
|
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
|
|
275
|
+
|
|
276
|
+
## npm Scripts
|
|
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
|
+
```
|
|
315
|
+
|
|
316
|
+
## License
|
|
317
|
+
|
|
318
|
+
MIT - see [LICENSE](LICENSE)
|