@nosto/search-js 1.4.1 → 1.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.
Files changed (69) hide show
  1. package/README.md +3 -39
  2. package/dist/core/core.d.ts +2 -2
  3. package/dist/core/src/applyDecorators.d.ts +9 -9
  4. package/dist/core/src/search.d.ts +9 -9
  5. package/dist/core/src/searchWithRetries.d.ts +2 -2
  6. package/dist/core/src/types.d.ts +5 -6
  7. package/dist/currencies/currencies.d.ts +2 -2
  8. package/dist/currencies/src/getCurrencyFormatting.d.ts +2 -3
  9. package/dist/currencies/src/priceDecorator.d.ts +2 -2
  10. package/dist/preact/preact.cjs.js +1 -1
  11. package/dist/preact/preact.d.ts +24 -5
  12. package/dist/preact/preact.es.js +741 -100
  13. package/dist/preact/src/actions/newSearch.d.ts +4 -0
  14. package/dist/preact/src/actions/replaceFilter.d.ts +3 -0
  15. package/dist/preact/src/actions/toggleProductFilter.d.ts +2 -0
  16. package/dist/preact/src/actions/types.d.ts +6 -0
  17. package/dist/preact/src/actions/updateSearch.d.ts +4 -0
  18. package/dist/preact/src/api/types.d.ts +4 -0
  19. package/dist/preact/src/components/SearchInput.d.ts +7 -0
  20. package/dist/preact/src/components/SerpElement.d.ts +18 -0
  21. package/dist/preact/src/config/base/baseConfig.d.ts +41 -0
  22. package/dist/preact/src/config/config.d.ts +4 -0
  23. package/dist/preact/src/config/configContext.d.ts +6 -0
  24. package/dist/preact/src/config/pages/autocomplete/config.d.ts +15 -0
  25. package/dist/preact/src/config/pages/autocomplete/provider.d.ts +10 -0
  26. package/dist/preact/src/config/pages/category/config.d.ts +44 -0
  27. package/dist/preact/src/config/pages/category/provider.d.ts +10 -0
  28. package/dist/preact/src/config/pages/serp/config.d.ts +34 -0
  29. package/dist/preact/src/config/pages/serp/provider.d.ts +10 -0
  30. package/dist/preact/src/dom/renderHeadless.d.ts +18 -0
  31. package/dist/preact/src/dom/utils.d.ts +5 -0
  32. package/dist/preact/src/hooks/useActions.d.ts +32 -0
  33. package/dist/preact/src/hooks/useCheckClientScript.d.ts +1 -0
  34. package/dist/preact/src/hooks/useDecoratedSearchResults.d.ts +27 -0
  35. package/dist/preact/src/hooks/useFacet.d.ts +60 -0
  36. package/dist/preact/src/hooks/useFacets.d.ts +39 -0
  37. package/dist/preact/src/hooks/useNostoAppState.d.ts +1 -1
  38. package/dist/preact/src/hooks/usePersonalization.d.ts +1 -1
  39. package/dist/preact/src/hooks/useProductFilters/useProductFilters.d.ts +59 -0
  40. package/dist/preact/src/hooks/useProductFilters/useProductFiltersUtils.d.ts +48 -0
  41. package/dist/preact/src/hooks/useRange.d.ts +34 -0
  42. package/dist/preact/src/hooks/useRangeSelector.d.ts +98 -0
  43. package/dist/preact/src/hooks/useResponse.d.ts +82 -0
  44. package/dist/preact/src/hooks/useSelectedFiltersCount.d.ts +17 -0
  45. package/dist/preact/src/hooks/useSizeOptions.d.ts +50 -0
  46. package/dist/preact/src/hooks/useSort/useSort.d.ts +47 -0
  47. package/dist/preact/src/hooks/useSort/utils.d.ts +9 -0
  48. package/dist/preact/src/search/defaults.d.ts +45 -0
  49. package/dist/preact/src/search/resultCaching.d.ts +4 -0
  50. package/dist/preact/src/store.d.ts +9 -1387
  51. package/dist/preact/src/storeContext.d.ts +5 -1389
  52. package/dist/preact/src/storeExtensions.d.ts +17 -0
  53. package/dist/thumbnails/src/generateThumbnailUrl.d.ts +1 -1
  54. package/dist/thumbnails/src/nostoThumbnailDecorator.d.ts +2 -2
  55. package/dist/thumbnails/src/shopifyThumbnailDecorator.d.ts +2 -2
  56. package/dist/thumbnails/src/thumbnailDecorator.d.ts +2 -2
  57. package/dist/thumbnails/thumbnails.d.ts +5 -5
  58. package/dist/utils/src/deepFreeze.d.ts +1 -1
  59. package/dist/utils/src/deepMerge.d.ts +5 -3
  60. package/dist/utils/src/mergeArrays.d.ts +2 -1
  61. package/dist/utils/src/parseNumber.d.ts +1 -0
  62. package/dist/utils/src/performance.d.ts +1 -0
  63. package/dist/utils/src/savePageScroll.d.ts +2 -0
  64. package/dist/utils/src/simplify.d.ts +1 -1
  65. package/dist/utils/src/storage.d.ts +6 -0
  66. package/dist/utils/src/types.d.ts +3 -0
  67. package/dist/utils/src/unique.d.ts +1 -0
  68. package/package.json +22 -15
  69. package/dist/preact/test/mocks/mocks.d.ts +0 -1384
package/README.md CHANGED
@@ -5,12 +5,6 @@ Search JS is a wrapper for the Nosto Search functionality with some extended fun
5
5
  * Nosto product thumbnails
6
6
  * Retry logic
7
7
 
8
- For @nosto/search-js specific API documentation, see [Our Typedoc](https://nosto.github.io/search-js/).
9
-
10
- For more information about Nosto platform, see [Our documentation](https://docs.nosto.com/techdocs).
11
-
12
- For sources, issues and contributions, see the [GitHub repository](https://github.com/Nosto/search-js).
13
-
14
8
  ## Installation
15
9
 
16
10
  To install the package, use your preferred package manager:
@@ -21,38 +15,8 @@ yarn add @nosto/search-js
21
15
  npm install @nosto/search-js --save
22
16
  ```
23
17
 
24
- ## Nosto stub
25
-
26
- When using this library, it is not necessary to create the Nosto stub. It will be created automatically as soon as the library is imported for the first time.
27
-
28
- ## Usage
29
-
30
- The main export of this library is the `search` function. It is compatible with the search function of the Nosto JS API and adds a couple of additional options
31
-
32
- ```ts
33
- import { search } from "@nosto/search-js"
34
- import { priceDecorator } from "@nosto/search-js/currencies"
35
- import { thumbnailDecorator } from "@nosto/search-js/thumbnails"
18
+ ## Documentation
36
19
 
37
- const response = await search({
38
- query: 'my search',
39
- products: {
40
- fields: [
41
- "productId",
42
- "name",
43
- "price",
44
- "listPrice",
45
- "priceCurrencyCode",
46
- "imageUrl",
47
- "imageHash"
48
- ]
49
- }
50
- }, {
51
- track: 'serp',
52
- hitDecorators: [
53
- priceDecorator(),
54
- thumbnailDecorator({ size: "9" })
55
- ]
56
- })
20
+ Read [Nosto Techdocs](https://docs.nosto.com/techdocs/apis/frontend/oss/search-js) for more information on how to use the library.
57
21
 
58
- ```
22
+ [Library TypeDoc page](https://nosto.github.io/search-js/) includes detailed library helpers documentation and examples.
@@ -1,3 +1,3 @@
1
1
  /** @module ./ */
2
- export { search } from "./src/search";
3
- export type { HitDecorator, Options } from "./src/types";
2
+ export { search } from './src/search';
3
+ export type { DecoratedProduct, DecoratedResult, HitDecorator, Options, ToIntersection } from './src/types';
@@ -1,21 +1,21 @@
1
- import { SearchResult } from "@nosto/nosto-js/client";
2
- import { HitDecorator } from "./types";
3
- export declare function applyDecorators<HD extends HitDecorator[]>(response: SearchResult, decorators?: HD): {
1
+ import { SearchResult } from '@nosto/nosto-js/client';
2
+ import { HitDecorator } from './types';
3
+ export declare function applyDecorators<HD extends readonly HitDecorator[]>(response: SearchResult, decorators?: HD): {
4
4
  products: {
5
- hits: ((ReturnType<HD[number]> extends infer T ? T extends ReturnType<HD[number]> ? T extends any ? (x: T) => void : never : never : never) extends (x: infer I) => void ? I : never)[];
5
+ hits: import('./types').ToIntersection<ReturnType<HD[number]>>[];
6
6
  categoryId?: string;
7
7
  categoryPath?: string;
8
8
  collapse?: string;
9
- facets?: import("@nosto/nosto-js/client").SearchFacet[];
9
+ facets?: import('@nosto/nosto-js/client').SearchFacet[];
10
10
  from?: number;
11
11
  fuzzy?: boolean;
12
12
  size?: number;
13
13
  total: number;
14
14
  };
15
- autocorrect?: import("@nosto/nosto-js/client").SearchAutocorrect;
16
- explain?: import("@nosto/nosto-js/client").SearchExplain;
17
- keywords?: import("@nosto/nosto-js/client").SearchKeywords;
15
+ autocorrect?: import('@nosto/nosto-js/client').SearchAutocorrect;
16
+ explain?: import('@nosto/nosto-js/client').SearchExplain;
17
+ keywords?: import('@nosto/nosto-js/client').SearchKeywords;
18
18
  query?: string;
19
19
  redirect?: string;
20
- abTests?: import("@nosto/nosto-js/client").ABTest[];
20
+ abTests?: import('@nosto/nosto-js/client').ABTest[];
21
21
  };
@@ -1,5 +1,5 @@
1
- import { SearchQuery } from "@nosto/nosto-js/client";
2
- import { HitDecorator, Options } from "./types";
1
+ import { SearchQuery } from '@nosto/nosto-js/client';
2
+ import { HitDecorator, Options } from './types';
3
3
  /**
4
4
  * Performs a search operation using the provided query and options.
5
5
  *
@@ -7,22 +7,22 @@ import { HitDecorator, Options } from "./types";
7
7
  * @param options - An object containing optional parameters for the search.
8
8
  * @returns A promise that resolves to the search result.
9
9
  */
10
- export declare function search<HD extends HitDecorator[]>(query: SearchQuery, options?: Options<HD>): Promise<{
10
+ export declare function search<HD extends readonly HitDecorator[]>(query: SearchQuery, options?: Options<HD>): Promise<{
11
11
  products: {
12
- hits: ((ReturnType<HD[number]> extends infer T ? T extends ReturnType<HD[number]> ? T extends any ? (x: T) => void : never : never : never) extends (x: infer I) => void ? I : never)[];
12
+ hits: import('./types').ToIntersection<ReturnType<HD[number]>>[];
13
13
  categoryId?: string;
14
14
  categoryPath?: string;
15
15
  collapse?: string;
16
- facets?: import("@nosto/nosto-js/client").SearchFacet[];
16
+ facets?: import('@nosto/nosto-js/client').SearchFacet[];
17
17
  from?: number;
18
18
  fuzzy?: boolean;
19
19
  size?: number;
20
20
  total: number;
21
21
  };
22
- autocorrect?: import("@nosto/nosto-js/client").SearchAutocorrect;
23
- explain?: import("@nosto/nosto-js/client").SearchExplain;
24
- keywords?: import("@nosto/nosto-js/client").SearchKeywords;
22
+ autocorrect?: import('@nosto/nosto-js/client').SearchAutocorrect;
23
+ explain?: import('@nosto/nosto-js/client').SearchExplain;
24
+ keywords?: import('@nosto/nosto-js/client').SearchKeywords;
25
25
  query?: string;
26
26
  redirect?: string;
27
- abTests?: import("@nosto/nosto-js/client").ABTest[];
27
+ abTests?: import('@nosto/nosto-js/client').ABTest[];
28
28
  }>;
@@ -1,5 +1,5 @@
1
- import type { API, SearchQuery, SearchResult } from "@nosto/nosto-js/client";
2
- import { Options } from "./types";
1
+ import { API, SearchQuery, SearchResult } from '@nosto/nosto-js/client';
2
+ import { Options } from './types';
3
3
  export declare function searchWithRetries(api: API, query: SearchQuery, options: Options<[]>): Promise<SearchResult>;
4
4
  /**
5
5
  * Should the error trigger a search retry
@@ -1,5 +1,5 @@
1
- import { SearchOptions, SearchProduct, SearchResult } from "@nosto/nosto-js/client";
2
- export type Options<HD extends HitDecorator[] = HitDecorator[]> = SearchOptions & {
1
+ import { SearchOptions, SearchProduct, SearchResult } from '@nosto/nosto-js/client';
2
+ export type Options<HD extends readonly HitDecorator[] = readonly HitDecorator[]> = SearchOptions & {
3
3
  /**
4
4
  * Hit decorators to apply to the search results.
5
5
  */
@@ -14,11 +14,10 @@ export type Options<HD extends HitDecorator[] = HitDecorator[]> = SearchOptions
14
14
  retryInterval?: number;
15
15
  };
16
16
  export type HitDecorator = (hit: SearchProduct) => SearchProduct;
17
- type ToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
18
- export type DecoratedProduct<HD extends HitDecorator[]> = ToIntersection<ReturnType<HD[number]>>;
19
- export type DecoratedResult<HD extends HitDecorator[]> = Omit<SearchResult, "products"> & {
17
+ export type ToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
18
+ export type DecoratedProduct<HD extends readonly HitDecorator[]> = ToIntersection<ReturnType<HD[number]>>;
19
+ export type DecoratedResult<HD extends readonly HitDecorator[]> = Omit<SearchResult, "products"> & {
20
20
  products: SearchResult["products"] & {
21
21
  hits: DecoratedProduct<HD>[];
22
22
  };
23
23
  };
24
- export {};
@@ -1,3 +1,3 @@
1
1
  /** @module currencies */
2
- export { type CurrencyConfig, getCurrencyFormatting } from "./src/getCurrencyFormatting";
3
- export { type FormattedPrices, priceDecorator, type Result } from "./src/priceDecorator";
2
+ export { type CurrencyConfig, type CurrencyFormats, getCurrencyFormatting } from './src/getCurrencyFormatting';
3
+ export { type FormattedPrices, priceDecorator, type Result } from './src/priceDecorator';
@@ -1,5 +1,5 @@
1
- import { CurrencySettingsDTO } from "@nosto/nosto-js/client";
2
- type CurrencyFormats = Record<string, CurrencySettingsDTO>;
1
+ import { CurrencySettingsDTO } from '@nosto/nosto-js/client';
2
+ export type CurrencyFormats = Record<string, CurrencySettingsDTO>;
3
3
  export interface CurrencyConfig {
4
4
  defaultCurrency: string;
5
5
  defaultLocale: string;
@@ -8,4 +8,3 @@ export interface CurrencyConfig {
8
8
  export declare function getCurrencyFormatting(overrides?: Partial<CurrencyConfig>): {
9
9
  formatCurrency: (value: number, currency?: string) => string;
10
10
  };
11
- export {};
@@ -1,5 +1,5 @@
1
- import { SearchProduct, SearchProductSku } from "@nosto/nosto-js/client";
2
- import { CurrencyConfig } from "./getCurrencyFormatting";
1
+ import { SearchProduct, SearchProductSku } from '@nosto/nosto-js/client';
2
+ import { CurrencyConfig } from './getCurrencyFormatting';
3
3
  export type FormattedPrices = {
4
4
  priceText?: string;
5
5
  listPriceText?: string;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _=require("preact"),l=require("preact/hooks"),C=require("../index.es-D_7T9vdv.cjs");var b=0;function q(t,e,n,r,o,u){e||(e={});var i,s,c=e;if("ref"in c)for(s in c={},e)s=="ref"?i=e[s]:c[s]=e[s];var p={type:t,props:c,key:n,ref:i,__k:null,__:null,__b:0,__e:null,__c:null,constructor:void 0,__v:--b,__i:-1,__u:0,__source:o,__self:u};if(typeof t=="function"&&(i=t.defaultProps))for(s in i)c[s]===void 0&&(c[s]=i[s]);return _.options.vnode&&_.options.vnode(p),p}const m=t=>String(t)==="[object Object]";function S(t){if(!m(t))return!1;const e=t.constructor;if(e===void 0)return!0;const n=e.prototype;return!(!m(n)||!n.hasOwnProperty("isPrototypeOf"))}const j=t=>{const e=Object.entries(t).reduce((n,[r,o])=>{const u=S(o)?j(o):Object.freeze(o);return{...n,[r]:u}},{});return Object.freeze(e)};function A(...t){return t.reduce((e,n)=>P(e,n))}function P(t,e){return S(t)&&S(e)?Object.entries(e).reduce((n,[r,o])=>(n[r]=P(n[r],o),n),{...t}):e}function y(t,e){if(t===e)return!0;if(t instanceof Date&&e instanceof Date)return t.getTime()===e.getTime();if(Array.isArray(t)&&Array.isArray(e))return t.length!==e.length?!1:t.every((n,r)=>y(n,e[r]));if(S(t)&&S(e)){const n=Object.entries(t);return n.length!==Object.keys(e).length?!1:n.every(([r,o])=>y(o,e[r]))}return!1}const h={loading:!0,pageType:void 0,query:{query:""},response:{query:""},initialized:!1,customParams:{}};function v(t){const e=new Map;let n=t?A(h,t):h;const r=j(n);function o(a){n=a(n);for(const f of e.values())f(n)}function u(a){o(f=>({...f,...a}))}function i(){return n}function s(){return r}function c(a,f){let O;e.set(f,z=>{const g=a(z);y(g,O)||(O=g,f(g))})}function p(a){e.delete(a)}return{updateState:u,getState:i,getInitialState:s,onChange:c,clearOnChange:p}}const d=_.createContext(v({}));function k({store:t,children:e}){return q(d,{value:t,children:e})}function x(t=T){const e=l.useContext(d),[n,r]=l.useState(t(e.getState()));return e.onChange(t,r),l.useEffect(()=>()=>e.clearOnChange(r),[e]),n}const T=t=>t;function w(){const[t,e]=l.useState([]),[n,r]=l.useState([]);return l.useEffect(()=>{C.s(async o=>{const{products:u,segments:i}=await o.getSearchSessionParams();e(i??[]),r((u==null?void 0:u.personalizationBoost)??[])})},[]),{segments:t,boost:n}}function E(t){return v(t)}exports.StoreContext=d;exports.StoreProvider=k;exports.mockStore=E;exports.useNostoAppState=x;exports.usePersonalization=w;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("preact"),E=require("../index.es-D_7T9vdv.cjs"),p=require("preact/hooks"),W=require("../core/core.cjs.js");function X(e){return e.type==="input"}function Z(e){return!!e&&typeof e=="object"&&"type"in e&&"props"in e}function O(e,t={depth:0}){const{children:r}=e;return b.toChildArray(r).map(n=>{if(!Z(n))return n;const o=e.updateElement(n,t);return o===null?null:(o.props={...o.props,children:O({...e,children:o.props.children},{depth:t.depth+1})},o)})}function ee({children:e,onSearchInput:t}){return O({children:e,updateElement:r=>(!X(r)||r.props.type!=="search"||(r.props={...r.props,onInput:n=>{n.target instanceof HTMLInputElement&&t(n.target)}}),r)})}const N=b.createContext(null),B=()=>{const e=p.useContext(N);if(!e)throw new Error("useConfig must be used within a ConfigProvider");return e},te="nosto:search:scrollPos";function re(){window.sessionStorage.setItem(te,window.scrollY.toString())}function ne({children:e,hit:t,onClick:r}){const{pageType:n}=B(),o=n==="autocomplete"?void 0:n;return O({children:e,updateElement:(u,a)=>(a.depth>0||(u.props={...u.props,onClick:c=>{t&&o&&E.s(l=>l.recordSearchClick(o,t)),re(),typeof r=="function"&&r(c)}}),u)})}var oe=0;function w(e,t,r,n,o,u){t||(t={});var a,c,l=t;if("ref"in l)for(c in l={},t)c=="ref"?a=t[c]:l[c]=t[c];var s={type:e,props:l,key:r,ref:a,__k:null,__:null,__b:0,__e:null,__c:null,constructor:void 0,__v:--oe,__i:-1,__u:0,__source:o,__self:u};if(typeof e=="function"&&(a=e.defaultProps))for(c in a)l[c]===void 0&&(l[c]=a[c]);return b.options.vnode&&b.options.vnode(s),s}function q(){p.useEffect(()=>{const e=window.setTimeout(()=>{console.error("Nosto client script has not loaded after 3 seconds.")},3e3);E.s(()=>{window.clearTimeout(e)})},[])}const $=e=>String(e)==="[object Object]";function k(e){if(!$(e))return!1;const t=e.constructor;if(t===void 0)return!0;const r=t.prototype;return!(!$(r)||!r.hasOwnProperty("isPrototypeOf"))}function x(...e){return e.reduce((t,r)=>z(t,r))}function D(...e){return e.reduce((t,r)=>z(t,r))}function z(e,t){return k(e)&&k(t)?Object.entries(t).reduce((r,[n,o])=>(r[n]=z(r[n],o),r),{...e}):k(e)?e:t}function F(e,t){if(e===t)return!0;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(Array.isArray(e)&&Array.isArray(t))return e.length!==t.length?!1:e.every((r,n)=>F(r,t[n]));if(k(e)&&k(t)){const r=Object.entries(e);return r.length!==Object.keys(t).length?!1:r.every(([n,o])=>F(o,t[n]))}return!1}function K(e,t={}){const r=new Map;let n=D(e,t??{});const o=D(e,t);function u(i){n=i(n);for(const f of r.values())f(n)}function a(i){u(f=>({...f,...i}))}function c(){return n}function l(){return structuredClone(o)}function s(i,f){let S;r.set(f,g=>{const y=i(g);F(y,S)||(S=y,f(y))})}function d(i){r.delete(i)}return{updateState:a,getState:c,getInitialState:l,onChange:s,clearOnChange:d}}const Q={loading:!0,query:{query:""},response:{query:""},initialized:!1};function A(e={}){return K(Q,e)}const P=b.createContext(A()),j={defaultCurrency:"EUR",queryModifications:e=>e},se={...j};function ce(e){return{pageType:"autocomplete",...se,...e}}function ae({config:e,store:t,children:r}){const n=t??A();return q(),w(N,{value:ce(e),children:w(P,{value:n,children:r})})}const ie={...j,persistentSearchCache:!1,preservePageScroll:!1};function ue(e){return{pageType:"category",...ie,...e}}function le({config:e,store:t,children:r}){const n=t??A();return q(),w(N,{value:ue(e),children:w(P,{value:n,children:r})})}const de={...j,persistentSearchCache:!1,preservePageScroll:!1};function fe(e={}){return{pageType:"serp",...de,...e}}function ge({config:e,store:t,children:r}){const n=t??A();return q(),w(N,{value:fe(e),children:w(P,{value:n,children:r})})}function pe(e){return Array.from(new Set(e))}function me(e,t){return{...t,products:{facets:e==="autocomplete"?void 0:["*"],fields:Se,...t.products},...t.keywords?{keywords:{...t.keywords,fields:pe([...ye,...t.keywords.fields??[]]),highlight:t.keywords.highlight??{preTag:"<strong>",postTag:"</strong>"}}}:{}}}const Se=["productId","url","name","imageUrl","imageHash","thumbUrl","description","brand","variantId","availability","price","priceText","categoryIds","categories","customFields.key","customFields.value","priceCurrencyCode","datePublished","listPrice","unitPricingBaseMeasure","unitPricingUnit","unitPricingMeasure","googleCategory","gtin","ageGroup","gender","condition","alternateImageUrls","ratingValue","reviewCount","inventoryLevel","skus.id","skus.name","skus.price","skus.listPrice","skus.priceText","skus.url","skus.imageUrl","skus.inventoryLevel","skus.customFields.key","skus.customFields.value","skus.availability","pid","onDiscount","extra.key","extra.value","saleable","available","tags1","tags2","tags3"],ye=["keyword","_redirect"];function he(...e){if(!e.every(t=>t==null))return e.filter(t=>Array.isArray(t)).reduce((t,r)=>t.concat(r),[])}const H="nosto.search.";function Ce(e){var r;const t=`${H}${e}`;(r=window.performance)==null||r.mark(`${t}.start`)}function ve(e){var r,n;const t=`${H}${e}`;(r=window.performance)==null||r.mark(`${t}.end`),(n=window.performance)==null||n.measure(t,`${t}.start`,`${t}.end`)}function I(e){return Ce(e),()=>ve(e)}function ke(e,t,r){const n=JSON.stringify(t);try{console.info("Setting storage",e,n),r.setItem(e,n)}catch(o){console.warn(o)}}function we(e,t){try{const r=t.getItem(e);if(r)return JSON.parse(r)}catch(r){console.warn(r)}}function Pe(e,t){ke(e,t,sessionStorage)}function be(e){return we(e,sessionStorage)}const L="nosto:search:searchResult";function Fe(e,t,r){if(!e)return;Pe(L,{query:t,result:r})}function Ae(e,t){if(!e)return null;const r=be(L);if(!r||!Re(r))return null;const n=U(r.query);return F(U(t),n)?r.result:null}function U(e){return{accountId:e.accountId,customRules:e.customRules,explain:e.explain,keywords:e.keywords,products:e.products,query:e.query,redirect:e.redirect,rules:e.rules,segments:e.segments,sessionParams:e.sessionParams}}function Re(e){return typeof e=="object"&&e!==null&&"query"in e&&"result"in e}async function G(e,t,r){var s,d,i;const n=I("newSearch"),o=e.config.pageType,u=x(e.store.getInitialState().query,t),a=x(e.config.search,r,{track:o,redirect:o!=="autocomplete",isKeyword:!!(r!=null&&r.isKeyword)});e.store.updateState({query:u,loading:!0,initialized:!0});const c=o!=="autocomplete"&&e.config.persistentSearchCache,l=e.config.queryModifications({...u,products:{...u.products,filter:he((d=(s=e.store.getInitialState().query)==null?void 0:s.products)==null?void 0:d.filter,(i=t.products)==null?void 0:i.filter)}},o);try{let f;const S=Ae(c,l);if(S)f=S;else{const g=me(o,l);f=await W.search(g,a),Fe(c,l,f)}e.store.updateState({response:f,loading:!1})}catch(f){console.error("Search action failed",f)}n()}async function V(e,t,r){const n=I("updateSearch"),o=x(e.store.getState().query,{products:{from:0}},t);await G(e,o,r),n()}async function Me(e,t,r){var a;const n=I("replaceFilter"),o=(a=e.store.getState().query.products)==null?void 0:a.filter,u=r!==void 0?[{field:t,[typeof r=="object"?"range":"value"]:[r]}]:[];await V(e,{products:{filter:[...(o==null?void 0:o.filter(c=>c.field!==t))??[],...u]}}),n()}async function _e(e,t,r,n){var l,s;const o=I("toggleProductFilter"),u=(l=e.store.getState().query.products)==null?void 0:l.filter,a=u==null?void 0:u.find(d=>d.value instanceof Array&&d.field===t),c=a!=null&&a.value?{...a,value:[...a.value.filter(d=>!F(d,r)),...n?[r]:[]]}:n?{field:t,value:[r]}:void 0;await V(e,{products:{filter:[...(u==null?void 0:u.filter(d=>d!==a))??[],...(s=c==null?void 0:c.value)!=null&&s.length?[c]:[]]}}),o()}function v(){const e=B(),t=p.useContext(P),r=p.useMemo(()=>({config:e,store:t}),[e,t]),n=p.useCallback((c,l)=>G(r,c,l),[r]),o=p.useCallback((c,l)=>V(r,c,l),[r]),u=p.useCallback((c,l,s)=>_e(r,c,l,s),[r]),a=p.useCallback((c,l)=>Me(r,c,l),[r]);return{newSearch:n,updateSearch:o,toggleProductFilter:u,replaceFilter:a}}function C(e=Ne){const t=p.useContext(P),[r,n]=p.useState(e(t.getState()));return t.onChange(e,n),p.useEffect(()=>()=>t.clearOnChange(n),[t]),r}const Ne=e=>e;function Ie(e){const t=C(r=>r.response);return e&&Array.isArray(e)||e&&k(e)?e:t}function Te(e){var a;const t=((a=e.data)==null?void 0:a.filter(c=>c.selected).length)??0,[r,n]=p.useState(t>0),{toggleProductFilter:o}=v();return{active:r,selectedFiltersCount:t,toggleActive:()=>{n(!r)},toggleProductFilter:o}}function xe(){const{loading:e,facets:t}=C(r=>{var n,o;return{loading:r.loading,facets:((o=(n=r.response)==null?void 0:n.products)==null?void 0:o.facets)??[]}});return{loading:e,facets:t}}function Ee(){const[e,t]=p.useState([]),[r,n]=p.useState([]);return p.useEffect(()=>{E.s(async o=>{const{products:u,segments:a}=await o.getSearchSessionParams();t(a??[]),n((u==null?void 0:u.personalizationBoost)??[])})},[]),{segments:e,boost:r}}function Oe(){const{facets:e}=C(s=>{var d;return{facets:((d=s.response.products)==null?void 0:d.facets)??[]}}),{replaceFilter:t,toggleProductFilter:r}=v(),n=p.useCallback(s=>{const d=e==null?void 0:e.find(i=>i.type==="stats"&&i.field===s);if(d&&"min"in d&&"max"in d)return d},[e]),o=p.useCallback(s=>{var d;return((d=e==null?void 0:e.find(i=>i.field===s))==null?void 0:d.name)??s},[e]),u=p.useCallback(s=>"field"in s&&(s.value instanceof Array||s.range instanceof Array),[]),a=p.useCallback(s=>{var d;return{...s,range:(d=s.range)==null?void 0:d.map(i=>({gt:i.gt?Number(i.gt):i.gt,gte:i.gte?Number(i.gte):i.gte,lt:i.lt?Number(i.lt):i.lt,lte:i.lte?Number(i.lte):i.lte}))}},[]),c=p.useCallback(s=>(s.value??[]).map(i=>({value:i,field:s.field,name:o(s.field),filter:a(s),remove:()=>{r(s.field,i,!1)}})),[a,o,r]),l=p.useCallback(s=>(s.range??[]).map(i=>{var g,y;const f=i.gte??i.gt??((g=n(s.field))==null?void 0:g.min),S=i.lte??i.lt??((y=n(s.field))==null?void 0:y.max);if(f!==void 0&&S!==void 0)return{value:`${f} - ${S}`,field:s.field,name:o(s.field),filter:a(s),remove:()=>{t(s.field,void 0)}}}).filter(Boolean),[a,n,o,t]);return{selectFilters:u,toValueFilter:c,toRangeFilter:l}}function J(){const{filter:e}=C(c=>{var l;return{filter:((l=c.query.products)==null?void 0:l.filter)??[]}}),{updateSearch:t}=v(),{selectFilters:r,toValueFilter:n,toRangeFilter:o}=Oe(),u=p.useMemo(()=>e?e.filter(r).flatMap(c=>"value"in c?n(c):"range"in c?o(c):[]).filter(Boolean):[],[e,r,o,n]),a=p.useCallback(()=>{t({products:{filter:[]}})},[t]);return{filters:u,removeAll:a}}function _(e){if(typeof e=="number")return e;if(typeof e!="string")return;const t=Number(e);return isNaN(t)?void 0:t}function Y(e){var i,f,S;const{replaceFilter:t}=v(),{query:r,products:n}=C(g=>({query:g.query,products:g.response.products})),o=(i=n==null?void 0:n.facets)==null?void 0:i.find(g=>g.id===e);if(!o)return{min:0,max:0,range:[0,0],updateRange:()=>{}};const u=(S=(f=r.products)==null?void 0:f.filter)==null?void 0:S.find(g=>g.field===o.field),a=u!=null&&u.range?u.range[0]:void 0,c=typeof a=="object"&&("gte"in a||"lte"in a)?[_(a.gte),_(a.lte)]:[void 0,void 0],l="min"in o?Math.floor(o.min):0,s="max"in o?Math.ceil(o.max):0,d=([g,y])=>{const m=g!==void 0?Math.floor(g):void 0,h=y!==void 0?Math.ceil(y):void 0,R=m!==void 0,M=h!==void 0;(l===m||!R)&&(s===h||!M)?t(o.field,void 0):(l===m||!R)&&M?t(o.field,{lte:h.toString()}):(s===h||!M)&&R?t(o.field,{gte:m.toString()}):R&&M&&t(o.field,{gte:m.toString(),lte:h.toString()})};return{min:l,max:s,range:[c[0]??l,c[1]??s],updateRange:d}}function qe(e,t){const{min:r,max:n,range:o,updateRange:u}=Y(e),{filters:a}=J(),c=p.useMemo(()=>{var y;const i=a.find(m=>{var h;return(h=m==null?void 0:m.filter)==null?void 0:h.range});let f=null;if(i){const m=(y=i.filter.range)==null?void 0:y[0];f=[_(m==null?void 0:m.gte),_(m==null?void 0:m.lte)]}const S=[];let g=Math.floor(r/t)*t;for(;g<n;){const m=g+t,h=f&&f[0]===g&&f[1]===m;S.push({min:g,max:m,selected:h}),g=m}return S},[a,r,n,t]),l=i=>{u([i,o[1]])},s=i=>{u([o[0],i])},d=r!==o[0]||n!==o[1];return{min:r,max:n,range:o,updateRange:u,ranges:c,handleMinChange:l,handleMaxChange:s,isSelected:d}}function ze(){const{products:e,keywords:t}=C(r=>r.response);return{products:e??{hits:[],total:0},keywords:t??{hits:[],total:0}}}function je(){const e=C(r=>{var n;return(n=r.query.products)==null?void 0:n.filter});return p.useMemo(()=>e?e.reduce((r,n)=>r+(Array.isArray(n.value)?n.value.length:1),0):0,[e])}function T(e){return e&&!Number.isNaN(e)?e:0}function Ve(e,t){const{from:r,size:n,total:o}=C(s=>{var d,i,f,S,g;return{from:T(((d=s.query.products)==null?void 0:d.from)??0),size:T(((f=(i=s.response)==null?void 0:i.products)==null?void 0:f.size)??0),total:T(((g=(S=s.response)==null?void 0:S.products)==null?void 0:g.total)??0)}}),{updateSearch:u}=v(),a=r+n,c=[...e].reverse().filter(s=>s+t<o);return{from:r,to:a,total:o,size:n,sizeOptions:c,handleSizeChange:s=>{u({products:{size:s}})}}}function $e(e,t){return e.length!==t.length?!1:e.every(r=>t.find(n=>r.field===n.field&&r.order===n.order))}function De(e){var u,a;const t=C(c=>c.query),{updateSearch:r}=v();return{activeSort:((u=e.find(c=>{var l;return $e(c.value.sort,((l=t.products)==null?void 0:l.sort)||[])}))==null?void 0:u.id)??((a=e[0])==null?void 0:a.id),setSort:c=>{const l=e.find(s=>s.id===c);l&&r({products:{sort:l.value.sort}})}}}exports.AutocompletePageProvider=ae;exports.CategoryPageProvider=le;exports.SearchInput=ee;exports.SearchPageProvider=ge;exports.SerpElement=ne;exports.StoreContext=P;exports.createExtendableStore=K;exports.createStore=A;exports.defaultState=Q;exports.useActions=v;exports.useDecoratedSearchResults=Ie;exports.useFacet=Te;exports.useFacets=xe;exports.useNostoAppState=C;exports.usePersonalization=Ee;exports.useProductFilters=J;exports.useRange=Y;exports.useRangeSelector=qe;exports.useResponse=ze;exports.useSelectedFiltersCount=je;exports.useSizeOptions=Ve;exports.useSort=De;
@@ -1,6 +1,25 @@
1
1
  /** @module preact */
2
- export { useNostoAppState } from "./src/hooks/useNostoAppState";
3
- export { usePersonalization } from "./src/hooks/usePersonalization";
4
- export { type State, type Store } from "./src/store";
5
- export { StoreContext, StoreProvider } from "./src/storeContext";
6
- export { mockStore } from "./test/mocks/mocks";
2
+ export { SearchInput } from './src/components/SearchInput';
3
+ export { SerpElement } from './src/components/SerpElement';
4
+ export type { PublicAutocompleteConfig as AutocompleteConfig } from './src/config/pages/autocomplete/config';
5
+ export { AutocompletePageProvider } from './src/config/pages/autocomplete/provider';
6
+ export type { PublicCategoryConfig as CategoryConfig } from './src/config/pages/category/config';
7
+ export { CategoryPageProvider } from './src/config/pages/category/provider';
8
+ export type { PublicSerpConfig as SerpConfig } from './src/config/pages/serp/config';
9
+ export { SearchPageProvider } from './src/config/pages/serp/provider';
10
+ export { useActions } from './src/hooks/useActions';
11
+ export { useDecoratedSearchResults } from './src/hooks/useDecoratedSearchResults';
12
+ export { useFacet } from './src/hooks/useFacet';
13
+ export { useFacets } from './src/hooks/useFacets';
14
+ export { useNostoAppState } from './src/hooks/useNostoAppState';
15
+ export { usePersonalization } from './src/hooks/usePersonalization';
16
+ export { useProductFilters } from './src/hooks/useProductFilters/useProductFilters';
17
+ export { useRange } from './src/hooks/useRange';
18
+ export { useRangeSelector } from './src/hooks/useRangeSelector';
19
+ export { useResponse } from './src/hooks/useResponse';
20
+ export { useSelectedFiltersCount } from './src/hooks/useSelectedFiltersCount';
21
+ export { useSizeOptions } from './src/hooks/useSizeOptions';
22
+ export { useSort } from './src/hooks/useSort/useSort';
23
+ export { createStore, defaultState, type State, type Store } from './src/store';
24
+ export { StoreContext } from './src/storeContext';
25
+ export { createExtendableStore, type ExtendedStore } from './src/storeExtensions';