doofinder
Advanced tools
Comparing version 6.1.2 to 7.0.2
@@ -1,1 +0,1 @@ | ||
var doofinder=function(e){"use strict";class t extends Error{}function r(e){return"[object String]"===Object.prototype.toString.call(e)}function i(e){return"[object Number]"===Object.prototype.toString.call(e)&&!isNaN(e)}function s(e){return"[object Object]"===Object.prototype.toString.call(e)}function n(e){return s(e)&&e.constructor===Object&&!e.nodeType&&!e.setInterval}const o=/^([0-9a-f]{32})@([\w-]+)@([0-9a-f]{32})$/i,a=/^([0-9a-f]{32})(-.*)?/i;function c(e){return r(e)&&o.test(e)}function l(e){return r(e)&&a.test(e)}function u(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function f(e,t){if(u(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;const r=Object.keys(e),i=Object.keys(t);if(r.length!==i.length)return!1;for(let i=0;i<r.length;i++)if(!Object.prototype.hasOwnProperty.call(t,r[i])||!u(e[r[i]],t[r[i]]))return!1;return!0}class h{constructor(){this._types=new Set}set(e){const i=Array.isArray(e)?e:[e];if(i.filter(r).length!==i.length)throw new t("types must be strings");this._types=new Set(i)}dump(){return Array.from(this._types)}clear(){this._types.clear()}add(e){if("string"!=typeof e)throw new t("types must be strings");this._types.add(e)}has(e){return this._types.has(e)}remove(e){this._types.delete(e)}}function d(...e){const t=e.length;let r,i,s,o,a,c,l=e[0]||{},u=1,f=!1;if("boolean"==typeof l&&(f=l,l=e[u]||{},u++),"object"!=typeof l&&"function"!=typeof l&&(l={}),u===t)return l;for(;u<t;u++)if(null!=(r=e[u]))for(i in r)o=r[i],"__proto__"!==i&&l!==o&&(f&&o&&(n(o)||(a=Array.isArray(o)))?(s=l[i],c=a&&!Array.isArray(s)?[]:!a&&n(s)?{}:s,a=!1,l[i]=d(f,c,o)):void 0!==o&&(l[i]=o));return l}function p(e){return Array.isArray(e)?d(!0,[],e):n(e)?d(!0,{},e):e}class y{constructor(){this._filters=new Map}get(e){return this._denormalize(this._filters.get(e))}set(e,r){const i=this._normalize(r);if(i instanceof Set)this._filters.set(e,i);else{if(n(i)&&0===Object.entries(i).length)throw new t("plain object filters can't be empty");this._filters.set(e,p(i))}}has(e){return this._filters.has(e)}contains(e,t){return this.has(e)&&this._filterContainsOrEqualsValue(e,t,!1)}equals(e,t){return this.has(e)&&this._filterContainsOrEqualsValue(e,t,!0)}add(e,t){const r=this._normalize(t),i=this._filters.get(e);i instanceof Set?r instanceof Set?this._filters.set(e,new Set([...i,...r])):i.add(r):this._filters.set(e,r)}remove(e,t){const r=this._filters.get(e);if(r instanceof Set&&null!=t){const i=this._normalize(t);if(i instanceof Set)for(const e of i)r.delete(e);else r.delete(i);0===r.size&&this._filters.delete(e)}else this._filters.delete(e)}clear(){this._filters.clear()}setMany(e,t=!1){t&&this.clear();for(const t in e)this.set(t,e[t])}dump(){const e={};if(this._filters.forEach((t,r)=>{e[r]=this._denormalize(t)}),Object.keys(e).length>0)return e}_normalize(e){return r(e)||i(e)?new Set([e]):Array.isArray(e)?new Set(e):e}_denormalize(e){return e instanceof Set?[...e]:e}_filterContainsOrEqualsValue(e,t,r){const i=this._normalize(t),s=this._filters.get(e);if(s instanceof Set&&i instanceof Set){if(r&&s.size!==i.size)return!1;for(const e of i)if(!s.has(e))return!1;return!0}return f(s,i)}}class m{constructor(){this._sortings=[]}set(e,t){return this.clear(),"string"==typeof e?this.add(e,t):(Array.isArray(e)?e:[e]).forEach(e=>{"string"==typeof e?this.add({[e]:"asc"}):this.add(e)}),this._sortings.length}add(e,t){if("string"==typeof e)return this._addFieldSorting(e,t);if(this._isLikeSorting(e)){const t=Object.keys(e)[0];return"_geo_distance"===t?this._addGeoDistanceSorting(p(e[t])):this._addFieldSorting(t,e[t])}}get(){return p(this._sortings)}clear(){this._sortings.length=0}has(e){const t=Object.keys(e)[0];return!!this._sortings.find(r=>r[t]===e[t])}_isLikeSorting(e){return n(e)&&1===Object.keys(e).length}_addFieldSorting(e,r="asc"){if("asc"===r||"desc"===r)return this._sortings.push({[e]:r});throw new t(`wrong sorting value for field '${e}': ${JSON.stringify(r)}`)}_addGeoDistanceSorting(e){if(n(e)&&2===Object.keys(e).length&&"order"in e){if("string"==typeof e[Object.keys(e).find(e=>"order"!==e)])return this._sortings.push({_geo_distance:e})}throw new t("wrong sorting value for 'geo_distance': "+JSON.stringify(e))}}class g extends Error{}function _(e){if(null==e)throw new g("hashid parameter is mandatory");if(l(e))return e;throw new g("invalid hashid")}function b(e){if(void 0!==e){const t=parseInt(""+e,10);if(!i(t)||t<=0)throw new g("page must be an integer greater than 0");return t}}function w(e){if(void 0!==e){const t=parseInt(""+e,10);if(!i(t)||t<=0||t>100)throw new g("rpp must be a number between 1 and 100");return t}}function v(e,t){if((Array.isArray(e)?e:[e]).filter(e=>null==e||"string"==typeof e&&0===e.trim().length).length>0)throw new g(t);return e}class j{constructor(e={}){this._defaults={query:"",page:1,rpp:20},this._types=new h,this._filters=new y,this._excludes=new y,this._sort=new m,this.reset(),this.load(e)}get defaults(){return this._defaults}set defaults(e){this._defaults=p(e),this.reset()}reset(){this._types.clear(),this._filters.clear(),this._excludes.clear(),this._sort.clear(),this._params={},this.load(p(this.defaults))}load(e={}){Object.keys(e).forEach(t=>{"nostats"===t?this.noStats=!!e.nostats:"filter"===t?this._filters.setMany(e.filter):"exclude"===t?this._excludes.setMany(e.exclude):"type"===t?this._types.set(e.type):"sort"===t?this._sort.set(e.sort):this.setParam(t,e[t])})}dump(e=!1){e&&(_(this.hashid),b(this.page),w(this.rpp));const t={...p(this._params),type:this.types.dump(),filter:this.filters.dump(),exclude:this.excludes.dump(),sort:this.sort.get()};return["nostats","filter","exclude"].forEach(e=>{t[e]||delete t[e]}),["type","sort"].forEach(e=>{0===t[e].length&&delete t[e]}),"items"in t&&(t.items.length>0?delete t.query:delete t.items),t}copy(){const e=new j;return e.defaults=p(this.defaults),e.reset(),e.load(this.dump()),e}getParam(e){return this._params[e]}setParam(e,t){void 0!==t?"hashid"===e?this._params.hashid=_(t):"page"===e?this._params.page=b(t):"rpp"===e?this._params.rpp=w(t):"items"===e?this._params.items=function(e){if(Array.isArray(e)&&e.length===e.filter(c).length)return e;throw new g("items must be an array of doofinder ids")}(t):this._params[e]=t:delete this._params[e]}get hashid(){return this.getParam("hashid")}set hashid(e){this.setParam("hashid",e)}get text(){return this.getParam("query")}set text(e){this.setParam("query",e)}get items(){return this.getParam("items")}set items(e){this.setParam("items",e)}get page(){return this.getParam("page")}set page(e){this.setParam("page",e)}get rpp(){return this.getParam("rpp")}set rpp(e){this.setParam("rpp",e)}get transformer(){return this.getParam("transformer")}set transformer(e){this.setParam("transformer",e)}get queryName(){return this.getParam("query_name")}set queryName(e){this.setParam("query_name",e)}get queryCounter(){return this.getParam("query_counter")}set queryCounter(e){isNaN(e)?this.setParam("query_counter",void 0):this.setParam("query_counter",e)}get noStats(){return!!this.getParam("nostats")}set noStats(e){this.setParam("nostats",!!e)}get types(){return this._types}get filters(){return this._filters}get excludes(){return this._excludes}get sort(){return this._sort}}function O(e){const t=e.selected.buckets.map(e=>e.key),r=e.terms.buckets.map(e=>(t.includes(e.key)&&(e.selected=!0),e));return r.filter(e=>null==e.doc_count).length!==r.length&&r.sort((e,t)=>{const r=e.doc_count||0,i=t.doc_count||0;return r<i?1:r>i?-1:e.key.localeCompare(t.key)}),{type:"terms",label:e.label,visible:e.visible,order:e.order,terms:r}}function S(e){const t=e;return null!=e.facets&&(t._rawFacets=p(e.facets),t.facets=function(e){const t={};for(const i in e)"terms"in e[i]?t[i]=O(e[i]):"range"in e[i]&&(t[i]={type:"range",label:(r=e[i]).label,visible:r.visible,range:r.range.buckets[0].stats});var r;return t}(e.facets)),t}var P=Object.prototype.hasOwnProperty,k=Array.isArray,A=function(){for(var e=[],t=0;t<256;++t)e.push("%"+((t<16?"0":"")+t.toString(16)).toUpperCase());return e}(),C=function(e,t){for(var r=t&&t.plainObjects?Object.create(null):{},i=0;i<e.length;++i)void 0!==e[i]&&(r[i]=e[i]);return r},x={arrayToObject:C,assign:function(e,t){return Object.keys(t).reduce((function(e,r){return e[r]=t[r],e}),e)},combine:function(e,t){return[].concat(e,t)},compact:function(e){for(var t=[{obj:{o:e},prop:"o"}],r=[],i=0;i<t.length;++i)for(var s=t[i],n=s.obj[s.prop],o=Object.keys(n),a=0;a<o.length;++a){var c=o[a],l=n[c];"object"==typeof l&&null!==l&&-1===r.indexOf(l)&&(t.push({obj:n,prop:c}),r.push(l))}return function(e){for(;e.length>1;){var t=e.pop(),r=t.obj[t.prop];if(k(r)){for(var i=[],s=0;s<r.length;++s)void 0!==r[s]&&i.push(r[s]);t.obj[t.prop]=i}}}(t),e},decode:function(e,t,r){var i=e.replace(/\+/g," ");if("iso-8859-1"===r)return i.replace(/%[0-9a-f]{2}/gi,unescape);try{return decodeURIComponent(i)}catch(e){return i}},encode:function(e,t,r){if(0===e.length)return e;var i=e;if("symbol"==typeof e?i=Symbol.prototype.toString.call(e):"string"!=typeof e&&(i=String(e)),"iso-8859-1"===r)return escape(i).replace(/%u[0-9a-f]{4}/gi,(function(e){return"%26%23"+parseInt(e.slice(2),16)+"%3B"}));for(var s="",n=0;n<i.length;++n){var o=i.charCodeAt(n);45===o||46===o||95===o||126===o||o>=48&&o<=57||o>=65&&o<=90||o>=97&&o<=122?s+=i.charAt(n):o<128?s+=A[o]:o<2048?s+=A[192|o>>6]+A[128|63&o]:o<55296||o>=57344?s+=A[224|o>>12]+A[128|o>>6&63]+A[128|63&o]:(n+=1,o=65536+((1023&o)<<10|1023&i.charCodeAt(n)),s+=A[240|o>>18]+A[128|o>>12&63]+A[128|o>>6&63]+A[128|63&o])}return s},isBuffer:function(e){return!(!e||"object"!=typeof e)&&!!(e.constructor&&e.constructor.isBuffer&&e.constructor.isBuffer(e))},isRegExp:function(e){return"[object RegExp]"===Object.prototype.toString.call(e)},maybeMap:function(e,t){if(k(e)){for(var r=[],i=0;i<e.length;i+=1)r.push(t(e[i]));return r}return t(e)},merge:function e(t,r,i){if(!r)return t;if("object"!=typeof r){if(k(t))t.push(r);else{if(!t||"object"!=typeof t)return[t,r];(i&&(i.plainObjects||i.allowPrototypes)||!P.call(Object.prototype,r))&&(t[r]=!0)}return t}if(!t||"object"!=typeof t)return[t].concat(r);var s=t;return k(t)&&!k(r)&&(s=C(t,i)),k(t)&&k(r)?(r.forEach((function(r,s){if(P.call(t,s)){var n=t[s];n&&"object"==typeof n&&r&&"object"==typeof r?t[s]=e(n,r,i):t.push(r)}else t[s]=r})),t):Object.keys(r).reduce((function(t,s){var n=r[s];return P.call(t,s)?t[s]=e(t[s],n,i):t[s]=n,t}),s)}},E=String.prototype.replace,N=/%20/g,q={RFC1738:"RFC1738",RFC3986:"RFC3986"},D=x.assign({default:q.RFC3986,formatters:{RFC1738:function(e){return E.call(e,N,"+")},RFC3986:function(e){return String(e)}}},q),z=Object.prototype.hasOwnProperty,Q={brackets:function(e){return e+"[]"},comma:"comma",indices:function(e,t){return e+"["+t+"]"},repeat:function(e){return e}},R=Array.isArray,T=Array.prototype.push,F=function(e,t){T.apply(e,R(t)?t:[t])},I=Date.prototype.toISOString,L=D.default,V={addQueryPrefix:!1,allowDots:!1,charset:"utf-8",charsetSentinel:!1,delimiter:"&",encode:!0,encoder:x.encode,encodeValuesOnly:!1,format:L,formatter:D.formatters[L],indices:!1,serializeDate:function(e){return I.call(e)},skipNulls:!1,strictNullHandling:!1},H=function e(t,r,i,s,n,o,a,c,l,u,f,h,d){var p,y=t;if("function"==typeof a?y=a(r,y):y instanceof Date?y=u(y):"comma"===i&&R(y)&&(y=x.maybeMap(y,(function(e){return e instanceof Date?u(e):e})).join(",")),null===y){if(s)return o&&!h?o(r,V.encoder,d,"key"):r;y=""}if("string"==typeof(p=y)||"number"==typeof p||"boolean"==typeof p||"symbol"==typeof p||"bigint"==typeof p||x.isBuffer(y))return o?[f(h?r:o(r,V.encoder,d,"key"))+"="+f(o(y,V.encoder,d,"value"))]:[f(r)+"="+f(String(y))];var m,g=[];if(void 0===y)return g;if(R(a))m=a;else{var _=Object.keys(y);m=c?_.sort(c):_}for(var b=0;b<m.length;++b){var w=m[b],v=y[w];if(!n||null!==v){var j=R(y)?"function"==typeof i?i(r,w):r:r+(l?"."+w:"["+w+"]");F(g,e(v,j,i,s,n,o,a,c,l,u,f,h,d))}}return g},$=Object.prototype.hasOwnProperty,M=Array.isArray,B={allowDots:!1,allowPrototypes:!1,arrayLimit:20,charset:"utf-8",charsetSentinel:!1,comma:!1,decoder:x.decode,delimiter:"&",depth:5,ignoreQueryPrefix:!1,interpretNumericEntities:!1,parameterLimit:1e3,parseArrays:!0,plainObjects:!1,strictNullHandling:!1},U=function(e){return e.replace(/&#(\d+);/g,(function(e,t){return String.fromCharCode(parseInt(t,10))}))},G=function(e,t){return e&&"string"==typeof e&&t.comma&&e.indexOf(",")>-1?e.split(","):e},J=function(e,t,r,i){if(e){var s=r.allowDots?e.replace(/\.([^.[]+)/g,"[$1]"):e,n=/(\[[^[\]]*])/g,o=r.depth>0&&/(\[[^[\]]*])/.exec(s),a=o?s.slice(0,o.index):s,c=[];if(a){if(!r.plainObjects&&$.call(Object.prototype,a)&&!r.allowPrototypes)return;c.push(a)}for(var l=0;r.depth>0&&null!==(o=n.exec(s))&&l<r.depth;){if(l+=1,!r.plainObjects&&$.call(Object.prototype,o[1].slice(1,-1))&&!r.allowPrototypes)return;c.push(o[1])}return o&&c.push("["+s.slice(o.index)+"]"),function(e,t,r,i){for(var s=i?t:G(t,r),n=e.length-1;n>=0;--n){var o,a=e[n];if("[]"===a&&r.parseArrays)o=[].concat(s);else{o=r.plainObjects?Object.create(null):{};var c="["===a.charAt(0)&&"]"===a.charAt(a.length-1)?a.slice(1,-1):a,l=parseInt(c,10);r.parseArrays||""!==c?!isNaN(l)&&a!==c&&String(l)===c&&l>=0&&r.parseArrays&&l<=r.arrayLimit?(o=[])[l]=s:o[c]=s:o={0:s}}s=o}return s}(c,t,r,i)}},K=function(e,t){var r=function(e){if(!e)return B;if(null!==e.decoder&&void 0!==e.decoder&&"function"!=typeof e.decoder)throw new TypeError("Decoder has to be a function.");if(void 0!==e.charset&&"utf-8"!==e.charset&&"iso-8859-1"!==e.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var t=void 0===e.charset?B.charset:e.charset;return{allowDots:void 0===e.allowDots?B.allowDots:!!e.allowDots,allowPrototypes:"boolean"==typeof e.allowPrototypes?e.allowPrototypes:B.allowPrototypes,arrayLimit:"number"==typeof e.arrayLimit?e.arrayLimit:B.arrayLimit,charset:t,charsetSentinel:"boolean"==typeof e.charsetSentinel?e.charsetSentinel:B.charsetSentinel,comma:"boolean"==typeof e.comma?e.comma:B.comma,decoder:"function"==typeof e.decoder?e.decoder:B.decoder,delimiter:"string"==typeof e.delimiter||x.isRegExp(e.delimiter)?e.delimiter:B.delimiter,depth:"number"==typeof e.depth||!1===e.depth?+e.depth:B.depth,ignoreQueryPrefix:!0===e.ignoreQueryPrefix,interpretNumericEntities:"boolean"==typeof e.interpretNumericEntities?e.interpretNumericEntities:B.interpretNumericEntities,parameterLimit:"number"==typeof e.parameterLimit?e.parameterLimit:B.parameterLimit,parseArrays:!1!==e.parseArrays,plainObjects:"boolean"==typeof e.plainObjects?e.plainObjects:B.plainObjects,strictNullHandling:"boolean"==typeof e.strictNullHandling?e.strictNullHandling:B.strictNullHandling}}(t);if(""===e||null==e)return r.plainObjects?Object.create(null):{};for(var i="string"==typeof e?function(e,t){var r,i={},s=t.ignoreQueryPrefix?e.replace(/^\?/,""):e,n=t.parameterLimit===1/0?void 0:t.parameterLimit,o=s.split(t.delimiter,n),a=-1,c=t.charset;if(t.charsetSentinel)for(r=0;r<o.length;++r)0===o[r].indexOf("utf8=")&&("utf8=%E2%9C%93"===o[r]?c="utf-8":"utf8=%26%2310003%3B"===o[r]&&(c="iso-8859-1"),a=r,r=o.length);for(r=0;r<o.length;++r)if(r!==a){var l,u,f=o[r],h=f.indexOf("]="),d=-1===h?f.indexOf("="):h+1;-1===d?(l=t.decoder(f,B.decoder,c,"key"),u=t.strictNullHandling?null:""):(l=t.decoder(f.slice(0,d),B.decoder,c,"key"),u=x.maybeMap(G(f.slice(d+1),t),(function(e){return t.decoder(e,B.decoder,c,"value")}))),u&&t.interpretNumericEntities&&"iso-8859-1"===c&&(u=U(u)),f.indexOf("[]=")>-1&&(u=M(u)?[u]:u),$.call(i,l)?i[l]=x.combine(i[l],u):i[l]=u}return i}(e,r):e,s=r.plainObjects?Object.create(null):{},n=Object.keys(i),o=0;o<n.length;++o){var a=n[o],c=J(a,i[a],r,"string"==typeof e);s=x.merge(s,c,r)}return x.compact(s)},W=function(e,t){var r,i=e,s=function(e){if(!e)return V;if(null!==e.encoder&&void 0!==e.encoder&&"function"!=typeof e.encoder)throw new TypeError("Encoder has to be a function.");var t=e.charset||V.charset;if(void 0!==e.charset&&"utf-8"!==e.charset&&"iso-8859-1"!==e.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var r=D.default;if(void 0!==e.format){if(!z.call(D.formatters,e.format))throw new TypeError("Unknown format option provided.");r=e.format}var i=D.formatters[r],s=V.filter;return("function"==typeof e.filter||R(e.filter))&&(s=e.filter),{addQueryPrefix:"boolean"==typeof e.addQueryPrefix?e.addQueryPrefix:V.addQueryPrefix,allowDots:void 0===e.allowDots?V.allowDots:!!e.allowDots,charset:t,charsetSentinel:"boolean"==typeof e.charsetSentinel?e.charsetSentinel:V.charsetSentinel,delimiter:void 0===e.delimiter?V.delimiter:e.delimiter,encode:"boolean"==typeof e.encode?e.encode:V.encode,encoder:"function"==typeof e.encoder?e.encoder:V.encoder,encodeValuesOnly:"boolean"==typeof e.encodeValuesOnly?e.encodeValuesOnly:V.encodeValuesOnly,filter:s,formatter:i,serializeDate:"function"==typeof e.serializeDate?e.serializeDate:V.serializeDate,skipNulls:"boolean"==typeof e.skipNulls?e.skipNulls:V.skipNulls,sort:"function"==typeof e.sort?e.sort:null,strictNullHandling:"boolean"==typeof e.strictNullHandling?e.strictNullHandling:V.strictNullHandling}}(t);"function"==typeof s.filter?i=(0,s.filter)("",i):R(s.filter)&&(r=s.filter);var n,o=[];if("object"!=typeof i||null===i)return"";n=t&&t.arrayFormat in Q?t.arrayFormat:t&&"indices"in t?t.indices?"indices":"repeat":"indices";var a=Q[n];r||(r=Object.keys(i)),s.sort&&r.sort(s.sort);for(var c=0;c<r.length;++c){var l=r[c];s.skipNulls&&null===i[l]||F(o,H(i[l],l,a,s.strictNullHandling,s.skipNulls,s.encode?s.encoder:null,s.filter,s.sort,s.allowDots,s.serializeDate,s.formatter,s.encodeValuesOnly,s.charset))}var u=o.join(s.delimiter),f=!0===s.addQueryPrefix?"?":"";return s.charsetSentinel&&("iso-8859-1"===s.charset?f+="utf8=%26%2310003%3B&":f+="utf8=%E2%9C%93&"),u.length>0?f+u:""};function X(e,t){return W(e,t)}const Y=new RegExp("^([a-z]+:)//","i");class Z extends Error{constructor(e){super(e.statusText),this.name="ClientResponseError",this.statusCode=e.status,this.response=e}}class ee{constructor({server:e,secret:t,headers:r}={}){if(null==e)throw new g("search server is required");let i="";const s={...r||{}};Y.test(e)&&(i=Y.exec(e)[1]),null!=t&&(this._secret=t.trim(),s.Authorization="Token "+this._secret,i="https:"),this._headers={Accept:"application/json",...s},this._server=e,this._endpoint=`${i}//${e.replace(i+"//","")}`}get server(){return this._server}get secret(){return this._secret}get headers(){return this._headers}get endpoint(){return this._endpoint}async request(e,t={},r){const i=X({random:(new Date).getTime(),...t}),s=this._buildUrl(e,i),n=r?"POST":"GET",o=r?{"Content-Type":"application/json"}:{},a=r?JSON.stringify(r):void 0,c=await fetch(s,{mode:"cors",headers:{...this.headers,...o},method:n,body:a});if(c.ok)return c;throw new Z(c)}async search(e){const t=this._buildSearchQueryObject(e).dump(!0);let r;null!=t.items&&(r={items:t.items},delete t.items);const i=await this.request("/search",t,r);return S(await i.json())}async suggest(e){const t=this._buildSearchQueryObject(e).dump(!0),r=await this.request("/suggest",t);return S(await r.json())}async stats(e,t){return v(t.session_id,"session_id is required"),_(t.hashid),await this.request("/stats/"+e,t)}async topStats(e,t){return _(t.hashid),await this.request("/topstats/"+e,t)}_buildUrl(e,t){const[r,i]=e.split("?");let s;return s=null!=i?`?${i}${t?"&"+t:""}`:t?"?"+t:"",`${this.endpoint}/5${r}${s}`}toString(){return`Client(${this.endpoint}${this._secret?" (+secret)":""})`}_buildSearchQueryObject(e){return e instanceof j?e:new j(e)}}class te{constructor(e){if(!(e instanceof ee))throw new g("expected an instance of Client");this._client=e}get client(){return this._client}async registerSession(e){return this.client.stats("init",e)}async registerClick(e){const t=p(e);return"dfid"in t?(!function(e){if(c(e))return e;throw new g("invalid doofinder id")}(t.dfid),delete t.id,delete t.datatype):(v([t.id,t.datatype],"dfid or id + datatype are required"),delete t.dfid),this.client.stats("click",t)}async registerCheckout(e){return this.client.stats("checkout",e)}async registerImageClick(e){return v(e.img_id,"img_id is required"),this.client.stats("img_click",e)}async registerRedirection(e){return v([e.redirection_id,e.link],"redirection_id and link are required"),this.client.stats("redirect",e)}async addToCart(e){return v([e.item_id,e.amount],"item_id and amount are required"),this.client.stats("add-to-cart",e)}async removeFromCart(e){return v([e.item_id,e.amount],"item_id and amount are required"),this.client.stats("remove-from-cart",e)}async clearCart(e){return this.client.stats("clear-cart",e)}async registerEvent(e,t){return this.client.stats(e,t)}}class re{constructor(){throw new Error("can't create instances of this class")}static get options(){return this._options}static set options(e){const{server:t,headers:r}=e,i={};t&&(i.server=t),r&&(i.headers=Object.freeze({...r})),this._options=Object.freeze(i),this.clear()}static getClient(e){return this._clientsPool.has(e)||this._clientsPool.set(e,new ee({...this._options,server:e})),this._clientsPool.get(e)}static getStatsClient(e){return this._statsClientsPool.has(e)||this._statsClientsPool.set(e,new te(this.getClient(e))),this._statsClientsPool.get(e)}static reset(){this.options={}}static clear(){this._clientsPool.clear(),this._statsClientsPool.clear()}}re._clientsPool=new Map,re._statsClientsPool=new Map,re._options={};return e.Client=ee,e.ClientPool=re,e.ClientResponseError=Z,e.Query=j,e.QueryFilter=y,e.QuerySort=m,e.QueryTypes=h,e.QueryValueError=t,e.StatsClient=te,e.__API_VERSION__=5,e.__VERSION__="6.1.2",e._processSearchResponse=S,e.clone=p,e.decode=function(e,t){return K(e,t)},e.encode=X,e.extend=d,e.isEmptyObject=function(e){return 0===Object.keys(e).length&&e.constructor===Object},e.isIdentical=u,e.isNumber=i,e.isObject=s,e.isPlainObject=n,e.isShallowEqual=f,e.isString=r,e.isValidDoofinderId=c,e.isValidHashId=l,e.merge=function e(...t){let r=t.shift();return null!=r&&(n(r)||Array.isArray(r))||(r={}),t.filter(e=>n(e)||Array.isArray(e)).forEach((function(t){if(null!=t)for(const i in t){if(!t.hasOwnProperty(i))continue;const s=t[i],o=r[i];r!==s&&(n(s)?r[i]=e(n(o)?o:{},s):Array.isArray(s)?r[i]=e(Array.isArray(o)?o:[],s):void 0!==s&&(r[i]=s))}})),r},e}({}); | ||
var doofinder=function(e){"use strict";var t;!function(e){e.POST="POST",e.GET="GET",e.PUT="PUT",e.PATCH="PATCH",e.DELETE="DELETE"}(t||(t={}));class r extends Error{}function i(e){return"[object String]"===Object.prototype.toString.call(e)}function n(e){return"[object Number]"===Object.prototype.toString.call(e)&&!isNaN(e)}function s(e){return"[object Object]"===Object.prototype.toString.call(e)}function o(e){return s(e)&&e.constructor===Object&&!e.nodeType&&!e.setInterval}const a=/^([0-9a-f]{32})@([\w-]+)@([0-9a-f]{32})$/i,c=/^([0-9a-f]{32})(-.*)?/i;function l(e){return i(e)&&a.test(e)}function u(e){return i(e)&&c.test(e)}function f(e,t){return e===t?0!==e||0!==t||1/e==1/t:e!=e&&t!=t}function d(e,t){if(f(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;const r=Object.keys(e),i=Object.keys(t);if(r.length!==i.length)return!1;for(let i=0;i<r.length;i++)if(!Object.prototype.hasOwnProperty.call(t,r[i])||!f(e[r[i]],t[r[i]]))return!1;return!0}class h{constructor(){this._indices=new Set}set(e){const t=Array.isArray(e)?e:[e];if(t.filter(i).length!==t.length)throw new r("Indices must be strings");this._indices=new Set(t)}dump(){return Array.from(this._indices)}clear(){this._indices.clear()}add(e){if("string"!=typeof e)throw new r("Indices must be strings");this._indices.add(e)}has(e){return this._indices.has(e)}remove(e){this._indices.delete(e)}}function p(...e){const t=e.length;let r,i,n,s,a,c,l=e[0]||{},u=1,f=!1;if("boolean"==typeof l&&(f=l,l=e[u]||{},u++),"object"!=typeof l&&"function"!=typeof l&&(l={}),u===t)return l;for(;u<t;u++)if(null!=(r=e[u]))for(i in r)s=r[i],"__proto__"!==i&&l!==s&&(f&&s&&(o(s)||(a=Array.isArray(s)))?(n=l[i],c=a&&!Array.isArray(n)?[]:!a&&o(n)?{}:n,a=!1,l[i]=p(f,c,s)):void 0!==s&&(l[i]=s));return l}function y(e){return Array.isArray(e)?p(!0,[],e):o(e)?p(!0,{},e):e}class g{constructor(){this._filters=new Map}get(e){return this._denormalize(this._filters.get(e))}set(e,t){const i=this._normalize(t);if(i instanceof Set)this._filters.set(e,i);else{if(o(i)&&0===Object.entries(i).length)throw new r("plain object filters can't be empty");this._filters.set(e,y(i))}}has(e){return this._filters.has(e)}contains(e,t){return this.has(e)&&this._filterContainsOrEqualsValue(e,t,!1)}equals(e,t){return this.has(e)&&this._filterContainsOrEqualsValue(e,t,!0)}add(e,t){const r=this._normalize(t),i=this._filters.get(e);i instanceof Set?r instanceof Set?this._filters.set(e,new Set([...i,...r])):i.add(r):this._filters.set(e,r)}remove(e,t){const r=this._filters.get(e);if(r instanceof Set&&null!=t){const i=this._normalize(t);if(i instanceof Set)for(const e of i)r.delete(e);else r.delete(i);0===r.size&&this._filters.delete(e)}else this._filters.delete(e)}clear(){this._filters.clear()}setMany(e,t=!1){t&&this.clear();for(const t in e)this.set(t,e[t])}dump(){const e={};if(this._filters.forEach((t,r)=>{e[r]=this._denormalize(t)}),Object.keys(e).length>0)return e}_normalize(e){return i(e)||n(e)?new Set([e]):Array.isArray(e)?new Set(e):e}_denormalize(e){return e instanceof Set?[...e]:e}_filterContainsOrEqualsValue(e,t,r){const i=this._normalize(t),n=this._filters.get(e);if(n instanceof Set&&i instanceof Set){if(r&&n.size!==i.size)return!1;for(const e of i)if(!n.has(e))return!1;return!0}return d(n,i)}}class m{constructor(){this._sortings=[]}set(e,t){return this.clear(),"string"==typeof e?this.add(e,t):(Array.isArray(e)?e:[e]).forEach(e=>{"string"==typeof e?this.add({[e]:"asc"}):this.add(e)}),this._sortings.length}add(e,t){if("string"==typeof e)return this._addFieldSorting(e,t);if(this._isLikeSorting(e)){const t=Object.keys(e)[0];return"_geo_distance"===t?this._addGeoDistanceSorting(y(e[t])):this._addFieldSorting(t,e[t])}}get(){return y(this._sortings)}clear(){this._sortings.length=0}has(e){const t=Object.keys(e)[0];return!!this._sortings.find(r=>r[t]===e[t])}_isLikeSorting(e){return o(e)&&1===Object.keys(e).length}_addFieldSorting(e,t="asc"){if("asc"===t||"desc"===t)return this._sortings.push({[e]:t});throw new r(`wrong sorting value for field '${e}': ${JSON.stringify(t)}`)}_addGeoDistanceSorting(e){if(o(e)&&2===Object.keys(e).length&&"order"in e){if("string"==typeof e[Object.keys(e).find(e=>"order"!==e)])return this._sortings.push({_geo_distance:e})}throw new r("wrong sorting value for 'geo_distance': "+JSON.stringify(e))}}class _ extends Error{}function b(e){if(null==e)throw new _("hashid parameter is mandatory");if(u(e))return e;throw new _("invalid hashid")}function w(e){if(void 0!==e){const t=parseInt(""+e,10);if(!n(t)||t<=0)throw new _("page must be an integer greater than 0");return t}}function v(e){if(void 0!==e){const t=parseInt(""+e,10);if(!n(t)||t<=0||t>100)throw new _("rpp must be a number between 1 and 100");return t}}function O(e,t){if((Array.isArray(e)?e:[e]).filter(e=>null==e||"string"==typeof e&&0===e.trim().length).length>0)throw new _(t);return e}class j{constructor(e={}){this._defaults={query:"",page:1,rpp:20},this._indices=new h,this._filters=new g,this._excludes=new g,this._sort=new m,this.reset(),this.load(e)}get defaults(){return this._defaults}set defaults(e){this._defaults=y(e),this.reset()}reset(){this._indices.clear(),this._filters.clear(),this._excludes.clear(),this._sort.clear(),this._params={},this.load(y(this.defaults))}load(e={}){Object.keys(e).forEach(t=>{"filter"===t?this._filters.setMany(e.filter):"exclude"===t?this._excludes.setMany(e.exclude):"indices"===t?this._indices.set(e.indices):"sort"===t?this._sort.set(e.sort):this.setParam(t,e[t])})}dump(e=!1){e&&(b(this.hashid),w(this.page),v(this.rpp));const t={...y(this._params),indices:this.indices.dump(),filter:this.filters.dump(),exclude:this.excludes.dump(),sort:this.sort.get()};return["filter","exclude"].forEach(e=>{t[e]||delete t[e]}),["indices","sort"].forEach(e=>{0===t[e].length&&delete t[e]}),t}copy(){const e=new j;return e.defaults=y(this.defaults),e.reset(),e.load(this.dump()),e}getParam(e){return this._params[e]}setParam(e,t){void 0!==t?"hashid"===e?this._params.hashid=b(t):"page"===e?this._params.page=w(t):"rpp"===e?this._params.rpp=v(t):this._params[e]=t:delete this._params[e]}get hashid(){return this.getParam("hashid")}set hashid(e){this.setParam("hashid",e)}get text(){return this.getParam("query")}set text(e){this.setParam("query",e)}get page(){return this.getParam("page")}set page(e){this.setParam("page",e)}get rpp(){return this.getParam("rpp")}set rpp(e){this.setParam("rpp",e)}get queryName(){return this.getParam("query_name")}set queryName(e){this.setParam("query_name",e)}get indices(){return this._indices}get filters(){return this._filters}get excludes(){return this._excludes}get sort(){return this._sort}}function S(e){const t=e.selected.buckets.map(e=>e.key),r=e.terms.buckets.map(e=>(t.includes(e.key)&&(e.selected=!0),e));return r.filter(e=>null==e.doc_count).length!==r.length&&r.sort((e,t)=>{const r=e.doc_count||0,i=t.doc_count||0;return r<i?1:r>i?-1:e.key.localeCompare(t.key)}),{type:"terms",label:e.label,visible:e.visible,order:e.order,terms:r}}function P(e){const t=e;return null!=e.facets&&(t._rawFacets=y(e.facets),t.facets=function(e){const t={};for(const i in e)"terms"in e[i]?t[i]=S(e[i]):"range"in e[i]&&(t[i]={type:"range",label:(r=e[i]).label,visible:r.visible,range:r.range.buckets[0].stats});var r;return t}(e.facets)),t}var E=Object.prototype.hasOwnProperty,x=Array.isArray,A=function(){for(var e=[],t=0;t<256;++t)e.push("%"+((t<16?"0":"")+t.toString(16)).toUpperCase());return e}(),k=function(e,t){for(var r=t&&t.plainObjects?Object.create(null):{},i=0;i<e.length;++i)void 0!==e[i]&&(r[i]=e[i]);return r},C={arrayToObject:k,assign:function(e,t){return Object.keys(t).reduce((function(e,r){return e[r]=t[r],e}),e)},combine:function(e,t){return[].concat(e,t)},compact:function(e){for(var t=[{obj:{o:e},prop:"o"}],r=[],i=0;i<t.length;++i)for(var n=t[i],s=n.obj[n.prop],o=Object.keys(s),a=0;a<o.length;++a){var c=o[a],l=s[c];"object"==typeof l&&null!==l&&-1===r.indexOf(l)&&(t.push({obj:s,prop:c}),r.push(l))}return function(e){for(;e.length>1;){var t=e.pop(),r=t.obj[t.prop];if(x(r)){for(var i=[],n=0;n<r.length;++n)void 0!==r[n]&&i.push(r[n]);t.obj[t.prop]=i}}}(t),e},decode:function(e,t,r){var i=e.replace(/\+/g," ");if("iso-8859-1"===r)return i.replace(/%[0-9a-f]{2}/gi,unescape);try{return decodeURIComponent(i)}catch(e){return i}},encode:function(e,t,r){if(0===e.length)return e;var i=e;if("symbol"==typeof e?i=Symbol.prototype.toString.call(e):"string"!=typeof e&&(i=String(e)),"iso-8859-1"===r)return escape(i).replace(/%u[0-9a-f]{4}/gi,(function(e){return"%26%23"+parseInt(e.slice(2),16)+"%3B"}));for(var n="",s=0;s<i.length;++s){var o=i.charCodeAt(s);45===o||46===o||95===o||126===o||o>=48&&o<=57||o>=65&&o<=90||o>=97&&o<=122?n+=i.charAt(s):o<128?n+=A[o]:o<2048?n+=A[192|o>>6]+A[128|63&o]:o<55296||o>=57344?n+=A[224|o>>12]+A[128|o>>6&63]+A[128|63&o]:(s+=1,o=65536+((1023&o)<<10|1023&i.charCodeAt(s)),n+=A[240|o>>18]+A[128|o>>12&63]+A[128|o>>6&63]+A[128|63&o])}return n},isBuffer:function(e){return!(!e||"object"!=typeof e)&&!!(e.constructor&&e.constructor.isBuffer&&e.constructor.isBuffer(e))},isRegExp:function(e){return"[object RegExp]"===Object.prototype.toString.call(e)},maybeMap:function(e,t){if(x(e)){for(var r=[],i=0;i<e.length;i+=1)r.push(t(e[i]));return r}return t(e)},merge:function e(t,r,i){if(!r)return t;if("object"!=typeof r){if(x(t))t.push(r);else{if(!t||"object"!=typeof t)return[t,r];(i&&(i.plainObjects||i.allowPrototypes)||!E.call(Object.prototype,r))&&(t[r]=!0)}return t}if(!t||"object"!=typeof t)return[t].concat(r);var n=t;return x(t)&&!x(r)&&(n=k(t,i)),x(t)&&x(r)?(r.forEach((function(r,n){if(E.call(t,n)){var s=t[n];s&&"object"==typeof s&&r&&"object"==typeof r?t[n]=e(s,r,i):t.push(r)}else t[n]=r})),t):Object.keys(r).reduce((function(t,n){var s=r[n];return E.call(t,n)?t[n]=e(t[n],s,i):t[n]=s,t}),n)}},N=String.prototype.replace,T=/%20/g,D={RFC1738:"RFC1738",RFC3986:"RFC3986"},q=C.assign({default:D.RFC3986,formatters:{RFC1738:function(e){return N.call(e,T,"+")},RFC3986:function(e){return String(e)}}},D),z=Object.prototype.hasOwnProperty,I={brackets:function(e){return e+"[]"},comma:"comma",indices:function(e,t){return e+"["+t+"]"},repeat:function(e){return e}},Q=Array.isArray,L=Array.prototype.push,R=function(e,t){L.apply(e,Q(t)?t:[t])},H=Date.prototype.toISOString,F=q.default,V={addQueryPrefix:!1,allowDots:!1,charset:"utf-8",charsetSentinel:!1,delimiter:"&",encode:!0,encoder:C.encode,encodeValuesOnly:!1,format:F,formatter:q.formatters[F],indices:!1,serializeDate:function(e){return H.call(e)},skipNulls:!1,strictNullHandling:!1},$=function e(t,r,i,n,s,o,a,c,l,u,f,d,h){var p,y=t;if("function"==typeof a?y=a(r,y):y instanceof Date?y=u(y):"comma"===i&&Q(y)&&(y=C.maybeMap(y,(function(e){return e instanceof Date?u(e):e})).join(",")),null===y){if(n)return o&&!d?o(r,V.encoder,h,"key"):r;y=""}if("string"==typeof(p=y)||"number"==typeof p||"boolean"==typeof p||"symbol"==typeof p||"bigint"==typeof p||C.isBuffer(y))return o?[f(d?r:o(r,V.encoder,h,"key"))+"="+f(o(y,V.encoder,h,"value"))]:[f(r)+"="+f(String(y))];var g,m=[];if(void 0===y)return m;if(Q(a))g=a;else{var _=Object.keys(y);g=c?_.sort(c):_}for(var b=0;b<g.length;++b){var w=g[b],v=y[w];if(!s||null!==v){var O=Q(y)?"function"==typeof i?i(r,w):r:r+(l?"."+w:"["+w+"]");R(m,e(v,O,i,n,s,o,a,c,l,u,f,d,h))}}return m},M=Object.prototype.hasOwnProperty,U=Array.isArray,B={allowDots:!1,allowPrototypes:!1,arrayLimit:20,charset:"utf-8",charsetSentinel:!1,comma:!1,decoder:C.decode,delimiter:"&",depth:5,ignoreQueryPrefix:!1,interpretNumericEntities:!1,parameterLimit:1e3,parseArrays:!0,plainObjects:!1,strictNullHandling:!1},G=function(e){return e.replace(/&#(\d+);/g,(function(e,t){return String.fromCharCode(parseInt(t,10))}))},J=function(e,t){return e&&"string"==typeof e&&t.comma&&e.indexOf(",")>-1?e.split(","):e},K=function(e,t,r,i){if(e){var n=r.allowDots?e.replace(/\.([^.[]+)/g,"[$1]"):e,s=/(\[[^[\]]*])/g,o=r.depth>0&&/(\[[^[\]]*])/.exec(n),a=o?n.slice(0,o.index):n,c=[];if(a){if(!r.plainObjects&&M.call(Object.prototype,a)&&!r.allowPrototypes)return;c.push(a)}for(var l=0;r.depth>0&&null!==(o=s.exec(n))&&l<r.depth;){if(l+=1,!r.plainObjects&&M.call(Object.prototype,o[1].slice(1,-1))&&!r.allowPrototypes)return;c.push(o[1])}return o&&c.push("["+n.slice(o.index)+"]"),function(e,t,r,i){for(var n=i?t:J(t,r),s=e.length-1;s>=0;--s){var o,a=e[s];if("[]"===a&&r.parseArrays)o=[].concat(n);else{o=r.plainObjects?Object.create(null):{};var c="["===a.charAt(0)&&"]"===a.charAt(a.length-1)?a.slice(1,-1):a,l=parseInt(c,10);r.parseArrays||""!==c?!isNaN(l)&&a!==c&&String(l)===c&&l>=0&&r.parseArrays&&l<=r.arrayLimit?(o=[])[l]=n:o[c]=n:o={0:n}}n=o}return n}(c,t,r,i)}},W=function(e,t){var r=function(e){if(!e)return B;if(null!==e.decoder&&void 0!==e.decoder&&"function"!=typeof e.decoder)throw new TypeError("Decoder has to be a function.");if(void 0!==e.charset&&"utf-8"!==e.charset&&"iso-8859-1"!==e.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var t=void 0===e.charset?B.charset:e.charset;return{allowDots:void 0===e.allowDots?B.allowDots:!!e.allowDots,allowPrototypes:"boolean"==typeof e.allowPrototypes?e.allowPrototypes:B.allowPrototypes,arrayLimit:"number"==typeof e.arrayLimit?e.arrayLimit:B.arrayLimit,charset:t,charsetSentinel:"boolean"==typeof e.charsetSentinel?e.charsetSentinel:B.charsetSentinel,comma:"boolean"==typeof e.comma?e.comma:B.comma,decoder:"function"==typeof e.decoder?e.decoder:B.decoder,delimiter:"string"==typeof e.delimiter||C.isRegExp(e.delimiter)?e.delimiter:B.delimiter,depth:"number"==typeof e.depth||!1===e.depth?+e.depth:B.depth,ignoreQueryPrefix:!0===e.ignoreQueryPrefix,interpretNumericEntities:"boolean"==typeof e.interpretNumericEntities?e.interpretNumericEntities:B.interpretNumericEntities,parameterLimit:"number"==typeof e.parameterLimit?e.parameterLimit:B.parameterLimit,parseArrays:!1!==e.parseArrays,plainObjects:"boolean"==typeof e.plainObjects?e.plainObjects:B.plainObjects,strictNullHandling:"boolean"==typeof e.strictNullHandling?e.strictNullHandling:B.strictNullHandling}}(t);if(""===e||null==e)return r.plainObjects?Object.create(null):{};for(var i="string"==typeof e?function(e,t){var r,i={},n=t.ignoreQueryPrefix?e.replace(/^\?/,""):e,s=t.parameterLimit===1/0?void 0:t.parameterLimit,o=n.split(t.delimiter,s),a=-1,c=t.charset;if(t.charsetSentinel)for(r=0;r<o.length;++r)0===o[r].indexOf("utf8=")&&("utf8=%E2%9C%93"===o[r]?c="utf-8":"utf8=%26%2310003%3B"===o[r]&&(c="iso-8859-1"),a=r,r=o.length);for(r=0;r<o.length;++r)if(r!==a){var l,u,f=o[r],d=f.indexOf("]="),h=-1===d?f.indexOf("="):d+1;-1===h?(l=t.decoder(f,B.decoder,c,"key"),u=t.strictNullHandling?null:""):(l=t.decoder(f.slice(0,h),B.decoder,c,"key"),u=C.maybeMap(J(f.slice(h+1),t),(function(e){return t.decoder(e,B.decoder,c,"value")}))),u&&t.interpretNumericEntities&&"iso-8859-1"===c&&(u=G(u)),f.indexOf("[]=")>-1&&(u=U(u)?[u]:u),M.call(i,l)?i[l]=C.combine(i[l],u):i[l]=u}return i}(e,r):e,n=r.plainObjects?Object.create(null):{},s=Object.keys(i),o=0;o<s.length;++o){var a=s[o],c=K(a,i[a],r,"string"==typeof e);n=C.merge(n,c,r)}return C.compact(n)},X=function(e,t){var r,i=e,n=function(e){if(!e)return V;if(null!==e.encoder&&void 0!==e.encoder&&"function"!=typeof e.encoder)throw new TypeError("Encoder has to be a function.");var t=e.charset||V.charset;if(void 0!==e.charset&&"utf-8"!==e.charset&&"iso-8859-1"!==e.charset)throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");var r=q.default;if(void 0!==e.format){if(!z.call(q.formatters,e.format))throw new TypeError("Unknown format option provided.");r=e.format}var i=q.formatters[r],n=V.filter;return("function"==typeof e.filter||Q(e.filter))&&(n=e.filter),{addQueryPrefix:"boolean"==typeof e.addQueryPrefix?e.addQueryPrefix:V.addQueryPrefix,allowDots:void 0===e.allowDots?V.allowDots:!!e.allowDots,charset:t,charsetSentinel:"boolean"==typeof e.charsetSentinel?e.charsetSentinel:V.charsetSentinel,delimiter:void 0===e.delimiter?V.delimiter:e.delimiter,encode:"boolean"==typeof e.encode?e.encode:V.encode,encoder:"function"==typeof e.encoder?e.encoder:V.encoder,encodeValuesOnly:"boolean"==typeof e.encodeValuesOnly?e.encodeValuesOnly:V.encodeValuesOnly,filter:n,formatter:i,serializeDate:"function"==typeof e.serializeDate?e.serializeDate:V.serializeDate,skipNulls:"boolean"==typeof e.skipNulls?e.skipNulls:V.skipNulls,sort:"function"==typeof e.sort?e.sort:null,strictNullHandling:"boolean"==typeof e.strictNullHandling?e.strictNullHandling:V.strictNullHandling}}(t);"function"==typeof n.filter?i=(0,n.filter)("",i):Q(n.filter)&&(r=n.filter);var s,o=[];if("object"!=typeof i||null===i)return"";s=t&&t.arrayFormat in I?t.arrayFormat:t&&"indices"in t?t.indices?"indices":"repeat":"indices";var a=I[s];r||(r=Object.keys(i)),n.sort&&r.sort(n.sort);for(var c=0;c<r.length;++c){var l=r[c];n.skipNulls&&null===i[l]||R(o,$(i[l],l,a,n.strictNullHandling,n.skipNulls,n.encode?n.encoder:null,n.filter,n.sort,n.allowDots,n.serializeDate,n.formatter,n.encodeValuesOnly,n.charset))}var u=o.join(n.delimiter),f=!0===n.addQueryPrefix?"?":"";return n.charsetSentinel&&("iso-8859-1"===n.charset?f+="utf8=%26%2310003%3B&":f+="utf8=%E2%9C%93&"),u.length>0?f+u:""};function Y(e,t){return X(e,t)}const Z=new RegExp("^([a-z]+:)//","i");class ee extends Error{constructor(e){super(e.statusText),this.name="ClientResponseError",this.statusCode=e.status,this.response=e}}class te{constructor({server:e,secret:t,headers:r}={}){if(null==e)throw new _("search server is required");let i="";const n={...r||{}};Z.test(e)&&(i=Z.exec(e)[1]),null!=t&&(this._secret=t.trim(),n.Authorization="Token "+this._secret,i="https:"),this._headers={Accept:"application/json",...n},this._server=e,this._endpoint=`${i}//${e.replace(i+"//","")}`}get server(){return this._server}get secret(){return this._secret}get headers(){return this._headers}get endpoint(){return this._endpoint}async request(e,r={},i,n=t.GET){const s=Y({random:(new Date).getTime(),...r}),o=this._buildUrl(e,s,r.hashid),a=i?{"Content-Type":"application/json"}:{},c=i?JSON.stringify(i):void 0,l=await fetch(o,{mode:"cors",headers:{...this.headers,...a},method:n,body:c});if(l.ok)return l;throw new ee(l)}async search(e){const t=this._buildSearchQueryObject(e).dump(!0);let r;null!=t.items&&(r={items:t.items},delete t.items);const i=await this.request("/_search",t,r);return P(await i.json())}async searchImage(e,r){const i=this._buildSearchQueryObject(e).dump(!0),n={image:r},s=await this.request("/_image_search",i,n,t.POST);return P(await s.json())}async suggest(e){const t=this._buildSearchQueryObject(e).dump(!0),r=await this.request("/_suggest",t);return P(await r.json())}async stats(e,r,i=t.PUT){return O(r.session_id,"session_id is required"),b(r.hashid),await this.request("/stats/"+e,r,null,i)}_buildUrl(e,t,r){const[i,n]=e.split("?");let s;return s=null!=n?`?${n}${t?"&"+t:""}`:t?"?"+t:"",`${this.endpoint}/6${r?"/"+r:""}${i}${s}`}toString(){return`Client(${this.endpoint}${this._secret?" (+secret)":""})`}_buildSearchQueryObject(e){return e instanceof j?e:new j(e)}}class re{constructor(e){if(!(e instanceof te))throw new _("expected an instance of Client");this._client=e}get client(){return this._client}async registerSession(e){return this.client.stats("init",e)}async registerClick(e){return function(e){if(l(e))return e;throw new _("invalid doofinder id")}(e.dfid),this.client.stats("click",e)}async registerCheckout(e){return this.client.stats("checkout",e)}async registerImageClick(e){return O(e.id,"id is required"),this.client.stats("image",e)}async registerRedirection(e){return O(e.id,"id is required"),this.client.stats("redirect",e)}async addToCart(e){const{id:t,amount:r,index:i,price:n,title:s}=e;return O([t,r,i,n,s],"id, amount, index, price and title are required"),this.client.stats("cart/"+e.session_id,e)}async removeFromCart(e){const{id:r,amount:i,index:n}=e;return O([r,i,n],"id, amount and index are required"),this.client.stats("cart/"+e.session_id,e,t.PATCH)}async clearCart(e){return this.client.stats("cart/"+e.session_id,e,t.DELETE)}async registerEvent(e,r,i=t.GET){return this.client.stats(e,r,i)}}class ie{constructor(){throw new Error("can't create instances of this class")}static get options(){return this._options}static set options(e){const{server:t,headers:r}=e,i={};t&&(i.server=t),r&&(i.headers=Object.freeze({...r})),this._options=Object.freeze(i),this.clear()}static getClient(e){return this._clientsPool.has(e)||this._clientsPool.set(e,new te({...this._options,server:e})),this._clientsPool.get(e)}static getStatsClient(e){return this._statsClientsPool.has(e)||this._statsClientsPool.set(e,new re(this.getClient(e))),this._statsClientsPool.get(e)}static reset(){this.options={}}static clear(){this._clientsPool.clear(),this._statsClientsPool.clear()}}ie._clientsPool=new Map,ie._statsClientsPool=new Map,ie._options={};return e.Client=te,e.ClientPool=ie,e.ClientResponseError=ee,e.Query=j,e.QueryFilter=g,e.QueryIndices=h,e.QuerySort=m,e.QueryValueError=r,e.StatsClient=re,e.__API_VERSION__=6,e.__VERSION__="7.0.2",e._processSearchResponse=P,e.clone=y,e.decode=function(e,t){return W(e,t)},e.encode=Y,e.extend=p,e.isEmptyObject=function(e){return 0===Object.keys(e).length&&e.constructor===Object},e.isIdentical=f,e.isNumber=n,e.isObject=s,e.isPlainObject=o,e.isShallowEqual=d,e.isString=i,e.isValidDoofinderId=l,e.isValidHashId=u,e.merge=function e(...t){let r=t.shift();return null!=r&&(o(r)||Array.isArray(r))||(r={}),t.filter(e=>o(e)||Array.isArray(e)).forEach((function(t){if(null!=t)for(const i in t){if(!t.hasOwnProperty(i))continue;const n=t[i],s=r[i];r!==n&&(o(n)?r[i]=e(o(s)?s:{},n):Array.isArray(n)?r[i]=e(Array.isArray(s)?s:[],n):void 0!==n&&(r[i]=n))}})),r},e}({}); |
{ | ||
"name": "doofinder", | ||
"version": "6.1.2", | ||
"version": "7.0.2", | ||
"description": "Javascript Library for Doofinder Search API", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
252201
9
6889