@elastic/search-application-client
Version:
A Search Application Client for Elasticsearch
3 lines • 6.34 kB
JavaScript
var SearchApplicationClientDefault=(()=>{var l=Object.defineProperty,k=Object.defineProperties,B=Object.getOwnPropertyDescriptor,T=Object.getOwnPropertyDescriptors,j=Object.getOwnPropertyNames,d=Object.getOwnPropertySymbols;var v=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var x=(s,e,t)=>e in s?l(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,h=(s,e)=>{for(var t in e||(e={}))v.call(e,t)&&x(s,t,e[t]);if(d)for(var t of d(e))S.call(e,t)&&x(s,t,e[t]);return s},f=(s,e)=>k(s,T(e));var A=(s,e)=>{var t={};for(var r in s)v.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&&S.call(s,r)&&(t[r]=s[r]);return t};var Q=(s,e)=>{for(var t in e)l(s,t,{get:e[t],enumerable:!0})},z=(s,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of j(e))!v.call(s,a)&&a!==t&&l(s,a,{get:()=>e[a],enumerable:!(r=B(e,a))||r.enumerable});return s};var $=s=>z(l({},"__esModule",{value:!0}),s);var y=(s,e,t)=>new Promise((r,a)=>{var n=o=>{try{c(t.next(o))}catch(p){a(p)}},i=o=>{try{c(t.throw(o))}catch(p){a(p)}},c=o=>o.done?r(o.value):Promise.resolve(o.value).then(n,i);c((t=t.apply(s,e)).next())});var D={};Q(D,{Highlight:()=>H,default:()=>b});var F=class{constructor(e=36e5){this.ttl=e;this.cache={}}createKeyByRequestParams(e,t,r){return e+t+JSON.stringify(r)}getByRequestParams(e,t,r){let a=this.createKeyByRequestParams(e,t,r);return this.get(a)}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,a){let n=this.createKeyByRequestParams(e,t,r);this.set(n,a)}};var R=class{constructor(e,t,r,a={cache:!0,headers:{}}){this.apiKey=e;this.endpoint=t;this.path=r;this.cacheService=new F(a.cacheExpiration),this.options=h({cache:!0},a)}request(e,t,r){let a=f(h({},r),{apiKey:this.apiKey,endpoint:this.endpoint}),n=this.options.cache&&this.cacheService.getByRequestParams(e,t,a);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(i=>i.json(),i=>{throw i.isNetworkError=!0,i}).then(i=>{var c,o,p;if(i.error){let g=((c=i==null?void 0:i.error)==null?void 0:c.reason)||((p=(o=i==null?void 0:i.error)==null?void 0:o.caused_by)==null?void 0:p.reason),u=new Error(g);throw u.isFetchError=!0,u.data=i,u}return this.options.cache&&this.cacheService.setByRequestParams(e,t,a,i),i}).catch(this.handleError)}post(e){return y(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 N=s=>({terms:{field:s.field,size:s.size}}),I=s=>({stats:{field:s.field}}),P=class{constructor(e,t,r,a,n){this.facetsConfiguration=e;this.facetFilters=t;this.filter=r;this.sort=a;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,a)=>{let n=a===0,i=r.reduce((o,p)=>{let g=this.facetsConfiguration[p],u=g.type==="terms"?N(g):I(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:i,_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 K=(s,e)=>{var n;let t=s.reduce((i,c)=>h(h({},i),c.aggregations),{}),r=Object.keys(t).reduce((i,c)=>{let o=c.replace("_facet",""),p=e[o];if(!p)return i;let g=t[c];if(p.type==="terms"){let{buckets:u}=g;return[...i,{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:O,sum:_,count:q}=g;return[...i,{name:o,stats:{min:u,max:m,avg:O,sum:_,count:q}}]}},[]),a=Object.keys(((n=s[0])==null?void 0:n.aggregations)||{}).reduce((i,c)=>e[c.replace("_facet","")]?i:f(h({},i),{[c]:s[0].aggregations[c]}),{});return f(h({},s[0]),{aggregations:a,facets:r})},E=class{constructor(e,t={}){this.apiClient=e;this.facets={};this.facetFilters={};this.params={};let n=t,{facets:r}=n,a=A(n,["facets"]);this.facets=r||{},this.params=a}addFacetFilter(e,t){var a;if(!((a=this.facets)==null?void 0:a[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 y(this,null,function*(){let e=new P(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 K(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 C=class{constructor({applicationName:e,endpoint:t,apiKey:r,baseParams:a,apiOptions:n}){this.baseParams=a||{},this.apiClient=new R(r,t,`/_application/search_application/${e}/_search`,n)}initQuery(){return new E(this.apiClient,this.baseParams)}};var H=(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 w=s=>{throw new Error(`${s} is required`)};function b(s,e,t,r,a){s||w("applicationName"),e||w("endpoint"),t||w("apiKey");let n=new C({applicationName:s,endpoint:e,apiKey:t,baseParams:r,apiOptions:a});return()=>n.initQuery()}return $(D);})();
var SearchApplicationClient = SearchApplicationClientDefault.default
//# sourceMappingURL=index.global.js.map