@nextgis/ngw-connector 1.19.0 → 2.0.0-alpha.1

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.
@@ -1,2 +1,2 @@
1
- var e,t={exports:{}},r="object"==typeof Reflect?Reflect:null,n=r&&"function"==typeof r.apply?r.apply:function(e,t,r){return Function.prototype.apply.call(e,t,r)};e=r&&"function"==typeof r.ownKeys?r.ownKeys:Object.getOwnPropertySymbols?function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:function(e){return Object.getOwnPropertyNames(e)};var s=Number.isNaN||function(e){return e!=e};function i(){i.init.call(this)}t.exports=i,t.exports.once=function(e,t){return new Promise((function(r,n){function s(r){e.removeListener(t,i),n(r)}function i(){"function"==typeof e.removeListener&&e.removeListener("error",s),r([].slice.call(arguments))}m(e,t,i,{once:!0}),"error"!==t&&function(e,t,r){"function"==typeof e.on&&m(e,"error",t,r)}(e,s,{once:!0})}))},i.EventEmitter=i,i.prototype._events=void 0,i.prototype._eventsCount=0,i.prototype._maxListeners=void 0;var o=10;function c(e){if("function"!=typeof e)throw new TypeError('The "listener" argument must be of type Function. Received type '+typeof e)}function a(e){return void 0===e._maxListeners?i.defaultMaxListeners:e._maxListeners}function h(e,t,r,n){var s,i,o,h;if(c(r),void 0===(i=e._events)?(i=e._events=Object.create(null),e._eventsCount=0):(void 0!==i.newListener&&(e.emit("newListener",t,r.listener?r.listener:r),i=e._events),o=i[t]),void 0===o)o=i[t]=r,++e._eventsCount;else if("function"==typeof o?o=i[t]=n?[r,o]:[o,r]:n?o.unshift(r):o.push(r),(s=a(e))>0&&o.length>s&&!o.warned){o.warned=!0;var u=new Error("Possible EventEmitter memory leak detected. "+o.length+" "+String(t)+" listeners added. Use emitter.setMaxListeners() to increase limit");u.name="MaxListenersExceededWarning",u.emitter=e,u.type=t,u.count=o.length,h=u,console&&console.warn&&console.warn(h)}return e}function u(){if(!this.fired)return this.target.removeListener(this.type,this.wrapFn),this.fired=!0,0===arguments.length?this.listener.call(this.target):this.listener.apply(this.target,arguments)}function l(e,t,r){var n={fired:!1,wrapFn:void 0,target:e,type:t,listener:r},s=u.bind(n);return s.listener=r,n.wrapFn=s,s}function f(e,t,r){var n=e._events;if(void 0===n)return[];var s=n[t];return void 0===s?[]:"function"==typeof s?r?[s.listener||s]:[s]:r?function(e){for(var t=new Array(e.length),r=0;r<t.length;++r)t[r]=e[r].listener||e[r];return t}(s):d(s,s.length)}function p(e){var t=this._events;if(void 0!==t){var r=t[e];if("function"==typeof r)return 1;if(void 0!==r)return r.length}return 0}function d(e,t){for(var r=new Array(t),n=0;n<t;++n)r[n]=e[n];return r}function m(e,t,r,n){if("function"==typeof e.on)n.once?e.once(t,r):e.on(t,r);else{if("function"!=typeof e.addEventListener)throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type '+typeof e);e.addEventListener(t,(function s(i){n.once&&e.removeEventListener(t,s),r(i)}))}}function v(e){return null!=e}function g(e){return"string"==typeof e?!!e:v(e)}function y(e,t,r,n){if(e instanceof Array)return t instanceof Array&&t.sort().join("")===e.sort().join("");if(e instanceof Date)return t instanceof Date&&""+e==""+t;if(e instanceof Function){if(!(t instanceof Function))return!1}else if(e instanceof Object)return t instanceof Object&&(e===r?t===n:_(e,t));return e===t}function _(e,t){const r=Object.keys(e).sort(),n=Object.keys(t).sort();if(r.length!==n.length)return!1;if(r.join("")!==n.join(""))return!1;for(let s=0;s<r.length;s++){if(!y(e[r[s]],t[n[s]],e,t))return!1}return!0}function w(e){const t={};return Object.keys(e).forEach((r=>{e[r]instanceof Array||e[r]!==Object(e[r])?void 0!==e[r]&&(t[r]=e[r]):t[r]=w(e[r])})),t}Object.defineProperty(i,"defaultMaxListeners",{enumerable:!0,get:function(){return o},set:function(e){if("number"!=typeof e||e<0||s(e))throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received '+e+".");o=e}}),i.init=function(){void 0!==this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=Object.create(null),this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},i.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||s(e))throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received '+e+".");return this._maxListeners=e,this},i.prototype.getMaxListeners=function(){return a(this)},i.prototype.emit=function(e){for(var t=[],r=1;r<arguments.length;r++)t.push(arguments[r]);var s="error"===e,i=this._events;if(void 0!==i)s=s&&void 0===i.error;else if(!s)return!1;if(s){var o;if(t.length>0&&(o=t[0]),o instanceof Error)throw o;var c=new Error("Unhandled error."+(o?" ("+o.message+")":""));throw c.context=o,c}var a=i[e];if(void 0===a)return!1;if("function"==typeof a)n(a,this,t);else{var h=a.length,u=d(a,h);for(r=0;r<h;++r)n(u[r],this,t)}return!0},i.prototype.on=i.prototype.addListener=function(e,t){return h(this,e,t,!1)},i.prototype.prependListener=function(e,t){return h(this,e,t,!0)},i.prototype.once=function(e,t){return c(t),this.on(e,l(this,e,t)),this},i.prototype.prependOnceListener=function(e,t){return c(t),this.prependListener(e,l(this,e,t)),this},i.prototype.removeListener=function(e,t){var r,n,s,i,o;if(c(t),void 0===(n=this._events))return this;if(void 0===(r=n[e]))return this;if(r===t||r.listener===t)0==--this._eventsCount?this._events=Object.create(null):(delete n[e],n.removeListener&&this.emit("removeListener",e,r.listener||t));else if("function"!=typeof r){for(s=-1,i=r.length-1;i>=0;i--)if(r[i]===t||r[i].listener===t){o=r[i].listener,s=i;break}if(s<0)return this;0===s?r.shift():function(e,t){for(;t+1<e.length;t++)e[t]=e[t+1];e.pop()}(r,s),1===r.length&&(n[e]=r[0]),void 0!==n.removeListener&&this.emit("removeListener",e,o||t)}return this},i.prototype.off=i.prototype.removeListener,i.prototype.removeAllListeners=function(e){var t,r,n;if(void 0===(r=this._events))return this;if(void 0===r.removeListener)return 0===arguments.length?(this._events=Object.create(null),this._eventsCount=0):void 0!==r[e]&&(0==--this._eventsCount?this._events=Object.create(null):delete r[e]),this;if(0===arguments.length){var s,i=Object.keys(r);for(n=0;n<i.length;++n)"removeListener"!==(s=i[n])&&this.removeAllListeners(s);return this.removeAllListeners("removeListener"),this._events=Object.create(null),this._eventsCount=0,this}if("function"==typeof(t=r[e]))this.removeListener(e,t);else if(void 0!==t)for(n=t.length-1;n>=0;n--)this.removeListener(e,t[n]);return this},i.prototype.listeners=function(e){return f(this,e,!0)},i.prototype.rawListeners=function(e){return f(this,e,!1)},i.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):p.call(e,t)},i.prototype.listenerCount=p,i.prototype.eventNames=function(){return this._eventsCount>0?e(this._events):[]};class b{constructor(e={}){var t;this.cache=[];const r=null!==(t=e.namespace)&&void 0!==t?t:"default";return b.instance[r]?b.instance[r]:(b.instance[r]=this,this)}clean(){this.cache.length=0}all(){return this.cache}addFull(e,t,r){return this.add(e,t,r,!0)}add(e,t,r,n){const s=this._find(e,r);if(s)return s.value;{let s;s=t instanceof Function?t():t;const i=r&&JSON.parse(JSON.stringify(w(r))),o={key:e,value:s,props:i,options:i};return n&&!g(s)?s:(this.cache.push(o),s instanceof Promise?(s.catch((t=>{throw this.delete(e,r),t})),n&&s.then((t=>(g(t)||this.delete(e,r),t))),s):s)}}match(e,t){const r=this._find(e,t);if(r)return r.value}matchAll(e,t){return e?this.cache.filter((r=>this._filter(r,e,t))).map((e=>e.value)):this.cache.map((e=>e.value))}delete(e,t){let r=[];"string"==typeof e?r=this.cache.filter((r=>this._filter(r,e,t))):r.push(e);for(const n of r){const e=this.cache.indexOf(n);this.cache.splice(e,1)}}_find(e,t){return this.cache.find((r=>this._filter(r,e,t)))}_filter(e,t,r){if(e.key===t){if(r){return _(e.props||e.options||{},w(r))}return!0}return!1}}b.instance={};var O=b;class C extends Error{constructor(){super(),this.name="CancelError",Object.setPrototypeOf(this,C.prototype)}}class E{constructor(e={}){this.options=e,this._promises=new Map}get isLoaded(){return this._promises.size>0}remove(e){this._promises.has(e)&&(this._promises.delete(e),this._onStop())}get(e){return this._promises.get(e)}add(e,t){const r=t||e,n=this._promises.get(r);return this.options.onStart&&!this.isLoaded&&this.options.onStart(),n||(this._promises.set(r,e),e.finally((()=>{this.remove(r)})),e)}abort(){this.isLoaded&&(this._promises.forEach((e=>{e.cancel&&e.cancel()})),this._promises.clear(),this._onStop())}waitFunc(e,t=""){const r=this.get(t=t||e.name);return r||this.add(e(),t)}WaitForMe(e=""){const t=this.get.bind(this),r=this.add.bind(this);return function(n,s,i){const o=i.value;return e=e||s,i.value=function(...n){const s=t(e);if(s)return s;return r(o.apply(this,n),e)},i}}GetOrCreateDecorator(e=""){return this.WaitForMe(e)}_onStop(){this.options.onStop&&!this.isLoaded&&this.options.onStop()}}class R extends Error{constructor(){super(),this.name="TimeoutError",Object.setPrototypeOf(this,R.prototype)}}const L=(e,t,r,n)=>{try{e(r(n))}catch(s){t(s)}};let P=0;class x{constructor(e,t){this.id=P++,this._isCanceled=!1,this._isPending=!0,this._cancelHandlers=[],this._children=[],this._cancelPromise=new Promise(((e,t)=>{this._setCanceledCallback=t=>e(t||new C)}));const r=[this._cancelPromise,new Promise(((t,r)=>e((e=>{e instanceof x?this.attach(e):this._isPending=!1,t(e)}),(e=>{this._isPending=!1,r(e)}),(e=>{if(!this._isPending)throw new Error("The `onCancel` handler was attached after the promise settled.");this._cancelHandlers.push(e)}))))];t&&r.push(new Promise(((e,r)=>{setTimeout((()=>{if(this._isPending)try{this.cancel()}finally{r(new R)}}),t)}))),this._promise=Promise.race(r)}static createControl(e){return new E(e)}static resolve(e){return new x((t=>t(e)))}static reject(e){return new x(((t,r)=>r(e)))}static all(e){return new x(((t,r)=>{Promise.all(e).then(t).catch(r)})).catch((t=>{if(t instanceof this.CancelError)for(const r of e){"cancel"in r&&r.cancel()}throw t}))}attach(e){this._isCanceled?e.cancel():this._children.push(e)}then(e,t){const r=new x(((r,n)=>{if(this._promise){const s=e=>{t?L(r,n,t,e):n(e)};this._promise.then((t=>{this._isCanceled?s(t):e?L(r,n,e,t):r(t)}),s)}}));return r._parentPromise=this,this._children.push(r),r}catch(e){return this._isCanceled&&e&&e(new C),this.then(void 0,e)}finally(e){return this._promise?this._promise.finally(e):Promise.reject(this._isCanceled?new C:e)}cancel(){if(this._isCanceled||!this._isPending)return this;this._isCanceled=!0;const e=this._getTopParent();if(e&&e.cancel(),this._children&&this._children.forEach((e=>e.cancel())),this._isPending){if(this._cancelHandlers.length)try{for(const e of this._cancelHandlers)e()}catch(t){}this._setCanceledCallback&&this._setCanceledCallback()}return this._destroy(),this}_getTopParent(){let e=this._parentPromise,t=!!e;for(;t;)e&&e._parentPromise?(e=e._parentPromise,t=!!e):t=!1;return e}_destroy(){this._setCanceledCallback=void 0,this._cancelPromise=void 0,this._promise=void 0}}x.CancelError=C,x.TimeoutError=R,x.PromiseControl=E,Object.setPrototypeOf(x.prototype,Promise.prototype);var j=x;class T extends Error{constructor(e){super(),this.name="NgwError",Object.assign(this,e),Object.setPrototypeOf(this,T.prototype)}}class k extends T{constructor(e){super(e),this.name="ResourceNotFoundError",this.exception="nextgisweb.resource.exception.ResourceNotFound",Object.setPrototypeOf(this,k.prototype)}}function q(e){return"[object Object]"===Object.prototype.toString.call(e)}const I=["description"];function F(e,t=""){t=t?t+"__":"";const r={};for(const[n,s]of Object.entries(e))if(-1===I.indexOf(n))if(q(s))if("owner_user"===n){const e=F(s,n);Object.assign(r,e)}else"parent"===n&&"id"in s&&(r.parent_id=s.id);else v(s)&&(r[t+n]=s);return r}class A{constructor(e){this.connector=e,this.cache=new O}getOne(e,t){const r=new O,n={},s={...t,cache:!1};"string"==typeof e?n.keyname=e:"number"==typeof e?n.id=e:q(e)&&(void 0!==e.id?n.id=e.id:e.keyname&&(n.keyname=e.keyname));return r.addFull("resource",(()=>"string"==typeof e?this._fetchResourceBy({keyname:e},s):"number"==typeof e?this._fetchResourceById(e,s):q(e)?this._fetchResourceBy(e,s):j.resolve(void 0)),n)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new k}))}getId(e,t){return"number"==typeof e?j.resolve(e):"string"==typeof e||q(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):j.resolve(void 0)}getIdOrFail(e,t){return this.getId(e,t).then((e=>{if(void 0===e)throw new Error;return e}))}getMany(e,t){return this._resourceCacheFilter(e).then((r=>{if(!r.length){const r={};return e.keyname?r.keyname=e.keyname:Object.assign(r,F(e)),this.connector.get("resource.search",t,{serialization:"full",...r}).then((e=>{if(e)for(const t of e)this.cache.add("resource.item",j.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>e?this.getOne(e.resource.parent.id,t):j.resolve(void 0)))}getChildrenOf(e,t){return this.getIdOrFail(e).then((e=>this._getChildrenOf(e,t)))}update(e,t){return this.getId(e).then((e=>{if(void 0!==e)return this.connector.put("resource.item",{data:t},{id:e})}))}delete(e){return this.getId(e).then((e=>{if(void 0!==e)return this.connector.delete("resource.item",null,{id:e}).then((()=>{this._cleanResourceItemCache(e)}))}))}_getChildrenOf(e,t,r=[]){return this.connector.get("resource.collection",{cache:!0,...t},{parent:e}).then((e=>{const n=[];for(const s of e)this.cache.add("resource.item",j.resolve(s),{id:s.resource.id}),r.push(s),(null==t?void 0:t.recursive)&&s.resource.children&&n.push(this._getChildrenOf(s.resource.id,t,r));return n.length?j.all(n).then((()=>r)):r}))}async _cleanResourceItemCache(e){const t=this.cache.all(),r=[];for(const n of t){const t=n.options&&n.options.id;if(["resource.item","resource"].includes(n.key)&&void 0!==t)if("number"==typeof t)t===e&&r.push(n);else{await this.getId(t)===e&&r.push(n)}}for(const n of r)this.cache.delete(n)}_fetchResourceById(e,t){return this.cache.add("resource.item",(()=>this.connector.get("resource.item",t,{id:e})),{id:e}).catch((e=>{if(!(e instanceof k))throw e}))}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(e){return j.all(this.cache.matchAll("resource.item")).then((t=>(t.filter((t=>{if(t)return e.keyname&&t.resource.keyname?e.keyname===t.resource.keyname:v(e.id)&&v(t.resource.id)?e.id===t.resource.id:_(e,t.resource)})),[])))}}const N=[];function S(e){N.push(e)}class U extends T{constructor(e){super(e),this.name="InsufficientPermissionsError",this.exception="nextgisweb.core.exception.InsufficientPermissions",Object.setPrototypeOf(this,U.prototype)}}const M=/\{ *([\w_-]+) *\}/g;function H(e,t){return e.replace(M,((e,r)=>{let n=t[r];if(void 0===n)throw new Error("No value provided for letiable "+e);return"function"==typeof n&&(n=n(t)),n}))}class B extends Error{constructor(e){super(),this.name="NetworkError",Object.setPrototypeOf(this,B.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}let $;$=(e,t,r={},n,s)=>{r.method=r.method||"GET";const i=new XMLHttpRequest;i.open(r.method||"GET",e,!0),"blob"===r.responseType&&(i.responseType=r.responseType);const o=()=>{try{return JSON.parse(i.responseText)}catch(e){return i.responseText}};i.onload=()=>{-1!==[401,403,404,422,500].indexOf(i.status)&&n(new T(o())),((e=!1)=>{const s=e?n:t;"blob"===r.responseType?s(i.response):i.responseText?s(o()):n({message:""})})()},i.onerror=t=>{n(0===i.status?new B(e):t)},i.upload.onprogress=function(e){if(e.lengthComputable){r.onProgress&&r.onProgress(e.loaded/e.total*100,e)}};const c=r.headers;if(c)for(const h in c){const e=c[h];"string"==typeof e&&i.setRequestHeader(h,e)}let a;if(void 0!==r.withCredentials&&(i.withCredentials=r.withCredentials),r.file){if(a=new FormData,a.append("file",r.file),r.data)for(const h in r.data)a.append(h,a[h])}else a=r.data?"string"==typeof r.data?r.data:JSON.stringify(r.data):null;s&&s((()=>{i.abort()})),i.send(a)};let D=0;class z{constructor(e){this.options=e,this.id=D++,this.emitter=new t.exports.EventEmitter,this.routeStr="/api/component/pyramid/route",this.activeRequests=[];const r=function(e){return N.find((t=>{if(t.options.baseUrl===e.baseUrl){if(!e.auth)return!0;if(t.options.auth)return _(t.options.auth,e.auth)}}))}(e);if(r)return r;this.options.route&&(this.routeStr=this.options.route),this.options.requestTransform&&(this.requestTransform=this.options.requestTransform),this.resources=new A(this),S(this)}static create(e){return new this(e)}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,S(this)}connect(){const e=new O,t=this.options.auth;return e.add("route",(()=>new j(((e,r)=>{const n=()=>this.makeQuery(this.routeStr,{},{}).then((t=>{e(t)})).catch((e=>{r(e)}));if(t){const{login:e,password:s}=t;if(e&&s)return this._login({login:e,password:s}).then((()=>n())).catch((e=>r(e)))}return n()}))),{id:this.id,auth:t,baseUrl:this.options.baseUrl})}login(e,t){return this.logout(),S(this),this._login(e,t)}logout(){this.abort(),function(e){const t=N.indexOf(e);-1!==t&&N.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.emitter.emit("logout"),this.resources.cache.clean()}getUserInfo(e,t){if(this.user&&this.user.id)return j.resolve(this.user);e&&(this.options.auth=e);const r={headers:this.getAuthorizationHeaders(e),...t};return this.makeQuery("/api/component/auth/current_user",{},r)}getAuthorizationHeaders(e){const t=this.makeClientId(e);if(t)return{Authorization:"Basic "+t}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,n=unescape(encodeURIComponent(`${t}:${r}`));return window.btoa(n)}}abort(){for(const e of this.activeRequests)e.cancel();this.activeRequests=[]}getActiveApiRequests(){return[...this.activeRequests]}apiRequest(e,t={},r={}){var n,s;const{method:i,headers:o,withCredentials:c,responseType:a}=r,h=w(t=null!==(n=r.params)&&void 0!==n?n:t),u=()=>function(e){const t=e.params;return new j(((r,n)=>{e.connector.connect().then((s=>{let i=s&&s[e.name];if(i){i=[...i];let s=i.shift();if(i.length){const e={};for(let r=0;r<i.length;r++){const n=i[r];if(e[r]="{"+n+"}",void 0===t[n])throw new Error("`"+n+"` url api argument is not specified")}s&&(s=H(s,e))}if(t){const e=[],r=t.paramList;Array.isArray(r)&&(delete t.paramList,r.forEach((t=>{e.push(`${t[0]}=${t[1]}`)})));for(const n in t)-1===i.indexOf(n)&&e.push(`${n}=${t[n]}`);e.length&&(s=s+"?"+e.join("&"))}s?r(e.connector.makeQuery(s,t,e.requestOptions)):n(new Error("request url is not set"))}else r(void 0)})).catch((e=>{n(e)}))}))}({name:e,params:h,requestOptions:r,connector:this});if(r.cache&&"GET"===i){return(new O).add(e,u,{params:h,...w({headers:o,withCredentials:c,responseType:a,baseUrl:this.options.baseUrl,userId:null===(s=this.user)||void 0===s?void 0:s.id})})}return u()}post(e,t,r){return(t=t||{}).method="POST",this.apiRequest(e,r,t)}get(e,t,r){return(t=t||{}).method="GET",this.apiRequest(e,r,t)}patch(e,t,r){return(t=t||{}).method="PATCH",this.apiRequest(e,r,t)}put(e,t,r){return(t=t||{}).method="PUT",this.apiRequest(e,r,t)}delete(e,t,r){return(t=t||{}).method="DELETE",this.apiRequest(e,r,t)}makeQuery(e,t,r={}){if(e=(this.options.baseUrl?this.options.baseUrl:"")+e)return t&&(e=H(e,t)),e=encodeURI(function(e){return e.replace(/([^:]\/)\/+/g,"$1")}(e)),this._loadData(e,r);throw new Error("Empty `url` not allowed")}getResource(e,t){return this.resources.getOne(e,t)}getResourceOrFail(e,t){return this.resources.getOneOrFail(e,t)}getResourceBy(e){return this.resources.getOne(e)}getResourceByKeyname(e){return this.resources.getOne(e)}getResourceById(e){return this.resources.getOne(e)}getResourceId(e,t){return this.resources.getId(e,t)}getResourceIdOrFail(e,t){return this.resources.getIdOrFail(e,t)}getResourcesBy(e,t){return this.resources.getMany(e,t)}getResourceParent(e,t){return this.resources.getParent(e,t)}getResourceChildren(e,t){return this.resources.getChildrenOf(e,t)}updateResource(e,t){return this.resources.update(e,t)}deleteResource(e){return this.resources.delete(e)}_loadData(e,t){t.responseType=t.responseType||"json";const r=new j(((r,n,s)=>{if(this.user&&((t=t||{}).headers={...this.getAuthorizationHeaders(),...t.headers}),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}$(e,r,t,n,s)})).then((e=>(this._cleanActiveRequests(r),e))).catch((e=>{if(this._cleanActiveRequests(r),e instanceof j.CancelError);else{const t=this._handleHttpError(e);if(t)throw t}}));return t.signal&&"function"==typeof t.signal.addEventListener&&t.signal.addEventListener("abort",(()=>{r.cancel(),this._cleanActiveRequests(r)})),this.activeRequests.push(r),r}_login(e,t){return this.getUserInfo(e,t).then((e=>(this.user=e,this.emitter.emit("login",e),e))).catch((e=>{throw this.emitter.emit("login:error",e),e}))}_cleanActiveRequests(e){const t=this.activeRequests.indexOf(e);-1!==t&&this.activeRequests.splice(t,1)}_handleHttpError(e){if(e&&e instanceof T){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new k(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new U(e)}return e}}z.errors={NgwError:T,ResourceNotFoundError:k};var G=z;export{G as default};
1
+ function e(){}function t(){t.init.call(this)}function r(e){return void 0===e._maxListeners?t.defaultMaxListeners:e._maxListeners}function n(t,n,s,i){var o,a,c,u;if("function"!=typeof s)throw new TypeError('"listener" argument must be a function');if((a=t._events)?(a.newListener&&(t.emit("newListener",n,s.listener?s.listener:s),a=t._events),c=a[n]):(a=t._events=new e,t._eventsCount=0),c){if("function"==typeof c?c=a[n]=i?[s,c]:[c,s]:i?c.unshift(s):c.push(s),!c.warned&&(o=r(t))&&o>0&&c.length>o){c.warned=!0;var h=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+n+" listeners added. Use emitter.setMaxListeners() to increase limit");h.name="MaxListenersExceededWarning",h.emitter=t,h.type=n,h.count=c.length,u=h,"function"==typeof console.warn?console.warn(u):console.log(u)}}else c=a[n]=s,++t._eventsCount;return t}function s(e,t,r){var n=!1;function s(){e.removeListener(t,s),n||(n=!0,r.apply(e,arguments))}return s.listener=r,s}function i(e){var t=this._events;if(t){var r=t[e];if("function"==typeof r)return 1;if(r)return r.length}return 0}function o(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}function a(e){return null!=e}function c(e){return"string"==typeof e?!!e:a(e)}function u(e,t,r,n){if(e instanceof Array)return t instanceof Array&&t.sort().join("")===e.sort().join("");if(e instanceof Date)return t instanceof Date&&""+e==""+t;if(e instanceof Function){if(!(t instanceof Function))return!1}else if(e instanceof Object)return t instanceof Object&&(e===r?t===n:h(e,t));return e===t}function h(e,t){const r=Object.keys(e).sort(),n=Object.keys(t).sort();if(r.length!==n.length)return!1;if(r.join("")!==n.join(""))return!1;for(let s=0;s<r.length;s++){if(!u(e[r[s]],t[n[s]],e,t))return!1}return!0}function l(e){const t={};return Object.keys(e).forEach((r=>{e[r]instanceof Array||e[r]!==Object(e[r])?void 0!==e[r]&&(t[r]=e[r]):t[r]=l(e[r])})),t}e.prototype=Object.create(null),t.EventEmitter=t,t.usingDomains=!1,t.prototype.domain=void 0,t.prototype._events=void 0,t.prototype._maxListeners=void 0,t.defaultMaxListeners=10,t.init=function(){this.domain=null,t.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new e,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},t.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},t.prototype.getMaxListeners=function(){return r(this)},t.prototype.emit=function(e){var t,r,n,s,i,a,c,u="error"===e;if(a=this._events)u=u&&null==a.error;else if(!u)return!1;if(c=this.domain,u){if(t=arguments[1],!c){if(t instanceof Error)throw t;var h=new Error('Uncaught, unspecified "error" event. ('+t+")");throw h.context=t,h}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=c,t.domainThrown=!1,c.emit("error",t),!1}if(!(r=a[e]))return!1;var l="function"==typeof r;switch(n=arguments.length){case 1:!function(e,t,r){if(t)e.call(r);else for(var n=e.length,s=o(e,n),i=0;i<n;++i)s[i].call(r)}(r,l,this);break;case 2:!function(e,t,r,n){if(t)e.call(r,n);else for(var s=e.length,i=o(e,s),a=0;a<s;++a)i[a].call(r,n)}(r,l,this,arguments[1]);break;case 3:!function(e,t,r,n,s){if(t)e.call(r,n,s);else for(var i=e.length,a=o(e,i),c=0;c<i;++c)a[c].call(r,n,s)}(r,l,this,arguments[1],arguments[2]);break;case 4:!function(e,t,r,n,s,i){if(t)e.call(r,n,s,i);else for(var a=e.length,c=o(e,a),u=0;u<a;++u)c[u].call(r,n,s,i)}(r,l,this,arguments[1],arguments[2],arguments[3]);break;default:for(s=new Array(n-1),i=1;i<n;i++)s[i-1]=arguments[i];!function(e,t,r,n){if(t)e.apply(r,n);else for(var s=e.length,i=o(e,s),a=0;a<s;++a)i[a].apply(r,n)}(r,l,this,s)}return!0},t.prototype.on=t.prototype.addListener=function(e,t){return n(this,e,t,!1)},t.prototype.prependListener=function(e,t){return n(this,e,t,!0)},t.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,s(this,e,t)),this},t.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,s(this,e,t)),this},t.prototype.removeListener=function(t,r){var n,s,i,o,a;if("function"!=typeof r)throw new TypeError('"listener" argument must be a function');if(!(s=this._events))return this;if(!(n=s[t]))return this;if(n===r||n.listener&&n.listener===r)0==--this._eventsCount?this._events=new e:(delete s[t],s.removeListener&&this.emit("removeListener",t,n.listener||r));else if("function"!=typeof n){for(i=-1,o=n.length;o-- >0;)if(n[o]===r||n[o].listener&&n[o].listener===r){a=n[o].listener,i=o;break}if(i<0)return this;if(1===n.length){if(n[0]=void 0,0==--this._eventsCount)return this._events=new e,this;delete s[t]}else!function(e,t){for(var r=t,n=r+1,s=e.length;n<s;r+=1,n+=1)e[r]=e[n];e.pop()}(n,i);s.removeListener&&this.emit("removeListener",t,a||r)}return this},t.prototype.off=function(e,t){return this.removeListener(e,t)},t.prototype.removeAllListeners=function(t){var r,n;if(!(n=this._events))return this;if(!n.removeListener)return 0===arguments.length?(this._events=new e,this._eventsCount=0):n[t]&&(0==--this._eventsCount?this._events=new e:delete n[t]),this;if(0===arguments.length){for(var s,i=Object.keys(n),o=0;o<i.length;++o)"removeListener"!==(s=i[o])&&this.removeAllListeners(s);return this.removeAllListeners("removeListener"),this._events=new e,this._eventsCount=0,this}if("function"==typeof(r=n[t]))this.removeListener(t,r);else if(r)do{this.removeListener(t,r[r.length-1])}while(r[0]);return this},t.prototype.listeners=function(e){var t,r=this._events;return r&&(t=r[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),r=0;r<t.length;++r)t[r]=e[r].listener||e[r];return t}(t):[]},t.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):i.call(e,t)},t.prototype.listenerCount=i,t.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var f=Object.defineProperty,p=(e,t,r)=>(((e,t,r)=>{t in e?f(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r})(e,"symbol"!=typeof t?t+"":t,r),r);const d=class e{constructor(t={}){var r;p(this,"cache",[]);const n=null!=(r=t.namespace)?r:"default";return e.instance[n]?e.instance[n]:(e.instance[n]=this,this)}clean(){this.cache.length=0}all(){return this.cache}addFull(e,t,r){return this.add(e,t,r,!0)}add(e,t,r,n){const s=this._find(e,r);if(s)return s.value;{let s;s=t instanceof Function?t():t;const i=r&&JSON.parse(JSON.stringify(l(r))),o={key:e,value:s,props:i,options:i};return n&&!c(s)?s:(this.cache.push(o),s instanceof Promise?(s.catch((t=>{throw this.delete(e,r),t})),n&&s.then((t=>(c(t)||this.delete(e,r),t))),s):s)}}match(e,t){const r=this._find(e,t);if(r)return r.value}matchAll(e,t){return e?this.cache.filter((r=>this._filter(r,e,t))).map((e=>e.value)):this.cache.map((e=>e.value))}delete(e,t){let r=[];"string"==typeof e?r=this.cache.filter((r=>this._filter(r,e,t))):r.push(e);for(const n of r){const e=this.cache.indexOf(n);this.cache.splice(e,1)}}_find(e,t){return this.cache.find((r=>this._filter(r,e,t)))}_filter(e,t,r){if(e.key===t){if(r){return h(e.props||e.options||{},l(r))}return!0}return!1}};p(d,"instance",{});let m=d;var y=Object.defineProperty,g=(e,t,r)=>(((e,t,r)=>{t in e?y(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r})(e,"symbol"!=typeof t?t+"":t,r),r);class v extends Error{constructor(){super(),g(this,"name","CancelError"),Object.setPrototypeOf(this,v.prototype)}}var b=Object.defineProperty,_=(e,t,r)=>(((e,t,r)=>{t in e?b(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r})(e,"symbol"!=typeof t?t+"":t,r),r);class w{constructor(e={}){this.options=e,_(this,"_promises",new Map)}get isLoaded(){return this._promises.size>0}remove(e){this._promises.has(e)&&(this._promises.delete(e),this._onStop())}get(e){return this._promises.get(e)}add(e,t){const r=t||e,n=this._promises.get(r);return this.options.onStart&&!this.isLoaded&&this.options.onStart(),n||(this._promises.set(r,e),e.finally((()=>{this.remove(r)})),e)}abort(){this.isLoaded&&(this._promises.forEach((e=>{e.cancel&&e.cancel()})),this._promises.clear(),this._onStop())}waitFunc(e,t=""){const r=this.get(t=t||e.name);return r||this.add(e(),t)}WaitForMe(e=""){const t=this.get.bind(this),r=this.add.bind(this);return function(n,s,i){const o=i.value;return e=e||s,i.value=function(...n){const s=t(e);if(s)return s;return r(o.apply(this,n),e)},i}}GetOrCreateDecorator(e=""){return this.WaitForMe(e)}_onStop(){this.options.onStop&&!this.isLoaded&&this.options.onStop()}}var O=Object.defineProperty,P=(e,t,r)=>(((e,t,r)=>{t in e?O(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r})(e,"symbol"!=typeof t?t+"":t,r),r);class C extends Error{constructor(){super(),P(this,"name","TimeoutError"),Object.setPrototypeOf(this,C.prototype)}}var E,j=Object.defineProperty,R=(e,t,r)=>(((e,t,r)=>{t in e?j(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r})(e,"symbol"!=typeof t?t+"":t,r),r);const x=(e,t,r,n)=>{try{e(r(n))}catch(s){t(s)}};let L=0;const T=class e{constructor(t,r){R(this,E),R(this,"id",L++),R(this,"_isCanceled",!1),R(this,"_isPending",!0),R(this,"_promise"),R(this,"_cancelPromise"),R(this,"_cancelHandlers",[]),R(this,"_setCanceledCallback"),R(this,"_parentPromise"),R(this,"_children",[]),this._cancelPromise=new Promise(((e,t)=>{this._setCanceledCallback=t=>e(t||new v)}));const n=[this._cancelPromise,new Promise(((r,n)=>t((t=>{t instanceof e?this.attach(t):this._isPending=!1,r(t)}),(e=>{this._isPending=!1,n(e)}),(e=>{if(!this._isPending)throw new Error("The `onCancel` handler was attached after the promise settled.");this._cancelHandlers.push(e)}))))];r&&n.push(new Promise(((e,t)=>{setTimeout((()=>{if(this._isPending)try{this.cancel()}finally{t(new C)}}),r)}))),this._promise=Promise.race(n)}static createControl(e){return new w(e)}static resolve(t){return new e((e=>e(t)))}static reject(t){return new e(((e,r)=>r(t)))}static all(t){return new e(((e,r)=>{Promise.all(t).then(e).catch(r)})).catch((e=>{if(e instanceof this.CancelError)for(const r of t){"cancel"in r&&r.cancel()}throw e}))}attach(e){this._isCanceled?e.cancel():this._children.push(e)}then(t,r){const n=new e(((e,n)=>{if(this._promise){const s=t=>{r?x(e,n,r,t):n(t)};this._promise.then((r=>{this._isCanceled?s(r):t?x(e,n,t,r):e(r)}),s)}}));return n._parentPromise=this,this._children.push(n),n}catch(e){return this._isCanceled&&e&&e(new v),this.then(void 0,e)}finally(e){return this._promise?this._promise.finally(e):Promise.reject(this._isCanceled?new v:e)}cancel(){if(this._isCanceled||!this._isPending)return this;this._isCanceled=!0;const e=this._getTopParent();if(e&&e.cancel(),this._children&&this._children.forEach((e=>e.cancel())),this._isPending){if(this._cancelHandlers.length)try{for(const e of this._cancelHandlers)e()}catch(t){}this._setCanceledCallback&&this._setCanceledCallback()}return this._destroy(),this}_getTopParent(){let e=this._parentPromise,t=!!e;for(;t;)e&&e._parentPromise?(e=e._parentPromise,t=!!e):t=!1;return e}_destroy(){this._setCanceledCallback=void 0,this._cancelPromise=void 0,this._promise=void 0}};E=Symbol.toStringTag,R(T,"CancelError",v),R(T,"TimeoutError",C),R(T,"PromiseControl",w);let k=T;Object.setPrototypeOf(k.prototype,Promise.prototype);var q=Object.defineProperty,I=(e,t,r)=>(((e,t,r)=>{t in e?q(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r})(e,"symbol"!=typeof t?t+"":t,r),r);class A extends Error{constructor(e){super(),I(this,"name","NgwError"),I(this,"title"),I(this,"message"),I(this,"detail"),I(this,"exception"),I(this,"status_code"),I(this,"data"),I(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,A.prototype)}}var F=Object.defineProperty,S=(e,t,r)=>(((e,t,r)=>{t in e?F(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r})(e,"symbol"!=typeof t?t+"":t,r),r);class U extends A{constructor(e){super(e),S(this,"name","ResourceNotFoundError"),S(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,U.prototype)}}function N(e){return"[object Object]"===Object.prototype.toString.call(e)}const M=["description"];function H(e,t=""){t=t?t+"__":"";const r={};for(const[n,s]of Object.entries(e))if(-1===M.indexOf(n))if(N(s))if("owner_user"===n){const e=H(s,n);Object.assign(r,e)}else"parent"===n&&"id"in s&&(r.parent_id=s.id);else a(s)&&(r[t+n]=s);return r}var B=Object.defineProperty,D=Object.defineProperties,$=Object.getOwnPropertyDescriptors,z=Object.getOwnPropertySymbols,G=Object.prototype.hasOwnProperty,J=Object.prototype.propertyIsEnumerable,Q=(e,t,r)=>t in e?B(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,W=(e,t)=>{for(var r in t||(t={}))G.call(t,r)&&Q(e,r,t[r]);if(z)for(var r of z(t))J.call(t,r)&&Q(e,r,t[r]);return e};class K{constructor(e){var t,r,n;this.connector=e,t=this,r="cache",n=new m,Q(t,"symbol"!=typeof r?r+"":r,n)}getOne(e,t){const r=new m,n={},s=(i=W({},t),D(i,$({cache:!1})));var i;"string"==typeof e?n.keyname=e:"number"==typeof e?n.id=e:N(e)&&(void 0!==e.id?n.id=e.id:(e.keyname&&(n.keyname=e.keyname),e.display_name&&(n.display_name=e.display_name)));return r.addFull("resource",(()=>"string"==typeof e?this._fetchResourceBy({keyname:e},s):"number"==typeof e?this._fetchResourceById(e,s):N(e)?this._fetchResourceBy(e,s):k.resolve(void 0)),n)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new U}))}getId(e,t){return"number"==typeof e?k.resolve(e):"string"==typeof e||N(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):k.resolve(void 0)}getIdOrFail(e,t){return this.getId(e,t).then((e=>{if(void 0===e)throw new Error;return e}))}getMany(e,t){return this._resourceCacheFilter(e).then((r=>{if(!r.length){const r={};return e.keyname?r.keyname=e.keyname:Object.assign(r,H(e)),this.connector.get("resource.search",t,W({serialization:"full"},r)).then((e=>{if(e)for(const t of e)this.cache.add("resource.item",k.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>e?this.getOne(e.resource.parent.id,t):k.resolve(void 0)))}getChildrenOf(e,t){return this.getIdOrFail(e).then((e=>this._getChildrenOf(e,t)))}update(e,t){return this.getId(e).then((e=>{if(void 0!==e)return this.connector.put("resource.item",{data:t},{id:e})}))}delete(e){return this.getId(e).then((e=>{if(void 0!==e)return this.connector.delete("resource.item",null,{id:e}).then((()=>{this._cleanResourceItemCache(e)}))}))}_getChildrenOf(e,t,r=[]){return this.connector.get("resource.collection",W({cache:!0},t),{parent:e}).then((e=>{const n=[];for(const s of e)this.cache.add("resource.item",k.resolve(s),{id:s.resource.id}),r.push(s),(null==t?void 0:t.recursive)&&s.resource.children&&n.push(this._getChildrenOf(s.resource.id,t,r));return n.length?k.all(n).then((()=>r)):r}))}_cleanResourceItemCache(e){return t=this,r=null,n=function*(){const t=this.cache.all(),r=[];for(const n of t){const t=n.options&&n.options.id;["resource.item","resource"].includes(n.key)&&void 0!==t&&("number"==typeof t?t===e&&r.push(n):(yield this.getId(t))===e&&r.push(n))}for(const e of r)this.cache.delete(e)},new Promise(((e,s)=>{var i=e=>{try{a(n.next(e))}catch(t){s(t)}},o=e=>{try{a(n.throw(e))}catch(t){s(t)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(i,o);a((n=n.apply(t,r)).next())}));var t,r,n}_fetchResourceById(e,t){return this.cache.add("resource.item",(()=>this.connector.get("resource.item",t,{id:e})),{id:e}).catch((e=>{if(!(e instanceof U))throw e}))}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(e){return k.all(this.cache.matchAll("resource.item")).then((t=>(t.filter((t=>{if(t)return e.keyname&&t.resource.keyname?e.keyname===t.resource.keyname:a(e.id)&&a(t.resource.id)?e.id===t.resource.id:h(e,t.resource)})),[])))}}const X=[];function V(e){X.push(e)}var Y=Object.defineProperty,Z=(e,t,r)=>(((e,t,r)=>{t in e?Y(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r})(e,"symbol"!=typeof t?t+"":t,r),r);class ee extends A{constructor(e){super(e),Z(this,"name","InsufficientPermissionsError"),Z(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,ee.prototype)}}const te=/\{ *([\w_-]+) *\}/g;function re(e,t){return e.replace(te,((e,r)=>{let n=t[r];if(void 0===n)throw new Error("No value provided for letiable "+e);return"function"==typeof n&&(n=n(t)),n}))}var ne=Object.defineProperty,se=(e,t,r)=>(((e,t,r)=>{t in e?ne(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r})(e,"symbol"!=typeof t?t+"":t,r),r);class ie extends Error{constructor(e){super(),se(this,"name","NetworkError"),Object.setPrototypeOf(this,ie.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}let oe;oe=(e,t,r={},n,s)=>{r.method=r.method||"GET";const i=new XMLHttpRequest;i.open(r.method||"GET",e,!0),"blob"===r.responseType&&(i.responseType=r.responseType);const o=()=>{try{return JSON.parse(i.responseText)}catch(e){return i.responseText}};i.onload=()=>{-1!==[401,403,404,422,500].indexOf(i.status)&&n(new A(o())),((e=!1)=>{const s=e?n:t;"blob"===r.responseType?s(i.response):i.responseText?s(o()):n({message:""})})()},i.onerror=t=>{n(0===i.status?new ie(e):t)},i.upload.onprogress=function(e){if(e.lengthComputable){r.onProgress&&r.onProgress(e.loaded/e.total*100,e)}};const a=r.headers;if(a)for(const u in a){const e=a[u];"string"==typeof e&&i.setRequestHeader(u,e)}let c;if(void 0!==r.withCredentials&&(i.withCredentials=r.withCredentials),r.file){if(c=new FormData,c.append("file",r.file),r.data)for(const u in r.data)c.append(u,c[u])}else c=r.data?"string"==typeof r.data?r.data:JSON.stringify(r.data):null;s&&s((()=>{i.abort()})),i.send(c)};var ae=Object.defineProperty,ce=Object.getOwnPropertySymbols,ue=Object.prototype.hasOwnProperty,he=Object.prototype.propertyIsEnumerable,le=(e,t,r)=>t in e?ae(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,fe=(e,t)=>{for(var r in t||(t={}))ue.call(t,r)&&le(e,r,t[r]);if(ce)for(var r of ce(t))he.call(t,r)&&le(e,r,t[r]);return e},pe=(e,t,r)=>(le(e,"symbol"!=typeof t?t+"":t,r),r);let de=0;class me{constructor(e){this.options=e,pe(this,"id",de++),pe(this,"emitter",new t),pe(this,"user"),pe(this,"resources"),pe(this,"routeStr","/api/component/pyramid/route"),pe(this,"activeRequests",[]),pe(this,"requestTransform");const r=function(e){return X.find((t=>{if(t.options.baseUrl===e.baseUrl){if(!e.auth)return!0;if(t.options.auth)return h(t.options.auth,e.auth)}}))}(e);if(r)return r;this.options.route&&(this.routeStr=this.options.route),this.options.requestTransform&&(this.requestTransform=this.options.requestTransform),this.resources=new K(this),V(this)}static create(e){return new this(e)}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,V(this)}connect(){const e=new m,t=this.options.auth;return e.add("route",(()=>new k(((e,r)=>{const n=()=>this.makeQuery(this.routeStr,{},{}).then((t=>{e(t)})).catch((e=>{r(e)}));if(t){const{login:e,password:s}=t;if(e&&s)return this._login({login:e,password:s}).then((()=>n())).catch((e=>r(e)))}return n()}))),{id:this.id,auth:t,baseUrl:this.options.baseUrl})}login(e,t){return this.logout(),V(this),this._login(e,t)}logout(){this.abort(),function(e){const t=X.indexOf(e);-1!==t&&X.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.emitter.emit("logout"),this.resources.cache.clean()}getUserInfo(e,t){if(this.user&&this.user.id)return k.resolve(this.user);e&&(this.options.auth=e);const r=fe({headers:this.getAuthorizationHeaders(e)},t);return this.makeQuery("/api/component/auth/current_user",{},r)}getAuthorizationHeaders(e){const t=this.makeClientId(e);if(t)return{Authorization:"Basic "+t}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,n=unescape(encodeURIComponent(`${t}:${r}`));return window.btoa(n)}}abort(){for(const e of this.activeRequests)e.cancel();this.activeRequests=[]}getActiveApiRequests(){return[...this.activeRequests]}apiRequest(e,t={},r={}){var n,s;const{method:i,headers:o,withCredentials:a,responseType:c}=r,u=l(t=null!=(n=r.params)?n:t),h=()=>function(e){const t=e.params;return new k(((r,n)=>{e.connector.connect().then((s=>{let i=s&&s[e.name];if(i){i=[...i];let s=i.shift();if(i.length){const e={};for(let r=0;r<i.length;r++){const n=i[r];if(e[r]="{"+n+"}",void 0===t[n])throw new Error("`"+n+"` url api argument is not specified")}s&&(s=re(s,e))}if(t){const e=[],r=t.paramList;Array.isArray(r)&&(delete t.paramList,r.forEach((t=>{e.push(`${t[0]}=${t[1]}`)})));for(const n in t)-1===i.indexOf(n)&&e.push(`${n}=${t[n]}`);e.length&&(s=s+"?"+e.join("&"))}s?r(e.connector.makeQuery(s,t,e.requestOptions)):n(new Error("request url is not set"))}else r(void 0)})).catch((e=>{n(e)}))}))}({name:e,params:u,requestOptions:r,connector:this});if(r.cache&&"GET"===i){return(new m).add(e,h,fe({params:u},l({headers:o,withCredentials:a,responseType:c,baseUrl:this.options.baseUrl,userId:null==(s=this.user)?void 0:s.id})))}return h()}post(e,t,r){return(t=t||{}).method="POST",this.apiRequest(e,r,t)}get(e,t,r){return(t=t||{}).method="GET",this.apiRequest(e,r,t)}patch(e,t,r){return(t=t||{}).method="PATCH",this.apiRequest(e,r,t)}put(e,t,r){return(t=t||{}).method="PUT",this.apiRequest(e,r,t)}delete(e,t,r){return(t=t||{}).method="DELETE",this.apiRequest(e,r,t)}makeQuery(e,t,r={}){if(e=(this.options.baseUrl?this.options.baseUrl:"")+e)return t&&(e=re(e,t)),e=encodeURI(function(e){return e.replace(/([^:]\/)\/+/g,"$1")}(e)),this._loadData(e,r);throw new Error("Empty `url` not allowed")}getResource(e,t){return this.resources.getOne(e,t)}getResourceOrFail(e,t){return this.resources.getOneOrFail(e,t)}getResourceBy(e){return this.resources.getOne(e)}getResourceByKeyname(e){return this.resources.getOne(e)}getResourceById(e){return this.resources.getOne(e)}getResourceId(e,t){return this.resources.getId(e,t)}getResourceIdOrFail(e,t){return this.resources.getIdOrFail(e,t)}getResourcesBy(e,t){return this.resources.getMany(e,t)}getResourceParent(e,t){return this.resources.getParent(e,t)}getResourceChildren(e,t){return this.resources.getChildrenOf(e,t)}updateResource(e,t){return this.resources.update(e,t)}deleteResource(e){return this.resources.delete(e)}_loadData(e,t){if(t.responseType=t.responseType||"json",t.signal&&t.signal.aborted)return k.reject(new k.CancelError);const r=new k(((r,n,s)=>{if(this.user&&((t=t||{}).headers=fe(fe({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}oe(e,r,t,n,s)})).then((e=>(this._cleanActiveRequests(r),e))).catch((e=>{if(this._cleanActiveRequests(r),e instanceof k.CancelError);else{const t=this._handleHttpError(e);if(t)throw t}}));return t.signal&&"function"==typeof t.signal.addEventListener&&t.signal.addEventListener("abort",(()=>{r.cancel(),this._cleanActiveRequests(r)})),this.activeRequests.push(r),r}_login(e,t){return this.getUserInfo(e,t).then((e=>(this.user=e,this.emitter.emit("login",e),e))).catch((e=>{throw this.emitter.emit("login:error",e),e}))}_cleanActiveRequests(e){const t=this.activeRequests.indexOf(e);-1!==t&&this.activeRequests.splice(t,1)}_handleHttpError(e){if(e&&e instanceof A){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new U(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new ee(e)}return e}}pe(me,"errors",{NgwError:A,ResourceNotFoundError:U});export{me as default};
2
2
  //# sourceMappingURL=ngw-connector.esm-browser.prod.js.map