UNPKG

meilisearch

Version:

The Meilisearch JS client for Node.js and the browser.

3 lines (2 loc) 26.1 kB
import t from"crypto";const e={ALL:"all",LAST:"last"},i={JSON:"application/json",CSV:"text/csv",NDJSON:"application/x-ndjson"};function s(t,e,i,s){return new(i||(i=Promise))((function(n,r){function o(t){try{d(s.next(t))}catch(t){r(t)}}function u(t){try{d(s.throw(t))}catch(t){r(t)}}function d(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,u)}d((s=s.apply(t,e||[])).next())}))}class n extends Error{constructor(t,e,i,s){var r,o,u;super(t),Object.setPrototypeOf(this,n.prototype),this.name="MeiliSearchCommunicationError",e instanceof Response&&(this.message=e.statusText,this.statusCode=e.status),e instanceof Error&&(this.errno=e.errno,this.code=e.code),s?(this.stack=s,this.stack=null===(r=this.stack)||void 0===r?void 0:r.replace(/(TypeError|FetchError)/,this.name),this.stack=null===(o=this.stack)||void 0===o?void 0:o.replace("Failed to fetch",`request to ${i} failed, reason: connect ECONNREFUSED`),this.stack=null===(u=this.stack)||void 0===u?void 0:u.replace("Not Found",`Not Found: ${i}`)):Error.captureStackTrace&&Error.captureStackTrace(this,n)}}const r=class extends Error{constructor(t,e){super(t.message),Object.setPrototypeOf(this,r.prototype),this.name="MeiliSearchApiError",this.code=t.code,this.type=t.type,this.link=t.link,this.message=t.message,this.httpStatus=e,Error.captureStackTrace&&Error.captureStackTrace(this,r)}};function o(t){return s(this,void 0,void 0,(function*(){if(!t.ok){let e;try{e=yield t.json()}catch(e){throw new n(t.statusText,t,t.url)}throw new r(e,t.status)}return t}))}function u(t,e,i){if("MeiliSearchApiError"!==t.name)throw new n(t.message,t,i,e);throw t}class d extends Error{constructor(t){super(t),Object.setPrototypeOf(this,d.prototype),this.name="MeiliSearchError",Error.captureStackTrace&&Error.captureStackTrace(this,d)}}class a extends Error{constructor(t){super(t),Object.setPrototypeOf(this,a.prototype),this.name="MeiliSearchTimeOutError",Error.captureStackTrace&&Error.captureStackTrace(this,a)}}function h(t,e){return`${t}\nHint: It might not be working because maybe you're not up to date with the Meilisearch version that ${e} call requires.`}function c(t){return Object.entries(t).reduce(((t,e)=>{const[i,s]=e;return void 0!==s&&(t[i]=s),t}),{})}function l(t){return s(this,void 0,void 0,(function*(){return yield new Promise((e=>setTimeout(e,t)))}))}function p(t){return Object.keys(t).reduce(((e,i)=>{const s=t[i];return void 0===s?e:Array.isArray(s)?Object.assign(Object.assign({},e),{[i]:s.join(",")}):s instanceof Date?Object.assign(Object.assign({},e),{[i]:s.toISOString()}):Object.assign(Object.assign({},e),{[i]:s})}),{})}function v(t){var e,i;const s="X-Meilisearch-Client",n="Meilisearch JavaScript (v0.35.0)",r=function(t){if(Array.isArray(t))return t.reduce(((t,e)=>(t[e[0]]=e[1],t)),{});if("has"in t){const e={};return t.forEach(((t,i)=>e[i]=t)),e}return Object.assign({},t)}(null!==(i=null===(e=t.requestConfig)||void 0===e?void 0:e.headers)&&void 0!==i?i:{});if(t.apiKey&&!r.Authorization&&(r.Authorization=`Bearer ${t.apiKey}`),r["Content-Type"]||(r["Content-Type"]="application/json"),t.clientAgents&&Array.isArray(t.clientAgents)){const e=t.clientAgents.concat(n);r[s]=e.join(" ; ")}else{if(t.clientAgents&&!Array.isArray(t.clientAgents))throw new d(`Meilisearch: The header "${s}" should be an array of string(s).\n`);r[s]=n}return r}class f{constructor(t){this.headers=v(t),this.requestConfig=t.requestConfig,this.httpClient=t.httpClient;try{const e=function(t){try{return(e=t=function(t){return t.startsWith("https://")||t.startsWith("http://")?t:`http://${t}`}(t)).endsWith("/")||(e+="/"),t=e}catch(t){throw new d("The provided host is not valid.")}var e}(t.host);this.url=new URL(e)}catch(t){throw new d("The provided host is not valid.")}}request({method:t,url:e,params:i,body:n,config:r={}}){var d;return s(this,void 0,void 0,(function*(){"undefined"==typeof fetch&&require("cross-fetch/polyfill");const s=new URL(e,this.url);if(i){const t=new URLSearchParams;Object.keys(i).filter((t=>null!==i[t])).map((e=>t.set(e,i[e]))),s.search=t.toString()}(null===(d=r.headers)||void 0===d?void 0:d["Content-Type"])||(n=JSON.stringify(n));const a=Object.assign(Object.assign({},this.headers),r.headers);try{const e=(this.httpClient?this.httpClient:fetch)(s.toString(),Object.assign(Object.assign(Object.assign({},r),this.requestConfig),{method:t,body:n,headers:a}));if(this.httpClient)return yield e;const i=yield e.then((t=>o(t)));return yield i.json().catch((()=>{}))}catch(t){u(t,t.stack,s.toString())}}))}get(t,e,i){return s(this,void 0,void 0,(function*(){return yield this.request({method:"GET",url:t,params:e,config:i})}))}post(t,e,i,n){return s(this,void 0,void 0,(function*(){return yield this.request({method:"POST",url:t,body:e,params:i,config:n})}))}put(t,e,i,n){return s(this,void 0,void 0,(function*(){return yield this.request({method:"PUT",url:t,body:e,params:i,config:n})}))}patch(t,e,i,n){return s(this,void 0,void 0,(function*(){return yield this.request({method:"PATCH",url:t,body:e,params:i,config:n})}))}delete(t,e,i,n){return s(this,void 0,void 0,(function*(){return yield this.request({method:"DELETE",url:t,body:e,params:i,config:n})}))}}class y{constructor(t){this.taskUid=t.taskUid,this.indexUid=t.indexUid,this.status=t.status,this.type=t.type,this.enqueuedAt=new Date(t.enqueuedAt)}}class g{constructor(t){this.indexUid=t.indexUid,this.status=t.status,this.type=t.type,this.uid=t.uid,this.details=t.details,this.canceledBy=t.canceledBy,this.error=t.error,this.duration=t.duration,this.startedAt=new Date(t.startedAt),this.enqueuedAt=new Date(t.enqueuedAt),this.finishedAt=new Date(t.finishedAt)}}class w{constructor(t){this.httpRequest=new f(t)}getTask(t){return s(this,void 0,void 0,(function*(){const e=`tasks/${t}`,i=yield this.httpRequest.get(e);return new g(i)}))}getTasks(t={}){return s(this,void 0,void 0,(function*(){const e=yield this.httpRequest.get("tasks",p(t));return Object.assign(Object.assign({},e),{results:e.results.map((t=>new g(t)))})}))}waitForTask(t,{timeOutMs:e=5e3,intervalMs:i=50}={}){return s(this,void 0,void 0,(function*(){const s=Date.now();for(;Date.now()-s<e;){const e=yield this.getTask(t);if(!["enqueued","processing"].includes(e.status))return e;yield l(i)}throw new a(`timeout of ${e}ms has exceeded on process ${t} when waiting a task to be resolved.`)}))}waitForTasks(t,{timeOutMs:e=5e3,intervalMs:i=50}={}){return s(this,void 0,void 0,(function*(){const s=[];for(const n of t){const t=yield this.waitForTask(n,{timeOutMs:e,intervalMs:i});s.push(t)}return s}))}cancelTasks(t={}){return s(this,void 0,void 0,(function*(){const e=yield this.httpRequest.post("tasks/cancel",{},p(t));return new y(e)}))}deleteTasks(t={}){return s(this,void 0,void 0,(function*(){const e=yield this.httpRequest.delete("tasks",{},p(t));return new y(e)}))}}class q{constructor(t,e,i){this.uid=e,this.primaryKey=i,this.httpRequest=new f(t),this.tasks=new w(t)}search(t,e,i){return s(this,void 0,void 0,(function*(){const s=`indexes/${this.uid}/search`;return yield this.httpRequest.post(s,c(Object.assign({q:t},e)),void 0,i)}))}searchGet(t,e,i){var n,r,o,u,a,h,l;return s(this,void 0,void 0,(function*(){const s=`indexes/${this.uid}/search`,p=Object.assign(Object.assign({q:t},e),{filter:(t=>{if("string"==typeof t)return t;if(Array.isArray(t))throw new d("The filter query parameter should be in string format when using searchGet")})(null==e?void 0:e.filter),sort:null===(n=null==e?void 0:e.sort)||void 0===n?void 0:n.join(","),facets:null===(r=null==e?void 0:e.facets)||void 0===r?void 0:r.join(","),attributesToRetrieve:null===(o=null==e?void 0:e.attributesToRetrieve)||void 0===o?void 0:o.join(","),attributesToCrop:null===(u=null==e?void 0:e.attributesToCrop)||void 0===u?void 0:u.join(","),attributesToHighlight:null===(a=null==e?void 0:e.attributesToHighlight)||void 0===a?void 0:a.join(","),vector:null===(h=null==e?void 0:e.vector)||void 0===h?void 0:h.join(","),attributesToSearchOn:null===(l=null==e?void 0:e.attributesToSearchOn)||void 0===l?void 0:l.join(",")});return yield this.httpRequest.get(s,c(p),i)}))}searchForFacetValues(t,e){return s(this,void 0,void 0,(function*(){const i=`indexes/${this.uid}/facet-search`;return yield this.httpRequest.post(i,c(t),void 0,e)}))}getRawInfo(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}`,e=yield this.httpRequest.get(t);return this.primaryKey=e.primaryKey,this.updatedAt=new Date(e.updatedAt),this.createdAt=new Date(e.createdAt),e}))}fetchInfo(){return s(this,void 0,void 0,(function*(){return yield this.getRawInfo(),this}))}fetchPrimaryKey(){return s(this,void 0,void 0,(function*(){return this.primaryKey=(yield this.getRawInfo()).primaryKey,this.primaryKey}))}static create(t,e={},i){return s(this,void 0,void 0,(function*(){const s=new f(i),n=yield s.post("indexes",Object.assign(Object.assign({},e),{uid:t}));return new y(n)}))}update(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}`,i=yield this.httpRequest.patch(e,t);return i.enqueuedAt=new Date(i.enqueuedAt),i}))}delete(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}`,e=yield this.httpRequest.delete(t);return new y(e)}))}getTasks(t={}){return s(this,void 0,void 0,(function*(){return yield this.tasks.getTasks(Object.assign(Object.assign({},t),{indexUids:[this.uid]}))}))}getTask(t){return s(this,void 0,void 0,(function*(){return yield this.tasks.getTask(t)}))}waitForTasks(t,{timeOutMs:e=5e3,intervalMs:i=50}={}){return s(this,void 0,void 0,(function*(){return yield this.tasks.waitForTasks(t,{timeOutMs:e,intervalMs:i})}))}waitForTask(t,{timeOutMs:e=5e3,intervalMs:i=50}={}){return s(this,void 0,void 0,(function*(){return yield this.tasks.waitForTask(t,{timeOutMs:e,intervalMs:i})}))}getStats(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/stats`;return yield this.httpRequest.get(t)}))}getDocuments(t={}){var e;return s(this,void 0,void 0,(function*(){if(void 0===(t=c(t)).filter){const i=`indexes/${this.uid}/documents`,s=Array.isArray(null==t?void 0:t.fields)?{fields:null===(e=null==t?void 0:t.fields)||void 0===e?void 0:e.join(",")}:{};return yield this.httpRequest.get(i,Object.assign(Object.assign({},t),s))}try{const e=`indexes/${this.uid}/documents/fetch`;return yield this.httpRequest.post(e,t)}catch(t){throw(t instanceof n||t instanceof r)&&(t.message=h(t.message,"getDocuments")),t}}))}getDocument(t,e){return s(this,void 0,void 0,(function*(){const i=`indexes/${this.uid}/documents/${t}`,s=(()=>{var t;if(Array.isArray(null==e?void 0:e.fields))return null===(t=null==e?void 0:e.fields)||void 0===t?void 0:t.join(",")})();return yield this.httpRequest.get(i,c(Object.assign(Object.assign({},e),{fields:s})))}))}addDocuments(t,e){return s(this,void 0,void 0,(function*(){const i=`indexes/${this.uid}/documents`,s=yield this.httpRequest.post(i,t,e);return new y(s)}))}addDocumentsFromString(t,e,i){return s(this,void 0,void 0,(function*(){const s=`indexes/${this.uid}/documents`,n=yield this.httpRequest.post(s,t,i,{headers:{"Content-Type":e}});return new y(n)}))}addDocumentsInBatches(t,e=1e3,i){return s(this,void 0,void 0,(function*(){const s=[];for(let n=0;n<t.length;n+=e)s.push(yield this.addDocuments(t.slice(n,n+e),i));return s}))}updateDocuments(t,e){return s(this,void 0,void 0,(function*(){const i=`indexes/${this.uid}/documents`,s=yield this.httpRequest.put(i,t,e);return new y(s)}))}updateDocumentsInBatches(t,e=1e3,i){return s(this,void 0,void 0,(function*(){const s=[];for(let n=0;n<t.length;n+=e)s.push(yield this.updateDocuments(t.slice(n,n+e),i));return s}))}updateDocumentsFromString(t,e,i){return s(this,void 0,void 0,(function*(){const s=`indexes/${this.uid}/documents`,n=yield this.httpRequest.put(s,t,i,{headers:{"Content-Type":e}});return new y(n)}))}deleteDocument(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/documents/${t}`,i=yield this.httpRequest.delete(e);return i.enqueuedAt=new Date(i.enqueuedAt),i}))}deleteDocuments(t){return s(this,void 0,void 0,(function*(){const e=!Array.isArray(t)&&"object"==typeof t,i=e?"documents/delete":"documents/delete-batch",s=`indexes/${this.uid}/${i}`;try{const e=yield this.httpRequest.post(s,t);return new y(e)}catch(t){throw(t instanceof n&&e||t instanceof r)&&(t.message=h(t.message,"deleteDocuments")),t}}))}deleteAllDocuments(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/documents`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getSettings(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings`;return yield this.httpRequest.get(t)}))}updateSettings(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings`,i=yield this.httpRequest.patch(e,t);return i.enqueued=new Date(i.enqueuedAt),i}))}resetSettings(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getPagination(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/pagination`;return yield this.httpRequest.get(t)}))}updatePagination(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/pagination`,i=yield this.httpRequest.patch(e,t);return new y(i)}))}resetPagination(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/pagination`,e=yield this.httpRequest.delete(t);return new y(e)}))}getSynonyms(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/synonyms`;return yield this.httpRequest.get(t)}))}updateSynonyms(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/synonyms`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetSynonyms(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/synonyms`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getStopWords(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/stop-words`;return yield this.httpRequest.get(t)}))}updateStopWords(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/stop-words`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetStopWords(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/stop-words`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getRankingRules(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/ranking-rules`;return yield this.httpRequest.get(t)}))}updateRankingRules(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/ranking-rules`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetRankingRules(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/ranking-rules`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getDistinctAttribute(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/distinct-attribute`;return yield this.httpRequest.get(t)}))}updateDistinctAttribute(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/distinct-attribute`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetDistinctAttribute(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/distinct-attribute`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getFilterableAttributes(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/filterable-attributes`;return yield this.httpRequest.get(t)}))}updateFilterableAttributes(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/filterable-attributes`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetFilterableAttributes(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/filterable-attributes`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getSortableAttributes(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/sortable-attributes`;return yield this.httpRequest.get(t)}))}updateSortableAttributes(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/sortable-attributes`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetSortableAttributes(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/sortable-attributes`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getSearchableAttributes(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/searchable-attributes`;return yield this.httpRequest.get(t)}))}updateSearchableAttributes(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/searchable-attributes`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetSearchableAttributes(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/searchable-attributes`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getDisplayedAttributes(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/displayed-attributes`;return yield this.httpRequest.get(t)}))}updateDisplayedAttributes(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/displayed-attributes`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetDisplayedAttributes(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/displayed-attributes`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getTypoTolerance(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/typo-tolerance`;return yield this.httpRequest.get(t)}))}updateTypoTolerance(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/typo-tolerance`,i=yield this.httpRequest.patch(e,t);return i.enqueuedAt=new Date(i.enqueuedAt),i}))}resetTypoTolerance(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/typo-tolerance`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getFaceting(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/faceting`;return yield this.httpRequest.get(t)}))}updateFaceting(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/faceting`,i=yield this.httpRequest.patch(e,t);return new y(i)}))}resetFaceting(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/faceting`,e=yield this.httpRequest.delete(t);return new y(e)}))}getSeparatorTokens(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/separator-tokens`;return yield this.httpRequest.get(t)}))}updateSeparatorTokens(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/separator-tokens`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetSeparatorTokens(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/separator-tokens`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getNonSeparatorTokens(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/non-separator-tokens`;return yield this.httpRequest.get(t)}))}updateNonSeparatorTokens(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/non-separator-tokens`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetNonSeparatorTokens(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/non-separator-tokens`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}getDictionary(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/dictionary`;return yield this.httpRequest.get(t)}))}updateDictionary(t){return s(this,void 0,void 0,(function*(){const e=`indexes/${this.uid}/settings/dictionary`,i=yield this.httpRequest.put(e,t);return new y(i)}))}resetDictionary(){return s(this,void 0,void 0,(function*(){const t=`indexes/${this.uid}/settings/dictionary`,e=yield this.httpRequest.delete(t);return e.enqueuedAt=new Date(e.enqueuedAt),e}))}}function b(t){return Buffer.from(JSON.stringify(t)).toString("base64")}class m{constructor(t){this.config=t}generateTenantToken(e,i,s){const n=(null==s?void 0:s.apiKey)||this.config.apiKey||"",r=e||"",o=null==s?void 0:s.expiresAt;!function(t){const{searchRules:e,uid:i,apiKey:s,expiresAt:n}=t;if(n){if(!(n instanceof Date))throw new d("Meilisearch: The expiredAt field must be an instance of Date.");if(n.getTime()<Date.now())throw new d("Meilisearch: The expiresAt field must be a date in the future.")}if(e&&"object"!=typeof e&&!Array.isArray(e))throw new d("Meilisearch: The search rules added in the token generation must be of type array or object.");if(!s||"string"!=typeof s)throw new d("Meilisearch: The API key used for the token generation must exist and be of type string.");if(!i||"string"!=typeof i)throw new d("Meilisearch: The uid of the api key used for the token generation must exist, be of type string and comply to the uuid4 format.");if(!/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi.test(i))throw new d("Meilisearch: The uid of your key is not a valid uuid4. To find out the uid of your key use getKey().")}({apiKey:n,uid:r,expiresAt:o,searchRules:i});const u=b({alg:"HS256",typ:"JWT"}).replace(/=/g,""),a=function(t){const{searchRules:e,uid:i,expiresAt:s}=t;return b({searchRules:e,apiKeyUid:i,exp:s?Math.floor(s.getTime()/1e3):void 0}).replace(/=/g,"")}({searchRules:i,uid:r,expiresAt:o}),h=function(e,i,s){return t.createHmac("sha256",e).update(`${i}.${s}`).digest("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}(n,u,a);return`${u}.${a}.${h}`}}class k extends class{constructor(t){this.config=t,this.httpRequest=new f(t),this.tasks=new w(t)}index(t){return new q(this.config,t)}getIndex(t){return s(this,void 0,void 0,(function*(){return new q(this.config,t).fetchInfo()}))}getRawIndex(t){return s(this,void 0,void 0,(function*(){return new q(this.config,t).getRawInfo()}))}getIndexes(t={}){return s(this,void 0,void 0,(function*(){const e=yield this.getRawIndexes(t),i=e.results.map((t=>new q(this.config,t.uid,t.primaryKey)));return Object.assign(Object.assign({},e),{results:i})}))}getRawIndexes(t={}){return s(this,void 0,void 0,(function*(){return yield this.httpRequest.get("indexes",t)}))}createIndex(t,e={}){return s(this,void 0,void 0,(function*(){return yield q.create(t,e,this.config)}))}updateIndex(t,e={}){return s(this,void 0,void 0,(function*(){return yield new q(this.config,t).update(e)}))}deleteIndex(t){return s(this,void 0,void 0,(function*(){return yield new q(this.config,t).delete()}))}deleteIndexIfExists(t){return s(this,void 0,void 0,(function*(){try{return yield this.deleteIndex(t),!0}catch(t){if("index_not_found"===t.code)return!1;throw t}}))}swapIndexes(t){return s(this,void 0,void 0,(function*(){return yield this.httpRequest.post("/swap-indexes",t)}))}multiSearch(t,e){return s(this,void 0,void 0,(function*(){return yield this.httpRequest.post("multi-search",t,void 0,e)}))}getTasks(t={}){return s(this,void 0,void 0,(function*(){return yield this.tasks.getTasks(t)}))}getTask(t){return s(this,void 0,void 0,(function*(){return yield this.tasks.getTask(t)}))}waitForTasks(t,{timeOutMs:e=5e3,intervalMs:i=50}={}){return s(this,void 0,void 0,(function*(){return yield this.tasks.waitForTasks(t,{timeOutMs:e,intervalMs:i})}))}waitForTask(t,{timeOutMs:e=5e3,intervalMs:i=50}={}){return s(this,void 0,void 0,(function*(){return yield this.tasks.waitForTask(t,{timeOutMs:e,intervalMs:i})}))}cancelTasks(t){return s(this,void 0,void 0,(function*(){return yield this.tasks.cancelTasks(t)}))}deleteTasks(t={}){return s(this,void 0,void 0,(function*(){return yield this.tasks.deleteTasks(t)}))}getKeys(t={}){return s(this,void 0,void 0,(function*(){const e=yield this.httpRequest.get("keys",t);return e.results=e.results.map((t=>Object.assign(Object.assign({},t),{createdAt:new Date(t.createdAt),updatedAt:new Date(t.updatedAt)}))),e}))}getKey(t){return s(this,void 0,void 0,(function*(){const e=`keys/${t}`;return yield this.httpRequest.get(e)}))}createKey(t){return s(this,void 0,void 0,(function*(){return yield this.httpRequest.post("keys",t)}))}updateKey(t,e){return s(this,void 0,void 0,(function*(){const i=`keys/${t}`;return yield this.httpRequest.patch(i,e)}))}deleteKey(t){return s(this,void 0,void 0,(function*(){const e=`keys/${t}`;return yield this.httpRequest.delete(e)}))}health(){return s(this,void 0,void 0,(function*(){return yield this.httpRequest.get("health")}))}isHealthy(){return s(this,void 0,void 0,(function*(){try{const t="health";return yield this.httpRequest.get(t),!0}catch(t){return!1}}))}getStats(){return s(this,void 0,void 0,(function*(){return yield this.httpRequest.get("stats")}))}getVersion(){return s(this,void 0,void 0,(function*(){return yield this.httpRequest.get("version")}))}createDump(){return s(this,void 0,void 0,(function*(){const t=yield this.httpRequest.post("dumps");return new y(t)}))}generateTenantToken(t,e,i){const s=new Error;throw new Error(`Meilisearch: failed to generate a tenant token. Generation of a token only works in a node environment \n ${s.stack}.`)}}{constructor(t){super(t),this.tokens=new m(t)}generateTenantToken(t,e,i){return"undefined"==typeof window?this.tokens.generateTenantToken(t,e,i):super.generateTenantToken(t,e,i)}}export{i as ContentTypeEnum,y as EnqueuedTask,q as Index,e as MatchingStrategies,k as MeiliSearch,r as MeiliSearchApiError,n as MeiliSearchCommunicationError,d as MeiliSearchError,a as MeiliSearchTimeOutError,g as Task,w as TaskClient,k as default,u as httpErrorHandler,o as httpResponseErrorHandler,h as versionErrorHintMessage}; //# sourceMappingURL=meilisearch.esm.min.js.map