@frontic/nuxt 0.1.11 → 0.1.13

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.
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Frontic Search Types
3
+ *
4
+ * Type definitions for the useFronticSearch composable.
5
+ * These types provide type-safe filtering, sorting, and pagination for Frontic search listings.
6
+ */
7
+ import type { Ref, ShallowRef } from 'vue';
8
+ import type { UseQueryReturn } from '@pinia/colada';
9
+ import type { Listings, ListingQueryFilters, ListingQuerySorts, Responses } from '@frontic/stack/generated-types';
10
+ import type { FronticRequestOptions, ListingFieldConfig, UiFilter, UiSort } from './listing.js';
11
+ export type { FronticRequestOptions, ListingFieldConfig, FilterOption, UiFilter, UiSort, InclusionMode } from './listing.js';
12
+ export type { Listings, ListingQueryFilters, ListingQuerySorts, Responses };
13
+ /**
14
+ * Search state exposed to consumers
15
+ * Contains both the current query state and processed data ready for UI consumption
16
+ *
17
+ * @template TFilters - Filter fields type for this listing (provides autocomplete for filter keys)
18
+ * @template TSorts - Sort fields type for this listing (provides autocomplete for sort fields)
19
+ */
20
+ export interface SearchState<TFilters = Record<string, unknown>, TSorts = Record<string, unknown>> {
21
+ /** Currently active query parameters */
22
+ active: {
23
+ /** Active filter values by field (keys typed based on listing schema) */
24
+ filter: Partial<Record<keyof TFilters & string, unknown>>;
25
+ /** Currently applied sort (undefined means default/backend sorting) */
26
+ sorting: {
27
+ field: keyof TSorts & string;
28
+ order: 'asc' | 'desc';
29
+ } | undefined;
30
+ /** Current search term */
31
+ search: string;
32
+ /** Current page number */
33
+ page: number;
34
+ /** Counts for active state */
35
+ count: {
36
+ /** Number of filter fields with active selections */
37
+ filter: number;
38
+ /** Number of active sorts (0 or 1) */
39
+ sorting: number;
40
+ };
41
+ };
42
+ /** Available options for UI controls */
43
+ available: {
44
+ /** Filter options ready for rendering (keys typed based on listing schema) */
45
+ filter: UiFilter<TFilters>[];
46
+ /** Sort options ready for rendering (keys typed based on listing schema) */
47
+ sorting: UiSort<TSorts>[];
48
+ /** Whether next page is available */
49
+ nextPage: boolean;
50
+ /** Whether previous page is available */
51
+ prevPage: boolean;
52
+ /** Counts for available options */
53
+ count: {
54
+ /** Number of filter fields available */
55
+ filter: number;
56
+ /** Number of sort options available */
57
+ sorting: number;
58
+ /** Total number of matching items */
59
+ result: number;
60
+ /** Total number of pages */
61
+ page: number;
62
+ };
63
+ };
64
+ }
65
+ /**
66
+ * Options for the useFronticSearch composable
67
+ *
68
+ * @template TSearch - The listing name key from Listings interface
69
+ * @template TFilters - Filter fields for this listing (defaults to ListingQueryFilters[TSearch] if available)
70
+ * @template TSorts - Sort fields for this listing (defaults to ListingQuerySorts[TSearch] if available)
71
+ */
72
+ export interface UseFronticSearchOptions<TSearch extends keyof Listings = keyof Listings, TFilters = TSearch extends keyof ListingQueryFilters ? ListingQueryFilters[TSearch] : Record<string, unknown>, TSorts = TSearch extends keyof ListingQuerySorts ? ListingQuerySorts[TSearch] : Record<string, unknown>> extends FronticRequestOptions {
73
+ /**
74
+ * Cache key for the query (defaults to name)
75
+ */
76
+ cacheKey?: string;
77
+ /**
78
+ * Minimum length of search term before triggering search (default: 2)
79
+ */
80
+ searchTermThreshold?: number;
81
+ /**
82
+ * Debounce delay in milliseconds for search input (default: 200)
83
+ * Controls how long to wait after the user stops typing before triggering a search request.
84
+ */
85
+ searchDebounce?: number;
86
+ /**
87
+ * Filter fields that should use OR logic (all others use AND)
88
+ * Type-safe: only allows valid filter field names for this listing
89
+ */
90
+ orFilter?: Array<keyof TFilters & string>;
91
+ /**
92
+ * Filter configuration using unified structure
93
+ * Type-safe: select and label must match actual filter fields
94
+ * @example
95
+ * // With select and label
96
+ * filter: {
97
+ * select: ['properties.color', 'properties.size'],
98
+ * mode: 'include',
99
+ * label: { 'properties.color': 'Color', 'properties.size': 'Size' }
100
+ * }
101
+ *
102
+ * // Just label (shows all filters with custom labels)
103
+ * filter: {
104
+ * label: { 'properties.color': 'Color' }
105
+ * }
106
+ *
107
+ * // Just select (shows only these filters)
108
+ * filter: {
109
+ * select: ['properties.color', 'properties.size']
110
+ * }
111
+ */
112
+ filter?: ListingFieldConfig<TFilters>;
113
+ /**
114
+ * Sort configuration using unified structure
115
+ * Type-safe: label must use valid sort field names with :asc/:desc suffix
116
+ *
117
+ * Special handling for 'default' sort:
118
+ * - The 'default' sort represents Frontstack backend's default sorting (no sort applied to query)
119
+ * - It's automatically included if a label is provided in sorting.label
120
+ * - Can be controlled via include/exclude mode like other sorts
121
+ * - Use resetSorting() to return to default sorting
122
+ *
123
+ * @example
124
+ * // Full configuration with default sort
125
+ * sorting: {
126
+ * label: {
127
+ * 'default': 'Relevance',
128
+ * 'name:asc': 'Name A-Z',
129
+ * 'price.amount:asc': 'Price Low-High'
130
+ * }
131
+ * }
132
+ *
133
+ * // Just label (all sorts allowed)
134
+ * sorting: {
135
+ * label: {
136
+ * 'name:asc': 'Name A-Z',
137
+ * 'name:desc': 'Name Z-A'
138
+ * }
139
+ * }
140
+ */
141
+ sorting?: ListingFieldConfig<TSorts>;
142
+ /**
143
+ * Disable search functionality (default: false)
144
+ */
145
+ disableSearch?: boolean;
146
+ /**
147
+ * Disable filtering functionality (default: false)
148
+ */
149
+ disableFilter?: boolean;
150
+ /**
151
+ * Disable sorting functionality (default: false)
152
+ */
153
+ disableSorting?: boolean;
154
+ /**
155
+ * Stale time in milliseconds (default: 5 minutes)
156
+ */
157
+ staleTime?: number;
158
+ }
159
+ /**
160
+ * Return type for the search composable
161
+ *
162
+ * @template TSearch - The listing name key from Listings interface
163
+ * @template TFilters - Filter fields for this listing
164
+ * @template TSorts - Sort fields for this listing
165
+ */
166
+ export interface UseSearchReturn<TSearch extends keyof Listings = keyof Listings, TFilters = TSearch extends keyof ListingQueryFilters ? ListingQueryFilters[TSearch] : Record<string, unknown>, TSorts = TSearch extends keyof ListingQuerySorts ? ListingQuerySorts[TSearch] : Record<string, unknown>> {
167
+ /** The full listing response containing items, pagination info, and filter metadata */
168
+ data: ShallowRef<(TSearch extends keyof Responses ? Responses[TSearch] : unknown) | undefined>;
169
+ /** The items array from the listing response */
170
+ result: Ref<unknown[] | undefined>;
171
+ /** Current fetch status of the search */
172
+ status: UseQueryReturn['status'];
173
+ /** Current state of the search including page, sort, search, and filter settings (fully typed) */
174
+ state: Ref<SearchState<TFilters, TSorts>>;
175
+ /** Current search term */
176
+ searchTerm: Ref<string>;
177
+ /**
178
+ * Loads more items in the current search results (infinite scroll)
179
+ */
180
+ loadNext: () => Promise<(TSearch extends keyof Responses ? Responses[TSearch] : unknown) | undefined>;
181
+ /**
182
+ * Fetches the search with current query parameters (uses cache if valid)
183
+ */
184
+ refresh: () => Promise<void>;
185
+ /**
186
+ * Forces a fresh fetch of the search, bypassing cache
187
+ */
188
+ refetch: () => Promise<void>;
189
+ /**
190
+ * Paginates the results in the specified direction
191
+ * @param direction - Direction to paginate ('next' or 'prev')
192
+ * @returns Promise<boolean> - Returns true if pagination was successful
193
+ */
194
+ paginate: (direction: 'next' | 'prev') => Promise<boolean>;
195
+ /**
196
+ * Sorts the results by the specified field. Call without arguments to reset to default.
197
+ * Type-safe: only accepts valid sort strings for this search
198
+ * @param sortBy - Sort field and direction (e.g., 'name:desc' or 'name:asc'), or omit to reset
199
+ */
200
+ sortResult: (sortBy?: string) => Promise<void>;
201
+ /**
202
+ * Resets sorting to default (Frontstack backend default)
203
+ */
204
+ resetSorting: () => Promise<void>;
205
+ /**
206
+ * Adds a filter option to the specified filter field
207
+ * Type-safe: field and value types are validated
208
+ * @param filterField - The field to filter on (autocomplete available)
209
+ * @param filterOption - The option value to add
210
+ */
211
+ addFilter: <K extends keyof TFilters & string>(filterField: K, filterOption: TFilters[K]) => Promise<void>;
212
+ /**
213
+ * Removes a filter option from the specified filter field
214
+ * Type-safe: field and value types are validated
215
+ * @param filterField - The field to remove the filter from (autocomplete available)
216
+ * @param filterOption - The option value to remove (optional - omit to remove entire filter)
217
+ */
218
+ removeFilter: <K extends keyof TFilters & string>(filterField: K, filterOption?: TFilters[K]) => Promise<void>;
219
+ /**
220
+ * Applies filter options to the specified filter field
221
+ * Type-safe: field and value types are validated
222
+ * @param filterField - The field to filter on (autocomplete available)
223
+ * @param filterOptions - Array of filter values to apply
224
+ */
225
+ filterResult: <K extends keyof TFilters & string>(filterField: K, filterOptions: Array<TFilters[K]>) => Promise<void>;
226
+ /**
227
+ * Resets filters either for a specific field or all filters if no field specified
228
+ * Type-safe: field names are validated
229
+ * @param filterField - Optional field to reset. If omitted, all filters are reset
230
+ */
231
+ resetFilter: (filterField?: keyof TFilters & string) => Promise<void>;
232
+ /**
233
+ * Clears the search term
234
+ */
235
+ resetSearch: () => Promise<void>;
236
+ /**
237
+ * Resets all state (search term, filters, and sorting) to initial values
238
+ * Equivalent to calling resetSearch(), resetFilter(), and resetSorting() together
239
+ */
240
+ reset: () => Promise<void>;
241
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Filter Helper Utilities
3
+ *
4
+ * Functions for creating, managing, and manipulating filters for Frontstack listings.
5
+ */
6
+ /**
7
+ * Filter type for equals comparison
8
+ */
9
+ export interface EqualsFilter<T = Record<string, unknown>> {
10
+ type: 'equals';
11
+ field: keyof T & string;
12
+ value: T[keyof T] | T[keyof T][];
13
+ }
14
+ /**
15
+ * Logical filter type for combining filters with AND/OR/NOT
16
+ */
17
+ export interface LogicalFilter<T = Record<string, unknown>> {
18
+ type: 'and' | 'or' | 'not';
19
+ filter: Array<EqualsFilter<T> | LogicalFilter<T>>;
20
+ }
21
+ /**
22
+ * Creates an equals filter for the API query
23
+ *
24
+ * @param field - The field to filter on
25
+ * @param value - The value(s) to filter by
26
+ * @returns An equals filter object
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const colorFilter = makeEqualsFilter('properties.color', ['Red', 'Blue'])
31
+ * // { type: 'equals', field: 'properties.color', value: ['Red', 'Blue'] }
32
+ * ```
33
+ */
34
+ export declare function makeEqualsFilter<T = Record<string, unknown>>(field: keyof T & string, value: T[keyof T] | T[keyof T][]): EqualsFilter<T>;
35
+ /**
36
+ * Creates a logical filter (AND/OR) for the API query
37
+ *
38
+ * @param type - The logical operator ('and' | 'or')
39
+ * @param filters - The filters to combine
40
+ * @returns An array containing a single logical filter, or empty array if no filters
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const orFilters = makeLogicalFilter('or', [
45
+ * { type: 'equals', field: 'color', value: 'Red' },
46
+ * { type: 'equals', field: 'color', value: 'Blue' }
47
+ * ])
48
+ * // [{ type: 'or', filter: [...] }]
49
+ * ```
50
+ */
51
+ export declare function makeLogicalFilter<T = Record<string, unknown>>(type: 'and' | 'or', filters: EqualsFilter<T>[]): LogicalFilter<T>[];
52
+ /**
53
+ * Manages filter operations (add/update) on a filter list
54
+ * If a filter for the field exists, it updates it; otherwise, adds a new filter.
55
+ *
56
+ * @param filterList - The current list of filters
57
+ * @param field - The field to filter on
58
+ * @param value - The value(s) to filter by
59
+ * @returns A new filter list with the updated/added filter
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * let filters: EqualsFilter[] = []
64
+ * filters = manageFilter(filters, 'color', ['Red'])
65
+ * // [{ type: 'equals', field: 'color', value: ['Red'] }]
66
+ *
67
+ * filters = manageFilter(filters, 'color', ['Red', 'Blue'])
68
+ * // [{ type: 'equals', field: 'color', value: ['Red', 'Blue'] }]
69
+ * ```
70
+ */
71
+ export declare function manageFilter<T = Record<string, unknown>>(filterList: EqualsFilter<T>[], field: keyof T & string, value: T[keyof T] | T[keyof T][]): EqualsFilter<T>[];
72
+ /**
73
+ * Removes a filter or specific option from a filter list
74
+ *
75
+ * @param filterList - The current list of filters
76
+ * @param field - The field to remove the filter from
77
+ * @param option - Optional specific option to remove. If omitted, removes entire filter
78
+ * @returns A new filter list with the filter/option removed
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * // Remove specific option
83
+ * let filters = [{ type: 'equals', field: 'color', value: ['Red', 'Blue'] }]
84
+ * filters = removeFilter(filters, 'color', 'Red')
85
+ * // [{ type: 'equals', field: 'color', value: ['Blue'] }]
86
+ *
87
+ * // Remove entire filter
88
+ * filters = removeFilter(filters, 'color')
89
+ * // []
90
+ * ```
91
+ */
92
+ export declare function removeFilter<T = Record<string, unknown>>(filterList: EqualsFilter<T>[], field: keyof T & string, option?: T[keyof T]): EqualsFilter<T>[];
93
+ /**
94
+ * Checks if a filter exists for a specific field
95
+ *
96
+ * @param filterList - The list of filters to check
97
+ * @param field - The field to check for
98
+ * @returns True if a filter exists for the field
99
+ */
100
+ export declare function hasFilter<T = Record<string, unknown>>(filterList: EqualsFilter<T>[], field: keyof T & string): boolean;
101
+ /**
102
+ * Gets the current values for a specific filter field
103
+ *
104
+ * @param filterList - The list of filters
105
+ * @param field - The field to get values for
106
+ * @returns Array of current filter values, or empty array if no filter exists
107
+ */
108
+ export declare function getFilterValues<T = Record<string, unknown>>(filterList: EqualsFilter<T>[], field: keyof T & string): T[keyof T][];
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Frontic Runtime Utilities
3
+ *
4
+ * Export all utility functions for auto-import.
5
+ */
6
+ export { makeEqualsFilter, makeLogicalFilter, manageFilter, removeFilter, hasFilter, getFilterValues, type EqualsFilter, type LogicalFilter } from './filter-helpers.js';
7
+ export { makeFilterState, processAvailableFilters, processAvailableSorts, applyFilterConfig, countActiveFilters, parseSortString, isSortAllowed } from './state-helpers.js';
@@ -0,0 +1,120 @@
1
+ /**
2
+ * State Helper Utilities
3
+ *
4
+ * Functions for processing and transforming listing state for UI consumption.
5
+ */
6
+ import type { ListingFieldConfig, UiFilter, UiSort, FilterOption } from '../types/listing.js';
7
+ /**
8
+ * Frontstack filter response structure
9
+ */
10
+ interface FrontstackFilterResponse {
11
+ [key: string]: FilterOption[] | undefined;
12
+ }
13
+ /**
14
+ * Extracts active (selected) filters from the API response
15
+ *
16
+ * @param filters - The filter response from Frontstack API
17
+ * @returns Record of active filters keyed by field name
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * const response = {
22
+ * 'properties.color': [
23
+ * { option: 'Red', value: 'red', count: 10, selected: true },
24
+ * { option: 'Blue', value: 'blue', count: 5, selected: false }
25
+ * ]
26
+ * }
27
+ * makeFilterState(response)
28
+ * // { 'properties.color': [{ option: 'Red', value: 'red', count: 10, selected: true }] }
29
+ * ```
30
+ */
31
+ export declare function makeFilterState(filters: FrontstackFilterResponse | undefined): Record<string, FilterOption[]>;
32
+ /**
33
+ * Processes raw filter response into UI-ready filter objects
34
+ *
35
+ * @template TFilters - Filter fields type for this listing (provides typed keys)
36
+ * @param rawFilters - Raw filter response from Frontstack API
37
+ * @param config - Filter configuration (select, mode, label, sort)
38
+ * @returns Array of UiFilter objects ready for rendering (with typed keys)
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * const filters = processAvailableFilters<ProductSearchFilters>(response.filter, {
43
+ * select: ['properties.color'],
44
+ * mode: 'include',
45
+ * label: { 'properties.color': 'Color' },
46
+ * sort: ['properties.color', 'properties.size']
47
+ * })
48
+ * // [{ key: 'properties.color', label: 'Color', options: [...] }]
49
+ * ```
50
+ */
51
+ export declare function processAvailableFilters<TFilters = Record<string, unknown>>(rawFilters: FrontstackFilterResponse | undefined, config?: ListingFieldConfig): UiFilter<TFilters>[];
52
+ /**
53
+ * Processes sort configuration into UI-ready sort options
54
+ *
55
+ * @template TSorts - Sort fields type for this listing (provides typed keys)
56
+ * @param config - Sort configuration (select, mode, label, sort)
57
+ * @returns Array of UiSort objects ready for rendering (with typed keys)
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const sorts = processAvailableSorts<ProductSearchSorts>({
62
+ * label: {
63
+ * 'default': 'Relevance',
64
+ * 'name:asc': 'Name A-Z',
65
+ * 'price.amount:asc': 'Price Low-High'
66
+ * },
67
+ * sort: ['default', 'price.amount:asc', 'name:asc']
68
+ * })
69
+ * // [
70
+ * // { key: 'default', label: 'Relevance', value: 'default' },
71
+ * // { key: 'price.amount:asc', label: 'Price Low-High', value: 'price.amount:asc' },
72
+ * // { key: 'name:asc', label: 'Name A-Z', value: 'name:asc' },
73
+ * // ]
74
+ * ```
75
+ */
76
+ export declare function processAvailableSorts<TSorts = Record<string, unknown>>(config?: ListingFieldConfig): UiSort<TSorts>[];
77
+ /**
78
+ * Applies filter configuration to transform the filter state
79
+ * Handles include/exclude mode and label renaming
80
+ *
81
+ * @param filters - The current filter state
82
+ * @param config - Filter configuration
83
+ * @returns Transformed filter state with applied configuration
84
+ */
85
+ export declare function applyFilterConfig(filters: Record<string, FilterOption[]>, config?: ListingFieldConfig): Record<string, FilterOption[]>;
86
+ /**
87
+ * Calculates the number of active filters from a filter state
88
+ *
89
+ * @param filters - The filter state record
90
+ * @returns Number of fields with active filters
91
+ */
92
+ export declare function countActiveFilters(filters: Record<string, FilterOption[]> | undefined): number;
93
+ /**
94
+ * Parses a sort string into field and order components
95
+ *
96
+ * @param sortString - Sort string in format "field:order" or "default"
97
+ * @returns Object with field and order, or undefined for "default"
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * parseSortString('name:asc')
102
+ * // { field: 'name', order: 'asc' }
103
+ *
104
+ * parseSortString('default')
105
+ * // undefined
106
+ * ```
107
+ */
108
+ export declare function parseSortString(sortString: string): {
109
+ field: string;
110
+ order: 'asc' | 'desc';
111
+ } | undefined;
112
+ /**
113
+ * Checks if a sort string is allowed based on configuration
114
+ *
115
+ * @param sortString - Sort string to check
116
+ * @param config - Sort configuration
117
+ * @returns True if the sort is allowed
118
+ */
119
+ export declare function isSortAllowed(sortString: string, config?: ListingFieldConfig): boolean;
120
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontic/nuxt",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Nuxt module for Frontstack headless commerce - zero config data fetching composables with smart caching, page routing, and context management",
5
5
  "license": "MIT",
6
6
  "author": "Frontic",