@scayle/storefront-product-listing 0.1.4 → 0.2.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/README.md CHANGED
@@ -70,9 +70,3 @@ Learn more about [SCAYLE’s architecture](https://scayle.dev/en/getting-started
70
70
  [npm-downloads-href]: https://npmjs.com/package/@scayle/storefront-product-listing
71
71
  [license-src]: https://img.shields.io/npm/l/@scayle/storefront-product-listing.svg?style=flat&colorA=18181B&colorB=28CF8D
72
72
  [license-href]: https://npmjs.com/package/@scayle/storefront-product-listing
73
-
74
- ## License
75
-
76
- Licensed under the [MIT License](https://opensource.org/license/mit/)
77
-
78
- ---
@@ -1,8 +1,18 @@
1
+ import { type ComputedRef } from 'vue';
1
2
  import type { ProductSearchQuery } from '@scayle/storefront-nuxt';
2
3
  import type { RouteLocationNormalizedLoadedGeneric } from 'vue-router';
4
+ export type AppliedAttributeValues = Record<string, (string | number)[]>;
5
+ export type AppliedBooleanValues = Record<string, boolean>;
3
6
  export interface AppliedFiltersOptions {
4
7
  filtersPrefix: string;
5
8
  }
9
+ export interface UseAppliedFiltersReturn {
10
+ appliedFilter: ComputedRef<ProductSearchQuery>;
11
+ appliedFiltersCount: ComputedRef<number>;
12
+ appliedBooleanValues: ComputedRef<AppliedBooleanValues>;
13
+ appliedAttributeValues: ComputedRef<AppliedAttributeValues>;
14
+ areFiltersApplied: ComputedRef<boolean>;
15
+ }
6
16
  /**
7
17
  * Composable to manage applied filters based on route query parameters.
8
18
  *
@@ -10,7 +20,7 @@ export interface AppliedFiltersOptions {
10
20
  * @param {AppliedFiltersOptions} options - Configuration options for applied filters.
11
21
  * @param {string} options.filtersPrefix - The prefix used to identify filter-related query parameters.
12
22
  *
13
- * @returns {object} - An object containing the following properties:
23
+ * @returns {UseAppliedFiltersReturn} - An object containing reactive data and computed properties for managing applied filters:
14
24
  *
15
25
  * - `appliedFilter` {ComputedRef<ProductSearchQuery>}: A computed property returning a `ProductSearchQuery` object based on the route's query parameters.
16
26
  * - `attributes`: An array of attribute filters, where each filter has:
@@ -23,17 +33,8 @@ export interface AppliedFiltersOptions {
23
33
  * - `term`: (Optional) A search term filter as a string.
24
34
  *
25
35
  * - `appliedFiltersCount` {ComputedRef<number>}: A computed property that returns the total count of applied filters. Counts minPrice/maxPrice as one and counts each attribute filter separately.
26
- *
27
- * - `appliedAttributeValues` {ComputedRef<Record<string, Array<number>>>}: A computed property that returns a record mapping attribute keys to arrays of numbers representing selected values for each attribute.
28
- *
29
- * - `appliedBooleanValues` {ComputedRef<Record<string, boolean>>}: A computed property that returns a record mapping attribute keys to their boolean values for boolean filters.
30
- *
36
+ * - `appliedAttributeValues` {ComputedRef<AppliedAttributeValues>}: A computed property that returns a record mapping attribute keys to arrays of numbers representing selected values for each attribute.
37
+ * - `appliedBooleanValues` {ComputedRef<appliedBooleanValues>}: A computed property that returns a record mapping attribute keys to their boolean values for boolean filters.
31
38
  * - `areFiltersApplied` {ComputedRef<boolean>}: A computed property that returns `true` if any filters are currently applied, otherwise `false`.
32
39
  */
33
- export declare function useAppliedFilters(route: RouteLocationNormalizedLoadedGeneric, { filtersPrefix }?: AppliedFiltersOptions): {
34
- appliedFilter: import("vue").ComputedRef<ProductSearchQuery>;
35
- appliedFiltersCount: import("vue").ComputedRef<number>;
36
- appliedAttributeValues: import("vue").ComputedRef<Record<string, (string | number)[]>>;
37
- appliedBooleanValues: import("vue").ComputedRef<Record<string, boolean>>;
38
- areFiltersApplied: import("vue").ComputedRef<boolean>;
39
- };
40
+ export declare function useAppliedFilters(route: RouteLocationNormalizedLoadedGeneric, { filtersPrefix }?: AppliedFiltersOptions): UseAppliedFiltersReturn;
@@ -12,24 +12,25 @@ export function useAppliedFilters(route, { filtersPrefix } = { filtersPrefix: "f
12
12
  }
13
13
  return count;
14
14
  });
15
- const appliedAttributeValues = computed(() => {
16
- const values = {};
17
- appliedFilter.value?.attributes?.forEach((attribute) => {
18
- if (attribute.type !== "attributes" || !attribute.key) {
19
- return;
20
- }
21
- values[attribute.key] = attribute.values;
22
- });
23
- return values;
24
- });
15
+ const appliedAttributeValues = computed(
16
+ () => {
17
+ const attributes = appliedFilter.value?.attributes ?? [];
18
+ return attributes.reduce((values, attribute) => {
19
+ if (attribute.type === "attributes" && attribute.key) {
20
+ values[attribute.key] = attribute.values;
21
+ }
22
+ return values;
23
+ }, {});
24
+ }
25
+ );
25
26
  const appliedBooleanValues = computed(() => {
26
- const values = {};
27
- appliedFilter.value?.attributes?.forEach((attribute) => {
27
+ const attributes = appliedFilter.value?.attributes ?? [];
28
+ return attributes.reduce((values, attribute) => {
28
29
  if (attribute.type === "boolean") {
29
30
  values[attribute.key] = attribute.value;
30
31
  }
31
- });
32
- return values;
32
+ return values;
33
+ }, {});
33
34
  });
34
35
  const areFiltersApplied = computed(() => {
35
36
  return appliedFiltersCount.value > 0;
@@ -6,7 +6,7 @@ export type ProductListFilterOptions = Partial<{
6
6
  immediate: boolean;
7
7
  keyPrefix: string;
8
8
  }>;
9
- type UseProductListFilterReturn = Pick<Awaited<ReturnType<typeof useFilters>>, 'error' | 'status' | 'fetch' | 'fetching'> & {
9
+ export type UseProductListFilterReturn = Pick<Awaited<ReturnType<typeof useFilters>>, 'error' | 'status' | 'fetch' | 'fetching'> & {
10
10
  availableFilters: ComputedRef<FilterItemWithValues[]>;
11
11
  clearedPriceQuery: ComputedRef<LocationQuery | undefined>;
12
12
  unfilteredCount: ComputedRef<number>;
@@ -20,7 +20,7 @@ type UseProductListFilterReturn = Pick<Awaited<ReturnType<typeof useFilters>>, '
20
20
  * @param {string} options.immediate - Will enable "immediate" flag via "useFilters" composable in it.
21
21
  * @param {string} options.keyPrefix - The prefix used to construct "key" via "useFilters" payload. Default is "base".
22
22
  *
23
- * @returns {UseProductListFilterReturn} - An object containing the following properties:
23
+ * @returns {UseProductListFilterReturn} - An object containing reactive data, computed properties and async RPC call for managing filters:
24
24
  * - `fetch`: call for fetching the filters.
25
25
  * - `error`: returns "useFilters" error.
26
26
  * - `status`: returns "useFilters" status.
@@ -30,4 +30,3 @@ type UseProductListFilterReturn = Pick<Awaited<ReturnType<typeof useFilters>>, '
30
30
  * - `clearedPriceQuery` {ComputedRef<LocationQuery | undefined>}: constructs an empty query object.
31
31
  */
32
32
  export declare function useProductListFilter(route: RouteLocationNormalizedLoadedGeneric, currentCategoryId?: MaybeRefOrGetter<number | undefined>, options?: ProductListFilterOptions): UseProductListFilterReturn & Promise<UseProductListFilterReturn>;
33
- export {};
@@ -1,9 +1,15 @@
1
- import { APISortOption, APISortOrder, type ProductSortConfig } from '@scayle/storefront-nuxt';
1
+ import { type ProductSortConfig } from '@scayle/storefront-nuxt';
2
+ import { type ComputedRef } from 'vue';
2
3
  import type { RouteLocationNormalizedLoadedGeneric } from 'vue-router';
3
4
  type SelectedSort = ProductSortConfig & {
4
5
  key: string;
5
6
  label: string;
6
7
  };
8
+ export interface UseProductListSortReturn {
9
+ selectedSort: ComputedRef<SelectedSort | undefined>;
10
+ sortLinks: ComputedRef<SelectedSort[]>;
11
+ isDefaultSortSelected: ComputedRef<boolean>;
12
+ }
7
13
  export interface ProductListSortOptions {
8
14
  sortingOptions?: SelectedSort[];
9
15
  defaultSortingKey?: string;
@@ -20,23 +26,5 @@ export interface ProductListSortOptions {
20
26
  * - {ComputedRef<SelectedSort[]>} sortLinks - Links with updated query parameters for each sorting option.
21
27
  * - {ComputedRef<boolean>} isDefaultSortSelected - Boolean indicating if the default sort is selected.
22
28
  */
23
- export declare function useProductListSort(route: RouteLocationNormalizedLoadedGeneric, { sortingOptions, defaultSortingKey }?: ProductListSortOptions): {
24
- selectedSort: import("vue").ComputedRef<SelectedSort | undefined>;
25
- sortLinks: import("vue").ComputedRef<{
26
- to: {
27
- path: string;
28
- query: {
29
- sort: string;
30
- };
31
- };
32
- by?: APISortOption;
33
- direction?: APISortOrder;
34
- score?: "category_scores" | "brand_scores";
35
- channel?: string;
36
- sortingKey?: string | string[];
37
- key: string;
38
- label: string;
39
- }[]>;
40
- isDefaultSortSelected: import("vue").ComputedRef<boolean>;
41
- };
29
+ export declare function useProductListSort(route: RouteLocationNormalizedLoadedGeneric, { sortingOptions, defaultSortingKey }?: ProductListSortOptions): UseProductListSortReturn;
42
30
  export {};
@@ -4,7 +4,7 @@ import type { BreadcrumbList, WithContext } from 'schema-dts';
4
4
  import type { RouteLocationNormalizedLoadedGeneric } from 'vue-router';
5
5
  type CanonicalLink = Record<'rel' | 'key' | 'href', string>;
6
6
  type UrlParams = Record<'baseUrl' | 'fullPath', string>;
7
- interface UseProductListingSeoData {
7
+ export interface UseProductListingSeoDataReturn {
8
8
  title: ComputedRef<string>;
9
9
  activeCategoryName: ComputedRef<string | undefined>;
10
10
  robots: ComputedRef<string>;
@@ -16,6 +16,7 @@ interface UseProductListingSeoData {
16
16
  *
17
17
  * @param {Ref<Category>} category - Reactive category
18
18
  * @param {BreadcrumbItem[]} breadcrumbs - Breadcrumb items.
19
+ * @param {RouteLocationNormalizedLoadedGeneric} [route] - route object to handle queries.
19
20
  * @param {UrlParams} URL params - URL data for constructing the canonical link.
20
21
  *
21
22
  * @returns {UseProductListingSeoData} An object containing reactive data and computed properties for managing SEO data.
@@ -26,5 +27,5 @@ interface UseProductListingSeoData {
26
27
  * @property {ComputedRef<CanonicalLink[]>} canonicalLink - A computed canonical link.
27
28
  * @property {ComputedRef<WithContext<BreadcrumbList> | []>} categoryBreadcrumbSchema - A computed breadcrumb schema.
28
29
  */
29
- export declare function useProductListingSeoData(category: Ref<Category | undefined | null>, breadcrumbs: BreadcrumbItem[], route: RouteLocationNormalizedLoadedGeneric, { baseUrl, fullPath }: UrlParams): UseProductListingSeoData;
30
+ export declare function useProductListingSeoData(category: Ref<Category | undefined | null>, breadcrumbs: BreadcrumbItem[], route: RouteLocationNormalizedLoadedGeneric, { baseUrl, fullPath }: UrlParams): UseProductListingSeoDataReturn;
30
31
  export {};
@@ -3,12 +3,24 @@ import { type MaybeRefOrGetter, type ComputedRef } from 'vue';
3
3
  import { useProducts } from '@scayle/storefront-nuxt/composables';
4
4
  import type { Pagination } from '@scayle/storefront-api';
5
5
  import type { RouteLocationNormalizedLoadedGeneric } from 'vue-router';
6
- type UseProductsByCategoryReturn = Pick<Awaited<ReturnType<typeof useProducts>>, 'error' | 'fetching'> & {
6
+ export type UseProductsByCategoryReturn = Pick<Awaited<ReturnType<typeof useProducts>>, 'error' | 'fetching'> & {
7
7
  products: ComputedRef<Product[]>;
8
8
  pagination: ComputedRef<Pagination | undefined>;
9
9
  totalProductsCount: ComputedRef<number>;
10
10
  paginationOffset: ComputedRef<number>;
11
11
  };
12
+ export interface ProductsByCategoryOptions {
13
+ params: Omit<Partial<FetchProductsByCategoryParams>, 'category' | 'categoryId'>;
14
+ fetchingOptions: Partial<{
15
+ lazy: boolean;
16
+ immediate: boolean;
17
+ }>;
18
+ productsPerPage: number;
19
+ fetchProductsCacheTtl: number;
20
+ cacheKeyPrefix: string;
21
+ fetchingKey?: string;
22
+ withParams?: ProductWith;
23
+ }
12
24
  /**
13
25
  * Composable to fetch and manage products by category with configurable options.
14
26
  *
@@ -31,17 +43,4 @@ type UseProductsByCategoryReturn = Pick<Awaited<ReturnType<typeof useProducts>>,
31
43
  * @property {Ref<boolean>} fetching - A reactive boolean indicating if products are currently being fetched.
32
44
  * @property {Ref<Error | null>} error - A reactive error object, null if no error occurred.
33
45
  */
34
- export interface ProductsByCategoryOptions {
35
- params: Omit<Partial<FetchProductsByCategoryParams>, 'category' | 'categoryId'>;
36
- fetchingOptions: Partial<{
37
- lazy: boolean;
38
- immediate: boolean;
39
- }>;
40
- productsPerPage: number;
41
- fetchProductsCacheTtl: number;
42
- cacheKeyPrefix: string;
43
- fetchingKey?: string;
44
- withParams?: ProductWith;
45
- }
46
46
  export declare function useProductsByCategory(categoryId: MaybeRefOrGetter<number>, route: RouteLocationNormalizedLoadedGeneric, { params, withParams, cacheKeyPrefix, fetchingKey, fetchingOptions, fetchProductsCacheTtl, productsPerPage, }?: Partial<ProductsByCategoryOptions>): UseProductsByCategoryReturn & Promise<UseProductsByCategoryReturn>;
47
- export {};
@@ -50,11 +50,15 @@ export function useProductsByCategory(categoryId, route, {
50
50
  key: fetchingKey || `${toValue(categoryId)}-products`,
51
51
  options: fetchingOptions
52
52
  });
53
- const products = computed(() => productsData.data.value?.products ?? []);
54
- const pagination = computed(() => productsData.data.value?.pagination);
55
- const totalProductsCount = computed(
56
- () => productsData.data.value?.pagination.total ?? 0
57
- );
53
+ const products = computed(() => {
54
+ return productsData.data.value?.products ?? [];
55
+ });
56
+ const pagination = computed(() => {
57
+ return productsData.data.value?.pagination;
58
+ });
59
+ const totalProductsCount = computed(() => {
60
+ return productsData.data.value?.pagination.total ?? 0;
61
+ });
58
62
  const paginationOffset = computed(() => {
59
63
  const page = pagination.value?.page ?? 1;
60
64
  const perPage = pagination.value?.perPage ?? productsPerPage ?? PRODUCTS_PER_PAGE;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-product-listing",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "description": "Collection of essential composables and utilities to work with product listing",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",