@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.
- package/dist/module.mjs +4 -0
- package/dist/runtime/composables/blocks.d.ts +68 -0
- package/dist/runtime/composables/index.d.ts +21 -0
- package/dist/runtime/composables/listings.d.ts +64 -0
- package/dist/runtime/composables/pages.d.ts +65 -0
- package/dist/runtime/composables/useFronticBlock.d.ts +54 -0
- package/dist/runtime/composables/useFronticClient.d.ts +56 -0
- package/dist/runtime/composables/useFronticClient.js +5 -4
- package/dist/runtime/composables/useFronticContext.d.ts +81 -0
- package/dist/runtime/composables/useFronticContext.js +6 -4
- package/dist/runtime/composables/useFronticListing.d.ts +58 -0
- package/dist/runtime/composables/useFronticPage.d.ts +96 -0
- package/dist/runtime/composables/useFronticPage.js +5 -3
- package/dist/runtime/composables/useFronticSearch.d.ts +124 -0
- package/dist/runtime/composables/useFronticSearch.js +4 -3
- package/dist/runtime/frontic-stack.d.ts +142 -0
- package/dist/runtime/server/api/frontic/[...path].d.ts +2 -0
- package/dist/runtime/server/api/frontic/[...path].js +1 -1
- package/dist/runtime/types/index.d.ts +13 -0
- package/dist/runtime/types/listing.d.ts +354 -0
- package/dist/runtime/types/search.d.ts +241 -0
- package/dist/runtime/utils/filter-helpers.d.ts +108 -0
- package/dist/runtime/utils/index.d.ts +7 -0
- package/dist/runtime/utils/state-helpers.d.ts +120 -0
- package/package.json +1 -1
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useFronticSearch Composable
|
|
3
|
+
*
|
|
4
|
+
* Full-featured composable for managing Frontic search listings with filtering,
|
|
5
|
+
* sorting, pagination, and search capabilities.
|
|
6
|
+
*
|
|
7
|
+
* For simple listing access without search features, use useFronticListing instead.
|
|
8
|
+
*/
|
|
9
|
+
import type { MaybeRef } from 'vue';
|
|
10
|
+
import type { Listings, ListingParameters, ListingQueryFilters, ListingQuerySorts } from '@frontic/stack/generated-types';
|
|
11
|
+
import type { UseFronticSearchOptions, SearchState } from '../types/search.js';
|
|
12
|
+
/**
|
|
13
|
+
* Full-featured composable for managing Frontic search listings with filtering, sorting, and pagination
|
|
14
|
+
*
|
|
15
|
+
* @param name - The search listing name (e.g., 'ProductSearch', 'CategoryProducts')
|
|
16
|
+
* @param params - Parameters to pass to the listing endpoint
|
|
17
|
+
* @param options - Optional configuration for filtering, sorting, and caching
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const productSearch = useFronticSearch('ProductSearch', {}, {
|
|
22
|
+
* orFilter: ['properties.color', 'options.size'],
|
|
23
|
+
* searchTermThreshold: 2,
|
|
24
|
+
* filter: {
|
|
25
|
+
* select: ['properties.color', 'properties.size'],
|
|
26
|
+
* label: { 'properties.color': 'Color' }
|
|
27
|
+
* },
|
|
28
|
+
* sorting: {
|
|
29
|
+
* label: { 'name:asc': 'Name A-Z', 'price.amount:asc': 'Price Low-High' }
|
|
30
|
+
* }
|
|
31
|
+
* })
|
|
32
|
+
*
|
|
33
|
+
* // Use the search
|
|
34
|
+
* await productSearch.filterResult('properties.color', ['Red', 'Blue'])
|
|
35
|
+
* await productSearch.sortResult('name:asc')
|
|
36
|
+
* await productSearch.loadNext()
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function useFronticSearch<TSearch extends keyof Listings, TFilters extends Record<string, unknown> = TSearch extends keyof ListingQueryFilters ? ListingQueryFilters[TSearch] : Record<string, unknown>, TSorts extends Record<string, unknown> = TSearch extends keyof ListingQuerySorts ? ListingQuerySorts[TSearch] : Record<string, unknown>>(name: TSearch, params: MaybeRef<TSearch extends keyof ListingParameters ? ListingParameters[TSearch] : Record<string, unknown>>, options?: UseFronticSearchOptions<TSearch, TFilters, TSorts>): {
|
|
40
|
+
/**
|
|
41
|
+
* The full listing response containing items, pagination info, and filter metadata.
|
|
42
|
+
*/
|
|
43
|
+
data: import("vue").ShallowRef<unknown>;
|
|
44
|
+
/**
|
|
45
|
+
* The items array from the listing response.
|
|
46
|
+
*/
|
|
47
|
+
result: import("vue").ComputedRef<unknown[] | undefined>;
|
|
48
|
+
/**
|
|
49
|
+
* Query status: 'pending' | 'error' | 'success'
|
|
50
|
+
*/
|
|
51
|
+
status: import("vue").ShallowRef<import("@pinia/colada").DataStateStatus>;
|
|
52
|
+
/**
|
|
53
|
+
* Current search state including active filters, sorts, pagination, and available options.
|
|
54
|
+
* Use this to build filter/sort UI components.
|
|
55
|
+
*/
|
|
56
|
+
state: import("@vue/reactivity").Ref<SearchState<TFilters, TSorts>, SearchState<TFilters, TSorts>>;
|
|
57
|
+
/**
|
|
58
|
+
* Reactive search term. Bind to an input field for live search.
|
|
59
|
+
* Search triggers automatically when term length exceeds searchTermThreshold.
|
|
60
|
+
*/
|
|
61
|
+
searchTerm: import("@vue/reactivity").Ref<string, string>;
|
|
62
|
+
/**
|
|
63
|
+
* Load the next page of results (infinite scroll).
|
|
64
|
+
* Appends new items to existing results.
|
|
65
|
+
*/
|
|
66
|
+
loadNext: (vars: void) => Promise<unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* Refresh the listing data (uses cache if valid).
|
|
69
|
+
* Call after external changes that should trigger a refresh.
|
|
70
|
+
*/
|
|
71
|
+
refresh: () => Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Force a fresh fetch, bypassing cache.
|
|
74
|
+
* Use when you need guaranteed fresh data.
|
|
75
|
+
*/
|
|
76
|
+
refetch: () => Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Navigate to next or previous page.
|
|
79
|
+
* @param direction - 'next' or 'prev'
|
|
80
|
+
* @returns true if navigation was successful, false if no more pages
|
|
81
|
+
*/
|
|
82
|
+
paginate: (direction: "next" | "prev") => Promise<boolean>;
|
|
83
|
+
/**
|
|
84
|
+
* Sort results by a specific field. Call without arguments to reset to default.
|
|
85
|
+
* @param sortBy - Sort string in format 'field:asc' or 'field:desc', or omit to reset
|
|
86
|
+
*/
|
|
87
|
+
sortResult: (sortBy?: string) => Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Reset sorting to default (backend default order).
|
|
90
|
+
*/
|
|
91
|
+
resetSorting: () => Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Set filter values for a specific field (replaces existing values).
|
|
94
|
+
* @param filterField - The field to filter on (e.g., 'properties.color')
|
|
95
|
+
* @param filterOptions - Array of values to filter by
|
|
96
|
+
*/
|
|
97
|
+
filterResult: <K extends keyof TFilters & string>(filterField: K, filterOptions: Array<TFilters[K]>) => Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Add a single filter option to a field (keeps existing values).
|
|
100
|
+
* @param filterField - The field to filter on
|
|
101
|
+
* @param filterOption - The value to add
|
|
102
|
+
*/
|
|
103
|
+
addFilter: <K extends keyof TFilters & string>(filterField: K, filterOption: TFilters[K]) => Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Remove a filter option from a field, or remove all values for that field.
|
|
106
|
+
* @param filterField - The field to remove filter from
|
|
107
|
+
* @param filterOption - Optional specific value to remove (removes all if omitted)
|
|
108
|
+
*/
|
|
109
|
+
removeFilter: <K extends keyof TFilters & string>(filterField: K, filterOption?: TFilters[K]) => Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Reset filters for a specific field or all fields.
|
|
112
|
+
* @param filterField - Optional field to reset (resets all if omitted)
|
|
113
|
+
*/
|
|
114
|
+
resetFilter: (filterField?: keyof TFilters & string) => Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Clear the search term and refresh results.
|
|
117
|
+
*/
|
|
118
|
+
resetSearch: () => Promise<void>;
|
|
119
|
+
/**
|
|
120
|
+
* Reset all state (search term, filters, and sorting) to initial values.
|
|
121
|
+
* Equivalent to calling resetSearch(), resetFilter(), and resetSorting() together.
|
|
122
|
+
*/
|
|
123
|
+
reset: () => Promise<void>;
|
|
124
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { useState
|
|
1
|
+
import { useState } from "nuxt/app";
|
|
2
|
+
import { computed, watch, ref } from "vue";
|
|
2
3
|
import { useQuery, useMutation, useQueryCache } from "@pinia/colada";
|
|
3
4
|
import { useDebounceFn } from "@vueuse/core";
|
|
4
5
|
import { makeLogicalFilter, manageFilter, removeFilter } from "../utils/filter-helpers.js";
|
|
@@ -99,8 +100,8 @@ export function useFronticSearch(name, params, options) {
|
|
|
99
100
|
}
|
|
100
101
|
});
|
|
101
102
|
if (!disableSearch) {
|
|
102
|
-
watch(searchTerm, (
|
|
103
|
-
if (
|
|
103
|
+
watch(searchTerm, () => {
|
|
104
|
+
if (searchTerm.value.trim().length > searchTermThreshold || searchState.value.active.search) {
|
|
104
105
|
triggerSearchFromInput();
|
|
105
106
|
}
|
|
106
107
|
});
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ambient type declarations for @frontic/stack/* virtual modules.
|
|
3
|
+
*
|
|
4
|
+
* At runtime, @frontic/stack/* resolves to the user's .frontic/ or .frontstack/
|
|
5
|
+
* directory via aliases configured by the @frontic/nuxt module.
|
|
6
|
+
* These declarations provide fallback types so that:
|
|
7
|
+
* 1. The module builds with proper .d.ts output (mkdist can resolve imports)
|
|
8
|
+
* 2. vue-tsc can typecheck without the generated client being present
|
|
9
|
+
*
|
|
10
|
+
* When the user generates their client (npx @frontic/cli generate), the actual
|
|
11
|
+
* generated types take precedence via the path alias.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
declare module '@frontic/stack/generated-client' {
|
|
15
|
+
import type { Blocks, Listings, ListingParameters, ListingQueryFilters, ListingQuerySorts, Responses } from '@frontic/stack/generated-types'
|
|
16
|
+
import type { Query } from '@frontic/stack/query-types'
|
|
17
|
+
|
|
18
|
+
export interface RequestOptions {
|
|
19
|
+
requestUrl?: string
|
|
20
|
+
contextKey?: string
|
|
21
|
+
contextDomain?: string
|
|
22
|
+
proxyUrl?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ClientConfig {
|
|
26
|
+
proxyUrl?: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface FrontstackClient {
|
|
30
|
+
block: <T extends keyof Blocks>(name: T, key: string, config?: RequestOptions) => Promise<Responses[T]>
|
|
31
|
+
listing: <T extends keyof Listings>(
|
|
32
|
+
name: T,
|
|
33
|
+
parameters: T extends keyof ListingParameters ? ListingParameters[T] : Record<string, unknown>,
|
|
34
|
+
config?: {
|
|
35
|
+
query?: Query<T extends keyof ListingQueryFilters ? ListingQueryFilters[T] : unknown, T extends keyof ListingQuerySorts ? ListingQuerySorts[T] : unknown>
|
|
36
|
+
} & RequestOptions
|
|
37
|
+
) => Promise<Responses[T]>
|
|
38
|
+
page: (slug: string, config?: RequestOptions) => Promise<Page>
|
|
39
|
+
context: (token: string, config?: RequestOptions) => Promise<Context>
|
|
40
|
+
contextList: (token?: string, config?: RequestOptions) => Promise<[ContextOption[], string]>
|
|
41
|
+
contextUpdate: (context: { region: string; locale: string }, token: string, config?: RequestOptions) => Promise<Context>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function createClient(config?: ClientConfig): FrontstackClient
|
|
45
|
+
export const client: FrontstackClient
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare module '@frontic/stack/generated-types' {
|
|
49
|
+
export interface Blocks {}
|
|
50
|
+
export interface BlockData {}
|
|
51
|
+
export interface Pages {}
|
|
52
|
+
export interface PageData {}
|
|
53
|
+
export interface Contexts {}
|
|
54
|
+
export interface ContextData {}
|
|
55
|
+
export interface Listings {}
|
|
56
|
+
export interface ListingParameters {}
|
|
57
|
+
export interface ListingQueryFilters {}
|
|
58
|
+
export interface ListingQuerySorts {}
|
|
59
|
+
export interface Responses {
|
|
60
|
+
[key: string]: unknown
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
declare module '@frontic/stack/query-types' {
|
|
65
|
+
export interface Query<TFilter = unknown, TSort = unknown> {
|
|
66
|
+
filter?: Filter[]
|
|
67
|
+
sort?: Sort | Sort[]
|
|
68
|
+
search?: string
|
|
69
|
+
limit?: number
|
|
70
|
+
page?: number
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface Sort {
|
|
74
|
+
field: string
|
|
75
|
+
order: 'asc' | 'desc'
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface EqualsFilter {
|
|
79
|
+
type: 'equals'
|
|
80
|
+
field: string
|
|
81
|
+
value: unknown | unknown[]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface LogicalFilter {
|
|
85
|
+
type: 'and' | 'or' | 'not'
|
|
86
|
+
filter: Array<EqualsFilter | LogicalFilter>
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type Filter = EqualsFilter | LogicalFilter | RangeFilter | ContainsFilter
|
|
90
|
+
|
|
91
|
+
export interface RangeFilter {
|
|
92
|
+
type: 'range'
|
|
93
|
+
field: string
|
|
94
|
+
from?: unknown
|
|
95
|
+
to?: unknown
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface ContainsFilter {
|
|
99
|
+
type: 'contains'
|
|
100
|
+
field: string
|
|
101
|
+
value: string
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Global types used by composables (Page, Context, etc.)
|
|
106
|
+
declare interface Page {
|
|
107
|
+
id?: string
|
|
108
|
+
slug?: string
|
|
109
|
+
title?: string
|
|
110
|
+
meta?: Record<string, unknown>
|
|
111
|
+
data?: Record<string, unknown>
|
|
112
|
+
route?: PageRoute
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
declare interface PageRoute {
|
|
116
|
+
code?: number
|
|
117
|
+
redirect?: { path: string }
|
|
118
|
+
alternates?: AlternateRoute[]
|
|
119
|
+
context?: { suggested?: string }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
declare interface AlternateRoute {
|
|
123
|
+
locale?: string
|
|
124
|
+
lang?: string
|
|
125
|
+
href: string
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
declare interface Context {
|
|
129
|
+
region: string
|
|
130
|
+
locale: string
|
|
131
|
+
scope: string
|
|
132
|
+
token: string
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
declare interface ContextOption {
|
|
136
|
+
region: string
|
|
137
|
+
currency: string
|
|
138
|
+
locales: Array<{
|
|
139
|
+
key: string
|
|
140
|
+
url: string
|
|
141
|
+
}>
|
|
142
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { proxyRequest, getHeader, createError, defineEventHandler } from "h3";
|
|
2
|
-
import { useRuntimeConfig } from "
|
|
2
|
+
import { useRuntimeConfig } from "nuxt/app";
|
|
3
3
|
export default defineEventHandler(async (event) => {
|
|
4
4
|
const targetUrl = getHeader(event, "fs-target-url");
|
|
5
5
|
if (!targetUrl) {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frontic Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Export all public types from this file.
|
|
5
|
+
*/
|
|
6
|
+
export type { FronticOptions, FronticComposable, FronticComposablesConfig } from '../../module.js';
|
|
7
|
+
export type { InclusionMode, FilterField, SortField, SortString, ListingFieldConfig, FilterOption, UiFilter, UiSort, } from './listing.js';
|
|
8
|
+
export type { SearchState, UseFronticSearchOptions, UseSearchReturn } from './search.js';
|
|
9
|
+
export type { UseFronticClientOptions } from '../composables/useFronticClient.js';
|
|
10
|
+
export type { UseFronticBlockOptions } from '../composables/useFronticBlock.js';
|
|
11
|
+
export type { UseFronticPageOptions } from '../composables/useFronticPage.js';
|
|
12
|
+
export type { UseFronticContextOptions } from '../composables/useFronticContext.js';
|
|
13
|
+
export type { UseFronticListingOptions } from '../composables/useFronticListing.js';
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frontic Listing Types
|
|
3
|
+
*
|
|
4
|
+
* Type definitions for the useFronticListing composable.
|
|
5
|
+
* These types provide type-safe filtering, sorting, and pagination for Frontic listings.
|
|
6
|
+
*/
|
|
7
|
+
import type { MaybeRef, Ref, ShallowRef } from 'vue';
|
|
8
|
+
import type { UseQueryReturn } from '@pinia/colada';
|
|
9
|
+
import type { Listings, ListingParameters, ListingQueryFilters, ListingQuerySorts, Responses } from '@frontic/stack/generated-types';
|
|
10
|
+
export type { Listings, ListingParameters, ListingQueryFilters, ListingQuerySorts, Responses };
|
|
11
|
+
export type { Query, Sort, EqualsFilter, LogicalFilter, Filter, RangeFilter, ContainsFilter } from '@frontic/stack/query-types';
|
|
12
|
+
/**
|
|
13
|
+
* Shared request options for all Frontic composables
|
|
14
|
+
* These options match the client's RequestOptions interface
|
|
15
|
+
*
|
|
16
|
+
* TODO(FSC-3044): Replace with imported RequestOptions from @frontic/stack/generated-types
|
|
17
|
+
* once the CLI exports it. See: https://linear.app/frontstack/issue/FSC-3044
|
|
18
|
+
*/
|
|
19
|
+
export interface FronticRequestOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Context key (token) for locale/region selection
|
|
22
|
+
*/
|
|
23
|
+
contextKey?: MaybeRef<string | undefined>;
|
|
24
|
+
/**
|
|
25
|
+
* Context domain to override the global contextDomain for this request
|
|
26
|
+
* Used to indicate the requested region and locale
|
|
27
|
+
*/
|
|
28
|
+
contextDomain?: MaybeRef<string | undefined>;
|
|
29
|
+
/**
|
|
30
|
+
* Request URL for analytics/tracking purposes
|
|
31
|
+
* URL to the current page, used by the Fetch API for tracking
|
|
32
|
+
*/
|
|
33
|
+
requestUrl?: MaybeRef<string | undefined>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Inclusion mode for configuring which fields to show
|
|
37
|
+
* - 'include': Only show specified keys
|
|
38
|
+
* - 'exclude': Show all except specified keys
|
|
39
|
+
*/
|
|
40
|
+
export type InclusionMode = 'include' | 'exclude';
|
|
41
|
+
/**
|
|
42
|
+
* Extract filter field keys for a specific listing
|
|
43
|
+
* Provides autocomplete for valid filter fields when the listing is in ListingQueryFilters
|
|
44
|
+
*
|
|
45
|
+
* @template TListing - The listing name (e.g., 'ProductSearch')
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* // If ListingQueryFilters['ProductSearch'] = { 'properties.color': string[], 'properties.size': string[] }
|
|
49
|
+
* type Fields = FilterField<'ProductSearch'>
|
|
50
|
+
* // Fields = 'properties.color' | 'properties.size'
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export type FilterField<TListing extends keyof Listings> = TListing extends keyof ListingQueryFilters ? keyof ListingQueryFilters[TListing] & string : string;
|
|
54
|
+
/**
|
|
55
|
+
* Extract sort field keys for a specific listing
|
|
56
|
+
* Provides autocomplete for valid sort fields when the listing is in ListingQuerySorts
|
|
57
|
+
*
|
|
58
|
+
* @template TListing - The listing name (e.g., 'ProductSearch')
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* // If ListingQuerySorts['ProductSearch'] = { 'name': 'asc'|'desc', 'price.amount': 'asc'|'desc' }
|
|
62
|
+
* type Fields = SortField<'ProductSearch'>
|
|
63
|
+
* // Fields = 'name' | 'price.amount'
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export type SortField<TListing extends keyof Listings> = TListing extends keyof ListingQuerySorts ? keyof ListingQuerySorts[TListing] & string : string;
|
|
67
|
+
/**
|
|
68
|
+
* Type-safe sort string with format "field:order" or "default"
|
|
69
|
+
* Provides autocomplete for valid sort combinations when the listing is known
|
|
70
|
+
*
|
|
71
|
+
* @template TListing - The listing name (e.g., 'ProductSearch')
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* type Sort = SortString<'ProductSearch'>
|
|
75
|
+
* // Sort = 'default' | 'name:asc' | 'name:desc' | 'price.amount:asc' | 'price.amount:desc'
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export type SortString<TListing extends keyof Listings> = TListing extends keyof ListingQuerySorts ? 'default' | `${keyof ListingQuerySorts[TListing] & string}:${'asc' | 'desc'}` : 'default' | `${string}:${'asc' | 'desc'}`;
|
|
79
|
+
/**
|
|
80
|
+
* Generic configuration for listing fields (filters or sorts)
|
|
81
|
+
* Allows flexible control over which fields to show and how to label them
|
|
82
|
+
* @template TFields - The field type (e.g., ListingQueryFilters[TListing] or ListingQuerySorts[TListing])
|
|
83
|
+
*/
|
|
84
|
+
export interface ListingFieldConfig<TFields = Record<string, unknown>> {
|
|
85
|
+
/** Field keys to include or exclude. If not provided, all fields are shown. */
|
|
86
|
+
select?: Array<keyof TFields & string>;
|
|
87
|
+
/** Mode: 'include' (show only specified) or 'exclude' (hide specified). Default: 'include' */
|
|
88
|
+
mode?: InclusionMode;
|
|
89
|
+
/** Custom labels for fields (e.g., { "properties.color": "Color" }) */
|
|
90
|
+
label?: Partial<Record<keyof TFields & string, string>>;
|
|
91
|
+
/**
|
|
92
|
+
* Define the display order for fields. Keys listed here appear first in the specified order.
|
|
93
|
+
* Any fields not in this array appear after in their original order.
|
|
94
|
+
* @example
|
|
95
|
+
* // Filters: show "Color" first, then "Size", then any others
|
|
96
|
+
* filter: { sort: ['properties.color', 'properties.size'] }
|
|
97
|
+
*
|
|
98
|
+
* // Sorts: show "Relevance" first, then "Price" options
|
|
99
|
+
* sorting: { sort: ['default', 'price.amount:asc', 'price.amount:desc'] }
|
|
100
|
+
*/
|
|
101
|
+
sort?: Array<keyof TFields & string>;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Filter option from API response (matches Frontstack response structure)
|
|
105
|
+
* @internal - Used internally, components should use UiFilter
|
|
106
|
+
*/
|
|
107
|
+
export interface FilterOption {
|
|
108
|
+
option: string;
|
|
109
|
+
value: string;
|
|
110
|
+
count: number;
|
|
111
|
+
selected?: boolean;
|
|
112
|
+
disabled?: boolean;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Processed filter ready for UI consumption
|
|
116
|
+
* @template TFilters - Filter fields type for this listing (provides autocomplete for key)
|
|
117
|
+
*/
|
|
118
|
+
export interface UiFilter<TFilters = Record<string, unknown>> {
|
|
119
|
+
/** Filter field key (typed based on listing schema) */
|
|
120
|
+
key: keyof TFilters & string;
|
|
121
|
+
/** Human-readable label for the filter */
|
|
122
|
+
label: string;
|
|
123
|
+
/** Available filter options with counts and selection state */
|
|
124
|
+
options: FilterOption[];
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Processed sort option ready for UI consumption
|
|
128
|
+
* @template TSorts - Sort fields type for this listing (provides autocomplete for key/value)
|
|
129
|
+
*/
|
|
130
|
+
export interface UiSort<TSorts = Record<string, unknown>> {
|
|
131
|
+
/** Sort option key (typed based on listing schema, or 'default') */
|
|
132
|
+
key: (keyof TSorts & string) | 'default';
|
|
133
|
+
/** Human-readable label for the sort option */
|
|
134
|
+
label: string;
|
|
135
|
+
/** Sort value to pass to sortResult (same as key) */
|
|
136
|
+
value: (keyof TSorts & string) | 'default';
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Listing state exposed to consumers
|
|
140
|
+
* Contains both the current query state and processed data ready for UI consumption
|
|
141
|
+
*/
|
|
142
|
+
export interface ListingState {
|
|
143
|
+
/** Current page number */
|
|
144
|
+
page?: number;
|
|
145
|
+
/** Currently applied sort */
|
|
146
|
+
sort?: {
|
|
147
|
+
field: string;
|
|
148
|
+
order: 'asc' | 'desc';
|
|
149
|
+
};
|
|
150
|
+
/** Current search term (if search is enabled) */
|
|
151
|
+
search?: string;
|
|
152
|
+
/** Currently active filters by field */
|
|
153
|
+
filter?: Record<string, unknown>;
|
|
154
|
+
/** Processed filters ready for UI (includes labels, counts, options) */
|
|
155
|
+
availableFilters?: UiFilter[];
|
|
156
|
+
/** Processed sort options ready for UI (includes labels) */
|
|
157
|
+
availableSorts?: UiSort[];
|
|
158
|
+
/** Number of currently active filters */
|
|
159
|
+
activeFilterCount?: number;
|
|
160
|
+
/** Total number of items matching the current query */
|
|
161
|
+
total?: number;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Configuration for the generic listing composable
|
|
165
|
+
*/
|
|
166
|
+
export interface UseListingConfig<TListing extends string = string, TFilters = Record<string, unknown>, _TSorts = Record<string, unknown>> {
|
|
167
|
+
/**
|
|
168
|
+
* The listing name to fetch (e.g., 'ProductSearch', 'CategoryProducts')
|
|
169
|
+
*/
|
|
170
|
+
name: TListing;
|
|
171
|
+
/**
|
|
172
|
+
* Parameters to pass to the listing endpoint
|
|
173
|
+
*/
|
|
174
|
+
params?: MaybeRef<Record<string, unknown>>;
|
|
175
|
+
/**
|
|
176
|
+
* Cache key for the query (defaults to name)
|
|
177
|
+
*/
|
|
178
|
+
cacheKey?: string;
|
|
179
|
+
/**
|
|
180
|
+
* Optional context key for the request
|
|
181
|
+
*/
|
|
182
|
+
contextKey?: MaybeRef<string | undefined>;
|
|
183
|
+
/**
|
|
184
|
+
* Minimum length of search term before triggering search (default: 2)
|
|
185
|
+
*/
|
|
186
|
+
searchTermThreshold?: number;
|
|
187
|
+
/**
|
|
188
|
+
* Filter fields that should use OR logic (all others use AND)
|
|
189
|
+
* Type-safe: only allows valid filter field names for this listing
|
|
190
|
+
*/
|
|
191
|
+
orFilter?: Array<keyof TFilters & string>;
|
|
192
|
+
/**
|
|
193
|
+
* Filter configuration using unified structure
|
|
194
|
+
* Type-safe: select and label must match actual filter fields
|
|
195
|
+
* @example
|
|
196
|
+
* // With select and label
|
|
197
|
+
* filters: {
|
|
198
|
+
* select: ['properties.color', 'properties.size'],
|
|
199
|
+
* mode: 'include',
|
|
200
|
+
* label: { 'properties.color': 'Color', 'properties.size': 'Size' }
|
|
201
|
+
* }
|
|
202
|
+
*
|
|
203
|
+
* // Just label (shows all filters with custom labels)
|
|
204
|
+
* filters: {
|
|
205
|
+
* label: { 'properties.color': 'Color' }
|
|
206
|
+
* }
|
|
207
|
+
*
|
|
208
|
+
* // Just select (shows only these filters)
|
|
209
|
+
* filters: {
|
|
210
|
+
* select: ['properties.color', 'properties.size']
|
|
211
|
+
* }
|
|
212
|
+
*/
|
|
213
|
+
filters?: ListingFieldConfig<TFilters>;
|
|
214
|
+
/**
|
|
215
|
+
* Sort configuration using unified structure
|
|
216
|
+
* Type-safe: label must use valid sort field names with :asc/:desc suffix
|
|
217
|
+
*
|
|
218
|
+
* Special handling for 'default' sort:
|
|
219
|
+
* - The 'default' sort represents Frontstack backend's default sorting (no sort applied to query)
|
|
220
|
+
* - It's automatically included if a label is provided in sorts.label
|
|
221
|
+
* - Can be controlled via include/exclude mode like other sorts
|
|
222
|
+
* - Use resetSorting() to return to default sorting
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* // Full configuration with default sort
|
|
226
|
+
* sorts: {
|
|
227
|
+
* label: {
|
|
228
|
+
* 'default': 'Relevance',
|
|
229
|
+
* 'name:asc': 'Name A-Z',
|
|
230
|
+
* 'price.amount:asc': 'Price Low-High'
|
|
231
|
+
* }
|
|
232
|
+
* }
|
|
233
|
+
*
|
|
234
|
+
* // Just label (all sorts allowed)
|
|
235
|
+
* sorts: {
|
|
236
|
+
* label: {
|
|
237
|
+
* 'name:asc': 'Name A-Z',
|
|
238
|
+
* 'name:desc': 'Name Z-A'
|
|
239
|
+
* }
|
|
240
|
+
* }
|
|
241
|
+
*/
|
|
242
|
+
sorts?: ListingFieldConfig<Record<string, unknown>>;
|
|
243
|
+
/**
|
|
244
|
+
* Enable search functionality (default: true)
|
|
245
|
+
*/
|
|
246
|
+
enableSearch?: boolean;
|
|
247
|
+
/**
|
|
248
|
+
* Enable filtering functionality (default: true)
|
|
249
|
+
*/
|
|
250
|
+
enableFilter?: boolean;
|
|
251
|
+
/**
|
|
252
|
+
* Enable sorting functionality (default: true)
|
|
253
|
+
*/
|
|
254
|
+
enableSorting?: boolean;
|
|
255
|
+
/**
|
|
256
|
+
* Stale time in milliseconds (default: 5 minutes)
|
|
257
|
+
*/
|
|
258
|
+
staleTime?: number;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Return type for the generic listing composable
|
|
262
|
+
*/
|
|
263
|
+
export interface UseListingReturn<TResponse = unknown, TFilters = Record<string, unknown>> {
|
|
264
|
+
/** The current result set containing items and metadata */
|
|
265
|
+
listing: ShallowRef<TResponse | undefined>;
|
|
266
|
+
/** Current fetch status of the listing */
|
|
267
|
+
status: UseQueryReturn['status'];
|
|
268
|
+
/** Current state of the result including page, sort, search, and filter settings */
|
|
269
|
+
state: Ref<ListingState>;
|
|
270
|
+
/** Current search term */
|
|
271
|
+
searchTerm: Ref<string>;
|
|
272
|
+
/**
|
|
273
|
+
* Loads more items in the current listing (infinite scroll)
|
|
274
|
+
*/
|
|
275
|
+
loadMore: () => Promise<TResponse | undefined>;
|
|
276
|
+
/**
|
|
277
|
+
* Fetches the listing with current query parameters
|
|
278
|
+
*/
|
|
279
|
+
refresh: () => Promise<void>;
|
|
280
|
+
/**
|
|
281
|
+
* Paginates the results in the specified direction
|
|
282
|
+
* @param direction - Direction to paginate ('next' or 'prev')
|
|
283
|
+
* @returns Promise<boolean> - Returns true if pagination was successful
|
|
284
|
+
*/
|
|
285
|
+
paginate: (direction: 'next' | 'prev') => Promise<boolean>;
|
|
286
|
+
/**
|
|
287
|
+
* Sorts the results by the specified field
|
|
288
|
+
* Type-safe: only accepts valid sort strings for this listing
|
|
289
|
+
* @param sortBy - Sort field and direction (e.g., 'name:desc' or 'name:asc' or 'default')
|
|
290
|
+
*/
|
|
291
|
+
sortItems: (sortBy: string) => Promise<void>;
|
|
292
|
+
/**
|
|
293
|
+
* Resets sorting to default (Frontstack backend default)
|
|
294
|
+
*/
|
|
295
|
+
resetSorting: () => Promise<void>;
|
|
296
|
+
/**
|
|
297
|
+
* Adds a filter option to the specified filter field
|
|
298
|
+
* Type-safe: field and value types are validated
|
|
299
|
+
* @param filterField - The field to filter on (autocomplete available)
|
|
300
|
+
* @param filterOption - The option value to add
|
|
301
|
+
*/
|
|
302
|
+
addFilter: <K extends keyof TFilters & string>(filterField: K, filterOption: TFilters[K]) => Promise<void>;
|
|
303
|
+
/**
|
|
304
|
+
* Removes a filter option from the specified filter field
|
|
305
|
+
* Type-safe: field and value types are validated
|
|
306
|
+
* @param filterField - The field to remove the filter from (autocomplete available)
|
|
307
|
+
* @param filterOption - The option value to remove (optional - omit to remove entire filter)
|
|
308
|
+
*/
|
|
309
|
+
removeFilter: <K extends keyof TFilters & string>(filterField: K, filterOption?: TFilters[K]) => Promise<void>;
|
|
310
|
+
/**
|
|
311
|
+
* Applies filter options to the specified filter field
|
|
312
|
+
* Type-safe: field and value types are validated
|
|
313
|
+
* @param filterField - The field to filter on (autocomplete available)
|
|
314
|
+
* @param filterOptions - Array of filter values to apply
|
|
315
|
+
*/
|
|
316
|
+
filterItems: <K extends keyof TFilters & string>(filterField: K, filterOptions: Array<TFilters[K]>) => Promise<void>;
|
|
317
|
+
/**
|
|
318
|
+
* Resets filters either for a specific field or all filters if no field specified
|
|
319
|
+
* Type-safe: field names are validated
|
|
320
|
+
* @param filterField - Optional field to reset. If omitted, all filters are reset
|
|
321
|
+
*/
|
|
322
|
+
resetFilter: (filterField?: keyof TFilters & string) => Promise<void>;
|
|
323
|
+
/**
|
|
324
|
+
* Clears the search term
|
|
325
|
+
*/
|
|
326
|
+
resetSearch: () => Promise<void>;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Configuration for listing queries
|
|
330
|
+
* @template TListing - The listing name, constrained to keyof Listings for type safety
|
|
331
|
+
*/
|
|
332
|
+
export interface FetchListingConfig<TListing extends keyof Listings = keyof Listings> extends FronticRequestOptions {
|
|
333
|
+
/**
|
|
334
|
+
* The listing name to fetch (e.g., 'ProductSearch', 'CategoryProducts')
|
|
335
|
+
*/
|
|
336
|
+
name: TListing;
|
|
337
|
+
/**
|
|
338
|
+
* Parameters to pass to the listing endpoint
|
|
339
|
+
* Type-safe when TListing is a specific listing name
|
|
340
|
+
*/
|
|
341
|
+
params: MaybeRef<TListing extends keyof ListingParameters ? ListingParameters[TListing] : Record<string, unknown>>;
|
|
342
|
+
/**
|
|
343
|
+
* Query configuration (filters, sort, search, pagination)
|
|
344
|
+
*/
|
|
345
|
+
query?: MaybeRef<Record<string, unknown>>;
|
|
346
|
+
/**
|
|
347
|
+
* Optional page number for pagination
|
|
348
|
+
*/
|
|
349
|
+
page?: MaybeRef<number | undefined>;
|
|
350
|
+
/**
|
|
351
|
+
* Stale time in milliseconds (default: 5 minutes)
|
|
352
|
+
*/
|
|
353
|
+
staleTime?: number;
|
|
354
|
+
}
|