@nosto/search-js 1.2.0 → 1.4.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 (46) hide show
  1. package/README.md +1 -1
  2. package/dist/core/core.cjs.js +1 -0
  3. package/dist/core/core.d.ts +3 -0
  4. package/dist/{search.es.js → core/core.es.js} +1 -1
  5. package/dist/{search → core/src}/searchWithRetries.d.ts +1 -1
  6. package/dist/currencies/currencies.cjs.js +1 -0
  7. package/dist/currencies/currencies.d.ts +3 -0
  8. package/dist/currencies/currencies.es.js +68 -0
  9. package/dist/preact/preact.cjs.js +1 -0
  10. package/dist/preact/preact.d.ts +6 -0
  11. package/dist/preact/preact.es.js +133 -0
  12. package/dist/preact/src/hooks/useNostoAppState.d.ts +40 -0
  13. package/dist/preact/src/hooks/usePersonalization.d.ts +41 -0
  14. package/dist/preact/src/store.d.ts +1421 -0
  15. package/dist/preact/src/storeContext.d.ts +1391 -0
  16. package/dist/preact/test/mocks/mocks.d.ts +1384 -0
  17. package/dist/thumbnails/{generateThumbnailUrl.d.ts → src/generateThumbnailUrl.d.ts} +4 -4
  18. package/dist/thumbnails/{thumbnailDecorator.d.ts → src/nostoThumbnailDecorator.d.ts} +2 -7
  19. package/dist/thumbnails/{shopifyThumbnailDecorator.d.ts → src/shopifyThumbnailDecorator.d.ts} +3 -16
  20. package/dist/thumbnails/src/thumbnailDecorator.d.ts +9 -0
  21. package/dist/thumbnails/{types.d.ts → src/types.d.ts} +17 -1
  22. package/dist/thumbnails/thumbnails.cjs.js +1 -0
  23. package/dist/thumbnails/thumbnails.d.ts +6 -0
  24. package/dist/thumbnails/thumbnails.es.js +105 -0
  25. package/dist/utils/src/deepFreeze.d.ts +27 -0
  26. package/dist/utils/src/deepMerge.d.ts +40 -0
  27. package/dist/utils/src/isEqual.d.ts +1 -0
  28. package/dist/utils/src/isPlainObject.d.ts +1 -0
  29. package/dist/utils/src/mergeArrays.d.ts +1 -0
  30. package/dist/utils/src/simplify.d.ts +14 -0
  31. package/dist/utils/src/types.d.ts +3 -0
  32. package/package.json +37 -27
  33. package/dist/currencies.cjs.js +0 -1
  34. package/dist/currencies.d.ts +0 -3
  35. package/dist/currencies.es.js +0 -68
  36. package/dist/search.cjs.js +0 -1
  37. package/dist/search.d.ts +0 -3
  38. package/dist/thumbnails.cjs.js +0 -1
  39. package/dist/thumbnails.d.ts +0 -5
  40. package/dist/thumbnails.es.js +0 -105
  41. /package/dist/{search → core/src}/applyDecorators.d.ts +0 -0
  42. /package/dist/{search → core/src}/search.d.ts +0 -0
  43. /package/dist/{search → core/src}/types.d.ts +0 -0
  44. /package/dist/{search → core/src}/utils/delay.d.ts +0 -0
  45. /package/dist/currencies/{getCurrencyFormatting.d.ts → src/getCurrencyFormatting.d.ts} +0 -0
  46. /package/dist/currencies/{priceDecorator.d.ts → src/priceDecorator.d.ts} +0 -0
package/README.md CHANGED
@@ -51,7 +51,7 @@ const response = await search({
51
51
  track: 'serp',
52
52
  hitDecorators: [
53
53
  priceDecorator(),
54
- thumbnailDecorator()
54
+ thumbnailDecorator({ size: "9" })
55
55
  ]
56
56
  })
57
57
 
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("../index.es-D_7T9vdv.cjs");function a(t,e){if(!t.products||!(e!=null&&e.length))return t;const r=i=>e.reduce((u,n)=>n(u),i);return{...t,products:{...t.products,hits:t.products.hits.map(r)}}}function o(t){return new Promise(e=>setTimeout(e,t))}async function l(t,e,r){const{maxRetries:i=0,retryInterval:u=1e3}=r;let n=0;for(;;)try{return await t.search(e,r)}catch(c){if(!f(c))throw console.info("Skipping retry logic for",c),c;if(n>=i)throw c;n++,await o(u)}}function f(t){return!t||typeof t!="object"?!1:!("status"in t)||h(t.status)}function h(t){return typeof t=="number"&&(t<400||t>=500)}async function y(t,e={}){const{hitDecorators:r,...i}=e,u=await new Promise(s.s),n=await l(u,t,i);return a(n,r)}exports.search=y;
@@ -0,0 +1,3 @@
1
+ /** @module ./ */
2
+ export { search } from "./src/search";
3
+ export type { HitDecorator, Options } from "./src/types";
@@ -1,4 +1,4 @@
1
- import { s as a } from "./index.es-Bcd5IQh9.js";
1
+ import { s as a } from "../index.es-Bcd5IQh9.js";
2
2
  function s(t, r) {
3
3
  if (!t.products || !(r != null && r.length))
4
4
  return t;
@@ -1,4 +1,4 @@
1
- import { API, SearchQuery, SearchResult } from "@nosto/nosto-js/client";
1
+ import type { API, SearchQuery, SearchResult } from "@nosto/nosto-js/client";
2
2
  import { Options } from "./types";
3
3
  export declare function searchWithRetries(api: API, query: SearchQuery, options: Options<[]>): Promise<SearchResult>;
4
4
  /**
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("../index.es-D_7T9vdv.cjs"),g={defaultCurrency:"EUR",defaultLocale:"en-US",currencySettings:{}},p={EUR:"de-DE",GBP:"en-GB",USD:"en-US",AUD:"en-AU",CAD:"en-CA",INR:"en-IN",AFN:"en-IN",BDT:"en-IN",BTN:"en-IN",MMK:"en-IN",NPR:"en-IN",PKR:"en-IN"};function y(u={}){const o={...g,...u};u.currencySettings||d.s(c=>{o.currencySettings=c.internal.getSettings().currencySettings??{}});function a(c,n){const{defaultCurrency:t,currencySettings:r,defaultLocale:i}=o,s=n??t,f=p[s]??i;if(s in r){const e=r[s],l=new Intl.NumberFormat(f,{useGrouping:!!e.groupingSeparator,minimumFractionDigits:e.decimalPlaces,maximumFractionDigits:e.decimalPlaces}).formatToParts(c).map(m=>m.type==="group"?e.groupingSeparator:m.type==="decimal"?e.decimalCharacter:m.value).join("");return e!=null&&e.currencyBeforeAmount?`${e.currencyToken}${l}`:`${l}${e==null?void 0:e.currencyToken}`}return new Intl.NumberFormat(f,{style:"currency",currency:s}).format(c)}return{formatCurrency:a}}function C(u){const{formatCurrency:o}=y(u);function a(n,t){const r={};return n.price!==void 0&&(r.priceText=o(n.price,t)),n.listPrice!==void 0&&(r.listPriceText=o(n.listPrice,t)),Object.assign({},n,r)}function c(n){return n.price!==void 0||n.listPrice!==void 0}return function(t){if(!c(t))return t;const r=a(t,t.priceCurrencyCode);return r.skus&&r.skus.some(c)&&(r.skus=r.skus.map(i=>c(i)?a(i,t.priceCurrencyCode):i)),r}}exports.getCurrencyFormatting=y;exports.priceDecorator=C;
@@ -0,0 +1,3 @@
1
+ /** @module currencies */
2
+ export { type CurrencyConfig, getCurrencyFormatting } from "./src/getCurrencyFormatting";
3
+ export { type FormattedPrices, priceDecorator, type Result } from "./src/priceDecorator";
@@ -0,0 +1,68 @@
1
+ import { s as y } from "../index.es-Bcd5IQh9.js";
2
+ const p = {
3
+ defaultCurrency: "EUR",
4
+ defaultLocale: "en-US",
5
+ /** @hidden */
6
+ currencySettings: {}
7
+ }, d = {
8
+ EUR: "de-DE",
9
+ GBP: "en-GB",
10
+ USD: "en-US",
11
+ AUD: "en-AU",
12
+ CAD: "en-CA",
13
+ //India, Afghanistan, Bangladesh, Bhutan, Myanmar, Nepal, and Pakistan uses lakhs and crores notation
14
+ INR: "en-IN",
15
+ AFN: "en-IN",
16
+ BDT: "en-IN",
17
+ BTN: "en-IN",
18
+ MMK: "en-IN",
19
+ NPR: "en-IN",
20
+ PKR: "en-IN"
21
+ };
22
+ function g(i = {}) {
23
+ const o = {
24
+ ...p,
25
+ ...i
26
+ };
27
+ i.currencySettings || y((c) => {
28
+ o.currencySettings = c.internal.getSettings().currencySettings ?? {};
29
+ });
30
+ function a(c, n) {
31
+ const { defaultCurrency: t, currencySettings: r, defaultLocale: u } = o, s = n ?? t, f = d[s] ?? u;
32
+ if (s in r) {
33
+ const e = r[s], l = new Intl.NumberFormat(f, {
34
+ useGrouping: !!e.groupingSeparator,
35
+ minimumFractionDigits: e.decimalPlaces,
36
+ maximumFractionDigits: e.decimalPlaces
37
+ }).formatToParts(c).map((m) => m.type === "group" ? e.groupingSeparator : m.type === "decimal" ? e.decimalCharacter : m.value).join("");
38
+ return e != null && e.currencyBeforeAmount ? `${e.currencyToken}${l}` : `${l}${e == null ? void 0 : e.currencyToken}`;
39
+ }
40
+ return new Intl.NumberFormat(f, {
41
+ style: "currency",
42
+ currency: s
43
+ }).format(c);
44
+ }
45
+ return {
46
+ formatCurrency: a
47
+ };
48
+ }
49
+ function S(i) {
50
+ const { formatCurrency: o } = g(i);
51
+ function a(n, t) {
52
+ const r = {};
53
+ return n.price !== void 0 && (r.priceText = o(n.price, t)), n.listPrice !== void 0 && (r.listPriceText = o(n.listPrice, t)), Object.assign({}, n, r);
54
+ }
55
+ function c(n) {
56
+ return n.price !== void 0 || n.listPrice !== void 0;
57
+ }
58
+ return function(t) {
59
+ if (!c(t))
60
+ return t;
61
+ const r = a(t, t.priceCurrencyCode);
62
+ return r.skus && r.skus.some(c) && (r.skus = r.skus.map((u) => c(u) ? a(u, t.priceCurrencyCode) : u)), r;
63
+ };
64
+ }
65
+ export {
66
+ g as getCurrencyFormatting,
67
+ S as priceDecorator
68
+ };
@@ -0,0 +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;
@@ -0,0 +1,6 @@
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";
@@ -0,0 +1,133 @@
1
+ import { options as m, createContext as b } from "preact";
2
+ import { useContext as A, useState as S, useEffect as h } from "preact/hooks";
3
+ import { s as k } from "../index.es-Bcd5IQh9.js";
4
+ var q = 0;
5
+ function x(t, e, n, r, o, i) {
6
+ e || (e = {});
7
+ var u, s, c = e;
8
+ if ("ref" in c) for (s in c = {}, e) s == "ref" ? u = e[s] : c[s] = e[s];
9
+ var p = { type: t, props: c, key: n, ref: u, __k: null, __: null, __b: 0, __e: null, __c: null, constructor: void 0, __v: --q, __i: -1, __u: 0, __source: o, __self: i };
10
+ if (typeof t == "function" && (u = t.defaultProps)) for (s in u) c[s] === void 0 && (c[s] = u[s]);
11
+ return m.vnode && m.vnode(p), p;
12
+ }
13
+ const d = (t) => String(t) === "[object Object]";
14
+ function l(t) {
15
+ if (!d(t))
16
+ return !1;
17
+ const e = t.constructor;
18
+ if (e === void 0)
19
+ return !0;
20
+ const n = e.prototype;
21
+ return !(!d(n) || !n.hasOwnProperty("isPrototypeOf"));
22
+ }
23
+ const j = (t) => {
24
+ const e = Object.entries(t).reduce((n, [r, o]) => {
25
+ const i = l(o) ? j(o) : Object.freeze(o);
26
+ return {
27
+ ...n,
28
+ [r]: i
29
+ };
30
+ }, {});
31
+ return Object.freeze(e);
32
+ };
33
+ function w(...t) {
34
+ return t.reduce((e, n) => v(e, n));
35
+ }
36
+ function v(t, e) {
37
+ return l(t) && l(e) ? Object.entries(e).reduce(
38
+ (n, [r, o]) => (n[r] = v(n[r], o), n),
39
+ { ...t }
40
+ ) : e;
41
+ }
42
+ function _(t, e) {
43
+ if (t === e)
44
+ return !0;
45
+ if (t instanceof Date && e instanceof Date)
46
+ return t.getTime() === e.getTime();
47
+ if (Array.isArray(t) && Array.isArray(e))
48
+ return t.length !== e.length ? !1 : t.every((n, r) => _(n, e[r]));
49
+ if (l(t) && l(e)) {
50
+ const n = Object.entries(t);
51
+ return n.length !== Object.keys(e).length ? !1 : n.every(([r, o]) => _(o, e[r]));
52
+ }
53
+ return !1;
54
+ }
55
+ const O = {
56
+ loading: !0,
57
+ pageType: void 0,
58
+ query: {
59
+ query: ""
60
+ },
61
+ response: {
62
+ query: ""
63
+ },
64
+ initialized: !1,
65
+ customParams: {}
66
+ };
67
+ function z(t) {
68
+ const e = /* @__PURE__ */ new Map();
69
+ let n = t ? w(O, t) : O;
70
+ const r = j(n);
71
+ function o(f) {
72
+ n = f(n);
73
+ for (const a of e.values())
74
+ a(n);
75
+ }
76
+ function i(f) {
77
+ o((a) => ({ ...a, ...f }));
78
+ }
79
+ function u() {
80
+ return n;
81
+ }
82
+ function s() {
83
+ return r;
84
+ }
85
+ function c(f, a) {
86
+ let y;
87
+ e.set(a, (P) => {
88
+ const g = f(P);
89
+ _(g, y) || (y = g, a(g));
90
+ });
91
+ }
92
+ function p(f) {
93
+ e.delete(f);
94
+ }
95
+ return {
96
+ updateState: i,
97
+ getState: u,
98
+ getInitialState: s,
99
+ onChange: c,
100
+ clearOnChange: p
101
+ };
102
+ }
103
+ const C = b(z({}));
104
+ function M({ store: t, children: e }) {
105
+ return /* @__PURE__ */ x(C, { value: t, children: e });
106
+ }
107
+ function V(t = T) {
108
+ const e = A(C), [n, r] = S(t(e.getState()));
109
+ return e.onChange(t, r), h(() => () => e.clearOnChange(r), [e]), n;
110
+ }
111
+ const T = (t) => t;
112
+ function F() {
113
+ const [t, e] = S([]), [n, r] = S([]);
114
+ return h(() => {
115
+ k(async (o) => {
116
+ const { products: i, segments: u } = await o.getSearchSessionParams();
117
+ e(u ?? []), r((i == null ? void 0 : i.personalizationBoost) ?? []);
118
+ });
119
+ }, []), {
120
+ segments: t,
121
+ boost: n
122
+ };
123
+ }
124
+ function I(t) {
125
+ return z(t);
126
+ }
127
+ export {
128
+ C as StoreContext,
129
+ M as StoreProvider,
130
+ I as mockStore,
131
+ V as useNostoAppState,
132
+ F as usePersonalization
133
+ };
@@ -0,0 +1,40 @@
1
+ import { State } from "@preact/store";
2
+ /**
3
+ * Imports a part of the Nosto state to the component.
4
+ *
5
+ * @example
6
+ * ```jsx
7
+ * import { useNostoAppState } from '@nosto/search-js/preact'
8
+ *
9
+ * export default () => {
10
+ * const pageType = useNostoAppState(state => state.pageType)
11
+ *
12
+ * return (
13
+ * <div>
14
+ * Page type is {pageType}
15
+ * </div>
16
+ * )
17
+ * }
18
+ * ```
19
+ *
20
+ * If the selector parameter is not provided, the full state is returned.
21
+ * Be mindful of the unnecessary rerenders it may cause.
22
+ *
23
+ * @example
24
+ * ```jsx
25
+ * import { useNostoAppState } from '@nosto/search-js/preact'
26
+ *
27
+ * export default () => {
28
+ * const state = useNostoAppState()
29
+ *
30
+ * return (
31
+ * <div>
32
+ * Page type is {state.pageType}
33
+ * </div>
34
+ * )
35
+ * }
36
+ * ```
37
+ *
38
+ * @group Hooks
39
+ */
40
+ export declare function useNostoAppState<Selected>(selector?: (state: State) => Selected): Selected;
@@ -0,0 +1,41 @@
1
+ import type { PersonalizationBoost } from "@nosto/nosto-js/client";
2
+ /**
3
+ * Custom hook to fetch personalization data such as segments and boost.
4
+ *
5
+ * This hook uses the `nostojs` library to fetch search session parameters and updates
6
+ * the state with the fetched segments and personalization boost.
7
+ *
8
+ * @returns An object containing:
9
+ * - `segments`: An array of strings representing the personalization segments.
10
+ * - `boost`: An array of `PersonalizationBoost` objects representing the personalization boost.
11
+ *
12
+ * @example
13
+ * ```jsx
14
+ * import { usePersonalization } from '@nosto/preact'
15
+ *
16
+ * export default () => {
17
+ * const { segments, boost } = usePersonalization()
18
+ *
19
+ * return (
20
+ * <div>
21
+ * <h1>Personalization Segments</h1>
22
+ * <ul>
23
+ * {segments.map(segment => (
24
+ * <li key={segment}>{segment}</li>
25
+ * ))}
26
+ * </ul>
27
+ * <h1>Personalization Boost</h1>
28
+ * <ul>
29
+ * {boost.map(item => (
30
+ * <li key={item.id}>{item.name}</li>
31
+ * ))}
32
+ * </ul>
33
+ * </div>
34
+ * )
35
+ * }
36
+ * ```
37
+ */
38
+ export declare function usePersonalization(): {
39
+ segments: string[];
40
+ boost: PersonalizationBoost[];
41
+ };