UNPKG

@elastic/search-application-client

Version:
10 lines (7 loc) 6 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _=Object.defineProperty,q=Object.defineProperties;var k=Object.getOwnPropertyDescriptors;var d=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable;var v=(s,e,t)=>e in s?_(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,h=(s,e)=>{for(var t in e||(e={}))w.call(e,t)&&v(s,t,e[t]);if(d)for(var t of d(e))x.call(e,t)&&v(s,t,e[t]);return s},f=(s,e)=>q(s,k(e));var S=(s,e)=>{var t={};for(var r in s)w.call(s,r)&&e.indexOf(r)<0&&(t[r]=s[r]);if(s!=null&&d)for(var r of d(s))e.indexOf(r)<0&&x.call(s,r)&&(t[r]=s[r]);return t};var l=(s,e,t)=>new Promise((r,i)=>{var n=o=>{try{c(t.next(o));}catch(p){i(p);}},a=o=>{try{c(t.throw(o));}catch(p){i(p);}},c=o=>o.done?r(o.value):Promise.resolve(o.value).then(n,a);c((t=t.apply(s,e)).next());});var y=class{constructor(e=36e5){this.ttl=e;this.cache={};}createKeyByRequestParams(e,t,r){return e+t+JSON.stringify(r)}getByRequestParams(e,t,r){let i=this.createKeyByRequestParams(e,t,r);return this.get(i)}get(e){let t=this.cache[e];return t?t.expiration<Date.now()?(delete this.cache[e],null):t.value:null}set(e,t){this.cache[e]={value:t,expiration:Date.now()+this.ttl};}setByRequestParams(e,t,r,i){let n=this.createKeyByRequestParams(e,t,r);this.set(n,i);}};var F=class{constructor(e,t,r,i={cache:!0,headers:{}}){this.apiKey=e;this.endpoint=t;this.path=r;this.cacheService=new y(i.cacheExpiration),this.options=h({cache:!0},i);}request(e,t,r){let i=f(h({},r),{apiKey:this.apiKey,endpoint:this.endpoint}),n=this.options.cache&&this.cacheService.getByRequestParams(e,t,i);return n?Promise.resolve(n):fetch(t,{method:e,headers:f(h({},this.options.headers),{"Content-Type":"application/json",Authorization:`Apikey ${this.apiKey}`}),body:r?JSON.stringify(r):void 0}).then(a=>a.json(),a=>{throw a.isNetworkError=!0,a}).then(a=>{var c,o,p;if(a.error){let g=((c=a==null?void 0:a.error)==null?void 0:c.reason)||((p=(o=a==null?void 0:a.error)==null?void 0:o.caused_by)==null?void 0:p.reason),u=new Error(g);throw u.isFetchError=!0,u.data=a,u}return this.options.cache&&this.cacheService.setByRequestParams(e,t,i,a),a}).catch(this.handleError)}post(e){return l(this,null,function*(){return yield this.request("POST",`${this.endpoint}${this.path}`,e)})}handleError(e){var t,r;e.isNetworkError?e.name="[Network Error]":e.isFetchError&&e.data.status===500&&((r=(t=e.data.error)==null?void 0:t.caused_by)!=null&&r.type.includes("format_exception")||e.data.error.type==="json_parse_exception")?e.name="[Parameter or type is invalid]":e.isFetchError&&e.data.status===401?e.name="[Authorization Error]":e.isFetchError&&e.data.status===404&&(e.name="[Not Found Error]",e.message=`${e.data.error.type}: ${e.message}`),console.error(e);}};var B=s=>({terms:{field:s.field,size:s.size}}),T=s=>({stats:{field:s.field}}),R=class{constructor(e,t,r,i,n){this.facetsConfiguration=e;this.facetFilters=t;this.filter=r;this.sort=i;this.params=n;}build(){return Object.keys(this.facetsConfiguration).reduce((t,r)=>{var n;return this.facetsConfiguration[r].disjunctive&&((n=this.facetFilters[r])==null?void 0:n.length)>0?(t.push([r]),t):(t[0].push(r),t)},[[]]).reduce((t,r,i)=>{let n=i===0,a=r.reduce((o,p)=>{let g=this.facetsConfiguration[p],u=g.type==="terms"?B(g):T(g);return f(h({},o),{[`${p}_facet`]:u})},{}),c=Object.keys(this.facetsConfiguration).reduce((o,p)=>{let g=this.facetsConfiguration[p],u=this.facetFilters[p]||[];return u.length===0||g.disjunctive&&!n?o:[...o,{[g.type==="terms"?"terms":"range"]:{[g.field]:u}}]},[]);return t.push(h(h(h({_es_aggs:a,_es_filters:{bool:{must:[...this.filter?[this.filter]:[],...c]}}},this.sort?{_es_sort_fields:this.sort}:{}),this.params),n?{}:{size:0,from:0})),t},[])}};var j=(s,e)=>{var n;let t=s.reduce((a,c)=>h(h({},a),c.aggregations),{}),r=Object.keys(t).reduce((a,c)=>{let o=c.replace("_facet",""),p=e[o];if(!p)return a;let g=t[c];if(p.type==="terms"){let{buckets:u}=g;return [...a,{name:o,entries:(Array.isArray(u)?u:Object.values(u)).map(m=>({value:m.key,count:m.doc_count}))}]}else if(p.type==="stats"){let{min:u,max:m,avg:A,sum:b,count:O}=g;return [...a,{name:o,stats:{min:u,max:m,avg:A,sum:b,count:O}}]}},[]),i=Object.keys(((n=s[0])==null?void 0:n.aggregations)||{}).reduce((a,c)=>e[c.replace("_facet","")]?a:f(h({},a),{[c]:s[0].aggregations[c]}),{});return f(h({},s[0]),{aggregations:i,facets:r})},P=class{constructor(e,t={}){this.apiClient=e;this.facets={};this.facetFilters={};this.params={};let n=t,{facets:r}=n,i=S(n,["facets"]);this.facets=r||{},this.params=i;}addFacetFilter(e,t){var i;if(!((i=this.facets)==null?void 0:i[e]))throw new Error(`Facet ${e} wasn't passed in configuration params`);return this.facetFilters[e]=[...this.facetFilters[e]||[],Array.isArray(t)?t:[t]].flat(),this}addParameter(e,t){return this.params[e]=t,this}query(e){return this.addParameter("query",e)}search(){return l(this,null,function*(){let e=new R(this.facets,this.facetFilters,this.filter,this.sort,this.params).build(),t=(yield Promise.all(e.map(r=>this.apiClient.post({params:r})))).filter(r=>!!r);return j(t,this.facets)})}setFilter(e){return this.filter=e,this}setFrom(e){return this.addParameter("from",e)}setPageSize(e){return this.addParameter("size",e)}setSort(e){return this.sort=e,this}};var E=class{constructor({applicationName:e,endpoint:t,apiKey:r,baseParams:i,apiOptions:n}){this.baseParams=i||{},this.apiClient=new F(r,t,`/_application/search_application/${e}/_search`,n);}initQuery(){return new P(this.apiClient,this.baseParams)}};var Y=(s,e)=>{var r;let t=(r=s.highlight)==null?void 0:r[e];return t!=null&&t.length?t.join("..."):s._source[e]};var C=s=>{throw new Error(`${s} is required`)};function Q(s,e,t,r,i){s||C("applicationName"),e||C("endpoint"),t||C("apiKey");let n=new E({applicationName:s,endpoint:e,apiKey:t,baseParams:r,apiOptions:i});return ()=>n.initQuery()} exports.Highlight = Y; exports.default = Q; //# sourceMappingURL=out.js.map //# sourceMappingURL=index.cjs.map