@nextgis/ngw-connector 3.0.0-alpha.18 → 3.0.0-alpha.19

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.
package/lib/index.d.ts CHANGED
@@ -1384,7 +1384,8 @@ declare class NgwConnectorExtended extends NgwConnector {
1384
1384
  */
1385
1385
  interface LayerLegend {
1386
1386
  layerId: string;
1387
- legend: LegendSymbol[];
1387
+ legend?: LegendSymbol[];
1388
+ resourceId: number;
1388
1389
  setSymbolRender?: (symbolIndex: number, status: boolean) => void;
1389
1390
  legendSymbols?: LegendSymbolsEnum;
1390
1391
  }
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.17; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.18; author: NextGIS */
2
2
  'use strict';
3
3
 
4
4
  var utils = require('@nextgis/utils');
@@ -70,7 +70,7 @@ async function apiRequest(opt) {
70
70
 
71
71
  var pkg = {
72
72
  name: "@nextgis/ngw-connector",
73
- version: "3.0.0-alpha.17",
73
+ version: "3.0.0-alpha.18",
74
74
  _priority: 12,
75
75
  description: "A lightweight HTTP client optimized for use with NextGIS Web API",
76
76
  main: "index.js",
@@ -1,2 +1,2 @@
1
- "use strict";var e=require("@nextgis/utils"),t=require("@nextgis/cache"),r=require("events");const s=/\{ *([\w_-]+) *\}/g;function n(e,t){return e.replace(s,((e,r)=>{let s=t[r];if(void 0===s)throw new Error("No value provided for letiable "+e);return"function"==typeof s&&(s=s(t)),s}))}var o="3.0.0-alpha.17";class i extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var a=Object.defineProperty,c=(e,t,r)=>((e,t,r)=>t in e?a(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class u extends Error{constructor(e){super(),c(this,"name","NgwError"),c(this,"title"),c(this,"message"),c(this,"detail"),c(this,"exception"),c(this,"status_code"),c(this,"data"),c(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,u.prototype)}}var l=Object.defineProperty,h=(e,t,r)=>((e,t,r)=>t in e?l(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class d extends u{constructor(e){super(e),h(this,"name","InsufficientPermissionsError"),h(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,d.prototype)}}var p=Object.defineProperty,f=(e,t,r)=>((e,t,r)=>t in e?p(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let m=class e extends u{constructor(t){super(t),f(this,"message","There is no response from the server or problem connecting to server."),f(this,"title","Network error"),f(this,"detail","Check network connectivity and try again later."),Object.setPrototypeOf(this,e.prototype)}};function g(t){return!!e.isObject(t)&&(t.status_code&&t.exception&&t.title)}var y=Object.defineProperty,w=(e,t,r)=>((e,t,r)=>t in e?y(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class b extends Error{constructor(e){super(),w(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}`}}var v=Object.defineProperty,O=(e,t,r)=>((e,t,r)=>t in e?v(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class E extends u{constructor(e){super(e),O(this,"name","ResourceNotFoundError"),O(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,E.prototype)}}var R=Object.freeze({__proto__:null,AbortError:i,InsufficientPermissionsError:d,NetworkError:b,NetworksResponseError:m,NgwError:u,ResourceNotFoundError:E,extractError:function(t){if(e.isObject(t))return t.name&&t.message&&t.title?{title:t.title,message:t.message,detail:t.detail||null,data:t.data&&t.data.data?t.data.data:null}:!t.exception||void 0!==t.status&&0!==t.status&&void 0!==t.data?{title:"string"==typeof t.title?t.title:"Unexpected error",message:"string"==typeof t.message?t.message:"Something went wrong."}:new m({title:t.title,status_code:t.status_code,exception:t.exception})},isError:g}),j=Object.defineProperty,x=(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);class q extends Error{constructor(e){super(e||"Something went wrong."),x(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,q)}}class C extends q{constructor(e){super(e||"There is no response from the server or problem connecting to server."),x(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class _ extends q{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class I extends q{constructor(e){super(e.message),x(this,"detail"),x(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class P extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),x(this,"title"),x(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,P)}}class k extends P{constructor(e){super("Long-running request was cancelled.",e)}}class U extends P{constructor(e){super(void 0,e)}}async function T(e){const t=await async function(e){try{return e.json()}catch{throw new Error}}(e);let r=t.delay_ms;const s=void 0!==t.retry_ms?t.retry_ms:2e3,n=`/api/lunkwill/${t.id}/summary`,o=`/api/lunkwill/${t.id}/response`,i=e=>new Promise((t=>setTimeout(t,e)));let a=!1,c=!1;for(;!c;){let e,t;await i(a?s:r),a=!1;try{e=await fetch(n,{credentials:"same-origin"}),t=await e.json()}catch{a=!0;continue}switch(t.status){case void 0:throw new P(void 0,t);case"ready":c=!0;break;case"cancelled":throw new k(t);case"failed":throw new U(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new P(void 0,t)}}return o}const $=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function A(e,t){let r="";return void 0!==t&&(r="?"+function(e){const t=[];for(const[r,s]of Object.entries(e))if("string"==typeof s||"number"==typeof s||"boolean"==typeof s)t.push(`${r}=${encodeURIComponent(s)}`);else if(Array.isArray(s))t.push(`${r}=${s.map(encodeURIComponent).join(",")}`);else for(const[e,n]of Object.entries(s)){const s=`${r}[${encodeURIComponent(e)}]`;if("string"==typeof n||"number"==typeof n||"boolean"==typeof n)t.push(`${s}=${encodeURIComponent(n)}`);else if(Array.isArray(n)){const e=n.map(encodeURIComponent);t.push(`${s}=${e.join(",")}`)}}return t.join("&")}(t)),e+r}async function S(t,r,s){var n,o;const{withCredentials:i,responseType:a,cacheProps:c,cacheName:u,lunkwill:l,cache:h,query:d,json:p,...f}={method:"GET",credentials:"same-origin",headers:{},...r};f.method=null==(n=f.method)?void 0:n.toUpperCase(),i&&(f.credentials="include");let m=!1;void 0!==l&&(l.toHeaders(f.headers||{}),m=!0);const g=!!f.lunkwillReturnUrl;if(delete f.lunkwillReturnUrl,void 0!==p){f.body=JSON.stringify(p);const e=f.headers||{};e["Content-Type"]="application/json",f.headers=e}const y=A(t,d),w=async()=>{let e;try{e=await fetch(y,f)}catch(n){if(f.signal&&f.signal.aborted)throw n;throw new C}if(m&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=await T(e);if(g)return t;e=await async function(e){try{return await window.fetch(e,{credentials:"same-origin"})}catch{throw new C}}(t)}const t=e.headers.get("content-type"),r=t&&(t.includes("application/json")||t.includes("application/vnd.lunkwill.request-summary+json"));let s;try{const n=t&&$.test(t);if("blob"===a||n)s=await e.blob();else{if(!r)throw new _;s=await e.json()}}catch(n){if("AbortError"===n.name||n instanceof _)throw n;throw new _}if(400<=e.status&&e.status<=599)throw new I(s);return s};if(s)if("GET"===(null==(o=f.method)?void 0:o.toUpperCase())){if(!1!==h){const t=c||{...e.objectRemoveEmpty({withCredentials:i,responseType:a})};return s.add(u||y,w,{props:t,expirationTime:h?void 0:500})}}else{["api/feature_layer/identify"].every((e=>!t.includes(e)))&&s.clean()}return w()}function N(t,r,s,...n){const[o,...i]=s[t],a=n[0];let c;if(void 0===a)c=[];else if("object"==typeof a&&null!==a){if(n.length>1)throw new Error("Too many arguments for route(name, object)!");c=[];for(const[e,t]of Object.entries(a))c[i.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return e.fixUrlStr(r+o.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),s=c[r];if(void 0===s){throw new Error(`Undefined parameter ${r} in "${o}".`)}return String(s)})))}function B(e){return"[object Object]"===Object.prototype.toString.call(e)}let F;{const e=require("url"),t=require("http"),r=require("https"),s=require("form-data"),n=s=>({"http:":t,"https:":r}[e.parse(s).protocol||"https:"]);F=(e,t,r={},o,i)=>{const{file:a,headers:c,method:u,data:l,responseType:h}=r;return new Promise(((t,r)=>{const o=n(e);if(!o)throw new Error(`Given URL '${e}' is not correct`);{const n={headers:c||{},method:u},d="string"==typeof l?l:JSON.stringify(l);let p,f=a;if(a){const e={};if(B(a)&&"file"in a&&("filename"in a||"name"in a)){const{file:t,name:r,...s}=a;r&&!s.filename&&(s.filename=r),Object.assign(e,s),f=t}if(p=new s,p.append("file",f,e),l)for(const t in l)p.append(t,l[t]);Object.assign(n.headers,{...p.getHeaders()})}void 0!==d&&Object.assign(n.headers,{"content-type":"application/json","content-length":Buffer.byteLength(d)});const m=o.request(e,n,(e=>{let s="";e.on("data",(e=>{s+=e})),e.on("end",(()=>{if(s)if("blob"===h)t(s);else{let n;try{n=JSON.parse(s),n&&n.status_code&&n.status_code&&r(n.message)}catch(e){r(e)}void 0!==n&&(g(n)?r("extractError(json)"):t(n))}r("no data")}))}));p&&p.pipe(m),m.on("error",(e=>{r(e)})),d&&m.write(d),i((()=>{m.abort()})),m.end()}})).then((e=>(t&&t(e),e))).catch((e=>{if(!o)throw new Error(e);o(e)}))}}const L=[];function G(e){L.push(e)}var H=Object.defineProperty,z=(e,t,r)=>((e,t,r)=>t in e?H(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let D=0,Q=0;class J{constructor(s){this.options=s,z(this,"id",D++),z(this,"emitter",new r.EventEmitter),z(this,"user"),z(this,"cache"),z(this,"withCredentials"),z(this,"routeCache"),z(this,"client",`NextGIS-NGW-Connector/${o}`),z(this,"routeStr","/api/component/pyramid/route"),z(this,"activeRequests",{}),z(this,"requestTransform");const n=function(t){return L.find((r=>{if(r.options.baseUrl===t.baseUrl){if(!t.auth)return!0;if(r.options.auth)return e.objectDeepEqual(r.options.auth,t.auth)}}))}(s);if(this.cache=new t({namespace:s.cacheId}),this.routeCache=new t({namespace:"routecache"}),n)return n;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),G(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,G(this)}async connect({signal:e}={}){const t=this.options.auth;if(t){const{login:e,password:r}=t;e&&r&&await this._login({login:e,password:r})}const r=`${this.routeStr}?client=${this.client}`;return this.routeCache.add(this.options.baseUrl||String(this.id),(()=>this.makeQuery(r,null,{signal:e,cache:!1})))}login(e,t){return this.logout(),G(this),this._login(e,t)}logout(){this.abort(),function(e){const t=L.indexOf(e);-1!==t&&L.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.routeCache.clean(),this.clearCache(),this.emitter.emit("logout")}async getUserInfo(e,t){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r={headers:this.getAuthorizationHeaders(e),cache:!0,...t};return this.makeQuery("/api/component/auth/current_user",{},r)}getAuthorizationHeaders(e){const t=this.makeClientId(e);return t?{Authorization:`Basic ${t}`}:{}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,s=[t,r].map(encodeURIComponent).join(":");return Buffer.from(s).toString("base64")}}abort(){for(const e of Object.values(this.activeRequests))e.abort();this.activeRequests={}}getActiveApiRequests(){return{...this.activeRequests}}route(t,...r){return function(t,r,...s){const n={url:async e=>{var n;const o=await r.connect();return A(N(t,null!=(n=r.options.baseUrl)?n:"",o,...s),null==e?void 0:e.query)}},o=["get","post","put","delete","patch"];for(const a of o)n[a]=n=>{var o;const{headers:c,...u}=n||{};if(null==(o=null==n?void 0:n.signal)?void 0:o.aborted)throw new i;return r.connect().then((n=>{var o;return S(N(t,null!=(o=r.options.baseUrl)?o:"",n,...s),{headers:e.objectRemoveEmpty({...r.getAuthorizationHeaders(),...null!=c?c:{}}),...u,method:a},r.cache)}))};return n}(t,this,...r)}async makeQuery(t,r,s={}){var o;if(!(t=(this.options.baseUrl?this.options.baseUrl:"")+t))throw new Error("Empty `url` not allowed");if(r){const{paramList:e,...s}=r;t=n(t,s)}t=encodeURI(e.fixUrlStr(t)),s={withCredentials:this.withCredentials,...s};const{cache:a,signal:c,method:u="GET",headers:l,cacheName:h,cacheProps:d,responseType:p,withCredentials:f}=s,m=new AbortController,g=m.signal;if(c){if(c.aborted)throw new i;c.addEventListener("abort",(()=>{m.abort()}))}s.signal=g;const y=async()=>{const e=Q++;this.activeRequests[e]=m;try{return this._loadData(t,s)}finally{this._cleanActiveRequest(e)}};if("GET"===u&&!1!==a){const s=d||{...e.objectRemoveEmpty({headers:l,withCredentials:f,responseType:p,baseUrl:this.options.baseUrl,userId:null==(o=this.user)?void 0:o.id}),params:r};return this.cache.add(h||t,y,{props:s,expirationTime:a?void 0:500})}return y()}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,s)=>{var n;if(this.user&&((t=t||{}).headers={...this.getAuthorizationHeaders(),...t.headers}),this.requestTransform){const[r,s]=this.requestTransform(e,t);e=r,t=s}let o;F(e,r,t,s,(e=>{o=e})),null==(n=t.signal)||n.addEventListener("abort",(()=>{void 0!==o&&o(),s(new i)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}async _login(e,t){try{const r=await this.getUserInfo(e,t);return this.user=r,this.emitter.emit("login",r),r}catch(r){throw this.emitter.emit("login:error",r),r}}_cleanActiveRequest(e){delete this.activeRequests[e]}_handleHttpError(e){if(e&&e instanceof u){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new E(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new d(e)}return e}}z(J,"errors",R);const M=["description"];function W(t,r=""){r=r?r+"__":"";const s={};for(const[n,o]of Object.entries(t))if(-1===M.indexOf(n))if(B(o))if("owner_user"===n){const e=W(o,n);Object.assign(s,e)}else"parent"===n&&"id"in o&&(s.parent_id=o.id);else e.defined(o)&&(s[r+n]=o);return s}var K=Object.defineProperty,V=(e,t,r)=>((e,t,r)=>t in e?K(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class X{constructor({connector:e,cacheId:r}){V(this,"cache"),V(this,"connector"),this.connector=e,this.cache=new t({namespace:r})}getOne(e,t){const r={...t};return"string"==typeof e||"number"==typeof e||B(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):B(e)?this._fetchResourceBy(e,r):Promise.resolve(void 0)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new E}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||B(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):Promise.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,W(e)),this.connector.route("resource.search").get({...t,query:{serialization:"full",...r}}).then((e=>{if((null==t?void 0:t.cache)&&e)for(const t of e)this.cache.add("resource.item",Promise.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>{var r,s;return(null==(s=null==(r=null==e?void 0:e.resource)?void 0:r.parent)?void 0:s.id)?this.getOne(e.resource.parent.id,t):Promise.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)}))}))}async _getChildrenOf(e,t,r=[]){let s;s="string"==typeof e?await this.getId(e,t):"object"==typeof e?e.id:e;const n=await this.connector.route("resource.collection").get({...t,query:{parent:s}}),o=[];for(const i of n)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(i),{id:i.resource.id}),r.push(i),(null==t?void 0:t.recursive)&&i.resource.children&&o.push(this._getChildrenOf(i.resource.id,t,r));return o.length?Promise.all(o).then((()=>r)):r}async _cleanResourceItemCache(e){var t;const r=this.cache.all(),s=[];for(const n of r){const r=null==(t=n.props)?void 0:t.id;if(["resource.item","resource"].includes(n.key)&&void 0!==r)if("number"==typeof r)r===e&&s.push(n);else{await this.getId(r)===e&&s.push(n)}}for(const n of s)this.cache.delete(n)}_fetchResourceById(e,t){return this.connector.route("resource.item",{id:e}).get(t)}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(t){return Promise.all(this.cache.matchAll("resource.item")).then((r=>(r.filter((r=>{if(r)return t.keyname&&r.resource.keyname?t.keyname===r.resource.keyname:e.defined(t.id)&&e.defined(r.resource.id)?t.id===r.resource.id:e.objectDeepEqual(t,r.resource)})),[])))}}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,t+"",r);module.exports=class extends J{constructor(e){super(e),Z(this,"resources"),this.resources=new X({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(t,r={},s={}){var o;r=null!=(o=s.params)?o:r;return async function(e){const{params:t,name:r,connector:s,requestOptions:o}=e,i=await s.connect();let a=i&&i[r];if(a){a=[...a];let e=a.shift();if(a.length){const r={};for(let e=0;e<a.length;e++){const s=a[e];if(r[e]=`{${s}}`,void 0===t[s])throw new Error(`\`${s}\` URL API argument is not specified`)}e&&(e=n(e,r))}if(t){const r=[],s=t.paramList;Array.isArray(s)&&s.forEach((([e,t])=>{r.push(`${e}=${t}`)}));for(const e in t)-1===a.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return s.makeQuery(e,t,{cacheName:r,...o});throw new Error("Request URL is not set")}}({name:t,params:e.objectRemoveEmpty(r),requestOptions:s,connector:this})}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)}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)}};
1
+ "use strict";var e=require("@nextgis/utils"),t=require("@nextgis/cache"),r=require("events");const s=/\{ *([\w_-]+) *\}/g;function n(e,t){return e.replace(s,((e,r)=>{let s=t[r];if(void 0===s)throw new Error("No value provided for letiable "+e);return"function"==typeof s&&(s=s(t)),s}))}var o="3.0.0-alpha.18";class i extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var a=Object.defineProperty,c=(e,t,r)=>((e,t,r)=>t in e?a(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class u extends Error{constructor(e){super(),c(this,"name","NgwError"),c(this,"title"),c(this,"message"),c(this,"detail"),c(this,"exception"),c(this,"status_code"),c(this,"data"),c(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,u.prototype)}}var l=Object.defineProperty,h=(e,t,r)=>((e,t,r)=>t in e?l(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class d extends u{constructor(e){super(e),h(this,"name","InsufficientPermissionsError"),h(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,d.prototype)}}var p=Object.defineProperty,f=(e,t,r)=>((e,t,r)=>t in e?p(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let m=class e extends u{constructor(t){super(t),f(this,"message","There is no response from the server or problem connecting to server."),f(this,"title","Network error"),f(this,"detail","Check network connectivity and try again later."),Object.setPrototypeOf(this,e.prototype)}};function g(t){return!!e.isObject(t)&&(t.status_code&&t.exception&&t.title)}var y=Object.defineProperty,w=(e,t,r)=>((e,t,r)=>t in e?y(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class b extends Error{constructor(e){super(),w(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}`}}var v=Object.defineProperty,O=(e,t,r)=>((e,t,r)=>t in e?v(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class E extends u{constructor(e){super(e),O(this,"name","ResourceNotFoundError"),O(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,E.prototype)}}var R=Object.freeze({__proto__:null,AbortError:i,InsufficientPermissionsError:d,NetworkError:b,NetworksResponseError:m,NgwError:u,ResourceNotFoundError:E,extractError:function(t){if(e.isObject(t))return t.name&&t.message&&t.title?{title:t.title,message:t.message,detail:t.detail||null,data:t.data&&t.data.data?t.data.data:null}:!t.exception||void 0!==t.status&&0!==t.status&&void 0!==t.data?{title:"string"==typeof t.title?t.title:"Unexpected error",message:"string"==typeof t.message?t.message:"Something went wrong."}:new m({title:t.title,status_code:t.status_code,exception:t.exception})},isError:g}),j=Object.defineProperty,x=(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);class q extends Error{constructor(e){super(e||"Something went wrong."),x(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,q)}}class C extends q{constructor(e){super(e||"There is no response from the server or problem connecting to server."),x(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class _ extends q{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class I extends q{constructor(e){super(e.message),x(this,"detail"),x(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class P extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),x(this,"title"),x(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,P)}}class k extends P{constructor(e){super("Long-running request was cancelled.",e)}}class U extends P{constructor(e){super(void 0,e)}}async function T(e){const t=await async function(e){try{return e.json()}catch{throw new Error}}(e);let r=t.delay_ms;const s=void 0!==t.retry_ms?t.retry_ms:2e3,n=`/api/lunkwill/${t.id}/summary`,o=`/api/lunkwill/${t.id}/response`,i=e=>new Promise((t=>setTimeout(t,e)));let a=!1,c=!1;for(;!c;){let e,t;await i(a?s:r),a=!1;try{e=await fetch(n,{credentials:"same-origin"}),t=await e.json()}catch{a=!0;continue}switch(t.status){case void 0:throw new P(void 0,t);case"ready":c=!0;break;case"cancelled":throw new k(t);case"failed":throw new U(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new P(void 0,t)}}return o}const $=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function A(e,t){let r="";return void 0!==t&&(r="?"+function(e){const t=[];for(const[r,s]of Object.entries(e))if("string"==typeof s||"number"==typeof s||"boolean"==typeof s)t.push(`${r}=${encodeURIComponent(s)}`);else if(Array.isArray(s))t.push(`${r}=${s.map(encodeURIComponent).join(",")}`);else for(const[e,n]of Object.entries(s)){const s=`${r}[${encodeURIComponent(e)}]`;if("string"==typeof n||"number"==typeof n||"boolean"==typeof n)t.push(`${s}=${encodeURIComponent(n)}`);else if(Array.isArray(n)){const e=n.map(encodeURIComponent);t.push(`${s}=${e.join(",")}`)}}return t.join("&")}(t)),e+r}async function S(t,r,s){var n,o;const{withCredentials:i,responseType:a,cacheProps:c,cacheName:u,lunkwill:l,cache:h,query:d,json:p,...f}={method:"GET",credentials:"same-origin",headers:{},...r};f.method=null==(n=f.method)?void 0:n.toUpperCase(),i&&(f.credentials="include");let m=!1;void 0!==l&&(l.toHeaders(f.headers||{}),m=!0);const g=!!f.lunkwillReturnUrl;if(delete f.lunkwillReturnUrl,void 0!==p){f.body=JSON.stringify(p);const e=f.headers||{};e["Content-Type"]="application/json",f.headers=e}const y=A(t,d),w=async()=>{let e;try{e=await fetch(y,f)}catch(n){if(f.signal&&f.signal.aborted)throw n;throw new C}if(m&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=await T(e);if(g)return t;e=await async function(e){try{return await window.fetch(e,{credentials:"same-origin"})}catch{throw new C}}(t)}const t=e.headers.get("content-type"),r=t&&(t.includes("application/json")||t.includes("application/vnd.lunkwill.request-summary+json"));let s;try{const n=t&&$.test(t);if("blob"===a||n)s=await e.blob();else{if(!r)throw new _;s=await e.json()}}catch(n){if("AbortError"===n.name||n instanceof _)throw n;throw new _}if(400<=e.status&&e.status<=599)throw new I(s);return s};if(s)if("GET"===(null==(o=f.method)?void 0:o.toUpperCase())){if(!1!==h){const t=c||{...e.objectRemoveEmpty({withCredentials:i,responseType:a})};return s.add(u||y,w,{props:t,expirationTime:h?void 0:500})}}else{["api/feature_layer/identify"].every((e=>!t.includes(e)))&&s.clean()}return w()}function N(t,r,s,...n){const[o,...i]=s[t],a=n[0];let c;if(void 0===a)c=[];else if("object"==typeof a&&null!==a){if(n.length>1)throw new Error("Too many arguments for route(name, object)!");c=[];for(const[e,t]of Object.entries(a))c[i.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return e.fixUrlStr(r+o.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),s=c[r];if(void 0===s){throw new Error(`Undefined parameter ${r} in "${o}".`)}return String(s)})))}function B(e){return"[object Object]"===Object.prototype.toString.call(e)}let F;{const e=require("url"),t=require("http"),r=require("https"),s=require("form-data"),n=s=>({"http:":t,"https:":r}[e.parse(s).protocol||"https:"]);F=(e,t,r={},o,i)=>{const{file:a,headers:c,method:u,data:l,responseType:h}=r;return new Promise(((t,r)=>{const o=n(e);if(!o)throw new Error(`Given URL '${e}' is not correct`);{const n={headers:c||{},method:u},d="string"==typeof l?l:JSON.stringify(l);let p,f=a;if(a){const e={};if(B(a)&&"file"in a&&("filename"in a||"name"in a)){const{file:t,name:r,...s}=a;r&&!s.filename&&(s.filename=r),Object.assign(e,s),f=t}if(p=new s,p.append("file",f,e),l)for(const t in l)p.append(t,l[t]);Object.assign(n.headers,{...p.getHeaders()})}void 0!==d&&Object.assign(n.headers,{"content-type":"application/json","content-length":Buffer.byteLength(d)});const m=o.request(e,n,(e=>{let s="";e.on("data",(e=>{s+=e})),e.on("end",(()=>{if(s)if("blob"===h)t(s);else{let n;try{n=JSON.parse(s),n&&n.status_code&&n.status_code&&r(n.message)}catch(e){r(e)}void 0!==n&&(g(n)?r("extractError(json)"):t(n))}r("no data")}))}));p&&p.pipe(m),m.on("error",(e=>{r(e)})),d&&m.write(d),i((()=>{m.abort()})),m.end()}})).then((e=>(t&&t(e),e))).catch((e=>{if(!o)throw new Error(e);o(e)}))}}const L=[];function G(e){L.push(e)}var H=Object.defineProperty,z=(e,t,r)=>((e,t,r)=>t in e?H(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let D=0,Q=0;class J{constructor(s){this.options=s,z(this,"id",D++),z(this,"emitter",new r.EventEmitter),z(this,"user"),z(this,"cache"),z(this,"withCredentials"),z(this,"routeCache"),z(this,"client",`NextGIS-NGW-Connector/${o}`),z(this,"routeStr","/api/component/pyramid/route"),z(this,"activeRequests",{}),z(this,"requestTransform");const n=function(t){return L.find((r=>{if(r.options.baseUrl===t.baseUrl){if(!t.auth)return!0;if(r.options.auth)return e.objectDeepEqual(r.options.auth,t.auth)}}))}(s);if(this.cache=new t({namespace:s.cacheId}),this.routeCache=new t({namespace:"routecache"}),n)return n;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),G(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,G(this)}async connect({signal:e}={}){const t=this.options.auth;if(t){const{login:e,password:r}=t;e&&r&&await this._login({login:e,password:r})}const r=`${this.routeStr}?client=${this.client}`;return this.routeCache.add(this.options.baseUrl||String(this.id),(()=>this.makeQuery(r,null,{signal:e,cache:!1})))}login(e,t){return this.logout(),G(this),this._login(e,t)}logout(){this.abort(),function(e){const t=L.indexOf(e);-1!==t&&L.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.routeCache.clean(),this.clearCache(),this.emitter.emit("logout")}async getUserInfo(e,t){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r={headers:this.getAuthorizationHeaders(e),cache:!0,...t};return this.makeQuery("/api/component/auth/current_user",{},r)}getAuthorizationHeaders(e){const t=this.makeClientId(e);return t?{Authorization:`Basic ${t}`}:{}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,s=[t,r].map(encodeURIComponent).join(":");return Buffer.from(s).toString("base64")}}abort(){for(const e of Object.values(this.activeRequests))e.abort();this.activeRequests={}}getActiveApiRequests(){return{...this.activeRequests}}route(t,...r){return function(t,r,...s){const n={url:async e=>{var n;const o=await r.connect();return A(N(t,null!=(n=r.options.baseUrl)?n:"",o,...s),null==e?void 0:e.query)}},o=["get","post","put","delete","patch"];for(const a of o)n[a]=n=>{var o;const{headers:c,...u}=n||{};if(null==(o=null==n?void 0:n.signal)?void 0:o.aborted)throw new i;return r.connect().then((n=>{var o;return S(N(t,null!=(o=r.options.baseUrl)?o:"",n,...s),{headers:e.objectRemoveEmpty({...r.getAuthorizationHeaders(),...null!=c?c:{}}),...u,method:a},r.cache)}))};return n}(t,this,...r)}async makeQuery(t,r,s={}){var o;if(!(t=(this.options.baseUrl?this.options.baseUrl:"")+t))throw new Error("Empty `url` not allowed");if(r){const{paramList:e,...s}=r;t=n(t,s)}t=encodeURI(e.fixUrlStr(t)),s={withCredentials:this.withCredentials,...s};const{cache:a,signal:c,method:u="GET",headers:l,cacheName:h,cacheProps:d,responseType:p,withCredentials:f}=s,m=new AbortController,g=m.signal;if(c){if(c.aborted)throw new i;c.addEventListener("abort",(()=>{m.abort()}))}s.signal=g;const y=async()=>{const e=Q++;this.activeRequests[e]=m;try{return this._loadData(t,s)}finally{this._cleanActiveRequest(e)}};if("GET"===u&&!1!==a){const s=d||{...e.objectRemoveEmpty({headers:l,withCredentials:f,responseType:p,baseUrl:this.options.baseUrl,userId:null==(o=this.user)?void 0:o.id}),params:r};return this.cache.add(h||t,y,{props:s,expirationTime:a?void 0:500})}return y()}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,s)=>{var n;if(this.user&&((t=t||{}).headers={...this.getAuthorizationHeaders(),...t.headers}),this.requestTransform){const[r,s]=this.requestTransform(e,t);e=r,t=s}let o;F(e,r,t,s,(e=>{o=e})),null==(n=t.signal)||n.addEventListener("abort",(()=>{void 0!==o&&o(),s(new i)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}async _login(e,t){try{const r=await this.getUserInfo(e,t);return this.user=r,this.emitter.emit("login",r),r}catch(r){throw this.emitter.emit("login:error",r),r}}_cleanActiveRequest(e){delete this.activeRequests[e]}_handleHttpError(e){if(e&&e instanceof u){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new E(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new d(e)}return e}}z(J,"errors",R);const M=["description"];function W(t,r=""){r=r?r+"__":"";const s={};for(const[n,o]of Object.entries(t))if(-1===M.indexOf(n))if(B(o))if("owner_user"===n){const e=W(o,n);Object.assign(s,e)}else"parent"===n&&"id"in o&&(s.parent_id=o.id);else e.defined(o)&&(s[r+n]=o);return s}var K=Object.defineProperty,V=(e,t,r)=>((e,t,r)=>t in e?K(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class X{constructor({connector:e,cacheId:r}){V(this,"cache"),V(this,"connector"),this.connector=e,this.cache=new t({namespace:r})}getOne(e,t){const r={...t};return"string"==typeof e||"number"==typeof e||B(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):B(e)?this._fetchResourceBy(e,r):Promise.resolve(void 0)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new E}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||B(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):Promise.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,W(e)),this.connector.route("resource.search").get({...t,query:{serialization:"full",...r}}).then((e=>{if((null==t?void 0:t.cache)&&e)for(const t of e)this.cache.add("resource.item",Promise.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>{var r,s;return(null==(s=null==(r=null==e?void 0:e.resource)?void 0:r.parent)?void 0:s.id)?this.getOne(e.resource.parent.id,t):Promise.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)}))}))}async _getChildrenOf(e,t,r=[]){let s;s="string"==typeof e?await this.getId(e,t):"object"==typeof e?e.id:e;const n=await this.connector.route("resource.collection").get({...t,query:{parent:s}}),o=[];for(const i of n)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(i),{id:i.resource.id}),r.push(i),(null==t?void 0:t.recursive)&&i.resource.children&&o.push(this._getChildrenOf(i.resource.id,t,r));return o.length?Promise.all(o).then((()=>r)):r}async _cleanResourceItemCache(e){var t;const r=this.cache.all(),s=[];for(const n of r){const r=null==(t=n.props)?void 0:t.id;if(["resource.item","resource"].includes(n.key)&&void 0!==r)if("number"==typeof r)r===e&&s.push(n);else{await this.getId(r)===e&&s.push(n)}}for(const n of s)this.cache.delete(n)}_fetchResourceById(e,t){return this.connector.route("resource.item",{id:e}).get(t)}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(t){return Promise.all(this.cache.matchAll("resource.item")).then((r=>(r.filter((r=>{if(r)return t.keyname&&r.resource.keyname?t.keyname===r.resource.keyname:e.defined(t.id)&&e.defined(r.resource.id)?t.id===r.resource.id:e.objectDeepEqual(t,r.resource)})),[])))}}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,t+"",r);module.exports=class extends J{constructor(e){super(e),Z(this,"resources"),this.resources=new X({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(t,r={},s={}){var o;r=null!=(o=s.params)?o:r;return async function(e){const{params:t,name:r,connector:s,requestOptions:o}=e,i=await s.connect();let a=i&&i[r];if(a){a=[...a];let e=a.shift();if(a.length){const r={};for(let e=0;e<a.length;e++){const s=a[e];if(r[e]=`{${s}}`,void 0===t[s])throw new Error(`\`${s}\` URL API argument is not specified`)}e&&(e=n(e,r))}if(t){const r=[],s=t.paramList;Array.isArray(s)&&s.forEach((([e,t])=>{r.push(`${e}=${t}`)}));for(const e in t)-1===a.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return s.makeQuery(e,t,{cacheName:r,...o});throw new Error("Request URL is not set")}}({name:t,params:e.objectRemoveEmpty(r),requestOptions:s,connector:this})}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)}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)}};
2
2
  //# sourceMappingURL=ngw-connector.cjs.prod.js.map
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.17; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.18; author: NextGIS */
2
2
  function defined(val) {
3
3
  return val !== void 0 && val !== null;
4
4
  }
@@ -773,7 +773,7 @@ function unwrapListeners(arr) {
773
773
 
774
774
  var pkg = {
775
775
  name: "@nextgis/ngw-connector",
776
- version: "3.0.0-alpha.17",
776
+ version: "3.0.0-alpha.18",
777
777
  _priority: 12,
778
778
  description: "A lightweight HTTP client optimized for use with NextGIS Web API",
779
779
  main: "index.js",
@@ -1,2 +1,2 @@
1
- function e(e){return null!=e}function t(t){return"string"==typeof t?!!t:e(t)}function r(e,t,r,o){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===o:n(e,t));return e===t}function n(e,t){const n=Object.keys(e).sort(),o=Object.keys(t).sort();if(n.length!==o.length)return!1;if(n.join("")!==o.join(""))return!1;for(let i=0;i<n.length;i++){if(!r(e[n[i]],t[o[i]],e,t))return!1}return!0}function o(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]=o(e[r])})),t}function i(e){return"[object Object]"===Object.prototype.toString.call(e)}function s(e){return e.replace(/([^:]\/)\/+/g,"$1")}const a=/\{ *([\w_-]+) *\}/g;function c(e,t){return e.replace(a,((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 u=Object.defineProperty,l=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable,p=(e,t,r)=>t in e?u(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function d(e){return t=this,r=null,n=function*(){const{params:t,name:r,connector:n,requestOptions:o}=e,i=yield n.connect();let s=i&&i[r];if(s){s=[...s];let e=s.shift();if(s.length){const r={};for(let e=0;e<s.length;e++){const n=s[e];if(r[e]=`{${n}}`,void 0===t[n])throw new Error(`\`${n}\` URL API argument is not specified`)}e&&(e=c(e,r))}if(t){const r=[],n=t.paramList;Array.isArray(n)&&n.forEach((([e,t])=>{r.push(`${e}=${t}`)}));for(const e in t)-1===s.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return n.makeQuery(e,t,((e,t)=>{for(var r in t||(t={}))h.call(t,r)&&p(e,r,t[r]);if(l)for(var r of l(t))f.call(t,r)&&p(e,r,t[r]);return e})({cacheName:r},o));throw new Error("Request URL is not set")}},new Promise(((e,o)=>{var i=e=>{try{a(n.next(e))}catch(t){o(t)}},s=e=>{try{a(n.throw(e))}catch(t){o(t)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(i,s);a((n=n.apply(t,r)).next())}));var t,r,n}var y=Object.defineProperty,m=(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);const v=class e{constructor(t={}){var r;m(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,n){return"object"==typeof r&&"valueToSet"in r&&(r.onlyFull=!0),this.add(e,t,r,!0,n)}add(e,r,n,i,s){let a,c;"object"==typeof n&&"valueToSet"in n?(a=n,i=a.onlyFull,s=a.expirationTime,c=a.props):c=n;const u=this._find(e,c);if(u)return u.value;{let n;n=r instanceof Function?r():r;const a=c&&JSON.parse(JSON.stringify(o(c)));s=s?Date.now()+s:void 0;const u={key:e,value:n,props:a,expirationTime:s};return i&&!t(n)?n:(this.cache.push(u),n instanceof Promise?(n.catch((()=>{this.delete(e,c)})),i&&n.then((r=>(t(r)||this.delete(e,c),r))),n):n)}}match(e,t){const r=this._find(e,t);if(!(r&&r.expirationTime&&Date.now()>r.expirationTime))return null==r?void 0:r.value;this.delete(r)}matchAll(e,t){const r=Date.now();return this.cache.forEach((e=>{e.expirationTime&&r>e.expirationTime&&this.delete(e)})),e?this.cache.filter((r=>this._filter(r,e,t))).map((e=>e.value)):this.cache.map((e=>e.value))}delete(e,t){const r="string"==typeof e?this.cache.filter((r=>this._filter(r,e,t))):[e];for(const n of r){const e=this.cache.indexOf(n);-1!==e&&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)return!1;if(!r)return!0;return n(e.props||{},o(r))}};m(v,"instance",{});let g=v;function w(){}function b(){b.init.call(this)}function O(e){return void 0===e._maxListeners?b.defaultMaxListeners:e._maxListeners}function j(e,t,r,n){var o,i,s,a;if("function"!=typeof r)throw new TypeError('"listener" argument must be a function');if((i=e._events)?(i.newListener&&(e.emit("newListener",t,r.listener?r.listener:r),i=e._events),s=i[t]):(i=e._events=new w,e._eventsCount=0),s){if("function"==typeof s?s=i[t]=n?[r,s]:[s,r]:n?s.unshift(r):s.push(r),!s.warned&&(o=O(e))&&o>0&&s.length>o){s.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+s.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=s.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else s=i[t]=r,++e._eventsCount;return e}function x(e,t,r){var n=!1;function o(){e.removeListener(t,o),n||(n=!0,r.apply(e,arguments))}return o.listener=r,o}function _(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 P(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}w.prototype=Object.create(null),b.EventEmitter=b,b.usingDomains=!1,b.prototype.domain=void 0,b.prototype._events=void 0,b.prototype._maxListeners=void 0,b.defaultMaxListeners=10,b.init=function(){this.domain=null,b.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new w,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},b.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},b.prototype.getMaxListeners=function(){return O(this)},b.prototype.emit=function(e){var t,r,n,o,i,s,a,c="error"===e;if(s=this._events)c=c&&null==s.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var u=new Error('Uncaught, unspecified "error" event. ('+t+")");throw u.context=t,u}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(r=s[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,o=P(e,n),i=0;i<n;++i)o[i].call(r)}(r,l,this);break;case 2:!function(e,t,r,n){if(t)e.call(r,n);else for(var o=e.length,i=P(e,o),s=0;s<o;++s)i[s].call(r,n)}(r,l,this,arguments[1]);break;case 3:!function(e,t,r,n,o){if(t)e.call(r,n,o);else for(var i=e.length,s=P(e,i),a=0;a<i;++a)s[a].call(r,n,o)}(r,l,this,arguments[1],arguments[2]);break;case 4:!function(e,t,r,n,o,i){if(t)e.call(r,n,o,i);else for(var s=e.length,a=P(e,s),c=0;c<s;++c)a[c].call(r,n,o,i)}(r,l,this,arguments[1],arguments[2],arguments[3]);break;default:for(o=new Array(n-1),i=1;i<n;i++)o[i-1]=arguments[i];!function(e,t,r,n){if(t)e.apply(r,n);else for(var o=e.length,i=P(e,o),s=0;s<o;++s)i[s].apply(r,n)}(r,l,this,o)}return!0},b.prototype.on=b.prototype.addListener=function(e,t){return j(this,e,t,!1)},b.prototype.prependListener=function(e,t){return j(this,e,t,!0)},b.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,x(this,e,t)),this},b.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,x(this,e,t)),this},b.prototype.removeListener=function(e,t){var r,n,o,i,s;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(r=n[e]))return this;if(r===t||r.listener&&r.listener===t)0==--this._eventsCount?this._events=new w:(delete n[e],n.removeListener&&this.emit("removeListener",e,r.listener||t));else if("function"!=typeof r){for(o=-1,i=r.length;i-- >0;)if(r[i]===t||r[i].listener&&r[i].listener===t){s=r[i].listener,o=i;break}if(o<0)return this;if(1===r.length){if(r[0]=void 0,0==--this._eventsCount)return this._events=new w,this;delete n[e]}else!function(e,t){for(var r=t,n=r+1,o=e.length;n<o;r+=1,n+=1)e[r]=e[n];e.pop()}(r,o);n.removeListener&&this.emit("removeListener",e,s||t)}return this},b.prototype.off=function(e,t){return this.removeListener(e,t)},b.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new w,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new w:delete r[e]),this;if(0===arguments.length){for(var n,o=Object.keys(r),i=0;i<o.length;++i)"removeListener"!==(n=o[i])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new w,this._eventsCount=0,this}if("function"==typeof(t=r[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},b.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):[]},b.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):_.call(e,t)},b.prototype.listenerCount=_,b.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var E="3.0.0-alpha.17";class C extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var R=Object.defineProperty,T=(e,t,r)=>((e,t,r)=>t in e?R(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class k extends Error{constructor(e){super(),T(this,"name","NgwError"),T(this,"title"),T(this,"message"),T(this,"detail"),T(this,"exception"),T(this,"status_code"),T(this,"data"),T(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,k.prototype)}}var I=Object.defineProperty,L=(e,t,r)=>((e,t,r)=>t in e?I(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class q extends k{constructor(e){super(e),L(this,"name","InsufficientPermissionsError"),L(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,q.prototype)}}var A=Object.defineProperty,U=(e,t,r)=>((e,t,r)=>t in e?A(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let S=class e extends k{constructor(t){super(t),U(this,"message","There is no response from the server or problem connecting to server."),U(this,"title","Network error"),U(this,"detail","Check network connectivity and try again later."),Object.setPrototypeOf(this,e.prototype)}};var $=Object.defineProperty,N=(e,t,r)=>((e,t,r)=>t in e?$(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class F extends Error{constructor(e){super(),N(this,"name","NetworkError"),Object.setPrototypeOf(this,F.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}var D=Object.defineProperty,B=(e,t,r)=>((e,t,r)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class G extends k{constructor(e){super(e),B(this,"name","ResourceNotFoundError"),B(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,G.prototype)}}var H=Object.freeze({__proto__:null,AbortError:C,InsufficientPermissionsError:q,NetworkError:F,NetworksResponseError:S,NgwError:k,ResourceNotFoundError:G,extractError:function(e){if(i(e))return e.name&&e.message&&e.title?{title:e.title,message:e.message,detail:e.detail||null,data:e.data&&e.data.data?e.data.data:null}:!e.exception||void 0!==e.status&&0!==e.status&&void 0!==e.data?{title:"string"==typeof e.title?e.title:"Unexpected error",message:"string"==typeof e.message?e.message:"Something went wrong."}:new S({title:e.title,status_code:e.status_code,exception:e.exception})},isError:function(e){return!!i(e)&&(e.status_code&&e.exception&&e.title)}}),M=Object.defineProperty,z=(e,t,r)=>((e,t,r)=>t in e?M(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class J extends Error{constructor(e){super(e||"Something went wrong."),z(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,J)}}class Q extends J{constructor(e){super(e||"There is no response from the server or problem connecting to server."),z(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class W extends J{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class K extends J{constructor(e){super(e.message),z(this,"detail"),z(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class X extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),z(this,"title"),z(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,X)}}class V extends X{constructor(e){super("Long-running request was cancelled.",e)}}class Y extends X{constructor(e){super(void 0,e)}}var Z=Object.defineProperty,ee=Object.getOwnPropertySymbols,te=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable,ne=(e,t,r)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,oe=(e,t)=>{for(var r in t||(t={}))te.call(t,r)&&ne(e,r,t[r]);if(ee)for(var r of ee(t))re.call(t,r)&&ne(e,r,t[r]);return e},ie=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));function se(e){return ie(this,null,(function*(){const t=yield function(e){return ie(this,null,(function*(){try{return e.json()}catch(t){throw new Error}}))}(e);let r=t.delay_ms;const n=void 0!==t.retry_ms?t.retry_ms:2e3,o=`/api/lunkwill/${t.id}/summary`,i=`/api/lunkwill/${t.id}/response`,s=e=>new Promise((t=>setTimeout(t,e)));let a=!1,c=!1;for(;!c;){let e,t;yield s(a?n:r),a=!1;try{e=yield fetch(o,{credentials:"same-origin"}),t=yield e.json()}catch(u){a=!0;continue}switch(t.status){case void 0:throw new X(void 0,t);case"ready":c=!0;break;case"cancelled":throw new V(t);case"failed":throw new Y(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new X(void 0,t)}}return i}))}const ae=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function ce(e,t){let r="";return void 0!==t&&(r="?"+function(e){const t=[];for(const[r,n]of Object.entries(e))if("string"==typeof n||"number"==typeof n||"boolean"==typeof n)t.push(`${r}=${encodeURIComponent(n)}`);else if(Array.isArray(n))t.push(`${r}=${n.map(encodeURIComponent).join(",")}`);else for(const[e,o]of Object.entries(n)){const n=`${r}[${encodeURIComponent(e)}]`;if("string"==typeof o||"number"==typeof o||"boolean"==typeof o)t.push(`${n}=${encodeURIComponent(o)}`);else if(Array.isArray(o)){const e=o.map(encodeURIComponent);t.push(`${n}=${e.join(",")}`)}}return t.join("&")}(t)),e+r}function ue(e,t,r){return ie(this,null,(function*(){var n,i;const s=oe(oe({},{method:"GET",credentials:"same-origin",headers:{}}),t),{withCredentials:a,responseType:c,cacheProps:u,cacheName:l,lunkwill:h,cache:f,query:p,json:d}=s,y=((e,t)=>{var r={};for(var n in e)te.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&ee)for(var n of ee(e))t.indexOf(n)<0&&re.call(e,n)&&(r[n]=e[n]);return r})(s,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);y.method=null==(n=y.method)?void 0:n.toUpperCase(),a&&(y.credentials="include");let m=!1;void 0!==h&&(h.toHeaders(y.headers||{}),m=!0);const v=!!y.lunkwillReturnUrl;if(delete y.lunkwillReturnUrl,void 0!==d){y.body=JSON.stringify(d);const e=y.headers||{};e["Content-Type"]="application/json",y.headers=e}const g=ce(e,p),w=()=>ie(this,null,(function*(){let e;try{e=yield fetch(g,y)}catch(o){if(y.signal&&y.signal.aborted)throw o;throw new Q}if(m&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield se(e);if(v)return t;e=yield function(e){return ie(this,null,(function*(){try{return yield window.fetch(e,{credentials:"same-origin"})}catch(o){throw new Q}}))}(t)}const t=e.headers.get("content-type"),r=t&&(t.includes("application/json")||t.includes("application/vnd.lunkwill.request-summary+json"));let n;try{const o=t&&ae.test(t);if("blob"===c||o)n=yield e.blob();else{if(!r)throw new W;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof W)throw o;throw new W}if(400<=e.status&&e.status<=599)throw new K(n);return n}));if(r)if("GET"===(null==(i=y.method)?void 0:i.toUpperCase())){if(!1!==f){const e=u||oe({},o({withCredentials:a,responseType:c}));return r.add(l||g,w,{props:e,expirationTime:f?void 0:500})}}else{["api/feature_layer/identify"].every((t=>!e.includes(t)))&&r.clean()}return w()}))}var le=Object.defineProperty,he=Object.defineProperties,fe=Object.getOwnPropertyDescriptors,pe=Object.getOwnPropertySymbols,de=Object.prototype.hasOwnProperty,ye=Object.prototype.propertyIsEnumerable,me=(e,t,r)=>t in e?le(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,ve=(e,t)=>{for(var r in t||(t={}))de.call(t,r)&&me(e,r,t[r]);if(pe)for(var r of pe(t))ye.call(t,r)&&me(e,r,t[r]);return e},ge=(e,t)=>{var r={};for(var n in e)de.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&pe)for(var n of pe(e))t.indexOf(n)<0&&ye.call(e,n)&&(r[n]=e[n]);return r};function we(e,t,r,...n){const[o,...i]=r[e],a=n[0];let c;if(void 0===a)c=[];else if("object"==typeof a&&null!==a){if(n.length>1)throw new Error("Too many arguments for route(name, object)!");c=[];for(const[e,t]of Object.entries(a))c[i.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return s(t+o.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),n=c[r];if(void 0===n){throw new Error(`Undefined parameter ${r} in "${o}".`)}return String(n)})))}function be(e,t,...r){const n={url:n=>{return o=this,i=null,s=function*(){var o;const i=yield t.connect();return ce(we(e,null!=(o=t.options.baseUrl)?o:"",i,...r),null==n?void 0:n.query)},new Promise(((e,t)=>{var r=e=>{try{a(s.next(e))}catch(r){t(r)}},n=e=>{try{a(s.throw(e))}catch(r){t(r)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,n);a((s=s.apply(o,i)).next())}));var o,i,s}},i=["get","post","put","delete","patch"];for(const s of i){const i=n=>{var i;const a=n||{},{headers:c}=a,u=ge(a,["headers"]);if(null==(i=null==n?void 0:n.signal)?void 0:i.aborted)throw new C;return t.connect().then((n=>{var i;const a=we(e,null!=(i=t.options.baseUrl)?i:"",n,...r),l=o(ve(ve({},t.getAuthorizationHeaders()),null!=c?c:{}));return ue(a,(h=ve({headers:l},u),he(h,fe({method:s}))),t.cache);var h}))};n[s]=i}return n}function Oe(e){return"[object Object]"===Object.prototype.toString.call(e)}let je;je=(e,t,r={},n,o)=>{r.method=r.method||"GET";const i=new XMLHttpRequest;i.open(r.method||"GET",e,!0),"blob"===r.responseType&&(i.responseType=r.responseType);const s=()=>{try{return JSON.parse(i.responseText)}catch(e){return i.responseText}};i.onload=()=>{-1!==[401,403,404,422,500].indexOf(i.status)&&n(new k(s())),((e=!1)=>{const o=e?n:t;"blob"===r.responseType?o(i.response):i.responseText?o(s()):n({message:""})})()},i.onerror=t=>{n(0===i.status?new F(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;o&&o((()=>{i.abort()})),i.send(c)};const xe=[];function _e(e){xe.push(e)}var Pe=Object.defineProperty,Ee=Object.defineProperties,Ce=Object.getOwnPropertyDescriptors,Re=Object.getOwnPropertySymbols,Te=Object.prototype.hasOwnProperty,ke=Object.prototype.propertyIsEnumerable,Ie=(e,t,r)=>t in e?Pe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Le=(e,t)=>{for(var r in t||(t={}))Te.call(t,r)&&Ie(e,r,t[r]);if(Re)for(var r of Re(t))ke.call(t,r)&&Ie(e,r,t[r]);return e},qe=(e,t,r)=>Ie(e,"symbol"!=typeof t?t+"":t,r),Ae=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));let Ue=0,Se=0;class $e{constructor(e){this.options=e,qe(this,"id",Ue++),qe(this,"emitter",new b),qe(this,"user"),qe(this,"cache"),qe(this,"withCredentials"),qe(this,"routeCache"),qe(this,"client",`NextGIS-NGW-Connector/${E}`),qe(this,"routeStr","/api/component/pyramid/route"),qe(this,"activeRequests",{}),qe(this,"requestTransform");const t=function(e){return xe.find((t=>{if(t.options.baseUrl===e.baseUrl){if(!e.auth)return!0;if(t.options.auth)return n(t.options.auth,e.auth)}}))}(e);if(this.cache=new g({namespace:e.cacheId}),this.routeCache=new g({namespace:"routecache"}),t)return t;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),_e(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,_e(this)}connect(){return Ae(this,arguments,(function*({signal:e}={}){const t=this.options.auth;if(t){const{login:e,password:r}=t;e&&r&&(yield this._login({login:e,password:r}))}const r=`${this.routeStr}?client=${this.client}`;return this.routeCache.add(this.options.baseUrl||String(this.id),(()=>this.makeQuery(r,null,{signal:e,cache:!1})))}))}login(e,t){return this.logout(),_e(this),this._login(e,t)}logout(){this.abort(),function(e){const t=xe.indexOf(e);-1!==t&&xe.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.routeCache.clean(),this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return Ae(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=Le({headers:this.getAuthorizationHeaders(e),cache:!0},t);return this.makeQuery("/api/component/auth/current_user",{},r)}))}getAuthorizationHeaders(e){const t=this.makeClientId(e);return t?{Authorization:`Basic ${t}`}:{}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,n=[t,r].map(encodeURIComponent).join(":");return window.btoa(n)}}abort(){for(const e of Object.values(this.activeRequests))e.abort();this.activeRequests={}}getActiveApiRequests(){return Le({},this.activeRequests)}route(e,...t){return be(e,this,...t)}makeQuery(e,t){return Ae(this,arguments,(function*(e,t,r={}){var n;if(!(e=(this.options.baseUrl?this.options.baseUrl:"")+e))throw new Error("Empty `url` not allowed");if(t){const r=((e,t)=>{var r={};for(var n in e)Te.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&Re)for(var n of Re(e))t.indexOf(n)<0&&ke.call(e,n)&&(r[n]=e[n]);return r})(t,["paramList"]);e=c(e,r)}e=encodeURI(s(e)),r=Le({withCredentials:this.withCredentials},r);const{cache:i,signal:a,method:u="GET",headers:l,cacheName:h,cacheProps:f,responseType:p,withCredentials:d}=r,y=new AbortController,m=y.signal;if(a){if(a.aborted)throw new C;a.addEventListener("abort",(()=>{y.abort()}))}r.signal=m;const v=()=>Ae(this,null,(function*(){const t=Se++;this.activeRequests[t]=y;try{return this._loadData(e,r)}finally{this._cleanActiveRequest(t)}}));if("GET"===u&&!1!==i){const r=f||(g=Le({},o({headers:l,withCredentials:d,responseType:p,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id})),Ee(g,Ce({params:t})));return this.cache.add(h||e,v,{props:r,expirationTime:i?void 0:500})}var g;return v()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=Le(Le({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let i;je(e,r,t,n,(e=>{i=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==i&&i(),n(new C)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return Ae(this,null,(function*(){try{const r=yield this.getUserInfo(e,t);return this.user=r,this.emitter.emit("login",r),r}catch(r){throw this.emitter.emit("login:error",r),r}}))}_cleanActiveRequest(e){delete this.activeRequests[e]}_handleHttpError(e){if(e&&e instanceof k){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new G(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new q(e)}return e}}qe($e,"errors",H);const Ne=["description"];function Fe(t,r=""){r=r?r+"__":"";const n={};for(const[o,i]of Object.entries(t))if(-1===Ne.indexOf(o))if(Oe(i))if("owner_user"===o){const e=Fe(i,o);Object.assign(n,e)}else"parent"===o&&"id"in i&&(n.parent_id=i.id);else e(i)&&(n[r+o]=i);return n}var De=Object.defineProperty,Be=Object.defineProperties,Ge=Object.getOwnPropertyDescriptors,He=Object.getOwnPropertySymbols,Me=Object.prototype.hasOwnProperty,ze=Object.prototype.propertyIsEnumerable,Je=(e,t,r)=>t in e?De(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Qe=(e,t)=>{for(var r in t||(t={}))Me.call(t,r)&&Je(e,r,t[r]);if(He)for(var r of He(t))ze.call(t,r)&&Je(e,r,t[r]);return e},We=(e,t)=>Be(e,Ge(t)),Ke=(e,t,r)=>Je(e,"symbol"!=typeof t?t+"":t,r),Xe=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));class Ve{constructor({connector:e,cacheId:t}){Ke(this,"cache"),Ke(this,"connector"),this.connector=e,this.cache=new g({namespace:t})}getOne(e,t){const r=Qe({},t);return"string"==typeof e||"number"==typeof e||Oe(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):Oe(e)?this._fetchResourceBy(e,r):Promise.resolve(void 0)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new G}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||Oe(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):Promise.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,Fe(e)),this.connector.route("resource.search").get(We(Qe({},t),{query:Qe({serialization:"full"},r)})).then((e=>{if((null==t?void 0:t.cache)&&e)for(const t of e)this.cache.add("resource.item",Promise.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>{var r,n;return(null==(n=null==(r=null==e?void 0:e.resource)?void 0:r.parent)?void 0:n.id)?this.getOne(e.resource.parent.id,t):Promise.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){return Xe(this,arguments,(function*(e,t,r=[]){let n;n="string"==typeof e?yield this.getId(e,t):"object"==typeof e?e.id:e;const o=yield this.connector.route("resource.collection").get(We(Qe({},t),{query:{parent:n}})),i=[];for(const s of o)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(s),{id:s.resource.id}),r.push(s),(null==t?void 0:t.recursive)&&s.resource.children&&i.push(this._getChildrenOf(s.resource.id,t,r));return i.length?Promise.all(i).then((()=>r)):r}))}_cleanResourceItemCache(e){return Xe(this,null,(function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;if(["resource.item","resource"].includes(o.key)&&void 0!==r)if("number"==typeof r)r===e&&n.push(o);else{(yield this.getId(r))===e&&n.push(o)}}for(const e of n)this.cache.delete(e)}))}_fetchResourceById(e,t){return this.connector.route("resource.item",{id:e}).get(t)}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(t){return Promise.all(this.cache.matchAll("resource.item")).then((r=>(r.filter((r=>{if(r)return t.keyname&&r.resource.keyname?t.keyname===r.resource.keyname:e(t.id)&&e(r.resource.id)?t.id===r.resource.id:n(t,r.resource)})),[])))}}var Ye=Object.defineProperty,Ze=(e,t,r)=>((e,t,r)=>t in e?Ye(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class et extends $e{constructor(e){super(e),Ze(this,"resources"),this.resources=new Ve({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(e,t={},r={}){var n;return d({name:e,params:o(t=null!=(n=r.params)?n:t),requestOptions:r,connector:this})}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)}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)}}export{et as default};
1
+ function e(e){return null!=e}function t(t){return"string"==typeof t?!!t:e(t)}function r(e,t,r,o){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===o:n(e,t));return e===t}function n(e,t){const n=Object.keys(e).sort(),o=Object.keys(t).sort();if(n.length!==o.length)return!1;if(n.join("")!==o.join(""))return!1;for(let i=0;i<n.length;i++){if(!r(e[n[i]],t[o[i]],e,t))return!1}return!0}function o(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]=o(e[r])})),t}function i(e){return"[object Object]"===Object.prototype.toString.call(e)}function s(e){return e.replace(/([^:]\/)\/+/g,"$1")}const a=/\{ *([\w_-]+) *\}/g;function c(e,t){return e.replace(a,((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 u=Object.defineProperty,l=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable,p=(e,t,r)=>t in e?u(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function d(e){return t=this,r=null,n=function*(){const{params:t,name:r,connector:n,requestOptions:o}=e,i=yield n.connect();let s=i&&i[r];if(s){s=[...s];let e=s.shift();if(s.length){const r={};for(let e=0;e<s.length;e++){const n=s[e];if(r[e]=`{${n}}`,void 0===t[n])throw new Error(`\`${n}\` URL API argument is not specified`)}e&&(e=c(e,r))}if(t){const r=[],n=t.paramList;Array.isArray(n)&&n.forEach((([e,t])=>{r.push(`${e}=${t}`)}));for(const e in t)-1===s.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return n.makeQuery(e,t,((e,t)=>{for(var r in t||(t={}))h.call(t,r)&&p(e,r,t[r]);if(l)for(var r of l(t))f.call(t,r)&&p(e,r,t[r]);return e})({cacheName:r},o));throw new Error("Request URL is not set")}},new Promise(((e,o)=>{var i=e=>{try{a(n.next(e))}catch(t){o(t)}},s=e=>{try{a(n.throw(e))}catch(t){o(t)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(i,s);a((n=n.apply(t,r)).next())}));var t,r,n}var y=Object.defineProperty,m=(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);const v=class e{constructor(t={}){var r;m(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,n){return"object"==typeof r&&"valueToSet"in r&&(r.onlyFull=!0),this.add(e,t,r,!0,n)}add(e,r,n,i,s){let a,c;"object"==typeof n&&"valueToSet"in n?(a=n,i=a.onlyFull,s=a.expirationTime,c=a.props):c=n;const u=this._find(e,c);if(u)return u.value;{let n;n=r instanceof Function?r():r;const a=c&&JSON.parse(JSON.stringify(o(c)));s=s?Date.now()+s:void 0;const u={key:e,value:n,props:a,expirationTime:s};return i&&!t(n)?n:(this.cache.push(u),n instanceof Promise?(n.catch((()=>{this.delete(e,c)})),i&&n.then((r=>(t(r)||this.delete(e,c),r))),n):n)}}match(e,t){const r=this._find(e,t);if(!(r&&r.expirationTime&&Date.now()>r.expirationTime))return null==r?void 0:r.value;this.delete(r)}matchAll(e,t){const r=Date.now();return this.cache.forEach((e=>{e.expirationTime&&r>e.expirationTime&&this.delete(e)})),e?this.cache.filter((r=>this._filter(r,e,t))).map((e=>e.value)):this.cache.map((e=>e.value))}delete(e,t){const r="string"==typeof e?this.cache.filter((r=>this._filter(r,e,t))):[e];for(const n of r){const e=this.cache.indexOf(n);-1!==e&&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)return!1;if(!r)return!0;return n(e.props||{},o(r))}};m(v,"instance",{});let g=v;function w(){}function b(){b.init.call(this)}function O(e){return void 0===e._maxListeners?b.defaultMaxListeners:e._maxListeners}function j(e,t,r,n){var o,i,s,a;if("function"!=typeof r)throw new TypeError('"listener" argument must be a function');if((i=e._events)?(i.newListener&&(e.emit("newListener",t,r.listener?r.listener:r),i=e._events),s=i[t]):(i=e._events=new w,e._eventsCount=0),s){if("function"==typeof s?s=i[t]=n?[r,s]:[s,r]:n?s.unshift(r):s.push(r),!s.warned&&(o=O(e))&&o>0&&s.length>o){s.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+s.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=s.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else s=i[t]=r,++e._eventsCount;return e}function x(e,t,r){var n=!1;function o(){e.removeListener(t,o),n||(n=!0,r.apply(e,arguments))}return o.listener=r,o}function _(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 P(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}w.prototype=Object.create(null),b.EventEmitter=b,b.usingDomains=!1,b.prototype.domain=void 0,b.prototype._events=void 0,b.prototype._maxListeners=void 0,b.defaultMaxListeners=10,b.init=function(){this.domain=null,b.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new w,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},b.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},b.prototype.getMaxListeners=function(){return O(this)},b.prototype.emit=function(e){var t,r,n,o,i,s,a,c="error"===e;if(s=this._events)c=c&&null==s.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var u=new Error('Uncaught, unspecified "error" event. ('+t+")");throw u.context=t,u}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(r=s[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,o=P(e,n),i=0;i<n;++i)o[i].call(r)}(r,l,this);break;case 2:!function(e,t,r,n){if(t)e.call(r,n);else for(var o=e.length,i=P(e,o),s=0;s<o;++s)i[s].call(r,n)}(r,l,this,arguments[1]);break;case 3:!function(e,t,r,n,o){if(t)e.call(r,n,o);else for(var i=e.length,s=P(e,i),a=0;a<i;++a)s[a].call(r,n,o)}(r,l,this,arguments[1],arguments[2]);break;case 4:!function(e,t,r,n,o,i){if(t)e.call(r,n,o,i);else for(var s=e.length,a=P(e,s),c=0;c<s;++c)a[c].call(r,n,o,i)}(r,l,this,arguments[1],arguments[2],arguments[3]);break;default:for(o=new Array(n-1),i=1;i<n;i++)o[i-1]=arguments[i];!function(e,t,r,n){if(t)e.apply(r,n);else for(var o=e.length,i=P(e,o),s=0;s<o;++s)i[s].apply(r,n)}(r,l,this,o)}return!0},b.prototype.on=b.prototype.addListener=function(e,t){return j(this,e,t,!1)},b.prototype.prependListener=function(e,t){return j(this,e,t,!0)},b.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,x(this,e,t)),this},b.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,x(this,e,t)),this},b.prototype.removeListener=function(e,t){var r,n,o,i,s;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(r=n[e]))return this;if(r===t||r.listener&&r.listener===t)0==--this._eventsCount?this._events=new w:(delete n[e],n.removeListener&&this.emit("removeListener",e,r.listener||t));else if("function"!=typeof r){for(o=-1,i=r.length;i-- >0;)if(r[i]===t||r[i].listener&&r[i].listener===t){s=r[i].listener,o=i;break}if(o<0)return this;if(1===r.length){if(r[0]=void 0,0==--this._eventsCount)return this._events=new w,this;delete n[e]}else!function(e,t){for(var r=t,n=r+1,o=e.length;n<o;r+=1,n+=1)e[r]=e[n];e.pop()}(r,o);n.removeListener&&this.emit("removeListener",e,s||t)}return this},b.prototype.off=function(e,t){return this.removeListener(e,t)},b.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new w,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new w:delete r[e]),this;if(0===arguments.length){for(var n,o=Object.keys(r),i=0;i<o.length;++i)"removeListener"!==(n=o[i])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new w,this._eventsCount=0,this}if("function"==typeof(t=r[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},b.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):[]},b.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):_.call(e,t)},b.prototype.listenerCount=_,b.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var E="3.0.0-alpha.18";class C extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var R=Object.defineProperty,T=(e,t,r)=>((e,t,r)=>t in e?R(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class k extends Error{constructor(e){super(),T(this,"name","NgwError"),T(this,"title"),T(this,"message"),T(this,"detail"),T(this,"exception"),T(this,"status_code"),T(this,"data"),T(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,k.prototype)}}var I=Object.defineProperty,L=(e,t,r)=>((e,t,r)=>t in e?I(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class q extends k{constructor(e){super(e),L(this,"name","InsufficientPermissionsError"),L(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,q.prototype)}}var A=Object.defineProperty,U=(e,t,r)=>((e,t,r)=>t in e?A(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let S=class e extends k{constructor(t){super(t),U(this,"message","There is no response from the server or problem connecting to server."),U(this,"title","Network error"),U(this,"detail","Check network connectivity and try again later."),Object.setPrototypeOf(this,e.prototype)}};var $=Object.defineProperty,N=(e,t,r)=>((e,t,r)=>t in e?$(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class F extends Error{constructor(e){super(),N(this,"name","NetworkError"),Object.setPrototypeOf(this,F.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}var D=Object.defineProperty,B=(e,t,r)=>((e,t,r)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class G extends k{constructor(e){super(e),B(this,"name","ResourceNotFoundError"),B(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,G.prototype)}}var H=Object.freeze({__proto__:null,AbortError:C,InsufficientPermissionsError:q,NetworkError:F,NetworksResponseError:S,NgwError:k,ResourceNotFoundError:G,extractError:function(e){if(i(e))return e.name&&e.message&&e.title?{title:e.title,message:e.message,detail:e.detail||null,data:e.data&&e.data.data?e.data.data:null}:!e.exception||void 0!==e.status&&0!==e.status&&void 0!==e.data?{title:"string"==typeof e.title?e.title:"Unexpected error",message:"string"==typeof e.message?e.message:"Something went wrong."}:new S({title:e.title,status_code:e.status_code,exception:e.exception})},isError:function(e){return!!i(e)&&(e.status_code&&e.exception&&e.title)}}),M=Object.defineProperty,z=(e,t,r)=>((e,t,r)=>t in e?M(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class J extends Error{constructor(e){super(e||"Something went wrong."),z(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,J)}}class Q extends J{constructor(e){super(e||"There is no response from the server or problem connecting to server."),z(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class W extends J{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class K extends J{constructor(e){super(e.message),z(this,"detail"),z(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class X extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),z(this,"title"),z(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,X)}}class V extends X{constructor(e){super("Long-running request was cancelled.",e)}}class Y extends X{constructor(e){super(void 0,e)}}var Z=Object.defineProperty,ee=Object.getOwnPropertySymbols,te=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable,ne=(e,t,r)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,oe=(e,t)=>{for(var r in t||(t={}))te.call(t,r)&&ne(e,r,t[r]);if(ee)for(var r of ee(t))re.call(t,r)&&ne(e,r,t[r]);return e},ie=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));function se(e){return ie(this,null,(function*(){const t=yield function(e){return ie(this,null,(function*(){try{return e.json()}catch(t){throw new Error}}))}(e);let r=t.delay_ms;const n=void 0!==t.retry_ms?t.retry_ms:2e3,o=`/api/lunkwill/${t.id}/summary`,i=`/api/lunkwill/${t.id}/response`,s=e=>new Promise((t=>setTimeout(t,e)));let a=!1,c=!1;for(;!c;){let e,t;yield s(a?n:r),a=!1;try{e=yield fetch(o,{credentials:"same-origin"}),t=yield e.json()}catch(u){a=!0;continue}switch(t.status){case void 0:throw new X(void 0,t);case"ready":c=!0;break;case"cancelled":throw new V(t);case"failed":throw new Y(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new X(void 0,t)}}return i}))}const ae=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function ce(e,t){let r="";return void 0!==t&&(r="?"+function(e){const t=[];for(const[r,n]of Object.entries(e))if("string"==typeof n||"number"==typeof n||"boolean"==typeof n)t.push(`${r}=${encodeURIComponent(n)}`);else if(Array.isArray(n))t.push(`${r}=${n.map(encodeURIComponent).join(",")}`);else for(const[e,o]of Object.entries(n)){const n=`${r}[${encodeURIComponent(e)}]`;if("string"==typeof o||"number"==typeof o||"boolean"==typeof o)t.push(`${n}=${encodeURIComponent(o)}`);else if(Array.isArray(o)){const e=o.map(encodeURIComponent);t.push(`${n}=${e.join(",")}`)}}return t.join("&")}(t)),e+r}function ue(e,t,r){return ie(this,null,(function*(){var n,i;const s=oe(oe({},{method:"GET",credentials:"same-origin",headers:{}}),t),{withCredentials:a,responseType:c,cacheProps:u,cacheName:l,lunkwill:h,cache:f,query:p,json:d}=s,y=((e,t)=>{var r={};for(var n in e)te.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&ee)for(var n of ee(e))t.indexOf(n)<0&&re.call(e,n)&&(r[n]=e[n]);return r})(s,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);y.method=null==(n=y.method)?void 0:n.toUpperCase(),a&&(y.credentials="include");let m=!1;void 0!==h&&(h.toHeaders(y.headers||{}),m=!0);const v=!!y.lunkwillReturnUrl;if(delete y.lunkwillReturnUrl,void 0!==d){y.body=JSON.stringify(d);const e=y.headers||{};e["Content-Type"]="application/json",y.headers=e}const g=ce(e,p),w=()=>ie(this,null,(function*(){let e;try{e=yield fetch(g,y)}catch(o){if(y.signal&&y.signal.aborted)throw o;throw new Q}if(m&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield se(e);if(v)return t;e=yield function(e){return ie(this,null,(function*(){try{return yield window.fetch(e,{credentials:"same-origin"})}catch(o){throw new Q}}))}(t)}const t=e.headers.get("content-type"),r=t&&(t.includes("application/json")||t.includes("application/vnd.lunkwill.request-summary+json"));let n;try{const o=t&&ae.test(t);if("blob"===c||o)n=yield e.blob();else{if(!r)throw new W;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof W)throw o;throw new W}if(400<=e.status&&e.status<=599)throw new K(n);return n}));if(r)if("GET"===(null==(i=y.method)?void 0:i.toUpperCase())){if(!1!==f){const e=u||oe({},o({withCredentials:a,responseType:c}));return r.add(l||g,w,{props:e,expirationTime:f?void 0:500})}}else{["api/feature_layer/identify"].every((t=>!e.includes(t)))&&r.clean()}return w()}))}var le=Object.defineProperty,he=Object.defineProperties,fe=Object.getOwnPropertyDescriptors,pe=Object.getOwnPropertySymbols,de=Object.prototype.hasOwnProperty,ye=Object.prototype.propertyIsEnumerable,me=(e,t,r)=>t in e?le(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,ve=(e,t)=>{for(var r in t||(t={}))de.call(t,r)&&me(e,r,t[r]);if(pe)for(var r of pe(t))ye.call(t,r)&&me(e,r,t[r]);return e},ge=(e,t)=>{var r={};for(var n in e)de.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&pe)for(var n of pe(e))t.indexOf(n)<0&&ye.call(e,n)&&(r[n]=e[n]);return r};function we(e,t,r,...n){const[o,...i]=r[e],a=n[0];let c;if(void 0===a)c=[];else if("object"==typeof a&&null!==a){if(n.length>1)throw new Error("Too many arguments for route(name, object)!");c=[];for(const[e,t]of Object.entries(a))c[i.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return s(t+o.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),n=c[r];if(void 0===n){throw new Error(`Undefined parameter ${r} in "${o}".`)}return String(n)})))}function be(e,t,...r){const n={url:n=>{return o=this,i=null,s=function*(){var o;const i=yield t.connect();return ce(we(e,null!=(o=t.options.baseUrl)?o:"",i,...r),null==n?void 0:n.query)},new Promise(((e,t)=>{var r=e=>{try{a(s.next(e))}catch(r){t(r)}},n=e=>{try{a(s.throw(e))}catch(r){t(r)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,n);a((s=s.apply(o,i)).next())}));var o,i,s}},i=["get","post","put","delete","patch"];for(const s of i){const i=n=>{var i;const a=n||{},{headers:c}=a,u=ge(a,["headers"]);if(null==(i=null==n?void 0:n.signal)?void 0:i.aborted)throw new C;return t.connect().then((n=>{var i;const a=we(e,null!=(i=t.options.baseUrl)?i:"",n,...r),l=o(ve(ve({},t.getAuthorizationHeaders()),null!=c?c:{}));return ue(a,(h=ve({headers:l},u),he(h,fe({method:s}))),t.cache);var h}))};n[s]=i}return n}function Oe(e){return"[object Object]"===Object.prototype.toString.call(e)}let je;je=(e,t,r={},n,o)=>{r.method=r.method||"GET";const i=new XMLHttpRequest;i.open(r.method||"GET",e,!0),"blob"===r.responseType&&(i.responseType=r.responseType);const s=()=>{try{return JSON.parse(i.responseText)}catch(e){return i.responseText}};i.onload=()=>{-1!==[401,403,404,422,500].indexOf(i.status)&&n(new k(s())),((e=!1)=>{const o=e?n:t;"blob"===r.responseType?o(i.response):i.responseText?o(s()):n({message:""})})()},i.onerror=t=>{n(0===i.status?new F(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;o&&o((()=>{i.abort()})),i.send(c)};const xe=[];function _e(e){xe.push(e)}var Pe=Object.defineProperty,Ee=Object.defineProperties,Ce=Object.getOwnPropertyDescriptors,Re=Object.getOwnPropertySymbols,Te=Object.prototype.hasOwnProperty,ke=Object.prototype.propertyIsEnumerable,Ie=(e,t,r)=>t in e?Pe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Le=(e,t)=>{for(var r in t||(t={}))Te.call(t,r)&&Ie(e,r,t[r]);if(Re)for(var r of Re(t))ke.call(t,r)&&Ie(e,r,t[r]);return e},qe=(e,t,r)=>Ie(e,"symbol"!=typeof t?t+"":t,r),Ae=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));let Ue=0,Se=0;class $e{constructor(e){this.options=e,qe(this,"id",Ue++),qe(this,"emitter",new b),qe(this,"user"),qe(this,"cache"),qe(this,"withCredentials"),qe(this,"routeCache"),qe(this,"client",`NextGIS-NGW-Connector/${E}`),qe(this,"routeStr","/api/component/pyramid/route"),qe(this,"activeRequests",{}),qe(this,"requestTransform");const t=function(e){return xe.find((t=>{if(t.options.baseUrl===e.baseUrl){if(!e.auth)return!0;if(t.options.auth)return n(t.options.auth,e.auth)}}))}(e);if(this.cache=new g({namespace:e.cacheId}),this.routeCache=new g({namespace:"routecache"}),t)return t;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),_e(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,_e(this)}connect(){return Ae(this,arguments,(function*({signal:e}={}){const t=this.options.auth;if(t){const{login:e,password:r}=t;e&&r&&(yield this._login({login:e,password:r}))}const r=`${this.routeStr}?client=${this.client}`;return this.routeCache.add(this.options.baseUrl||String(this.id),(()=>this.makeQuery(r,null,{signal:e,cache:!1})))}))}login(e,t){return this.logout(),_e(this),this._login(e,t)}logout(){this.abort(),function(e){const t=xe.indexOf(e);-1!==t&&xe.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.routeCache.clean(),this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return Ae(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=Le({headers:this.getAuthorizationHeaders(e),cache:!0},t);return this.makeQuery("/api/component/auth/current_user",{},r)}))}getAuthorizationHeaders(e){const t=this.makeClientId(e);return t?{Authorization:`Basic ${t}`}:{}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,n=[t,r].map(encodeURIComponent).join(":");return window.btoa(n)}}abort(){for(const e of Object.values(this.activeRequests))e.abort();this.activeRequests={}}getActiveApiRequests(){return Le({},this.activeRequests)}route(e,...t){return be(e,this,...t)}makeQuery(e,t){return Ae(this,arguments,(function*(e,t,r={}){var n;if(!(e=(this.options.baseUrl?this.options.baseUrl:"")+e))throw new Error("Empty `url` not allowed");if(t){const r=((e,t)=>{var r={};for(var n in e)Te.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&Re)for(var n of Re(e))t.indexOf(n)<0&&ke.call(e,n)&&(r[n]=e[n]);return r})(t,["paramList"]);e=c(e,r)}e=encodeURI(s(e)),r=Le({withCredentials:this.withCredentials},r);const{cache:i,signal:a,method:u="GET",headers:l,cacheName:h,cacheProps:f,responseType:p,withCredentials:d}=r,y=new AbortController,m=y.signal;if(a){if(a.aborted)throw new C;a.addEventListener("abort",(()=>{y.abort()}))}r.signal=m;const v=()=>Ae(this,null,(function*(){const t=Se++;this.activeRequests[t]=y;try{return this._loadData(e,r)}finally{this._cleanActiveRequest(t)}}));if("GET"===u&&!1!==i){const r=f||(g=Le({},o({headers:l,withCredentials:d,responseType:p,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id})),Ee(g,Ce({params:t})));return this.cache.add(h||e,v,{props:r,expirationTime:i?void 0:500})}var g;return v()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=Le(Le({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let i;je(e,r,t,n,(e=>{i=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==i&&i(),n(new C)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return Ae(this,null,(function*(){try{const r=yield this.getUserInfo(e,t);return this.user=r,this.emitter.emit("login",r),r}catch(r){throw this.emitter.emit("login:error",r),r}}))}_cleanActiveRequest(e){delete this.activeRequests[e]}_handleHttpError(e){if(e&&e instanceof k){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new G(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new q(e)}return e}}qe($e,"errors",H);const Ne=["description"];function Fe(t,r=""){r=r?r+"__":"";const n={};for(const[o,i]of Object.entries(t))if(-1===Ne.indexOf(o))if(Oe(i))if("owner_user"===o){const e=Fe(i,o);Object.assign(n,e)}else"parent"===o&&"id"in i&&(n.parent_id=i.id);else e(i)&&(n[r+o]=i);return n}var De=Object.defineProperty,Be=Object.defineProperties,Ge=Object.getOwnPropertyDescriptors,He=Object.getOwnPropertySymbols,Me=Object.prototype.hasOwnProperty,ze=Object.prototype.propertyIsEnumerable,Je=(e,t,r)=>t in e?De(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Qe=(e,t)=>{for(var r in t||(t={}))Me.call(t,r)&&Je(e,r,t[r]);if(He)for(var r of He(t))ze.call(t,r)&&Je(e,r,t[r]);return e},We=(e,t)=>Be(e,Ge(t)),Ke=(e,t,r)=>Je(e,"symbol"!=typeof t?t+"":t,r),Xe=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));class Ve{constructor({connector:e,cacheId:t}){Ke(this,"cache"),Ke(this,"connector"),this.connector=e,this.cache=new g({namespace:t})}getOne(e,t){const r=Qe({},t);return"string"==typeof e||"number"==typeof e||Oe(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):Oe(e)?this._fetchResourceBy(e,r):Promise.resolve(void 0)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new G}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||Oe(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):Promise.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,Fe(e)),this.connector.route("resource.search").get(We(Qe({},t),{query:Qe({serialization:"full"},r)})).then((e=>{if((null==t?void 0:t.cache)&&e)for(const t of e)this.cache.add("resource.item",Promise.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>{var r,n;return(null==(n=null==(r=null==e?void 0:e.resource)?void 0:r.parent)?void 0:n.id)?this.getOne(e.resource.parent.id,t):Promise.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){return Xe(this,arguments,(function*(e,t,r=[]){let n;n="string"==typeof e?yield this.getId(e,t):"object"==typeof e?e.id:e;const o=yield this.connector.route("resource.collection").get(We(Qe({},t),{query:{parent:n}})),i=[];for(const s of o)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(s),{id:s.resource.id}),r.push(s),(null==t?void 0:t.recursive)&&s.resource.children&&i.push(this._getChildrenOf(s.resource.id,t,r));return i.length?Promise.all(i).then((()=>r)):r}))}_cleanResourceItemCache(e){return Xe(this,null,(function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;if(["resource.item","resource"].includes(o.key)&&void 0!==r)if("number"==typeof r)r===e&&n.push(o);else{(yield this.getId(r))===e&&n.push(o)}}for(const e of n)this.cache.delete(e)}))}_fetchResourceById(e,t){return this.connector.route("resource.item",{id:e}).get(t)}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(t){return Promise.all(this.cache.matchAll("resource.item")).then((r=>(r.filter((r=>{if(r)return t.keyname&&r.resource.keyname?t.keyname===r.resource.keyname:e(t.id)&&e(r.resource.id)?t.id===r.resource.id:n(t,r.resource)})),[])))}}var Ye=Object.defineProperty,Ze=(e,t,r)=>((e,t,r)=>t in e?Ye(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class et extends $e{constructor(e){super(e),Ze(this,"resources"),this.resources=new Ve({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(e,t={},r={}){var n;return d({name:e,params:o(t=null!=(n=r.params)?n:t),requestOptions:r,connector:this})}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)}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)}}export{et as default};
2
2
  //# sourceMappingURL=ngw-connector.esm-browser.prod.js.map
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.17; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.18; author: NextGIS */
2
2
  import { isObject as isObject$1, objectRemoveEmpty, fixUrlStr, objectDeepEqual, defined } from '@nextgis/utils';
3
3
  import Cache from '@nextgis/cache';
4
4
  import { EventEmitter } from 'events';
@@ -105,7 +105,7 @@ function apiRequest(opt) {
105
105
 
106
106
  var pkg = {
107
107
  name: "@nextgis/ngw-connector",
108
- version: "3.0.0-alpha.17",
108
+ version: "3.0.0-alpha.18",
109
109
  _priority: 12,
110
110
  description: "A lightweight HTTP client optimized for use with NextGIS Web API",
111
111
  main: "index.js",
@@ -1,2 +1,2 @@
1
- import{isObject as e,objectRemoveEmpty as t,fixUrlStr as r,objectDeepEqual as n,defined as o}from"@nextgis/utils";import s from"@nextgis/cache";import{EventEmitter as i}from"events";const a=/\{ *([\w_-]+) *\}/g;function c(e,t){return e.replace(a,((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 l=Object.defineProperty,u=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,d=Object.prototype.propertyIsEnumerable,p=(e,t,r)=>t in e?l(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function f(e){return t=this,r=null,n=function*(){const{params:t,name:r,connector:n,requestOptions:o}=e,s=yield n.connect();let i=s&&s[r];if(i){i=[...i];let e=i.shift();if(i.length){const r={};for(let e=0;e<i.length;e++){const n=i[e];if(r[e]=`{${n}}`,void 0===t[n])throw new Error(`\`${n}\` URL API argument is not specified`)}e&&(e=c(e,r))}if(t){const r=[],n=t.paramList;Array.isArray(n)&&n.forEach((([e,t])=>{r.push(`${e}=${t}`)}));for(const e in t)-1===i.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return n.makeQuery(e,t,((e,t)=>{for(var r in t||(t={}))h.call(t,r)&&p(e,r,t[r]);if(u)for(var r of u(t))d.call(t,r)&&p(e,r,t[r]);return e})({cacheName:r},o));throw new Error("Request URL is not set")}},new Promise(((e,o)=>{var s=e=>{try{a(n.next(e))}catch(t){o(t)}},i=e=>{try{a(n.throw(e))}catch(t){o(t)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(s,i);a((n=n.apply(t,r)).next())}));var t,r,n}var y="3.0.0-alpha.17";class m extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var g=Object.defineProperty,b=(e,t,r)=>((e,t,r)=>t in e?g(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class w extends Error{constructor(e){super(),b(this,"name","NgwError"),b(this,"title"),b(this,"message"),b(this,"detail"),b(this,"exception"),b(this,"status_code"),b(this,"data"),b(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,w.prototype)}}var v=Object.defineProperty,O=(e,t,r)=>((e,t,r)=>t in e?v(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class P extends w{constructor(e){super(e),O(this,"name","InsufficientPermissionsError"),O(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,P.prototype)}}var j=Object.defineProperty,x=(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);let E=class e extends w{constructor(t){super(t),x(this,"message","There is no response from the server or problem connecting to server."),x(this,"title","Network error"),x(this,"detail","Check network connectivity and try again later."),Object.setPrototypeOf(this,e.prototype)}};var R=Object.defineProperty,C=(e,t,r)=>((e,t,r)=>t in e?R(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class I extends Error{constructor(e){super(),C(this,"name","NetworkError"),Object.setPrototypeOf(this,I.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}var T=Object.defineProperty,_=(e,t,r)=>((e,t,r)=>t in e?T(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class k extends w{constructor(e){super(e),_(this,"name","ResourceNotFoundError"),_(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,k.prototype)}}var q=Object.freeze({__proto__:null,AbortError:m,InsufficientPermissionsError:P,NetworkError:I,NetworksResponseError:E,NgwError:w,ResourceNotFoundError:k,extractError:function(t){if(e(t))return t.name&&t.message&&t.title?{title:t.title,message:t.message,detail:t.detail||null,data:t.data&&t.data.data?t.data.data:null}:!t.exception||void 0!==t.status&&0!==t.status&&void 0!==t.data?{title:"string"==typeof t.title?t.title:"Unexpected error",message:"string"==typeof t.message?t.message:"Something went wrong."}:new E({title:t.title,status_code:t.status_code,exception:t.exception})},isError:function(t){return!!e(t)&&(t.status_code&&t.exception&&t.title)}}),U=Object.defineProperty,$=(e,t,r)=>((e,t,r)=>t in e?U(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class A extends Error{constructor(e){super(e||"Something went wrong."),$(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,A)}}class N extends A{constructor(e){super(e||"There is no response from the server or problem connecting to server."),$(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class S extends A{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class F extends A{constructor(e){super(e.message),$(this,"detail"),$(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class L extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),$(this,"title"),$(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,L)}}class B extends L{constructor(e){super("Long-running request was cancelled.",e)}}class G extends L{constructor(e){super(void 0,e)}}var H=Object.defineProperty,D=Object.getOwnPropertySymbols,z=Object.prototype.hasOwnProperty,M=Object.prototype.propertyIsEnumerable,Q=(e,t,r)=>t in e?H(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,J=(e,t)=>{for(var r in t||(t={}))z.call(t,r)&&Q(e,r,t[r]);if(D)for(var r of D(t))M.call(t,r)&&Q(e,r,t[r]);return e},W=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{a(r.next(e))}catch(t){o(t)}},i=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);a((r=r.apply(e,t)).next())}));function V(e){return W(this,null,(function*(){const t=yield function(e){return W(this,null,(function*(){try{return e.json()}catch(t){throw new Error}}))}(e);let r=t.delay_ms;const n=void 0!==t.retry_ms?t.retry_ms:2e3,o=`/api/lunkwill/${t.id}/summary`,s=`/api/lunkwill/${t.id}/response`,i=e=>new Promise((t=>setTimeout(t,e)));let a=!1,c=!1;for(;!c;){let e,t;yield i(a?n:r),a=!1;try{e=yield fetch(o,{credentials:"same-origin"}),t=yield e.json()}catch(l){a=!0;continue}switch(t.status){case void 0:throw new L(void 0,t);case"ready":c=!0;break;case"cancelled":throw new B(t);case"failed":throw new G(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new L(void 0,t)}}return s}))}const K=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function X(e,t){let r="";return void 0!==t&&(r="?"+function(e){const t=[];for(const[r,n]of Object.entries(e))if("string"==typeof n||"number"==typeof n||"boolean"==typeof n)t.push(`${r}=${encodeURIComponent(n)}`);else if(Array.isArray(n))t.push(`${r}=${n.map(encodeURIComponent).join(",")}`);else for(const[e,o]of Object.entries(n)){const n=`${r}[${encodeURIComponent(e)}]`;if("string"==typeof o||"number"==typeof o||"boolean"==typeof o)t.push(`${n}=${encodeURIComponent(o)}`);else if(Array.isArray(o)){const e=o.map(encodeURIComponent);t.push(`${n}=${e.join(",")}`)}}return t.join("&")}(t)),e+r}function Y(e,r,n){return W(this,null,(function*(){var o,s;const i=J(J({},{method:"GET",credentials:"same-origin",headers:{}}),r),{withCredentials:a,responseType:c,cacheProps:l,cacheName:u,lunkwill:h,cache:d,query:p,json:f}=i,y=((e,t)=>{var r={};for(var n in e)z.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&D)for(var n of D(e))t.indexOf(n)<0&&M.call(e,n)&&(r[n]=e[n]);return r})(i,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);y.method=null==(o=y.method)?void 0:o.toUpperCase(),a&&(y.credentials="include");let m=!1;void 0!==h&&(h.toHeaders(y.headers||{}),m=!0);const g=!!y.lunkwillReturnUrl;if(delete y.lunkwillReturnUrl,void 0!==f){y.body=JSON.stringify(f);const e=y.headers||{};e["Content-Type"]="application/json",y.headers=e}const b=X(e,p),w=()=>W(this,null,(function*(){let e;try{e=yield fetch(b,y)}catch(o){if(y.signal&&y.signal.aborted)throw o;throw new N}if(m&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield V(e);if(g)return t;e=yield function(e){return W(this,null,(function*(){try{return yield window.fetch(e,{credentials:"same-origin"})}catch(o){throw new N}}))}(t)}const t=e.headers.get("content-type"),r=t&&(t.includes("application/json")||t.includes("application/vnd.lunkwill.request-summary+json"));let n;try{const o=t&&K.test(t);if("blob"===c||o)n=yield e.blob();else{if(!r)throw new S;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof S)throw o;throw new S}if(400<=e.status&&e.status<=599)throw new F(n);return n}));if(n)if("GET"===(null==(s=y.method)?void 0:s.toUpperCase())){if(!1!==d){const e=l||J({},t({withCredentials:a,responseType:c}));return n.add(u||b,w,{props:e,expirationTime:d?void 0:500})}}else{["api/feature_layer/identify"].every((t=>!e.includes(t)))&&n.clean()}return w()}))}var Z=Object.defineProperty,ee=Object.defineProperties,te=Object.getOwnPropertyDescriptors,re=Object.getOwnPropertySymbols,ne=Object.prototype.hasOwnProperty,oe=Object.prototype.propertyIsEnumerable,se=(e,t,r)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,ie=(e,t)=>{for(var r in t||(t={}))ne.call(t,r)&&se(e,r,t[r]);if(re)for(var r of re(t))oe.call(t,r)&&se(e,r,t[r]);return e},ae=(e,t)=>{var r={};for(var n in e)ne.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&re)for(var n of re(e))t.indexOf(n)<0&&oe.call(e,n)&&(r[n]=e[n]);return r};function ce(e,t,n,...o){const[s,...i]=n[e],a=o[0];let c;if(void 0===a)c=[];else if("object"==typeof a&&null!==a){if(o.length>1)throw new Error("Too many arguments for route(name, object)!");c=[];for(const[e,t]of Object.entries(a))c[i.indexOf(e)]=String(t)}else c=o.map((e=>String(e)));return r(t+s.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),n=c[r];if(void 0===n){throw new Error(`Undefined parameter ${r} in "${s}".`)}return String(n)})))}function le(e,r,...n){const o={url:t=>{return o=this,s=null,i=function*(){var o;const s=yield r.connect();return X(ce(e,null!=(o=r.options.baseUrl)?o:"",s,...n),null==t?void 0:t.query)},new Promise(((e,t)=>{var r=e=>{try{a(i.next(e))}catch(r){t(r)}},n=e=>{try{a(i.throw(e))}catch(r){t(r)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,n);a((i=i.apply(o,s)).next())}));var o,s,i}},s=["get","post","put","delete","patch"];for(const i of s){const s=o=>{var s;const a=o||{},{headers:c}=a,l=ae(a,["headers"]);if(null==(s=null==o?void 0:o.signal)?void 0:s.aborted)throw new m;return r.connect().then((o=>{var s;const a=ce(e,null!=(s=r.options.baseUrl)?s:"",o,...n),u=t(ie(ie({},r.getAuthorizationHeaders()),null!=c?c:{}));return Y(a,(h=ie({headers:u},l),ee(h,te({method:i}))),r.cache);var h}))};o[i]=s}return o}function ue(e){return"[object Object]"===Object.prototype.toString.call(e)}let he;he=(e,t,r={},n,o)=>{r.method=r.method||"GET";const s=new XMLHttpRequest;s.open(r.method||"GET",e,!0),"blob"===r.responseType&&(s.responseType=r.responseType);const i=()=>{try{return JSON.parse(s.responseText)}catch(e){return s.responseText}};s.onload=()=>{-1!==[401,403,404,422,500].indexOf(s.status)&&n(new w(i())),((e=!1)=>{const o=e?n:t;"blob"===r.responseType?o(s.response):s.responseText?o(i()):n({message:""})})()},s.onerror=t=>{n(0===s.status?new I(e):t)},s.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 l in a){const e=a[l];"string"==typeof e&&s.setRequestHeader(l,e)}let c;if(void 0!==r.withCredentials&&(s.withCredentials=r.withCredentials),r.file){if(c=new FormData,c.append("file",r.file),r.data)for(const l in r.data)c.append(l,c[l])}else c=r.data?"string"==typeof r.data?r.data:JSON.stringify(r.data):null;o&&o((()=>{s.abort()})),s.send(c)};const de=[];function pe(e){de.push(e)}var fe=Object.defineProperty,ye=Object.defineProperties,me=Object.getOwnPropertyDescriptors,ge=Object.getOwnPropertySymbols,be=Object.prototype.hasOwnProperty,we=Object.prototype.propertyIsEnumerable,ve=(e,t,r)=>t in e?fe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Oe=(e,t)=>{for(var r in t||(t={}))be.call(t,r)&&ve(e,r,t[r]);if(ge)for(var r of ge(t))we.call(t,r)&&ve(e,r,t[r]);return e},Pe=(e,t,r)=>ve(e,"symbol"!=typeof t?t+"":t,r),je=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{a(r.next(e))}catch(t){o(t)}},i=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);a((r=r.apply(e,t)).next())}));let xe=0,Ee=0;class Re{constructor(e){this.options=e,Pe(this,"id",xe++),Pe(this,"emitter",new i),Pe(this,"user"),Pe(this,"cache"),Pe(this,"withCredentials"),Pe(this,"routeCache"),Pe(this,"client",`NextGIS-NGW-Connector/${y}`),Pe(this,"routeStr","/api/component/pyramid/route"),Pe(this,"activeRequests",{}),Pe(this,"requestTransform");const t=function(e){return de.find((t=>{if(t.options.baseUrl===e.baseUrl){if(!e.auth)return!0;if(t.options.auth)return n(t.options.auth,e.auth)}}))}(e);if(this.cache=new s({namespace:e.cacheId}),this.routeCache=new s({namespace:"routecache"}),t)return t;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),pe(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,pe(this)}connect(){return je(this,arguments,(function*({signal:e}={}){const t=this.options.auth;if(t){const{login:e,password:r}=t;e&&r&&(yield this._login({login:e,password:r}))}const r=`${this.routeStr}?client=${this.client}`;return this.routeCache.add(this.options.baseUrl||String(this.id),(()=>this.makeQuery(r,null,{signal:e,cache:!1})))}))}login(e,t){return this.logout(),pe(this),this._login(e,t)}logout(){this.abort(),function(e){const t=de.indexOf(e);-1!==t&&de.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.routeCache.clean(),this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return je(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=Oe({headers:this.getAuthorizationHeaders(e),cache:!0},t);return this.makeQuery("/api/component/auth/current_user",{},r)}))}getAuthorizationHeaders(e){const t=this.makeClientId(e);return t?{Authorization:`Basic ${t}`}:{}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,n=[t,r].map(encodeURIComponent).join(":");return window.btoa(n)}}abort(){for(const e of Object.values(this.activeRequests))e.abort();this.activeRequests={}}getActiveApiRequests(){return Oe({},this.activeRequests)}route(e,...t){return le(e,this,...t)}makeQuery(e,n){return je(this,arguments,(function*(e,n,o={}){var s;if(!(e=(this.options.baseUrl?this.options.baseUrl:"")+e))throw new Error("Empty `url` not allowed");if(n){const t=((e,t)=>{var r={};for(var n in e)be.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&ge)for(var n of ge(e))t.indexOf(n)<0&&we.call(e,n)&&(r[n]=e[n]);return r})(n,["paramList"]);e=c(e,t)}e=encodeURI(r(e)),o=Oe({withCredentials:this.withCredentials},o);const{cache:i,signal:a,method:l="GET",headers:u,cacheName:h,cacheProps:d,responseType:p,withCredentials:f}=o,y=new AbortController,g=y.signal;if(a){if(a.aborted)throw new m;a.addEventListener("abort",(()=>{y.abort()}))}o.signal=g;const b=()=>je(this,null,(function*(){const t=Ee++;this.activeRequests[t]=y;try{return this._loadData(e,o)}finally{this._cleanActiveRequest(t)}}));if("GET"===l&&!1!==i){const r=d||(w=Oe({},t({headers:u,withCredentials:f,responseType:p,baseUrl:this.options.baseUrl,userId:null==(s=this.user)?void 0:s.id})),ye(w,me({params:n})));return this.cache.add(h||e,b,{props:r,expirationTime:i?void 0:500})}var w;return b()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=Oe(Oe({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let s;he(e,r,t,n,(e=>{s=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==s&&s(),n(new m)}))})).catch((e=>{if("AbortError"!==e.name){"production"!==process.env.NODE_ENV&&console.warn("DEV WARN",e);const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return je(this,null,(function*(){try{const r=yield this.getUserInfo(e,t);return this.user=r,this.emitter.emit("login",r),r}catch(r){throw this.emitter.emit("login:error",r),r}}))}_cleanActiveRequest(e){delete this.activeRequests[e]}_handleHttpError(e){if(e&&e instanceof w){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new k(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new P(e)}return e}}Pe(Re,"errors",q);const Ce=["description"];function Ie(e,t=""){t=t?t+"__":"";const r={};for(const[n,s]of Object.entries(e))if(-1===Ce.indexOf(n))if(ue(s))if("owner_user"===n){const e=Ie(s,n);Object.assign(r,e)}else"parent"===n&&"id"in s&&(r.parent_id=s.id);else o(s)&&(r[t+n]=s);return r}var Te=Object.defineProperty,_e=Object.defineProperties,ke=Object.getOwnPropertyDescriptors,qe=Object.getOwnPropertySymbols,Ue=Object.prototype.hasOwnProperty,$e=Object.prototype.propertyIsEnumerable,Ae=(e,t,r)=>t in e?Te(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Ne=(e,t)=>{for(var r in t||(t={}))Ue.call(t,r)&&Ae(e,r,t[r]);if(qe)for(var r of qe(t))$e.call(t,r)&&Ae(e,r,t[r]);return e},Se=(e,t)=>_e(e,ke(t)),Fe=(e,t,r)=>Ae(e,"symbol"!=typeof t?t+"":t,r),Le=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{a(r.next(e))}catch(t){o(t)}},i=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);a((r=r.apply(e,t)).next())}));class Be{constructor({connector:e,cacheId:t}){Fe(this,"cache"),Fe(this,"connector"),this.connector=e,this.cache=new s({namespace:t})}getOne(e,t){const r=Ne({},t);return"string"==typeof e||"number"==typeof e||ue(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):ue(e)?this._fetchResourceBy(e,r):Promise.resolve(void 0)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new k}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||ue(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):Promise.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,Ie(e)),this.connector.route("resource.search").get(Se(Ne({},t),{query:Ne({serialization:"full"},r)})).then((e=>{if((null==t?void 0:t.cache)&&e)for(const t of e)this.cache.add("resource.item",Promise.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>{var r,n;return(null==(n=null==(r=null==e?void 0:e.resource)?void 0:r.parent)?void 0:n.id)?this.getOne(e.resource.parent.id,t):Promise.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){return Le(this,arguments,(function*(e,t,r=[]){let n;n="string"==typeof e?yield this.getId(e,t):"object"==typeof e?e.id:e;const o=yield this.connector.route("resource.collection").get(Se(Ne({},t),{query:{parent:n}})),s=[];for(const i of o)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(i),{id:i.resource.id}),r.push(i),(null==t?void 0:t.recursive)&&i.resource.children&&s.push(this._getChildrenOf(i.resource.id,t,r));return s.length?Promise.all(s).then((()=>r)):r}))}_cleanResourceItemCache(e){return Le(this,null,(function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;if(["resource.item","resource"].includes(o.key)&&void 0!==r)if("number"==typeof r)r===e&&n.push(o);else{(yield this.getId(r))===e&&n.push(o)}}for(const e of n)this.cache.delete(e)}))}_fetchResourceById(e,t){return this.connector.route("resource.item",{id:e}).get(t)}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(e){return Promise.all(this.cache.matchAll("resource.item")).then((t=>(t.filter((t=>{if(t)return e.keyname&&t.resource.keyname?e.keyname===t.resource.keyname:o(e.id)&&o(t.resource.id)?e.id===t.resource.id:n(e,t.resource)})),[])))}}var Ge=Object.defineProperty,He=(e,t,r)=>((e,t,r)=>t in e?Ge(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class De extends Re{constructor(e){super(e),He(this,"resources"),this.resources=new Be({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(e,r={},n={}){var o;r=null!=(o=n.params)?o:r;return f({name:e,params:t(r),requestOptions:n,connector:this})}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)}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)}}export{De as default};
1
+ import{isObject as e,objectRemoveEmpty as t,fixUrlStr as r,objectDeepEqual as n,defined as o}from"@nextgis/utils";import s from"@nextgis/cache";import{EventEmitter as i}from"events";const a=/\{ *([\w_-]+) *\}/g;function c(e,t){return e.replace(a,((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 l=Object.defineProperty,u=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,d=Object.prototype.propertyIsEnumerable,p=(e,t,r)=>t in e?l(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function f(e){return t=this,r=null,n=function*(){const{params:t,name:r,connector:n,requestOptions:o}=e,s=yield n.connect();let i=s&&s[r];if(i){i=[...i];let e=i.shift();if(i.length){const r={};for(let e=0;e<i.length;e++){const n=i[e];if(r[e]=`{${n}}`,void 0===t[n])throw new Error(`\`${n}\` URL API argument is not specified`)}e&&(e=c(e,r))}if(t){const r=[],n=t.paramList;Array.isArray(n)&&n.forEach((([e,t])=>{r.push(`${e}=${t}`)}));for(const e in t)-1===i.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return n.makeQuery(e,t,((e,t)=>{for(var r in t||(t={}))h.call(t,r)&&p(e,r,t[r]);if(u)for(var r of u(t))d.call(t,r)&&p(e,r,t[r]);return e})({cacheName:r},o));throw new Error("Request URL is not set")}},new Promise(((e,o)=>{var s=e=>{try{a(n.next(e))}catch(t){o(t)}},i=e=>{try{a(n.throw(e))}catch(t){o(t)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(s,i);a((n=n.apply(t,r)).next())}));var t,r,n}var y="3.0.0-alpha.18";class m extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var g=Object.defineProperty,b=(e,t,r)=>((e,t,r)=>t in e?g(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class w extends Error{constructor(e){super(),b(this,"name","NgwError"),b(this,"title"),b(this,"message"),b(this,"detail"),b(this,"exception"),b(this,"status_code"),b(this,"data"),b(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,w.prototype)}}var v=Object.defineProperty,O=(e,t,r)=>((e,t,r)=>t in e?v(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class P extends w{constructor(e){super(e),O(this,"name","InsufficientPermissionsError"),O(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,P.prototype)}}var j=Object.defineProperty,x=(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);let E=class e extends w{constructor(t){super(t),x(this,"message","There is no response from the server or problem connecting to server."),x(this,"title","Network error"),x(this,"detail","Check network connectivity and try again later."),Object.setPrototypeOf(this,e.prototype)}};var R=Object.defineProperty,C=(e,t,r)=>((e,t,r)=>t in e?R(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class I extends Error{constructor(e){super(),C(this,"name","NetworkError"),Object.setPrototypeOf(this,I.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}var T=Object.defineProperty,_=(e,t,r)=>((e,t,r)=>t in e?T(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class k extends w{constructor(e){super(e),_(this,"name","ResourceNotFoundError"),_(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,k.prototype)}}var q=Object.freeze({__proto__:null,AbortError:m,InsufficientPermissionsError:P,NetworkError:I,NetworksResponseError:E,NgwError:w,ResourceNotFoundError:k,extractError:function(t){if(e(t))return t.name&&t.message&&t.title?{title:t.title,message:t.message,detail:t.detail||null,data:t.data&&t.data.data?t.data.data:null}:!t.exception||void 0!==t.status&&0!==t.status&&void 0!==t.data?{title:"string"==typeof t.title?t.title:"Unexpected error",message:"string"==typeof t.message?t.message:"Something went wrong."}:new E({title:t.title,status_code:t.status_code,exception:t.exception})},isError:function(t){return!!e(t)&&(t.status_code&&t.exception&&t.title)}}),U=Object.defineProperty,$=(e,t,r)=>((e,t,r)=>t in e?U(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class A extends Error{constructor(e){super(e||"Something went wrong."),$(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,A)}}class N extends A{constructor(e){super(e||"There is no response from the server or problem connecting to server."),$(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class S extends A{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class F extends A{constructor(e){super(e.message),$(this,"detail"),$(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class L extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),$(this,"title"),$(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,L)}}class B extends L{constructor(e){super("Long-running request was cancelled.",e)}}class G extends L{constructor(e){super(void 0,e)}}var H=Object.defineProperty,D=Object.getOwnPropertySymbols,z=Object.prototype.hasOwnProperty,M=Object.prototype.propertyIsEnumerable,Q=(e,t,r)=>t in e?H(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,J=(e,t)=>{for(var r in t||(t={}))z.call(t,r)&&Q(e,r,t[r]);if(D)for(var r of D(t))M.call(t,r)&&Q(e,r,t[r]);return e},W=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{a(r.next(e))}catch(t){o(t)}},i=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);a((r=r.apply(e,t)).next())}));function V(e){return W(this,null,(function*(){const t=yield function(e){return W(this,null,(function*(){try{return e.json()}catch(t){throw new Error}}))}(e);let r=t.delay_ms;const n=void 0!==t.retry_ms?t.retry_ms:2e3,o=`/api/lunkwill/${t.id}/summary`,s=`/api/lunkwill/${t.id}/response`,i=e=>new Promise((t=>setTimeout(t,e)));let a=!1,c=!1;for(;!c;){let e,t;yield i(a?n:r),a=!1;try{e=yield fetch(o,{credentials:"same-origin"}),t=yield e.json()}catch(l){a=!0;continue}switch(t.status){case void 0:throw new L(void 0,t);case"ready":c=!0;break;case"cancelled":throw new B(t);case"failed":throw new G(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new L(void 0,t)}}return s}))}const K=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function X(e,t){let r="";return void 0!==t&&(r="?"+function(e){const t=[];for(const[r,n]of Object.entries(e))if("string"==typeof n||"number"==typeof n||"boolean"==typeof n)t.push(`${r}=${encodeURIComponent(n)}`);else if(Array.isArray(n))t.push(`${r}=${n.map(encodeURIComponent).join(",")}`);else for(const[e,o]of Object.entries(n)){const n=`${r}[${encodeURIComponent(e)}]`;if("string"==typeof o||"number"==typeof o||"boolean"==typeof o)t.push(`${n}=${encodeURIComponent(o)}`);else if(Array.isArray(o)){const e=o.map(encodeURIComponent);t.push(`${n}=${e.join(",")}`)}}return t.join("&")}(t)),e+r}function Y(e,r,n){return W(this,null,(function*(){var o,s;const i=J(J({},{method:"GET",credentials:"same-origin",headers:{}}),r),{withCredentials:a,responseType:c,cacheProps:l,cacheName:u,lunkwill:h,cache:d,query:p,json:f}=i,y=((e,t)=>{var r={};for(var n in e)z.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&D)for(var n of D(e))t.indexOf(n)<0&&M.call(e,n)&&(r[n]=e[n]);return r})(i,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);y.method=null==(o=y.method)?void 0:o.toUpperCase(),a&&(y.credentials="include");let m=!1;void 0!==h&&(h.toHeaders(y.headers||{}),m=!0);const g=!!y.lunkwillReturnUrl;if(delete y.lunkwillReturnUrl,void 0!==f){y.body=JSON.stringify(f);const e=y.headers||{};e["Content-Type"]="application/json",y.headers=e}const b=X(e,p),w=()=>W(this,null,(function*(){let e;try{e=yield fetch(b,y)}catch(o){if(y.signal&&y.signal.aborted)throw o;throw new N}if(m&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield V(e);if(g)return t;e=yield function(e){return W(this,null,(function*(){try{return yield window.fetch(e,{credentials:"same-origin"})}catch(o){throw new N}}))}(t)}const t=e.headers.get("content-type"),r=t&&(t.includes("application/json")||t.includes("application/vnd.lunkwill.request-summary+json"));let n;try{const o=t&&K.test(t);if("blob"===c||o)n=yield e.blob();else{if(!r)throw new S;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof S)throw o;throw new S}if(400<=e.status&&e.status<=599)throw new F(n);return n}));if(n)if("GET"===(null==(s=y.method)?void 0:s.toUpperCase())){if(!1!==d){const e=l||J({},t({withCredentials:a,responseType:c}));return n.add(u||b,w,{props:e,expirationTime:d?void 0:500})}}else{["api/feature_layer/identify"].every((t=>!e.includes(t)))&&n.clean()}return w()}))}var Z=Object.defineProperty,ee=Object.defineProperties,te=Object.getOwnPropertyDescriptors,re=Object.getOwnPropertySymbols,ne=Object.prototype.hasOwnProperty,oe=Object.prototype.propertyIsEnumerable,se=(e,t,r)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,ie=(e,t)=>{for(var r in t||(t={}))ne.call(t,r)&&se(e,r,t[r]);if(re)for(var r of re(t))oe.call(t,r)&&se(e,r,t[r]);return e},ae=(e,t)=>{var r={};for(var n in e)ne.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&re)for(var n of re(e))t.indexOf(n)<0&&oe.call(e,n)&&(r[n]=e[n]);return r};function ce(e,t,n,...o){const[s,...i]=n[e],a=o[0];let c;if(void 0===a)c=[];else if("object"==typeof a&&null!==a){if(o.length>1)throw new Error("Too many arguments for route(name, object)!");c=[];for(const[e,t]of Object.entries(a))c[i.indexOf(e)]=String(t)}else c=o.map((e=>String(e)));return r(t+s.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),n=c[r];if(void 0===n){throw new Error(`Undefined parameter ${r} in "${s}".`)}return String(n)})))}function le(e,r,...n){const o={url:t=>{return o=this,s=null,i=function*(){var o;const s=yield r.connect();return X(ce(e,null!=(o=r.options.baseUrl)?o:"",s,...n),null==t?void 0:t.query)},new Promise(((e,t)=>{var r=e=>{try{a(i.next(e))}catch(r){t(r)}},n=e=>{try{a(i.throw(e))}catch(r){t(r)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,n);a((i=i.apply(o,s)).next())}));var o,s,i}},s=["get","post","put","delete","patch"];for(const i of s){const s=o=>{var s;const a=o||{},{headers:c}=a,l=ae(a,["headers"]);if(null==(s=null==o?void 0:o.signal)?void 0:s.aborted)throw new m;return r.connect().then((o=>{var s;const a=ce(e,null!=(s=r.options.baseUrl)?s:"",o,...n),u=t(ie(ie({},r.getAuthorizationHeaders()),null!=c?c:{}));return Y(a,(h=ie({headers:u},l),ee(h,te({method:i}))),r.cache);var h}))};o[i]=s}return o}function ue(e){return"[object Object]"===Object.prototype.toString.call(e)}let he;he=(e,t,r={},n,o)=>{r.method=r.method||"GET";const s=new XMLHttpRequest;s.open(r.method||"GET",e,!0),"blob"===r.responseType&&(s.responseType=r.responseType);const i=()=>{try{return JSON.parse(s.responseText)}catch(e){return s.responseText}};s.onload=()=>{-1!==[401,403,404,422,500].indexOf(s.status)&&n(new w(i())),((e=!1)=>{const o=e?n:t;"blob"===r.responseType?o(s.response):s.responseText?o(i()):n({message:""})})()},s.onerror=t=>{n(0===s.status?new I(e):t)},s.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 l in a){const e=a[l];"string"==typeof e&&s.setRequestHeader(l,e)}let c;if(void 0!==r.withCredentials&&(s.withCredentials=r.withCredentials),r.file){if(c=new FormData,c.append("file",r.file),r.data)for(const l in r.data)c.append(l,c[l])}else c=r.data?"string"==typeof r.data?r.data:JSON.stringify(r.data):null;o&&o((()=>{s.abort()})),s.send(c)};const de=[];function pe(e){de.push(e)}var fe=Object.defineProperty,ye=Object.defineProperties,me=Object.getOwnPropertyDescriptors,ge=Object.getOwnPropertySymbols,be=Object.prototype.hasOwnProperty,we=Object.prototype.propertyIsEnumerable,ve=(e,t,r)=>t in e?fe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Oe=(e,t)=>{for(var r in t||(t={}))be.call(t,r)&&ve(e,r,t[r]);if(ge)for(var r of ge(t))we.call(t,r)&&ve(e,r,t[r]);return e},Pe=(e,t,r)=>ve(e,"symbol"!=typeof t?t+"":t,r),je=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{a(r.next(e))}catch(t){o(t)}},i=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);a((r=r.apply(e,t)).next())}));let xe=0,Ee=0;class Re{constructor(e){this.options=e,Pe(this,"id",xe++),Pe(this,"emitter",new i),Pe(this,"user"),Pe(this,"cache"),Pe(this,"withCredentials"),Pe(this,"routeCache"),Pe(this,"client",`NextGIS-NGW-Connector/${y}`),Pe(this,"routeStr","/api/component/pyramid/route"),Pe(this,"activeRequests",{}),Pe(this,"requestTransform");const t=function(e){return de.find((t=>{if(t.options.baseUrl===e.baseUrl){if(!e.auth)return!0;if(t.options.auth)return n(t.options.auth,e.auth)}}))}(e);if(this.cache=new s({namespace:e.cacheId}),this.routeCache=new s({namespace:"routecache"}),t)return t;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),pe(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,pe(this)}connect(){return je(this,arguments,(function*({signal:e}={}){const t=this.options.auth;if(t){const{login:e,password:r}=t;e&&r&&(yield this._login({login:e,password:r}))}const r=`${this.routeStr}?client=${this.client}`;return this.routeCache.add(this.options.baseUrl||String(this.id),(()=>this.makeQuery(r,null,{signal:e,cache:!1})))}))}login(e,t){return this.logout(),pe(this),this._login(e,t)}logout(){this.abort(),function(e){const t=de.indexOf(e);-1!==t&&de.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.routeCache.clean(),this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return je(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=Oe({headers:this.getAuthorizationHeaders(e),cache:!0},t);return this.makeQuery("/api/component/auth/current_user",{},r)}))}getAuthorizationHeaders(e){const t=this.makeClientId(e);return t?{Authorization:`Basic ${t}`}:{}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,n=[t,r].map(encodeURIComponent).join(":");return window.btoa(n)}}abort(){for(const e of Object.values(this.activeRequests))e.abort();this.activeRequests={}}getActiveApiRequests(){return Oe({},this.activeRequests)}route(e,...t){return le(e,this,...t)}makeQuery(e,n){return je(this,arguments,(function*(e,n,o={}){var s;if(!(e=(this.options.baseUrl?this.options.baseUrl:"")+e))throw new Error("Empty `url` not allowed");if(n){const t=((e,t)=>{var r={};for(var n in e)be.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&ge)for(var n of ge(e))t.indexOf(n)<0&&we.call(e,n)&&(r[n]=e[n]);return r})(n,["paramList"]);e=c(e,t)}e=encodeURI(r(e)),o=Oe({withCredentials:this.withCredentials},o);const{cache:i,signal:a,method:l="GET",headers:u,cacheName:h,cacheProps:d,responseType:p,withCredentials:f}=o,y=new AbortController,g=y.signal;if(a){if(a.aborted)throw new m;a.addEventListener("abort",(()=>{y.abort()}))}o.signal=g;const b=()=>je(this,null,(function*(){const t=Ee++;this.activeRequests[t]=y;try{return this._loadData(e,o)}finally{this._cleanActiveRequest(t)}}));if("GET"===l&&!1!==i){const r=d||(w=Oe({},t({headers:u,withCredentials:f,responseType:p,baseUrl:this.options.baseUrl,userId:null==(s=this.user)?void 0:s.id})),ye(w,me({params:n})));return this.cache.add(h||e,b,{props:r,expirationTime:i?void 0:500})}var w;return b()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=Oe(Oe({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let s;he(e,r,t,n,(e=>{s=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==s&&s(),n(new m)}))})).catch((e=>{if("AbortError"!==e.name){"production"!==process.env.NODE_ENV&&console.warn("DEV WARN",e);const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return je(this,null,(function*(){try{const r=yield this.getUserInfo(e,t);return this.user=r,this.emitter.emit("login",r),r}catch(r){throw this.emitter.emit("login:error",r),r}}))}_cleanActiveRequest(e){delete this.activeRequests[e]}_handleHttpError(e){if(e&&e instanceof w){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new k(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new P(e)}return e}}Pe(Re,"errors",q);const Ce=["description"];function Ie(e,t=""){t=t?t+"__":"";const r={};for(const[n,s]of Object.entries(e))if(-1===Ce.indexOf(n))if(ue(s))if("owner_user"===n){const e=Ie(s,n);Object.assign(r,e)}else"parent"===n&&"id"in s&&(r.parent_id=s.id);else o(s)&&(r[t+n]=s);return r}var Te=Object.defineProperty,_e=Object.defineProperties,ke=Object.getOwnPropertyDescriptors,qe=Object.getOwnPropertySymbols,Ue=Object.prototype.hasOwnProperty,$e=Object.prototype.propertyIsEnumerable,Ae=(e,t,r)=>t in e?Te(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Ne=(e,t)=>{for(var r in t||(t={}))Ue.call(t,r)&&Ae(e,r,t[r]);if(qe)for(var r of qe(t))$e.call(t,r)&&Ae(e,r,t[r]);return e},Se=(e,t)=>_e(e,ke(t)),Fe=(e,t,r)=>Ae(e,"symbol"!=typeof t?t+"":t,r),Le=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{a(r.next(e))}catch(t){o(t)}},i=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);a((r=r.apply(e,t)).next())}));class Be{constructor({connector:e,cacheId:t}){Fe(this,"cache"),Fe(this,"connector"),this.connector=e,this.cache=new s({namespace:t})}getOne(e,t){const r=Ne({},t);return"string"==typeof e||"number"==typeof e||ue(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):ue(e)?this._fetchResourceBy(e,r):Promise.resolve(void 0)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new k}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||ue(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):Promise.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,Ie(e)),this.connector.route("resource.search").get(Se(Ne({},t),{query:Ne({serialization:"full"},r)})).then((e=>{if((null==t?void 0:t.cache)&&e)for(const t of e)this.cache.add("resource.item",Promise.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>{var r,n;return(null==(n=null==(r=null==e?void 0:e.resource)?void 0:r.parent)?void 0:n.id)?this.getOne(e.resource.parent.id,t):Promise.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){return Le(this,arguments,(function*(e,t,r=[]){let n;n="string"==typeof e?yield this.getId(e,t):"object"==typeof e?e.id:e;const o=yield this.connector.route("resource.collection").get(Se(Ne({},t),{query:{parent:n}})),s=[];for(const i of o)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(i),{id:i.resource.id}),r.push(i),(null==t?void 0:t.recursive)&&i.resource.children&&s.push(this._getChildrenOf(i.resource.id,t,r));return s.length?Promise.all(s).then((()=>r)):r}))}_cleanResourceItemCache(e){return Le(this,null,(function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;if(["resource.item","resource"].includes(o.key)&&void 0!==r)if("number"==typeof r)r===e&&n.push(o);else{(yield this.getId(r))===e&&n.push(o)}}for(const e of n)this.cache.delete(e)}))}_fetchResourceById(e,t){return this.connector.route("resource.item",{id:e}).get(t)}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(e){return Promise.all(this.cache.matchAll("resource.item")).then((t=>(t.filter((t=>{if(t)return e.keyname&&t.resource.keyname?e.keyname===t.resource.keyname:o(e.id)&&o(t.resource.id)?e.id===t.resource.id:n(e,t.resource)})),[])))}}var Ge=Object.defineProperty,He=(e,t,r)=>((e,t,r)=>t in e?Ge(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class De extends Re{constructor(e){super(e),He(this,"resources"),this.resources=new Be({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(e,r={},n={}){var o;r=null!=(o=n.params)?o:r;return f({name:e,params:t(r),requestOptions:n,connector:this})}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)}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)}}export{De as default};
2
2
  //# sourceMappingURL=ngw-connector.esm-bundler.prod.js.map
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.17; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.18; author: NextGIS */
2
2
  var NgwConnector = (function () {
3
3
  'use strict';
4
4
 
@@ -776,7 +776,7 @@ var NgwConnector = (function () {
776
776
 
777
777
  var pkg = {
778
778
  name: "@nextgis/ngw-connector",
779
- version: "3.0.0-alpha.17",
779
+ version: "3.0.0-alpha.18",
780
780
  _priority: 12,
781
781
  description: "A lightweight HTTP client optimized for use with NextGIS Web API",
782
782
  main: "index.js",
@@ -1,2 +1,2 @@
1
- var NgwConnector=function(){"use strict";function e(e){return null!=e}function t(t){return"string"==typeof t?!!t:e(t)}function r(e,t,r,o){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===o:n(e,t));return e===t}function n(e,t){const n=Object.keys(e).sort(),o=Object.keys(t).sort();if(n.length!==o.length)return!1;if(n.join("")!==o.join(""))return!1;for(let i=0;i<n.length;i++){if(!r(e[n[i]],t[o[i]],e,t))return!1}return!0}function o(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]=o(e[r])})),t}function i(e){return"[object Object]"===Object.prototype.toString.call(e)}function s(e){return e.replace(/([^:]\/)\/+/g,"$1")}const a=/\{ *([\w_-]+) *\}/g;function c(e,t){return e.replace(a,((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 u=Object.defineProperty,l=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable,p=(e,t,r)=>t in e?u(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function d(e){return t=this,r=null,n=function*(){const{params:t,name:r,connector:n,requestOptions:o}=e,i=yield n.connect();let s=i&&i[r];if(s){s=[...s];let e=s.shift();if(s.length){const r={};for(let e=0;e<s.length;e++){const n=s[e];if(r[e]=`{${n}}`,void 0===t[n])throw new Error(`\`${n}\` URL API argument is not specified`)}e&&(e=c(e,r))}if(t){const r=[],n=t.paramList;Array.isArray(n)&&n.forEach((([e,t])=>{r.push(`${e}=${t}`)}));for(const e in t)-1===s.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return n.makeQuery(e,t,((e,t)=>{for(var r in t||(t={}))h.call(t,r)&&p(e,r,t[r]);if(l)for(var r of l(t))f.call(t,r)&&p(e,r,t[r]);return e})({cacheName:r},o));throw new Error("Request URL is not set")}},new Promise(((e,o)=>{var i=e=>{try{a(n.next(e))}catch(t){o(t)}},s=e=>{try{a(n.throw(e))}catch(t){o(t)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(i,s);a((n=n.apply(t,r)).next())}));var t,r,n}var y=Object.defineProperty,m=(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);const v=class e{constructor(t={}){var r;m(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,n){return"object"==typeof r&&"valueToSet"in r&&(r.onlyFull=!0),this.add(e,t,r,!0,n)}add(e,r,n,i,s){let a,c;"object"==typeof n&&"valueToSet"in n?(a=n,i=a.onlyFull,s=a.expirationTime,c=a.props):c=n;const u=this._find(e,c);if(u)return u.value;{let n;n=r instanceof Function?r():r;const a=c&&JSON.parse(JSON.stringify(o(c)));s=s?Date.now()+s:void 0;const u={key:e,value:n,props:a,expirationTime:s};return i&&!t(n)?n:(this.cache.push(u),n instanceof Promise?(n.catch((()=>{this.delete(e,c)})),i&&n.then((r=>(t(r)||this.delete(e,c),r))),n):n)}}match(e,t){const r=this._find(e,t);if(!(r&&r.expirationTime&&Date.now()>r.expirationTime))return null==r?void 0:r.value;this.delete(r)}matchAll(e,t){const r=Date.now();return this.cache.forEach((e=>{e.expirationTime&&r>e.expirationTime&&this.delete(e)})),e?this.cache.filter((r=>this._filter(r,e,t))).map((e=>e.value)):this.cache.map((e=>e.value))}delete(e,t){const r="string"==typeof e?this.cache.filter((r=>this._filter(r,e,t))):[e];for(const n of r){const e=this.cache.indexOf(n);-1!==e&&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)return!1;if(!r)return!0;return n(e.props||{},o(r))}};m(v,"instance",{});let g=v;function w(){}function b(){b.init.call(this)}function O(e){return void 0===e._maxListeners?b.defaultMaxListeners:e._maxListeners}function j(e,t,r,n){var o,i,s,a;if("function"!=typeof r)throw new TypeError('"listener" argument must be a function');if((i=e._events)?(i.newListener&&(e.emit("newListener",t,r.listener?r.listener:r),i=e._events),s=i[t]):(i=e._events=new w,e._eventsCount=0),s){if("function"==typeof s?s=i[t]=n?[r,s]:[s,r]:n?s.unshift(r):s.push(r),!s.warned&&(o=O(e))&&o>0&&s.length>o){s.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+s.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=s.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else s=i[t]=r,++e._eventsCount;return e}function x(e,t,r){var n=!1;function o(){e.removeListener(t,o),n||(n=!0,r.apply(e,arguments))}return o.listener=r,o}function _(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 P(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}w.prototype=Object.create(null),b.EventEmitter=b,b.usingDomains=!1,b.prototype.domain=void 0,b.prototype._events=void 0,b.prototype._maxListeners=void 0,b.defaultMaxListeners=10,b.init=function(){this.domain=null,b.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new w,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},b.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},b.prototype.getMaxListeners=function(){return O(this)},b.prototype.emit=function(e){var t,r,n,o,i,s,a,c="error"===e;if(s=this._events)c=c&&null==s.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var u=new Error('Uncaught, unspecified "error" event. ('+t+")");throw u.context=t,u}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(r=s[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,o=P(e,n),i=0;i<n;++i)o[i].call(r)}(r,l,this);break;case 2:!function(e,t,r,n){if(t)e.call(r,n);else for(var o=e.length,i=P(e,o),s=0;s<o;++s)i[s].call(r,n)}(r,l,this,arguments[1]);break;case 3:!function(e,t,r,n,o){if(t)e.call(r,n,o);else for(var i=e.length,s=P(e,i),a=0;a<i;++a)s[a].call(r,n,o)}(r,l,this,arguments[1],arguments[2]);break;case 4:!function(e,t,r,n,o,i){if(t)e.call(r,n,o,i);else for(var s=e.length,a=P(e,s),c=0;c<s;++c)a[c].call(r,n,o,i)}(r,l,this,arguments[1],arguments[2],arguments[3]);break;default:for(o=new Array(n-1),i=1;i<n;i++)o[i-1]=arguments[i];!function(e,t,r,n){if(t)e.apply(r,n);else for(var o=e.length,i=P(e,o),s=0;s<o;++s)i[s].apply(r,n)}(r,l,this,o)}return!0},b.prototype.on=b.prototype.addListener=function(e,t){return j(this,e,t,!1)},b.prototype.prependListener=function(e,t){return j(this,e,t,!0)},b.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,x(this,e,t)),this},b.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,x(this,e,t)),this},b.prototype.removeListener=function(e,t){var r,n,o,i,s;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(r=n[e]))return this;if(r===t||r.listener&&r.listener===t)0==--this._eventsCount?this._events=new w:(delete n[e],n.removeListener&&this.emit("removeListener",e,r.listener||t));else if("function"!=typeof r){for(o=-1,i=r.length;i-- >0;)if(r[i]===t||r[i].listener&&r[i].listener===t){s=r[i].listener,o=i;break}if(o<0)return this;if(1===r.length){if(r[0]=void 0,0==--this._eventsCount)return this._events=new w,this;delete n[e]}else!function(e,t){for(var r=t,n=r+1,o=e.length;n<o;r+=1,n+=1)e[r]=e[n];e.pop()}(r,o);n.removeListener&&this.emit("removeListener",e,s||t)}return this},b.prototype.off=function(e,t){return this.removeListener(e,t)},b.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new w,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new w:delete r[e]),this;if(0===arguments.length){for(var n,o=Object.keys(r),i=0;i<o.length;++i)"removeListener"!==(n=o[i])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new w,this._eventsCount=0,this}if("function"==typeof(t=r[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},b.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):[]},b.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):_.call(e,t)},b.prototype.listenerCount=_,b.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var E="3.0.0-alpha.17";class C extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var R=Object.defineProperty,T=(e,t,r)=>((e,t,r)=>t in e?R(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class k extends Error{constructor(e){super(),T(this,"name","NgwError"),T(this,"title"),T(this,"message"),T(this,"detail"),T(this,"exception"),T(this,"status_code"),T(this,"data"),T(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,k.prototype)}}var I=Object.defineProperty,L=(e,t,r)=>((e,t,r)=>t in e?I(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class q extends k{constructor(e){super(e),L(this,"name","InsufficientPermissionsError"),L(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,q.prototype)}}var A=Object.defineProperty,U=(e,t,r)=>((e,t,r)=>t in e?A(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let S=class e extends k{constructor(t){super(t),U(this,"message","There is no response from the server or problem connecting to server."),U(this,"title","Network error"),U(this,"detail","Check network connectivity and try again later."),Object.setPrototypeOf(this,e.prototype)}};var N=Object.defineProperty,$=(e,t,r)=>((e,t,r)=>t in e?N(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class F extends Error{constructor(e){super(),$(this,"name","NetworkError"),Object.setPrototypeOf(this,F.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}var D=Object.defineProperty,B=(e,t,r)=>((e,t,r)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class G extends k{constructor(e){super(e),B(this,"name","ResourceNotFoundError"),B(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,G.prototype)}}var H=Object.freeze({__proto__:null,AbortError:C,InsufficientPermissionsError:q,NetworkError:F,NetworksResponseError:S,NgwError:k,ResourceNotFoundError:G,extractError:function(e){if(i(e))return e.name&&e.message&&e.title?{title:e.title,message:e.message,detail:e.detail||null,data:e.data&&e.data.data?e.data.data:null}:!e.exception||void 0!==e.status&&0!==e.status&&void 0!==e.data?{title:"string"==typeof e.title?e.title:"Unexpected error",message:"string"==typeof e.message?e.message:"Something went wrong."}:new S({title:e.title,status_code:e.status_code,exception:e.exception})},isError:function(e){return!!i(e)&&(e.status_code&&e.exception&&e.title)}}),M=Object.defineProperty,z=(e,t,r)=>((e,t,r)=>t in e?M(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class J extends Error{constructor(e){super(e||"Something went wrong."),z(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,J)}}class Q extends J{constructor(e){super(e||"There is no response from the server or problem connecting to server."),z(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class W extends J{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class K extends J{constructor(e){super(e.message),z(this,"detail"),z(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class X extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),z(this,"title"),z(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,X)}}class V extends X{constructor(e){super("Long-running request was cancelled.",e)}}class Y extends X{constructor(e){super(void 0,e)}}var Z=Object.defineProperty,ee=Object.getOwnPropertySymbols,te=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable,ne=(e,t,r)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,oe=(e,t)=>{for(var r in t||(t={}))te.call(t,r)&&ne(e,r,t[r]);if(ee)for(var r of ee(t))re.call(t,r)&&ne(e,r,t[r]);return e},ie=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));function se(e){return ie(this,null,(function*(){const t=yield function(e){return ie(this,null,(function*(){try{return e.json()}catch(t){throw new Error}}))}(e);let r=t.delay_ms;const n=void 0!==t.retry_ms?t.retry_ms:2e3,o=`/api/lunkwill/${t.id}/summary`,i=`/api/lunkwill/${t.id}/response`,s=e=>new Promise((t=>setTimeout(t,e)));let a=!1,c=!1;for(;!c;){let e,t;yield s(a?n:r),a=!1;try{e=yield fetch(o,{credentials:"same-origin"}),t=yield e.json()}catch(u){a=!0;continue}switch(t.status){case void 0:throw new X(void 0,t);case"ready":c=!0;break;case"cancelled":throw new V(t);case"failed":throw new Y(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new X(void 0,t)}}return i}))}const ae=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function ce(e,t){let r="";return void 0!==t&&(r="?"+function(e){const t=[];for(const[r,n]of Object.entries(e))if("string"==typeof n||"number"==typeof n||"boolean"==typeof n)t.push(`${r}=${encodeURIComponent(n)}`);else if(Array.isArray(n))t.push(`${r}=${n.map(encodeURIComponent).join(",")}`);else for(const[e,o]of Object.entries(n)){const n=`${r}[${encodeURIComponent(e)}]`;if("string"==typeof o||"number"==typeof o||"boolean"==typeof o)t.push(`${n}=${encodeURIComponent(o)}`);else if(Array.isArray(o)){const e=o.map(encodeURIComponent);t.push(`${n}=${e.join(",")}`)}}return t.join("&")}(t)),e+r}function ue(e,t,r){return ie(this,null,(function*(){var n,i;const s=oe(oe({},{method:"GET",credentials:"same-origin",headers:{}}),t),{withCredentials:a,responseType:c,cacheProps:u,cacheName:l,lunkwill:h,cache:f,query:p,json:d}=s,y=((e,t)=>{var r={};for(var n in e)te.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&ee)for(var n of ee(e))t.indexOf(n)<0&&re.call(e,n)&&(r[n]=e[n]);return r})(s,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);y.method=null==(n=y.method)?void 0:n.toUpperCase(),a&&(y.credentials="include");let m=!1;void 0!==h&&(h.toHeaders(y.headers||{}),m=!0);const v=!!y.lunkwillReturnUrl;if(delete y.lunkwillReturnUrl,void 0!==d){y.body=JSON.stringify(d);const e=y.headers||{};e["Content-Type"]="application/json",y.headers=e}const g=ce(e,p),w=()=>ie(this,null,(function*(){let e;try{e=yield fetch(g,y)}catch(o){if(y.signal&&y.signal.aborted)throw o;throw new Q}if(m&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield se(e);if(v)return t;e=yield function(e){return ie(this,null,(function*(){try{return yield window.fetch(e,{credentials:"same-origin"})}catch(o){throw new Q}}))}(t)}const t=e.headers.get("content-type"),r=t&&(t.includes("application/json")||t.includes("application/vnd.lunkwill.request-summary+json"));let n;try{const o=t&&ae.test(t);if("blob"===c||o)n=yield e.blob();else{if(!r)throw new W;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof W)throw o;throw new W}if(400<=e.status&&e.status<=599)throw new K(n);return n}));if(r)if("GET"===(null==(i=y.method)?void 0:i.toUpperCase())){if(!1!==f){const e=u||oe({},o({withCredentials:a,responseType:c}));return r.add(l||g,w,{props:e,expirationTime:f?void 0:500})}}else{["api/feature_layer/identify"].every((t=>!e.includes(t)))&&r.clean()}return w()}))}var le=Object.defineProperty,he=Object.defineProperties,fe=Object.getOwnPropertyDescriptors,pe=Object.getOwnPropertySymbols,de=Object.prototype.hasOwnProperty,ye=Object.prototype.propertyIsEnumerable,me=(e,t,r)=>t in e?le(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,ve=(e,t)=>{for(var r in t||(t={}))de.call(t,r)&&me(e,r,t[r]);if(pe)for(var r of pe(t))ye.call(t,r)&&me(e,r,t[r]);return e},ge=(e,t)=>{var r={};for(var n in e)de.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&pe)for(var n of pe(e))t.indexOf(n)<0&&ye.call(e,n)&&(r[n]=e[n]);return r};function we(e,t,r,...n){const[o,...i]=r[e],a=n[0];let c;if(void 0===a)c=[];else if("object"==typeof a&&null!==a){if(n.length>1)throw new Error("Too many arguments for route(name, object)!");c=[];for(const[e,t]of Object.entries(a))c[i.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return s(t+o.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),n=c[r];if(void 0===n){throw new Error(`Undefined parameter ${r} in "${o}".`)}return String(n)})))}function be(e,t,...r){const n={url:n=>{return o=this,i=null,s=function*(){var o;const i=yield t.connect();return ce(we(e,null!=(o=t.options.baseUrl)?o:"",i,...r),null==n?void 0:n.query)},new Promise(((e,t)=>{var r=e=>{try{a(s.next(e))}catch(r){t(r)}},n=e=>{try{a(s.throw(e))}catch(r){t(r)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,n);a((s=s.apply(o,i)).next())}));var o,i,s}},i=["get","post","put","delete","patch"];for(const s of i){const i=n=>{var i;const a=n||{},{headers:c}=a,u=ge(a,["headers"]);if(null==(i=null==n?void 0:n.signal)?void 0:i.aborted)throw new C;return t.connect().then((n=>{var i;const a=we(e,null!=(i=t.options.baseUrl)?i:"",n,...r),l=o(ve(ve({},t.getAuthorizationHeaders()),null!=c?c:{}));return ue(a,(h=ve({headers:l},u),he(h,fe({method:s}))),t.cache);var h}))};n[s]=i}return n}function Oe(e){return"[object Object]"===Object.prototype.toString.call(e)}let je;je=(e,t,r={},n,o)=>{r.method=r.method||"GET";const i=new XMLHttpRequest;i.open(r.method||"GET",e,!0),"blob"===r.responseType&&(i.responseType=r.responseType);const s=()=>{try{return JSON.parse(i.responseText)}catch(e){return i.responseText}};i.onload=()=>{-1!==[401,403,404,422,500].indexOf(i.status)&&n(new k(s())),((e=!1)=>{const o=e?n:t;"blob"===r.responseType?o(i.response):i.responseText?o(s()):n({message:""})})()},i.onerror=t=>{n(0===i.status?new F(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;o&&o((()=>{i.abort()})),i.send(c)};const xe=[];function _e(e){xe.push(e)}var Pe=Object.defineProperty,Ee=Object.defineProperties,Ce=Object.getOwnPropertyDescriptors,Re=Object.getOwnPropertySymbols,Te=Object.prototype.hasOwnProperty,ke=Object.prototype.propertyIsEnumerable,Ie=(e,t,r)=>t in e?Pe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Le=(e,t)=>{for(var r in t||(t={}))Te.call(t,r)&&Ie(e,r,t[r]);if(Re)for(var r of Re(t))ke.call(t,r)&&Ie(e,r,t[r]);return e},qe=(e,t,r)=>Ie(e,"symbol"!=typeof t?t+"":t,r),Ae=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));let Ue=0,Se=0;class Ne{constructor(e){this.options=e,qe(this,"id",Ue++),qe(this,"emitter",new b),qe(this,"user"),qe(this,"cache"),qe(this,"withCredentials"),qe(this,"routeCache"),qe(this,"client",`NextGIS-NGW-Connector/${E}`),qe(this,"routeStr","/api/component/pyramid/route"),qe(this,"activeRequests",{}),qe(this,"requestTransform");const t=function(e){return xe.find((t=>{if(t.options.baseUrl===e.baseUrl){if(!e.auth)return!0;if(t.options.auth)return n(t.options.auth,e.auth)}}))}(e);if(this.cache=new g({namespace:e.cacheId}),this.routeCache=new g({namespace:"routecache"}),t)return t;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),_e(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,_e(this)}connect(){return Ae(this,arguments,(function*({signal:e}={}){const t=this.options.auth;if(t){const{login:e,password:r}=t;e&&r&&(yield this._login({login:e,password:r}))}const r=`${this.routeStr}?client=${this.client}`;return this.routeCache.add(this.options.baseUrl||String(this.id),(()=>this.makeQuery(r,null,{signal:e,cache:!1})))}))}login(e,t){return this.logout(),_e(this),this._login(e,t)}logout(){this.abort(),function(e){const t=xe.indexOf(e);-1!==t&&xe.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.routeCache.clean(),this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return Ae(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=Le({headers:this.getAuthorizationHeaders(e),cache:!0},t);return this.makeQuery("/api/component/auth/current_user",{},r)}))}getAuthorizationHeaders(e){const t=this.makeClientId(e);return t?{Authorization:`Basic ${t}`}:{}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,n=[t,r].map(encodeURIComponent).join(":");return window.btoa(n)}}abort(){for(const e of Object.values(this.activeRequests))e.abort();this.activeRequests={}}getActiveApiRequests(){return Le({},this.activeRequests)}route(e,...t){return be(e,this,...t)}makeQuery(e,t){return Ae(this,arguments,(function*(e,t,r={}){var n;if(!(e=(this.options.baseUrl?this.options.baseUrl:"")+e))throw new Error("Empty `url` not allowed");if(t){const r=((e,t)=>{var r={};for(var n in e)Te.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&Re)for(var n of Re(e))t.indexOf(n)<0&&ke.call(e,n)&&(r[n]=e[n]);return r})(t,["paramList"]);e=c(e,r)}e=encodeURI(s(e)),r=Le({withCredentials:this.withCredentials},r);const{cache:i,signal:a,method:u="GET",headers:l,cacheName:h,cacheProps:f,responseType:p,withCredentials:d}=r,y=new AbortController,m=y.signal;if(a){if(a.aborted)throw new C;a.addEventListener("abort",(()=>{y.abort()}))}r.signal=m;const v=()=>Ae(this,null,(function*(){const t=Se++;this.activeRequests[t]=y;try{return this._loadData(e,r)}finally{this._cleanActiveRequest(t)}}));if("GET"===u&&!1!==i){const r=f||(g=Le({},o({headers:l,withCredentials:d,responseType:p,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id})),Ee(g,Ce({params:t})));return this.cache.add(h||e,v,{props:r,expirationTime:i?void 0:500})}var g;return v()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=Le(Le({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let i;je(e,r,t,n,(e=>{i=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==i&&i(),n(new C)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return Ae(this,null,(function*(){try{const r=yield this.getUserInfo(e,t);return this.user=r,this.emitter.emit("login",r),r}catch(r){throw this.emitter.emit("login:error",r),r}}))}_cleanActiveRequest(e){delete this.activeRequests[e]}_handleHttpError(e){if(e&&e instanceof k){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new G(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new q(e)}return e}}qe(Ne,"errors",H);const $e=["description"];function Fe(t,r=""){r=r?r+"__":"";const n={};for(const[o,i]of Object.entries(t))if(-1===$e.indexOf(o))if(Oe(i))if("owner_user"===o){const e=Fe(i,o);Object.assign(n,e)}else"parent"===o&&"id"in i&&(n.parent_id=i.id);else e(i)&&(n[r+o]=i);return n}var De=Object.defineProperty,Be=Object.defineProperties,Ge=Object.getOwnPropertyDescriptors,He=Object.getOwnPropertySymbols,Me=Object.prototype.hasOwnProperty,ze=Object.prototype.propertyIsEnumerable,Je=(e,t,r)=>t in e?De(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Qe=(e,t)=>{for(var r in t||(t={}))Me.call(t,r)&&Je(e,r,t[r]);if(He)for(var r of He(t))ze.call(t,r)&&Je(e,r,t[r]);return e},We=(e,t)=>Be(e,Ge(t)),Ke=(e,t,r)=>Je(e,"symbol"!=typeof t?t+"":t,r),Xe=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));class Ve{constructor({connector:e,cacheId:t}){Ke(this,"cache"),Ke(this,"connector"),this.connector=e,this.cache=new g({namespace:t})}getOne(e,t){const r=Qe({},t);return"string"==typeof e||"number"==typeof e||Oe(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):Oe(e)?this._fetchResourceBy(e,r):Promise.resolve(void 0)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new G}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||Oe(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):Promise.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,Fe(e)),this.connector.route("resource.search").get(We(Qe({},t),{query:Qe({serialization:"full"},r)})).then((e=>{if((null==t?void 0:t.cache)&&e)for(const t of e)this.cache.add("resource.item",Promise.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>{var r,n;return(null==(n=null==(r=null==e?void 0:e.resource)?void 0:r.parent)?void 0:n.id)?this.getOne(e.resource.parent.id,t):Promise.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){return Xe(this,arguments,(function*(e,t,r=[]){let n;n="string"==typeof e?yield this.getId(e,t):"object"==typeof e?e.id:e;const o=yield this.connector.route("resource.collection").get(We(Qe({},t),{query:{parent:n}})),i=[];for(const s of o)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(s),{id:s.resource.id}),r.push(s),(null==t?void 0:t.recursive)&&s.resource.children&&i.push(this._getChildrenOf(s.resource.id,t,r));return i.length?Promise.all(i).then((()=>r)):r}))}_cleanResourceItemCache(e){return Xe(this,null,(function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;if(["resource.item","resource"].includes(o.key)&&void 0!==r)if("number"==typeof r)r===e&&n.push(o);else{(yield this.getId(r))===e&&n.push(o)}}for(const e of n)this.cache.delete(e)}))}_fetchResourceById(e,t){return this.connector.route("resource.item",{id:e}).get(t)}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(t){return Promise.all(this.cache.matchAll("resource.item")).then((r=>(r.filter((r=>{if(r)return t.keyname&&r.resource.keyname?t.keyname===r.resource.keyname:e(t.id)&&e(r.resource.id)?t.id===r.resource.id:n(t,r.resource)})),[])))}}var Ye=Object.defineProperty,Ze=(e,t,r)=>((e,t,r)=>t in e?Ye(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);return class extends Ne{constructor(e){super(e),Ze(this,"resources"),this.resources=new Ve({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(e,t={},r={}){var n;return d({name:e,params:o(t=null!=(n=r.params)?n:t),requestOptions:r,connector:this})}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)}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)}}}();
1
+ var NgwConnector=function(){"use strict";function e(e){return null!=e}function t(t){return"string"==typeof t?!!t:e(t)}function r(e,t,r,o){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===o:n(e,t));return e===t}function n(e,t){const n=Object.keys(e).sort(),o=Object.keys(t).sort();if(n.length!==o.length)return!1;if(n.join("")!==o.join(""))return!1;for(let i=0;i<n.length;i++){if(!r(e[n[i]],t[o[i]],e,t))return!1}return!0}function o(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]=o(e[r])})),t}function i(e){return"[object Object]"===Object.prototype.toString.call(e)}function s(e){return e.replace(/([^:]\/)\/+/g,"$1")}const a=/\{ *([\w_-]+) *\}/g;function c(e,t){return e.replace(a,((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 u=Object.defineProperty,l=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable,p=(e,t,r)=>t in e?u(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function d(e){return t=this,r=null,n=function*(){const{params:t,name:r,connector:n,requestOptions:o}=e,i=yield n.connect();let s=i&&i[r];if(s){s=[...s];let e=s.shift();if(s.length){const r={};for(let e=0;e<s.length;e++){const n=s[e];if(r[e]=`{${n}}`,void 0===t[n])throw new Error(`\`${n}\` URL API argument is not specified`)}e&&(e=c(e,r))}if(t){const r=[],n=t.paramList;Array.isArray(n)&&n.forEach((([e,t])=>{r.push(`${e}=${t}`)}));for(const e in t)-1===s.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return n.makeQuery(e,t,((e,t)=>{for(var r in t||(t={}))h.call(t,r)&&p(e,r,t[r]);if(l)for(var r of l(t))f.call(t,r)&&p(e,r,t[r]);return e})({cacheName:r},o));throw new Error("Request URL is not set")}},new Promise(((e,o)=>{var i=e=>{try{a(n.next(e))}catch(t){o(t)}},s=e=>{try{a(n.throw(e))}catch(t){o(t)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(i,s);a((n=n.apply(t,r)).next())}));var t,r,n}var y=Object.defineProperty,m=(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);const v=class e{constructor(t={}){var r;m(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,n){return"object"==typeof r&&"valueToSet"in r&&(r.onlyFull=!0),this.add(e,t,r,!0,n)}add(e,r,n,i,s){let a,c;"object"==typeof n&&"valueToSet"in n?(a=n,i=a.onlyFull,s=a.expirationTime,c=a.props):c=n;const u=this._find(e,c);if(u)return u.value;{let n;n=r instanceof Function?r():r;const a=c&&JSON.parse(JSON.stringify(o(c)));s=s?Date.now()+s:void 0;const u={key:e,value:n,props:a,expirationTime:s};return i&&!t(n)?n:(this.cache.push(u),n instanceof Promise?(n.catch((()=>{this.delete(e,c)})),i&&n.then((r=>(t(r)||this.delete(e,c),r))),n):n)}}match(e,t){const r=this._find(e,t);if(!(r&&r.expirationTime&&Date.now()>r.expirationTime))return null==r?void 0:r.value;this.delete(r)}matchAll(e,t){const r=Date.now();return this.cache.forEach((e=>{e.expirationTime&&r>e.expirationTime&&this.delete(e)})),e?this.cache.filter((r=>this._filter(r,e,t))).map((e=>e.value)):this.cache.map((e=>e.value))}delete(e,t){const r="string"==typeof e?this.cache.filter((r=>this._filter(r,e,t))):[e];for(const n of r){const e=this.cache.indexOf(n);-1!==e&&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)return!1;if(!r)return!0;return n(e.props||{},o(r))}};m(v,"instance",{});let g=v;function w(){}function b(){b.init.call(this)}function O(e){return void 0===e._maxListeners?b.defaultMaxListeners:e._maxListeners}function j(e,t,r,n){var o,i,s,a;if("function"!=typeof r)throw new TypeError('"listener" argument must be a function');if((i=e._events)?(i.newListener&&(e.emit("newListener",t,r.listener?r.listener:r),i=e._events),s=i[t]):(i=e._events=new w,e._eventsCount=0),s){if("function"==typeof s?s=i[t]=n?[r,s]:[s,r]:n?s.unshift(r):s.push(r),!s.warned&&(o=O(e))&&o>0&&s.length>o){s.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+s.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=s.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else s=i[t]=r,++e._eventsCount;return e}function x(e,t,r){var n=!1;function o(){e.removeListener(t,o),n||(n=!0,r.apply(e,arguments))}return o.listener=r,o}function _(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 P(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}w.prototype=Object.create(null),b.EventEmitter=b,b.usingDomains=!1,b.prototype.domain=void 0,b.prototype._events=void 0,b.prototype._maxListeners=void 0,b.defaultMaxListeners=10,b.init=function(){this.domain=null,b.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new w,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},b.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},b.prototype.getMaxListeners=function(){return O(this)},b.prototype.emit=function(e){var t,r,n,o,i,s,a,c="error"===e;if(s=this._events)c=c&&null==s.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var u=new Error('Uncaught, unspecified "error" event. ('+t+")");throw u.context=t,u}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(r=s[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,o=P(e,n),i=0;i<n;++i)o[i].call(r)}(r,l,this);break;case 2:!function(e,t,r,n){if(t)e.call(r,n);else for(var o=e.length,i=P(e,o),s=0;s<o;++s)i[s].call(r,n)}(r,l,this,arguments[1]);break;case 3:!function(e,t,r,n,o){if(t)e.call(r,n,o);else for(var i=e.length,s=P(e,i),a=0;a<i;++a)s[a].call(r,n,o)}(r,l,this,arguments[1],arguments[2]);break;case 4:!function(e,t,r,n,o,i){if(t)e.call(r,n,o,i);else for(var s=e.length,a=P(e,s),c=0;c<s;++c)a[c].call(r,n,o,i)}(r,l,this,arguments[1],arguments[2],arguments[3]);break;default:for(o=new Array(n-1),i=1;i<n;i++)o[i-1]=arguments[i];!function(e,t,r,n){if(t)e.apply(r,n);else for(var o=e.length,i=P(e,o),s=0;s<o;++s)i[s].apply(r,n)}(r,l,this,o)}return!0},b.prototype.on=b.prototype.addListener=function(e,t){return j(this,e,t,!1)},b.prototype.prependListener=function(e,t){return j(this,e,t,!0)},b.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,x(this,e,t)),this},b.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,x(this,e,t)),this},b.prototype.removeListener=function(e,t){var r,n,o,i,s;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(r=n[e]))return this;if(r===t||r.listener&&r.listener===t)0==--this._eventsCount?this._events=new w:(delete n[e],n.removeListener&&this.emit("removeListener",e,r.listener||t));else if("function"!=typeof r){for(o=-1,i=r.length;i-- >0;)if(r[i]===t||r[i].listener&&r[i].listener===t){s=r[i].listener,o=i;break}if(o<0)return this;if(1===r.length){if(r[0]=void 0,0==--this._eventsCount)return this._events=new w,this;delete n[e]}else!function(e,t){for(var r=t,n=r+1,o=e.length;n<o;r+=1,n+=1)e[r]=e[n];e.pop()}(r,o);n.removeListener&&this.emit("removeListener",e,s||t)}return this},b.prototype.off=function(e,t){return this.removeListener(e,t)},b.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new w,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new w:delete r[e]),this;if(0===arguments.length){for(var n,o=Object.keys(r),i=0;i<o.length;++i)"removeListener"!==(n=o[i])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new w,this._eventsCount=0,this}if("function"==typeof(t=r[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},b.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):[]},b.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):_.call(e,t)},b.prototype.listenerCount=_,b.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var E="3.0.0-alpha.18";class C extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var R=Object.defineProperty,T=(e,t,r)=>((e,t,r)=>t in e?R(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class k extends Error{constructor(e){super(),T(this,"name","NgwError"),T(this,"title"),T(this,"message"),T(this,"detail"),T(this,"exception"),T(this,"status_code"),T(this,"data"),T(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,k.prototype)}}var I=Object.defineProperty,L=(e,t,r)=>((e,t,r)=>t in e?I(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class q extends k{constructor(e){super(e),L(this,"name","InsufficientPermissionsError"),L(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,q.prototype)}}var A=Object.defineProperty,U=(e,t,r)=>((e,t,r)=>t in e?A(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let S=class e extends k{constructor(t){super(t),U(this,"message","There is no response from the server or problem connecting to server."),U(this,"title","Network error"),U(this,"detail","Check network connectivity and try again later."),Object.setPrototypeOf(this,e.prototype)}};var N=Object.defineProperty,$=(e,t,r)=>((e,t,r)=>t in e?N(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class F extends Error{constructor(e){super(),$(this,"name","NetworkError"),Object.setPrototypeOf(this,F.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}var D=Object.defineProperty,B=(e,t,r)=>((e,t,r)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class G extends k{constructor(e){super(e),B(this,"name","ResourceNotFoundError"),B(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,G.prototype)}}var H=Object.freeze({__proto__:null,AbortError:C,InsufficientPermissionsError:q,NetworkError:F,NetworksResponseError:S,NgwError:k,ResourceNotFoundError:G,extractError:function(e){if(i(e))return e.name&&e.message&&e.title?{title:e.title,message:e.message,detail:e.detail||null,data:e.data&&e.data.data?e.data.data:null}:!e.exception||void 0!==e.status&&0!==e.status&&void 0!==e.data?{title:"string"==typeof e.title?e.title:"Unexpected error",message:"string"==typeof e.message?e.message:"Something went wrong."}:new S({title:e.title,status_code:e.status_code,exception:e.exception})},isError:function(e){return!!i(e)&&(e.status_code&&e.exception&&e.title)}}),M=Object.defineProperty,z=(e,t,r)=>((e,t,r)=>t in e?M(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class J extends Error{constructor(e){super(e||"Something went wrong."),z(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,J)}}class Q extends J{constructor(e){super(e||"There is no response from the server or problem connecting to server."),z(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class W extends J{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class K extends J{constructor(e){super(e.message),z(this,"detail"),z(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class X extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),z(this,"title"),z(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,X)}}class V extends X{constructor(e){super("Long-running request was cancelled.",e)}}class Y extends X{constructor(e){super(void 0,e)}}var Z=Object.defineProperty,ee=Object.getOwnPropertySymbols,te=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable,ne=(e,t,r)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,oe=(e,t)=>{for(var r in t||(t={}))te.call(t,r)&&ne(e,r,t[r]);if(ee)for(var r of ee(t))re.call(t,r)&&ne(e,r,t[r]);return e},ie=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));function se(e){return ie(this,null,(function*(){const t=yield function(e){return ie(this,null,(function*(){try{return e.json()}catch(t){throw new Error}}))}(e);let r=t.delay_ms;const n=void 0!==t.retry_ms?t.retry_ms:2e3,o=`/api/lunkwill/${t.id}/summary`,i=`/api/lunkwill/${t.id}/response`,s=e=>new Promise((t=>setTimeout(t,e)));let a=!1,c=!1;for(;!c;){let e,t;yield s(a?n:r),a=!1;try{e=yield fetch(o,{credentials:"same-origin"}),t=yield e.json()}catch(u){a=!0;continue}switch(t.status){case void 0:throw new X(void 0,t);case"ready":c=!0;break;case"cancelled":throw new V(t);case"failed":throw new Y(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new X(void 0,t)}}return i}))}const ae=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function ce(e,t){let r="";return void 0!==t&&(r="?"+function(e){const t=[];for(const[r,n]of Object.entries(e))if("string"==typeof n||"number"==typeof n||"boolean"==typeof n)t.push(`${r}=${encodeURIComponent(n)}`);else if(Array.isArray(n))t.push(`${r}=${n.map(encodeURIComponent).join(",")}`);else for(const[e,o]of Object.entries(n)){const n=`${r}[${encodeURIComponent(e)}]`;if("string"==typeof o||"number"==typeof o||"boolean"==typeof o)t.push(`${n}=${encodeURIComponent(o)}`);else if(Array.isArray(o)){const e=o.map(encodeURIComponent);t.push(`${n}=${e.join(",")}`)}}return t.join("&")}(t)),e+r}function ue(e,t,r){return ie(this,null,(function*(){var n,i;const s=oe(oe({},{method:"GET",credentials:"same-origin",headers:{}}),t),{withCredentials:a,responseType:c,cacheProps:u,cacheName:l,lunkwill:h,cache:f,query:p,json:d}=s,y=((e,t)=>{var r={};for(var n in e)te.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&ee)for(var n of ee(e))t.indexOf(n)<0&&re.call(e,n)&&(r[n]=e[n]);return r})(s,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);y.method=null==(n=y.method)?void 0:n.toUpperCase(),a&&(y.credentials="include");let m=!1;void 0!==h&&(h.toHeaders(y.headers||{}),m=!0);const v=!!y.lunkwillReturnUrl;if(delete y.lunkwillReturnUrl,void 0!==d){y.body=JSON.stringify(d);const e=y.headers||{};e["Content-Type"]="application/json",y.headers=e}const g=ce(e,p),w=()=>ie(this,null,(function*(){let e;try{e=yield fetch(g,y)}catch(o){if(y.signal&&y.signal.aborted)throw o;throw new Q}if(m&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield se(e);if(v)return t;e=yield function(e){return ie(this,null,(function*(){try{return yield window.fetch(e,{credentials:"same-origin"})}catch(o){throw new Q}}))}(t)}const t=e.headers.get("content-type"),r=t&&(t.includes("application/json")||t.includes("application/vnd.lunkwill.request-summary+json"));let n;try{const o=t&&ae.test(t);if("blob"===c||o)n=yield e.blob();else{if(!r)throw new W;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof W)throw o;throw new W}if(400<=e.status&&e.status<=599)throw new K(n);return n}));if(r)if("GET"===(null==(i=y.method)?void 0:i.toUpperCase())){if(!1!==f){const e=u||oe({},o({withCredentials:a,responseType:c}));return r.add(l||g,w,{props:e,expirationTime:f?void 0:500})}}else{["api/feature_layer/identify"].every((t=>!e.includes(t)))&&r.clean()}return w()}))}var le=Object.defineProperty,he=Object.defineProperties,fe=Object.getOwnPropertyDescriptors,pe=Object.getOwnPropertySymbols,de=Object.prototype.hasOwnProperty,ye=Object.prototype.propertyIsEnumerable,me=(e,t,r)=>t in e?le(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,ve=(e,t)=>{for(var r in t||(t={}))de.call(t,r)&&me(e,r,t[r]);if(pe)for(var r of pe(t))ye.call(t,r)&&me(e,r,t[r]);return e},ge=(e,t)=>{var r={};for(var n in e)de.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&pe)for(var n of pe(e))t.indexOf(n)<0&&ye.call(e,n)&&(r[n]=e[n]);return r};function we(e,t,r,...n){const[o,...i]=r[e],a=n[0];let c;if(void 0===a)c=[];else if("object"==typeof a&&null!==a){if(n.length>1)throw new Error("Too many arguments for route(name, object)!");c=[];for(const[e,t]of Object.entries(a))c[i.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return s(t+o.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),n=c[r];if(void 0===n){throw new Error(`Undefined parameter ${r} in "${o}".`)}return String(n)})))}function be(e,t,...r){const n={url:n=>{return o=this,i=null,s=function*(){var o;const i=yield t.connect();return ce(we(e,null!=(o=t.options.baseUrl)?o:"",i,...r),null==n?void 0:n.query)},new Promise(((e,t)=>{var r=e=>{try{a(s.next(e))}catch(r){t(r)}},n=e=>{try{a(s.throw(e))}catch(r){t(r)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,n);a((s=s.apply(o,i)).next())}));var o,i,s}},i=["get","post","put","delete","patch"];for(const s of i){const i=n=>{var i;const a=n||{},{headers:c}=a,u=ge(a,["headers"]);if(null==(i=null==n?void 0:n.signal)?void 0:i.aborted)throw new C;return t.connect().then((n=>{var i;const a=we(e,null!=(i=t.options.baseUrl)?i:"",n,...r),l=o(ve(ve({},t.getAuthorizationHeaders()),null!=c?c:{}));return ue(a,(h=ve({headers:l},u),he(h,fe({method:s}))),t.cache);var h}))};n[s]=i}return n}function Oe(e){return"[object Object]"===Object.prototype.toString.call(e)}let je;je=(e,t,r={},n,o)=>{r.method=r.method||"GET";const i=new XMLHttpRequest;i.open(r.method||"GET",e,!0),"blob"===r.responseType&&(i.responseType=r.responseType);const s=()=>{try{return JSON.parse(i.responseText)}catch(e){return i.responseText}};i.onload=()=>{-1!==[401,403,404,422,500].indexOf(i.status)&&n(new k(s())),((e=!1)=>{const o=e?n:t;"blob"===r.responseType?o(i.response):i.responseText?o(s()):n({message:""})})()},i.onerror=t=>{n(0===i.status?new F(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;o&&o((()=>{i.abort()})),i.send(c)};const xe=[];function _e(e){xe.push(e)}var Pe=Object.defineProperty,Ee=Object.defineProperties,Ce=Object.getOwnPropertyDescriptors,Re=Object.getOwnPropertySymbols,Te=Object.prototype.hasOwnProperty,ke=Object.prototype.propertyIsEnumerable,Ie=(e,t,r)=>t in e?Pe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Le=(e,t)=>{for(var r in t||(t={}))Te.call(t,r)&&Ie(e,r,t[r]);if(Re)for(var r of Re(t))ke.call(t,r)&&Ie(e,r,t[r]);return e},qe=(e,t,r)=>Ie(e,"symbol"!=typeof t?t+"":t,r),Ae=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));let Ue=0,Se=0;class Ne{constructor(e){this.options=e,qe(this,"id",Ue++),qe(this,"emitter",new b),qe(this,"user"),qe(this,"cache"),qe(this,"withCredentials"),qe(this,"routeCache"),qe(this,"client",`NextGIS-NGW-Connector/${E}`),qe(this,"routeStr","/api/component/pyramid/route"),qe(this,"activeRequests",{}),qe(this,"requestTransform");const t=function(e){return xe.find((t=>{if(t.options.baseUrl===e.baseUrl){if(!e.auth)return!0;if(t.options.auth)return n(t.options.auth,e.auth)}}))}(e);if(this.cache=new g({namespace:e.cacheId}),this.routeCache=new g({namespace:"routecache"}),t)return t;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),_e(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,_e(this)}connect(){return Ae(this,arguments,(function*({signal:e}={}){const t=this.options.auth;if(t){const{login:e,password:r}=t;e&&r&&(yield this._login({login:e,password:r}))}const r=`${this.routeStr}?client=${this.client}`;return this.routeCache.add(this.options.baseUrl||String(this.id),(()=>this.makeQuery(r,null,{signal:e,cache:!1})))}))}login(e,t){return this.logout(),_e(this),this._login(e,t)}logout(){this.abort(),function(e){const t=xe.indexOf(e);-1!==t&&xe.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.routeCache.clean(),this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return Ae(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=Le({headers:this.getAuthorizationHeaders(e),cache:!0},t);return this.makeQuery("/api/component/auth/current_user",{},r)}))}getAuthorizationHeaders(e){const t=this.makeClientId(e);return t?{Authorization:`Basic ${t}`}:{}}makeClientId(e){if(e=e||this.options.auth){const{login:t,password:r}=e,n=[t,r].map(encodeURIComponent).join(":");return window.btoa(n)}}abort(){for(const e of Object.values(this.activeRequests))e.abort();this.activeRequests={}}getActiveApiRequests(){return Le({},this.activeRequests)}route(e,...t){return be(e,this,...t)}makeQuery(e,t){return Ae(this,arguments,(function*(e,t,r={}){var n;if(!(e=(this.options.baseUrl?this.options.baseUrl:"")+e))throw new Error("Empty `url` not allowed");if(t){const r=((e,t)=>{var r={};for(var n in e)Te.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&Re)for(var n of Re(e))t.indexOf(n)<0&&ke.call(e,n)&&(r[n]=e[n]);return r})(t,["paramList"]);e=c(e,r)}e=encodeURI(s(e)),r=Le({withCredentials:this.withCredentials},r);const{cache:i,signal:a,method:u="GET",headers:l,cacheName:h,cacheProps:f,responseType:p,withCredentials:d}=r,y=new AbortController,m=y.signal;if(a){if(a.aborted)throw new C;a.addEventListener("abort",(()=>{y.abort()}))}r.signal=m;const v=()=>Ae(this,null,(function*(){const t=Se++;this.activeRequests[t]=y;try{return this._loadData(e,r)}finally{this._cleanActiveRequest(t)}}));if("GET"===u&&!1!==i){const r=f||(g=Le({},o({headers:l,withCredentials:d,responseType:p,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id})),Ee(g,Ce({params:t})));return this.cache.add(h||e,v,{props:r,expirationTime:i?void 0:500})}var g;return v()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=Le(Le({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let i;je(e,r,t,n,(e=>{i=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==i&&i(),n(new C)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return Ae(this,null,(function*(){try{const r=yield this.getUserInfo(e,t);return this.user=r,this.emitter.emit("login",r),r}catch(r){throw this.emitter.emit("login:error",r),r}}))}_cleanActiveRequest(e){delete this.activeRequests[e]}_handleHttpError(e){if(e&&e instanceof k){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new G(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new q(e)}return e}}qe(Ne,"errors",H);const $e=["description"];function Fe(t,r=""){r=r?r+"__":"";const n={};for(const[o,i]of Object.entries(t))if(-1===$e.indexOf(o))if(Oe(i))if("owner_user"===o){const e=Fe(i,o);Object.assign(n,e)}else"parent"===o&&"id"in i&&(n.parent_id=i.id);else e(i)&&(n[r+o]=i);return n}var De=Object.defineProperty,Be=Object.defineProperties,Ge=Object.getOwnPropertyDescriptors,He=Object.getOwnPropertySymbols,Me=Object.prototype.hasOwnProperty,ze=Object.prototype.propertyIsEnumerable,Je=(e,t,r)=>t in e?De(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Qe=(e,t)=>{for(var r in t||(t={}))Me.call(t,r)&&Je(e,r,t[r]);if(He)for(var r of He(t))ze.call(t,r)&&Je(e,r,t[r]);return e},We=(e,t)=>Be(e,Ge(t)),Ke=(e,t,r)=>Je(e,"symbol"!=typeof t?t+"":t,r),Xe=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{a(r.next(e))}catch(t){o(t)}},s=e=>{try{a(r.throw(e))}catch(t){o(t)}},a=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,s);a((r=r.apply(e,t)).next())}));class Ve{constructor({connector:e,cacheId:t}){Ke(this,"cache"),Ke(this,"connector"),this.connector=e,this.cache=new g({namespace:t})}getOne(e,t){const r=Qe({},t);return"string"==typeof e||"number"==typeof e||Oe(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):Oe(e)?this._fetchResourceBy(e,r):Promise.resolve(void 0)}getOneOrFail(e,t){return this.getOne(e,t).then((e=>{if(e)return e;throw new G}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||Oe(e)?this.getOne(e,t).then((e=>{if(e)return e.resource.id})):Promise.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,Fe(e)),this.connector.route("resource.search").get(We(Qe({},t),{query:Qe({serialization:"full"},r)})).then((e=>{if((null==t?void 0:t.cache)&&e)for(const t of e)this.cache.add("resource.item",Promise.resolve(t),{id:t.resource.id});return e}))}return r}))}getParent(e,t){return this.getOne(e,t).then((e=>{var r,n;return(null==(n=null==(r=null==e?void 0:e.resource)?void 0:r.parent)?void 0:n.id)?this.getOne(e.resource.parent.id,t):Promise.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){return Xe(this,arguments,(function*(e,t,r=[]){let n;n="string"==typeof e?yield this.getId(e,t):"object"==typeof e?e.id:e;const o=yield this.connector.route("resource.collection").get(We(Qe({},t),{query:{parent:n}})),i=[];for(const s of o)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(s),{id:s.resource.id}),r.push(s),(null==t?void 0:t.recursive)&&s.resource.children&&i.push(this._getChildrenOf(s.resource.id,t,r));return i.length?Promise.all(i).then((()=>r)):r}))}_cleanResourceItemCache(e){return Xe(this,null,(function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;if(["resource.item","resource"].includes(o.key)&&void 0!==r)if("number"==typeof r)r===e&&n.push(o);else{(yield this.getId(r))===e&&n.push(o)}}for(const e of n)this.cache.delete(e)}))}_fetchResourceById(e,t){return this.connector.route("resource.item",{id:e}).get(t)}_fetchResourceBy(e,t){return this.getMany(e,t).then((e=>e[0]))}_resourceCacheFilter(t){return Promise.all(this.cache.matchAll("resource.item")).then((r=>(r.filter((r=>{if(r)return t.keyname&&r.resource.keyname?t.keyname===r.resource.keyname:e(t.id)&&e(r.resource.id)?t.id===r.resource.id:n(t,r.resource)})),[])))}}var Ye=Object.defineProperty,Ze=(e,t,r)=>((e,t,r)=>t in e?Ye(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);return class extends Ne{constructor(e){super(e),Ze(this,"resources"),this.resources=new Ve({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(e,t={},r={}){var n;return d({name:e,params:o(t=null!=(n=r.params)?n:t),requestOptions:r,connector:this})}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)}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)}}}();
2
2
  //# sourceMappingURL=ngw-connector.global.prod.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextgis/ngw-connector",
3
- "version": "3.0.0-alpha.18",
3
+ "version": "3.0.0-alpha.19",
4
4
  "_priority": 12,
5
5
  "description": "A lightweight HTTP client optimized for use with NextGIS Web API",
6
6
  "main": "index.js",
@@ -54,5 +54,5 @@
54
54
  "engines": {
55
55
  "node": ">=18.20.4"
56
56
  },
57
- "gitHead": "0de9eccaa82b43981b9463d78a3ef1a135b13b55"
57
+ "gitHead": "9fb7d36c5da4abe919840d1e300b2c510c40e489"
58
58
  }