@frontic/nuxt 0.1.0 → 0.1.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
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @frontic/nuxt
|
|
2
|
+
|
|
3
|
+
Nuxt module for integrating with [Frontstack](https://frontstack.dev) headless commerce. Provides auto-imported composables for data fetching with smart caching, page routing, and context management.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Smart caching** via [Pinia Colada](https://pinia-colada.esm.dev/) - stale-while-revalidate, request deduplication, SSR hydration
|
|
8
|
+
- **Type-safe composables** - Full TypeScript support with generated types from your Frontstack schema
|
|
9
|
+
- **Search & filtering** - Ready-to-use search state management with filters, sorting, and pagination
|
|
10
|
+
- **Page routing** - Dynamic page resolution with automatic redirects and 404 handling
|
|
11
|
+
- **Context management** - Locale and region switching with cookie persistence
|
|
12
|
+
- **Built-in proxy** - CORS-free API requests through your Nuxt server
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npx nuxi@latest module add @frontic/nuxt
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
// nuxt.config.ts
|
|
24
|
+
export default defineNuxtConfig({
|
|
25
|
+
modules: ['@frontic/nuxt'],
|
|
26
|
+
frontic: {
|
|
27
|
+
contextDomain: 'your-shop.com',
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Generate the Frontstack client:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npx @frontic/cli generate
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Composables
|
|
39
|
+
|
|
40
|
+
### useFronticPage
|
|
41
|
+
|
|
42
|
+
Dynamic page routing with automatic redirects and 404 handling:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const { page, data, type, block, alternates } = useFronticPage()
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### useFronticSearch
|
|
49
|
+
|
|
50
|
+
Full-featured search with filtering, sorting, and pagination:
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
const { result, state, searchTerm, filterResult, sortResult, loadNext } = useFronticSearch(
|
|
54
|
+
'ProductSearch',
|
|
55
|
+
{},
|
|
56
|
+
{
|
|
57
|
+
filter: {
|
|
58
|
+
select: ['properties.color', 'properties.size'],
|
|
59
|
+
label: { 'properties.color': 'Color' },
|
|
60
|
+
},
|
|
61
|
+
sorting: {
|
|
62
|
+
label: { 'name:asc': 'Name A-Z', 'price.amount:asc': 'Price: Low to High' },
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### useFronticListing
|
|
69
|
+
|
|
70
|
+
Simple listing access:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
const { listing, status, refresh } = useFronticListing('FeaturedProducts', { limit: 10 })
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### useFronticBlock
|
|
77
|
+
|
|
78
|
+
Fetch single blocks (products, categories):
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const { block, status } = useFronticBlock('Product', 'product-handle')
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### useFronticContext
|
|
85
|
+
|
|
86
|
+
Manage locale and region:
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
const { contexts, current, update } = useFronticContext()
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Configuration
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
export default defineNuxtConfig({
|
|
96
|
+
frontic: {
|
|
97
|
+
// Page routing
|
|
98
|
+
contextDomain: 'your-shop.com',
|
|
99
|
+
redirectOn301: true,
|
|
100
|
+
throwOn404: true,
|
|
101
|
+
|
|
102
|
+
// API proxy
|
|
103
|
+
proxy: true, // or '/api/custom-path'
|
|
104
|
+
fetchApiSecret: process.env.FRONTSTACK_FETCH_SECRET,
|
|
105
|
+
|
|
106
|
+
// Context cookies
|
|
107
|
+
contextCookieName: 'fs-context',
|
|
108
|
+
contextCookieMaxAge: 31536000,
|
|
109
|
+
|
|
110
|
+
// Composables
|
|
111
|
+
composables: true, // or ['page', 'search', 'listing', 'block', 'context', 'client']
|
|
112
|
+
|
|
113
|
+
// UI components (optional)
|
|
114
|
+
prefix: 'Ui',
|
|
115
|
+
componentDir: '@/components/ui',
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Documentation
|
|
121
|
+
|
|
122
|
+
Full documentation at [docs.frontstack.dev/reference/nuxt-module](https://docs.frontstack.dev/reference/nuxt-module)
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT
|
|
@@ -229,6 +229,13 @@ export function useFronticSearch(name, params, options) {
|
|
|
229
229
|
searchTerm.value = "";
|
|
230
230
|
await refreshListing();
|
|
231
231
|
}
|
|
232
|
+
async function reset() {
|
|
233
|
+
searchTerm.value = "";
|
|
234
|
+
_orFilterList.value = [];
|
|
235
|
+
_andFilterList.value = [];
|
|
236
|
+
_sortQuery.value = void 0;
|
|
237
|
+
await refreshListing();
|
|
238
|
+
}
|
|
232
239
|
const result = computed(() => data.value?.items);
|
|
233
240
|
return {
|
|
234
241
|
// ========== State ==========
|
|
@@ -316,6 +323,11 @@ export function useFronticSearch(name, params, options) {
|
|
|
316
323
|
/**
|
|
317
324
|
* Clear the search term and refresh results.
|
|
318
325
|
*/
|
|
319
|
-
resetSearch
|
|
326
|
+
resetSearch,
|
|
327
|
+
/**
|
|
328
|
+
* Reset all state (search term, filters, and sorting) to initial values.
|
|
329
|
+
* Equivalent to calling resetSearch(), resetFilter(), and resetSorting() together.
|
|
330
|
+
*/
|
|
331
|
+
reset
|
|
320
332
|
};
|
|
321
333
|
}
|
|
@@ -11,9 +11,25 @@ export function makeFilterState(filters) {
|
|
|
11
11
|
}
|
|
12
12
|
return result;
|
|
13
13
|
}
|
|
14
|
+
function sortByOrder(items, order, getKey) {
|
|
15
|
+
if (!order || order.length === 0) return items;
|
|
16
|
+
const orderMap = new Map(order.map((key, index) => [key, index]));
|
|
17
|
+
return [...items].sort((a, b) => {
|
|
18
|
+
const keyA = getKey(a);
|
|
19
|
+
const keyB = getKey(b);
|
|
20
|
+
const indexA = orderMap.get(keyA);
|
|
21
|
+
const indexB = orderMap.get(keyB);
|
|
22
|
+
if (indexA !== void 0 && indexB !== void 0) {
|
|
23
|
+
return indexA - indexB;
|
|
24
|
+
}
|
|
25
|
+
if (indexA !== void 0) return -1;
|
|
26
|
+
if (indexB !== void 0) return 1;
|
|
27
|
+
return 0;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
14
30
|
export function processAvailableFilters(rawFilters, config = {}) {
|
|
15
31
|
if (!rawFilters) return [];
|
|
16
|
-
const { select = [], mode = "include", label: labels = {} } = config;
|
|
32
|
+
const { select = [], mode = "include", label: labels = {}, sort: sortOrder } = config;
|
|
17
33
|
const availableFilters = [];
|
|
18
34
|
const responseFilterKeys = Object.keys(rawFilters);
|
|
19
35
|
for (const key of responseFilterKeys) {
|
|
@@ -29,11 +45,11 @@ export function processAvailableFilters(rawFilters, config = {}) {
|
|
|
29
45
|
options: filterOptions
|
|
30
46
|
});
|
|
31
47
|
}
|
|
32
|
-
return availableFilters;
|
|
48
|
+
return sortByOrder(availableFilters, sortOrder, (f) => f.key);
|
|
33
49
|
}
|
|
34
50
|
export function processAvailableSorts(config = {}) {
|
|
35
|
-
const { select = [], mode = "include", label: labels = {} } = config;
|
|
36
|
-
|
|
51
|
+
const { select = [], mode = "include", label: labels = {}, sort: sortOrder } = config;
|
|
52
|
+
const availableSorts = Object.entries(labels).filter(([value]) => {
|
|
37
53
|
if (select.length === 0) return true;
|
|
38
54
|
const isInSelect = select.includes(value);
|
|
39
55
|
return mode === "include" ? isInSelect : !isInSelect;
|
|
@@ -44,6 +60,7 @@ export function processAvailableSorts(config = {}) {
|
|
|
44
60
|
value
|
|
45
61
|
})
|
|
46
62
|
);
|
|
63
|
+
return sortByOrder(availableSorts, sortOrder, (s) => s.key);
|
|
47
64
|
}
|
|
48
65
|
export function applyFilterConfig(filters, config = {}) {
|
|
49
66
|
const { select = [], mode = "include", label: labels = {} } = config;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontic/nuxt",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Nuxt module for
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Nuxt module for Frontstack headless commerce - data fetching composables with smart caching, page routing, and context management",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Frontic",
|
|
7
7
|
"repository": {
|
|
@@ -19,7 +19,12 @@
|
|
|
19
19
|
"frontic",
|
|
20
20
|
"frontstack",
|
|
21
21
|
"vue",
|
|
22
|
-
"
|
|
22
|
+
"headless-commerce",
|
|
23
|
+
"e-commerce",
|
|
24
|
+
"pinia-colada",
|
|
25
|
+
"composables",
|
|
26
|
+
"ssr",
|
|
27
|
+
"typescript"
|
|
23
28
|
],
|
|
24
29
|
"type": "module",
|
|
25
30
|
"exports": {
|
|
@@ -30,7 +35,8 @@
|
|
|
30
35
|
},
|
|
31
36
|
"main": "./dist/module.mjs",
|
|
32
37
|
"files": [
|
|
33
|
-
"dist"
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md"
|
|
34
40
|
],
|
|
35
41
|
"dependencies": {
|
|
36
42
|
"@nuxt/kit": "^3.17.0",
|