@nextgis/ngw-connector 3.0.0-alpha.7 → 3.0.0-alpha.8
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 +1 -0
- package/lib/ngw-connector.cjs.js +2 -2
- package/lib/ngw-connector.cjs.prod.js +1 -1
- package/lib/ngw-connector.esm-browser.js +2 -2
- package/lib/ngw-connector.esm-browser.prod.js +1 -1
- package/lib/ngw-connector.esm-bundler.js +2 -2
- package/lib/ngw-connector.esm-bundler.prod.js +1 -1
- package/lib/ngw-connector.global.js +2 -2
- package/lib/ngw-connector.global.prod.js +1 -1
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
package/lib/ngw-connector.cjs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.
|
|
1
|
+
/** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.8; author: NextGIS */
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
var utils = require('@nextgis/utils');
|
|
@@ -7,7 +7,7 @@ var Cache = require('@nextgis/cache');
|
|
|
7
7
|
|
|
8
8
|
var pkg = {
|
|
9
9
|
name: "@nextgis/ngw-connector",
|
|
10
|
-
version: "3.0.0-alpha.
|
|
10
|
+
version: "3.0.0-alpha.8",
|
|
11
11
|
_priority: 12,
|
|
12
12
|
description: "A lightweight HTTP client optimized for use with NextGIS Web API",
|
|
13
13
|
main: "index.js",
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=require("@nextgis/utils"),t=require("events"),r=require("@nextgis/cache"),s="3.0.0-alpha.7";const n=[];function o(e){n.push(e)}class i extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var c=Object.defineProperty,a=(e,t,r)=>((e,t,r)=>t in e?c(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(),a(this,"name","NgwError"),a(this,"title"),a(this,"message"),a(this,"detail"),a(this,"exception"),a(this,"status_code"),a(this,"data"),a(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,u.prototype)}}var h=Object.defineProperty,l=(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);class d extends u{constructor(e){super(e),l(this,"name","InsufficientPermissionsError"),l(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);class m extends u{constructor(e){super(e),f(this,"name","ResourceNotFoundError"),f(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,m.prototype)}}var g=Object.defineProperty,w=(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 y extends Error{constructor(e){super(e||"Something went wrong."),w(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,y)}}class b extends y{constructor(e){super(e||"There is no response from the server or problem connecting to server."),w(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class v extends y{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class O extends y{constructor(e){super(e.message),w(this,"detail"),w(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class R extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),w(this,"title"),w(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,R)}}class E extends R{constructor(e){super("Long-running request was cancelled.",e)}}class j extends R{constructor(e){super(void 0,e)}}async function q(e){const t=await async function(e){try{return e.json()}catch(t){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 c=!1,a=!1;for(;!a;){let e,t;await i(c?s:r),c=!1;try{e=await fetch(n,{credentials:"same-origin"}),t=await e.json()}catch(u){c=!0;continue}switch(t.status){case void 0:throw new R(void 0,t);case"ready":a=!0;break;case"cancelled":throw new E(t);case"failed":throw new j(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new R(void 0,t)}}return o}const x=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function I(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 C(t,r,s){var n;const{withCredentials:o,responseType:i,cacheProps:c,cacheName:a,lunkwill:u,cache:h,query:l,json:d,...p}={method:"GET",credentials:"same-origin",headers:{},...r};o&&(p.credentials="include");let f=!1;void 0!==u&&(u.toHeaders(p.headers||{}),f=!0);const m=!!p.lunkwillReturnUrl;if(delete p.lunkwillReturnUrl,void 0!==d){p.body=JSON.stringify(d);const e=p.headers||{};e["Content-Type"]="application/json",p.headers=e}const g=I(t,l),w=async()=>{let e;try{e=await fetch(g,p)}catch(n){if("AbortError"===n.name)throw n;throw new b}if(f&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=await q(e);if(m)return t;e=await async function(e){try{return await window.fetch(e,{credentials:"same-origin"})}catch(n){throw new b}}(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&&x.test(t);if("blob"===i||n)s=await e.blob();else{if(!r)throw new v;s=await e.json()}}catch(n){if("AbortError"===n.name||n instanceof v)throw n;throw new v}if(400<=e.status&&e.status<=599)throw new O(s);return s};if(s&&h&&"GET"===(null==(n=p.method)?void 0:n.toUpperCase())){const t=c||{...e.objectRemoveEmpty({withCredentials:o,responseType:i})};return s.add(a||g,w,t,!1)}return w()}function P(t,r,s,...n){const[o,...i]=s[t],c=n[0];let a;if(void 0===c)a=[];else if("object"==typeof c&&null!==c){if(n.length>1)throw new Error("Too many arguments for route(name, object)!");a=[];for(const[e,t]of Object.entries(c))a[i.indexOf(e)]=String(t)}else a=n.map((e=>String(e)));return e.fixUrlStr(r+o.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),s=a[r];if(void 0===s){throw new Error(`Undefined parameter ${r} in "${o}".`)}return String(s)})))}function _(e){return"[object Object]"===Object.prototype.toString.call(e)}let k;{const t=require("url"),r=require("http"),s=require("https"),n=require("form-data"),o=e=>({"http:":r,"https:":s}[t.parse(e).protocol||"https:"]);k=(t,r,s={},i,c)=>{const{file:a,headers:u,method:h,data:l,responseType:d}=s,p=new Promise(((r,s)=>{const i=o(t);if(!i)throw new Error(`Given URL '${t}' is not correct`);{const o={headers:u||{},method:h},p="string"==typeof l?l:JSON.stringify(l);let f,m=a;if(a){const e={};if(_(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),m=t}if(f=new n,f.append("file",m,e),l)for(const t in l)f.append(t,l[t]);Object.assign(o.headers,{...f.getHeaders()})}void 0!==p&&Object.assign(o.headers,{"content-type":"application/json","content-length":Buffer.byteLength(p)});const g=i.request(t,o,(t=>{let n="";t.on("data",(e=>{n+=e})),t.on("end",(()=>{if(n)if("blob"===d)r(n);else{let o;try{o=JSON.parse(n),o&&o.status_code&&o.status_code&&s(o.message)}catch(t){s(t)}void 0!==o&&(!function(t){return!!e.isObject(t)&&t.status_code&&t.exception&&t.title}(o)?r(o):s("extractError(json)"))}s("no data")}))}));f&&f.pipe(g),g.on("error",(e=>{s(e)})),p&&g.write(p),c((()=>{g.abort()})),g.end()}}));return p.then((e=>(r&&r(e),e))).catch((e=>{if(!i)throw new Error(e);i(e)}))}}const U=/\{ *([\w_-]+) *\}/g;function T(e,t){return e.replace(U,((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 $=Object.defineProperty,A=(e,t,r)=>((e,t,r)=>t in e?$(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let S=0,N=0;class B{constructor(i){this.options=i,A(this,"id",S++),A(this,"emitter",new t.EventEmitter),A(this,"user"),A(this,"cache"),A(this,"withCredentials"),A(this,"client",`NextGIS-NGW-Connector/${s}`),A(this,"routeStr","/api/component/pyramid/route"),A(this,"activeRequests",{}),A(this,"requestTransform");const c=function(t){return n.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)}}))}(i);if(this.cache=new r({namespace:i.cacheId}),c)return c;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),o(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,o(this)}async connect(){const e=this.options.auth;if(e){const{login:t,password:r}=e;t&&r&&await this._login({login:t,password:r})}const t=`${this.routeStr}?client=${this.client}`;return await this.makeQuery(t,{},{cacheName:"route",cache:!0,cacheProps:{id:this.id,auth:e,baseUrl:this.options.baseUrl}})}login(e,t){return this.logout(),o(this),this._login(e,t)}logout(){this.abort(),function(e){const t=n.indexOf(e);-1!==t&&n.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.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 I(P(t,null!=(n=r.options.baseUrl)?n:"",o,...s),null==e?void 0:e.query)}},o=["get","post","put","delete","patch"];for(const i of o)n[i]=async n=>{var o;const{headers:c,...a}=n||{},u=await r.connect();return C(P(t,null!=(o=r.options.baseUrl)?o:"",u,...s),{headers:e.objectRemoveEmpty({...r.getAuthorizationHeaders(),...null!=c?c:{}}),...a,method:i},r.cache)};return n}(t,this,...r)}async makeQuery(t,r,s={}){var n;if(!(t=(this.options.baseUrl?this.options.baseUrl:"")+t))throw new Error("Empty `url` not allowed");if(r){const{paramList:e,...s}=r;t=T(t,s)}t=encodeURI(e.fixUrlStr(t)),s={withCredentials:this.withCredentials,...s};const{cache:o,signal:c,method:a="GET",headers:u,cacheName:h,cacheProps:l,responseType:d,withCredentials:p}=s,f=new AbortController,m=f.signal;if(c){if(c.aborted)throw new i;c.addEventListener("abort",(()=>{f.abort()}))}s.signal=m;const g=async()=>{const e=N++;this.activeRequests[e]=f;try{return this._loadData(t,s)}finally{this._cleanActiveRequest(e)}};if("GET"===a){const s=l||{...e.objectRemoveEmpty({headers:u,withCredentials:p,responseType:d,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id}),params:r};return this.cache.add(h||t,g,{props:s,expirationTime:o?void 0:500})}return g()}_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;k(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 m(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new d(e)}return e}}A(B,"errors",{NgwError:u,ResourceNotFoundError:m});const F=["description"];function L(t,r=""){r=r?r+"__":"";const s={};for(const[n,o]of Object.entries(t))if(-1===F.indexOf(n))if(_(o))if("owner_user"===n){const e=L(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 H=Object.defineProperty,G=(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);class z{constructor({connector:e,cacheId:t}){G(this,"cache"),G(this,"connector"),this.connector=e,this.cache=new r({namespace:t})}getOne(e,t){const r={...t};return"string"==typeof e||"number"==typeof e||_(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):_(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 m}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||_(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,L(e)),this.connector.get("resource.search",t,{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=>e?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,r=[]){return this.connector.get("resource.collection",t,{parent:e}).then((e=>{const s=[];for(const n of e)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(n),{id:n.resource.id}),r.push(n),(null==t?void 0:t.recursive)&&n.resource.children&&s.push(this._getChildrenOf(n.resource.id,t,r));return s.length?Promise.all(s).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.get("resource.item",t,{id:e})}_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 D=Object.defineProperty,Q=(e,t,r)=>((e,t,r)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);module.exports=class extends B{constructor(e){super(e),Q(this,"resources"),this.resources=new z({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(t,r={},s={}){var n;r=null!=(n=s.params)?n:r;return async function(e){const{params:t,name:r,connector:s,requestOptions:n}=e,o=await s.connect();let i=o&&o[r];if(i){i=[...i];let e=i.shift();if(i.length){const r={};for(let e=0;e<i.length;e++){const s=i[e];if(r[e]=`{${s}}`,void 0===t[s])throw new Error(`\`${s}\` URL API argument is not specified`)}e&&(e=T(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===i.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return s.makeQuery(e,t,{cacheName:r,...n});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("events"),r=require("@nextgis/cache"),s="3.0.0-alpha.8";const n=[];function o(e){n.push(e)}class i extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var c=Object.defineProperty,a=(e,t,r)=>((e,t,r)=>t in e?c(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(),a(this,"name","NgwError"),a(this,"title"),a(this,"message"),a(this,"detail"),a(this,"exception"),a(this,"status_code"),a(this,"data"),a(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,u.prototype)}}var h=Object.defineProperty,l=(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);class d extends u{constructor(e){super(e),l(this,"name","InsufficientPermissionsError"),l(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);class m extends u{constructor(e){super(e),f(this,"name","ResourceNotFoundError"),f(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,m.prototype)}}var g=Object.defineProperty,w=(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 y extends Error{constructor(e){super(e||"Something went wrong."),w(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,y)}}class b extends y{constructor(e){super(e||"There is no response from the server or problem connecting to server."),w(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class v extends y{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class O extends y{constructor(e){super(e.message),w(this,"detail"),w(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class R extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),w(this,"title"),w(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,R)}}class E extends R{constructor(e){super("Long-running request was cancelled.",e)}}class j extends R{constructor(e){super(void 0,e)}}async function q(e){const t=await async function(e){try{return e.json()}catch(t){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 c=!1,a=!1;for(;!a;){let e,t;await i(c?s:r),c=!1;try{e=await fetch(n,{credentials:"same-origin"}),t=await e.json()}catch(u){c=!0;continue}switch(t.status){case void 0:throw new R(void 0,t);case"ready":a=!0;break;case"cancelled":throw new E(t);case"failed":throw new j(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new R(void 0,t)}}return o}const x=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function I(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 C(t,r,s){var n;const{withCredentials:o,responseType:i,cacheProps:c,cacheName:a,lunkwill:u,cache:h,query:l,json:d,...p}={method:"GET",credentials:"same-origin",headers:{},...r};o&&(p.credentials="include");let f=!1;void 0!==u&&(u.toHeaders(p.headers||{}),f=!0);const m=!!p.lunkwillReturnUrl;if(delete p.lunkwillReturnUrl,void 0!==d){p.body=JSON.stringify(d);const e=p.headers||{};e["Content-Type"]="application/json",p.headers=e}const g=I(t,l),w=async()=>{let e;try{e=await fetch(g,p)}catch(n){if("AbortError"===n.name)throw n;throw new b}if(f&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=await q(e);if(m)return t;e=await async function(e){try{return await window.fetch(e,{credentials:"same-origin"})}catch(n){throw new b}}(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&&x.test(t);if("blob"===i||n)s=await e.blob();else{if(!r)throw new v;s=await e.json()}}catch(n){if("AbortError"===n.name||n instanceof v)throw n;throw new v}if(400<=e.status&&e.status<=599)throw new O(s);return s};if(s&&h&&"GET"===(null==(n=p.method)?void 0:n.toUpperCase())){const t=c||{...e.objectRemoveEmpty({withCredentials:o,responseType:i})};return s.add(a||g,w,t,!1)}return w()}function P(t,r,s,...n){const[o,...i]=s[t],c=n[0];let a;if(void 0===c)a=[];else if("object"==typeof c&&null!==c){if(n.length>1)throw new Error("Too many arguments for route(name, object)!");a=[];for(const[e,t]of Object.entries(c))a[i.indexOf(e)]=String(t)}else a=n.map((e=>String(e)));return e.fixUrlStr(r+o.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),s=a[r];if(void 0===s){throw new Error(`Undefined parameter ${r} in "${o}".`)}return String(s)})))}function _(e){return"[object Object]"===Object.prototype.toString.call(e)}let k;{const t=require("url"),r=require("http"),s=require("https"),n=require("form-data"),o=e=>({"http:":r,"https:":s}[t.parse(e).protocol||"https:"]);k=(t,r,s={},i,c)=>{const{file:a,headers:u,method:h,data:l,responseType:d}=s,p=new Promise(((r,s)=>{const i=o(t);if(!i)throw new Error(`Given URL '${t}' is not correct`);{const o={headers:u||{},method:h},p="string"==typeof l?l:JSON.stringify(l);let f,m=a;if(a){const e={};if(_(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),m=t}if(f=new n,f.append("file",m,e),l)for(const t in l)f.append(t,l[t]);Object.assign(o.headers,{...f.getHeaders()})}void 0!==p&&Object.assign(o.headers,{"content-type":"application/json","content-length":Buffer.byteLength(p)});const g=i.request(t,o,(t=>{let n="";t.on("data",(e=>{n+=e})),t.on("end",(()=>{if(n)if("blob"===d)r(n);else{let o;try{o=JSON.parse(n),o&&o.status_code&&o.status_code&&s(o.message)}catch(t){s(t)}void 0!==o&&(!function(t){return!!e.isObject(t)&&t.status_code&&t.exception&&t.title}(o)?r(o):s("extractError(json)"))}s("no data")}))}));f&&f.pipe(g),g.on("error",(e=>{s(e)})),p&&g.write(p),c((()=>{g.abort()})),g.end()}}));return p.then((e=>(r&&r(e),e))).catch((e=>{if(!i)throw new Error(e);i(e)}))}}const U=/\{ *([\w_-]+) *\}/g;function T(e,t){return e.replace(U,((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 $=Object.defineProperty,A=(e,t,r)=>((e,t,r)=>t in e?$(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);let S=0,N=0;class B{constructor(i){this.options=i,A(this,"id",S++),A(this,"emitter",new t.EventEmitter),A(this,"user"),A(this,"cache"),A(this,"withCredentials"),A(this,"client",`NextGIS-NGW-Connector/${s}`),A(this,"routeStr","/api/component/pyramid/route"),A(this,"activeRequests",{}),A(this,"requestTransform");const c=function(t){return n.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)}}))}(i);if(this.cache=new r({namespace:i.cacheId}),c)return c;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),o(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,o(this)}async connect(){const e=this.options.auth;if(e){const{login:t,password:r}=e;t&&r&&await this._login({login:t,password:r})}const t=`${this.routeStr}?client=${this.client}`;return await this.makeQuery(t,{},{cacheName:"route",cache:!0,cacheProps:{id:this.id,auth:e,baseUrl:this.options.baseUrl}})}login(e,t){return this.logout(),o(this),this._login(e,t)}logout(){this.abort(),function(e){const t=n.indexOf(e);-1!==t&&n.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.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 I(P(t,null!=(n=r.options.baseUrl)?n:"",o,...s),null==e?void 0:e.query)}},o=["get","post","put","delete","patch"];for(const i of o)n[i]=async n=>{var o;const{headers:c,...a}=n||{},u=await r.connect();return C(P(t,null!=(o=r.options.baseUrl)?o:"",u,...s),{headers:e.objectRemoveEmpty({...r.getAuthorizationHeaders(),...null!=c?c:{}}),...a,method:i},r.cache)};return n}(t,this,...r)}async makeQuery(t,r,s={}){var n;if(!(t=(this.options.baseUrl?this.options.baseUrl:"")+t))throw new Error("Empty `url` not allowed");if(r){const{paramList:e,...s}=r;t=T(t,s)}t=encodeURI(e.fixUrlStr(t)),s={withCredentials:this.withCredentials,...s};const{cache:o,signal:c,method:a="GET",headers:u,cacheName:h,cacheProps:l,responseType:d,withCredentials:p}=s,f=new AbortController,m=f.signal;if(c){if(c.aborted)throw new i;c.addEventListener("abort",(()=>{f.abort()}))}s.signal=m;const g=async()=>{const e=N++;this.activeRequests[e]=f;try{return this._loadData(t,s)}finally{this._cleanActiveRequest(e)}};if("GET"===a){const s=l||{...e.objectRemoveEmpty({headers:u,withCredentials:p,responseType:d,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id}),params:r};return this.cache.add(h||t,g,{props:s,expirationTime:o?void 0:500})}return g()}_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;k(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 m(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new d(e)}return e}}A(B,"errors",{NgwError:u,ResourceNotFoundError:m});const F=["description"];function L(t,r=""){r=r?r+"__":"";const s={};for(const[n,o]of Object.entries(t))if(-1===F.indexOf(n))if(_(o))if("owner_user"===n){const e=L(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 H=Object.defineProperty,G=(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);class z{constructor({connector:e,cacheId:t}){G(this,"cache"),G(this,"connector"),this.connector=e,this.cache=new r({namespace:t})}getOne(e,t){const r={...t};return"string"==typeof e||"number"==typeof e||_(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):_(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 m}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||_(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,L(e)),this.connector.get("resource.search",t,{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=>e?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,r=[]){return this.connector.get("resource.collection",t,{parent:e}).then((e=>{const s=[];for(const n of e)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(n),{id:n.resource.id}),r.push(n),(null==t?void 0:t.recursive)&&n.resource.children&&s.push(this._getChildrenOf(n.resource.id,t,r));return s.length?Promise.all(s).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.get("resource.item",t,{id:e})}_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 D=Object.defineProperty,Q=(e,t,r)=>((e,t,r)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);module.exports=class extends B{constructor(e){super(e),Q(this,"resources"),this.resources=new z({connector:this,cacheId:e.cacheId})}static create(e){return new this(e)}clearCache(){super.clearCache(),this.resources.cache.clean()}apiRequest(t,r={},s={}){var n;r=null!=(n=s.params)?n:r;return async function(e){const{params:t,name:r,connector:s,requestOptions:n}=e,o=await s.connect();let i=o&&o[r];if(i){i=[...i];let e=i.shift();if(i.length){const r={};for(let e=0;e<i.length;e++){const s=i[e];if(r[e]=`{${s}}`,void 0===t[s])throw new Error(`\`${s}\` URL API argument is not specified`)}e&&(e=T(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===i.indexOf(e)&&r.push(`${e}=${t[e]}`);r.length&&(e=`${e}?${r.join("&")}`)}if(e)return s.makeQuery(e,t,{cacheName:r,...n});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.
|
|
1
|
+
/** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.8; author: NextGIS */
|
|
2
2
|
function defined(val) {
|
|
3
3
|
return val !== void 0 && val !== null;
|
|
4
4
|
}
|
|
@@ -669,7 +669,7 @@ let Cache = _Cache;
|
|
|
669
669
|
|
|
670
670
|
var pkg = {
|
|
671
671
|
name: "@nextgis/ngw-connector",
|
|
672
|
-
version: "3.0.0-alpha.
|
|
672
|
+
version: "3.0.0-alpha.8",
|
|
673
673
|
_priority: 12,
|
|
674
674
|
description: "A lightweight HTTP client optimized for use with NextGIS Web API",
|
|
675
675
|
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 e.replace(/([^:]\/)\/+/g,"$1")}function s(){}function a(){a.init.call(this)}function c(e){return void 0===e._maxListeners?a.defaultMaxListeners:e._maxListeners}function u(e,t,r,n){var o,i,a,u;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),a=i[t]):(i=e._events=new s,e._eventsCount=0),a){if("function"==typeof a?a=i[t]=n?[r,a]:[a,r]:n?a.unshift(r):a.push(r),!a.warned&&(o=c(e))&&o>0&&a.length>o){a.warned=!0;var l=new Error("Possible EventEmitter memory leak detected. "+a.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");l.name="MaxListenersExceededWarning",l.emitter=e,l.type=t,l.count=a.length,u=l,"function"==typeof console.warn?console.warn(u):console.log(u)}}else a=i[t]=r,++e._eventsCount;return e}function l(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 h(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 f(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}s.prototype=Object.create(null),a.EventEmitter=a,a.usingDomains=!1,a.prototype.domain=void 0,a.prototype._events=void 0,a.prototype._maxListeners=void 0,a.defaultMaxListeners=10,a.init=function(){this.domain=null,a.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new s,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},a.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},a.prototype.getMaxListeners=function(){return c(this)},a.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=f(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=f(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=f(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=f(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=f(e,o),s=0;s<o;++s)i[s].apply(r,n)}(r,l,this,o)}return!0},a.prototype.on=a.prototype.addListener=function(e,t){return u(this,e,t,!1)},a.prototype.prependListener=function(e,t){return u(this,e,t,!0)},a.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,l(this,e,t)),this},a.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,l(this,e,t)),this},a.prototype.removeListener=function(e,t){var r,n,o,i,a;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 s:(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){a=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 s,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,a||t)}return this},a.prototype.off=function(e,t){return this.removeListener(e,t)},a.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new s,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new s: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 s,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},a.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):[]},a.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):h.call(e,t)},a.prototype.listenerCount=h,a.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var p=Object.defineProperty,d=(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);const y=class e{constructor(t={}){var r;d(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((t=>{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))}};d(y,"instance",{});let m=y;var v="3.0.0-alpha.7";const g=[];function w(e){g.push(e)}class b extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var O=Object.defineProperty,j=(e,t,r)=>((e,t,r)=>t in e?O(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class x extends Error{constructor(e){super(),j(this,"name","NgwError"),j(this,"title"),j(this,"message"),j(this,"detail"),j(this,"exception"),j(this,"status_code"),j(this,"data"),j(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,x.prototype)}}var _=Object.defineProperty,P=(e,t,r)=>((e,t,r)=>t in e?_(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class E extends x{constructor(e){super(e),P(this,"name","InsufficientPermissionsError"),P(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),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,"symbol"!=typeof t?t+"":t,r);class T extends x{constructor(e){super(e),C(this,"name","ResourceNotFoundError"),C(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,T.prototype)}}var k=Object.defineProperty,I=(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 L extends Error{constructor(e){super(e||"Something went wrong."),I(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,L)}}class q extends L{constructor(e){super(e||"There is no response from the server or problem connecting to server."),I(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class A extends L{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class U extends L{constructor(e){super(e.message),I(this,"detail"),I(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class $ extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),I(this,"title"),I(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,$)}}class S extends ${constructor(e){super("Long-running request was cancelled.",e)}}class N extends ${constructor(e){super(void 0,e)}}var F=Object.defineProperty,D=Object.getOwnPropertySymbols,B=Object.prototype.hasOwnProperty,G=Object.prototype.propertyIsEnumerable,H=(e,t,r)=>t in e?F(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,M=(e,t)=>{for(var r in t||(t={}))B.call(t,r)&&H(e,r,t[r]);if(D)for(var r of D(t))G.call(t,r)&&H(e,r,t[r]);return e},z=(e,t)=>{var r={};for(var n in e)B.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&&G.call(e,n)&&(r[n]=e[n]);return r},J=(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 Q(e){return J(this,null,(function*(){const t=yield function(e){return J(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 $(void 0,t);case"ready":c=!0;break;case"cancelled":throw new S(t);case"failed":throw new N(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new $(void 0,t)}}return i}))}const W=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function K(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 X(e,t,r){return J(this,null,(function*(){var n;const i=M(M({},{method:"GET",credentials:"same-origin",headers:{}}),t),{withCredentials:s,responseType:a,cacheProps:c,cacheName:u,lunkwill:l,cache:h,query:f,json:p}=i,d=z(i,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);s&&(d.credentials="include");let y=!1;void 0!==l&&(l.toHeaders(d.headers||{}),y=!0);const m=!!d.lunkwillReturnUrl;if(delete d.lunkwillReturnUrl,void 0!==p){d.body=JSON.stringify(p);const e=d.headers||{};e["Content-Type"]="application/json",d.headers=e}const v=K(e,f),g=()=>J(this,null,(function*(){let e;try{e=yield fetch(v,d)}catch(o){if("AbortError"===o.name)throw o;throw new q}if(y&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield Q(e);if(m)return t;e=yield function(e){return J(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&&W.test(t);if("blob"===a||o)n=yield e.blob();else{if(!r)throw new A;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof A)throw o;throw new A}if(400<=e.status&&e.status<=599)throw new U(n);return n}));if(r&&h&&"GET"===(null==(n=d.method)?void 0:n.toUpperCase())){const e=c||M({},o({withCredentials:s,responseType:a}));return r.add(u||v,g,e,!1)}return g()}))}var V=Object.defineProperty,Y=Object.defineProperties,Z=Object.getOwnPropertyDescriptors,ee=Object.getOwnPropertySymbols,te=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable,ne=(e,t,r)=>t in e?V(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)=>{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},se=(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 ae(e,t,r,...n){const[o,...s]=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[s.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return i(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)})))}var ce=Object.defineProperty,ue=(e,t,r)=>((e,t,r)=>t in e?ce(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class le extends Error{constructor(e){super(),ue(this,"name","NetworkError"),Object.setPrototypeOf(this,le.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}function he(e){return"[object Object]"===Object.prototype.toString.call(e)}let fe;fe=(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 x(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 le(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 pe=/\{ *([\w_-]+) *\}/g;function de(e,t){return e.replace(pe,((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 ye=Object.defineProperty,me=Object.defineProperties,ve=Object.getOwnPropertyDescriptors,ge=Object.getOwnPropertySymbols,we=Object.prototype.hasOwnProperty,be=Object.prototype.propertyIsEnumerable,Oe=(e,t,r)=>t in e?ye(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,je=(e,t)=>{for(var r in t||(t={}))we.call(t,r)&&Oe(e,r,t[r]);if(ge)for(var r of ge(t))be.call(t,r)&&Oe(e,r,t[r]);return e},xe=(e,t,r)=>Oe(e,"symbol"!=typeof t?t+"":t,r),_e=(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 Pe=0,Ee=0;class Re{constructor(e){this.options=e,xe(this,"id",Pe++),xe(this,"emitter",new a),xe(this,"user"),xe(this,"cache"),xe(this,"withCredentials"),xe(this,"client",`NextGIS-NGW-Connector/${v}`),xe(this,"routeStr","/api/component/pyramid/route"),xe(this,"activeRequests",{}),xe(this,"requestTransform");const t=function(e){return g.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 m({namespace:e.cacheId}),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),w(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,w(this)}connect(){return _e(this,null,(function*(){const e=this.options.auth;if(e){const{login:t,password:r}=e;t&&r&&(yield this._login({login:t,password:r}))}const t=`${this.routeStr}?client=${this.client}`;return yield this.makeQuery(t,{},{cacheName:"route",cache:!0,cacheProps:{id:this.id,auth:e,baseUrl:this.options.baseUrl}})}))}login(e,t){return this.logout(),w(this),this._login(e,t)}logout(){this.abort(),function(e){const t=g.indexOf(e);-1!==t&&g.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return _e(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=je({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 je({},this.activeRequests)}route(e,...t){return function(e,t,...r){const n={url:n=>se(this,null,(function*(){var o;const i=yield t.connect();return K(ae(e,null!=(o=t.options.baseUrl)?o:"",i,...r),null==n?void 0:n.query)}))},i=["get","post","put","delete","patch"];for(const s of i){const i=n=>se(this,null,(function*(){var i;const a=n||{},{headers:c}=a,u=ie(a,["headers"]),l=yield t.connect(),h=ae(e,null!=(i=t.options.baseUrl)?i:"",l,...r),f=o(oe(oe({},t.getAuthorizationHeaders()),null!=c?c:{}));return X(h,(p=oe({headers:f},u),Y(p,Z({method:s}))),t.cache);var p}));n[s]=i}return n}(e,this,...t)}makeQuery(e,t){return _e(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)we.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&&be.call(e,n)&&(r[n]=e[n]);return r})(t,["paramList"]);e=de(e,r)}e=encodeURI(i(e)),r=je({withCredentials:this.withCredentials},r);const{cache:s,signal:a,method:c="GET",headers:u,cacheName:l,cacheProps:h,responseType:f,withCredentials:p}=r,d=new AbortController,y=d.signal;if(a){if(a.aborted)throw new b;a.addEventListener("abort",(()=>{d.abort()}))}r.signal=y;const m=()=>_e(this,null,(function*(){const t=Ee++;this.activeRequests[t]=d;try{return this._loadData(e,r)}finally{this._cleanActiveRequest(t)}}));if("GET"===c){const r=h||(v=je({},o({headers:u,withCredentials:p,responseType:f,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id})),me(v,ve({params:t})));return this.cache.add(l||e,m,{props:r,expirationTime:s?void 0:500})}var v;return m()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=je(je({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let i;fe(e,r,t,n,(e=>{i=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==i&&i(),n(new b)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return _e(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 x){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new T(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new E(e)}return e}}xe(Re,"errors",{NgwError:x,ResourceNotFoundError:T});const Ce=["description"];function Te(t,r=""){r=r?r+"__":"";const n={};for(const[o,i]of Object.entries(t))if(-1===Ce.indexOf(o))if(he(i))if("owner_user"===o){const e=Te(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 ke=Object.defineProperty,Ie=Object.getOwnPropertySymbols,Le=Object.prototype.hasOwnProperty,qe=Object.prototype.propertyIsEnumerable,Ae=(e,t,r)=>t in e?ke(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Ue=(e,t)=>{for(var r in t||(t={}))Le.call(t,r)&&Ae(e,r,t[r]);if(Ie)for(var r of Ie(t))qe.call(t,r)&&Ae(e,r,t[r]);return e},$e=(e,t,r)=>Ae(e,"symbol"!=typeof t?t+"":t,r);class Se{constructor({connector:e,cacheId:t}){$e(this,"cache"),$e(this,"connector"),this.connector=e,this.cache=new m({namespace:t})}getOne(e,t){const r=Ue({},t);return"string"==typeof e||"number"==typeof e||he(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):he(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 T}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||he(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,Te(e)),this.connector.get("resource.search",t,Ue({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=>e?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,r=[]){return this.connector.get("resource.collection",t,{parent:e}).then((e=>{const n=[];for(const o of e)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(o),{id:o.resource.id}),r.push(o),(null==t?void 0:t.recursive)&&o.resource.children&&n.push(this._getChildrenOf(o.resource.id,t,r));return n.length?Promise.all(n).then((()=>r)):r}))}_cleanResourceItemCache(e){return t=this,r=null,n=function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;["resource.item","resource"].includes(o.key)&&void 0!==r&&("number"==typeof r?r===e&&n.push(o):(yield this.getId(r))===e&&n.push(o))}for(const e of n)this.cache.delete(e)},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}_fetchResourceById(e,t){return this.connector.get("resource.item",t,{id:e})}_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 Ne=Object.defineProperty,Fe=Object.getOwnPropertySymbols,De=Object.prototype.hasOwnProperty,Be=Object.prototype.propertyIsEnumerable,Ge=(e,t,r)=>t in e?Ne(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function He(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=de(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={}))De.call(t,r)&&Ge(e,r,t[r]);if(Fe)for(var r of Fe(t))Be.call(t,r)&&Ge(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 Me=Object.defineProperty,ze=(e,t,r)=>((e,t,r)=>t in e?Me(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class Je extends Re{constructor(e){super(e),ze(this,"resources"),this.resources=new Se({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 He({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{Je 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 e.replace(/([^:]\/)\/+/g,"$1")}function s(){}function a(){a.init.call(this)}function c(e){return void 0===e._maxListeners?a.defaultMaxListeners:e._maxListeners}function u(e,t,r,n){var o,i,a,u;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),a=i[t]):(i=e._events=new s,e._eventsCount=0),a){if("function"==typeof a?a=i[t]=n?[r,a]:[a,r]:n?a.unshift(r):a.push(r),!a.warned&&(o=c(e))&&o>0&&a.length>o){a.warned=!0;var l=new Error("Possible EventEmitter memory leak detected. "+a.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");l.name="MaxListenersExceededWarning",l.emitter=e,l.type=t,l.count=a.length,u=l,"function"==typeof console.warn?console.warn(u):console.log(u)}}else a=i[t]=r,++e._eventsCount;return e}function l(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 h(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 f(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}s.prototype=Object.create(null),a.EventEmitter=a,a.usingDomains=!1,a.prototype.domain=void 0,a.prototype._events=void 0,a.prototype._maxListeners=void 0,a.defaultMaxListeners=10,a.init=function(){this.domain=null,a.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new s,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},a.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},a.prototype.getMaxListeners=function(){return c(this)},a.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=f(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=f(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=f(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=f(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=f(e,o),s=0;s<o;++s)i[s].apply(r,n)}(r,l,this,o)}return!0},a.prototype.on=a.prototype.addListener=function(e,t){return u(this,e,t,!1)},a.prototype.prependListener=function(e,t){return u(this,e,t,!0)},a.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,l(this,e,t)),this},a.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,l(this,e,t)),this},a.prototype.removeListener=function(e,t){var r,n,o,i,a;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 s:(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){a=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 s,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,a||t)}return this},a.prototype.off=function(e,t){return this.removeListener(e,t)},a.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new s,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new s: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 s,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},a.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):[]},a.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):h.call(e,t)},a.prototype.listenerCount=h,a.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var p=Object.defineProperty,d=(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);const y=class e{constructor(t={}){var r;d(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((t=>{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))}};d(y,"instance",{});let m=y;var v="3.0.0-alpha.8";const g=[];function w(e){g.push(e)}class b extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var O=Object.defineProperty,j=(e,t,r)=>((e,t,r)=>t in e?O(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class x extends Error{constructor(e){super(),j(this,"name","NgwError"),j(this,"title"),j(this,"message"),j(this,"detail"),j(this,"exception"),j(this,"status_code"),j(this,"data"),j(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,x.prototype)}}var _=Object.defineProperty,P=(e,t,r)=>((e,t,r)=>t in e?_(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class E extends x{constructor(e){super(e),P(this,"name","InsufficientPermissionsError"),P(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),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,"symbol"!=typeof t?t+"":t,r);class T extends x{constructor(e){super(e),C(this,"name","ResourceNotFoundError"),C(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,T.prototype)}}var k=Object.defineProperty,I=(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 L extends Error{constructor(e){super(e||"Something went wrong."),I(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,L)}}class q extends L{constructor(e){super(e||"There is no response from the server or problem connecting to server."),I(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class A extends L{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class U extends L{constructor(e){super(e.message),I(this,"detail"),I(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class $ extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),I(this,"title"),I(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,$)}}class S extends ${constructor(e){super("Long-running request was cancelled.",e)}}class N extends ${constructor(e){super(void 0,e)}}var F=Object.defineProperty,D=Object.getOwnPropertySymbols,B=Object.prototype.hasOwnProperty,G=Object.prototype.propertyIsEnumerable,H=(e,t,r)=>t in e?F(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,M=(e,t)=>{for(var r in t||(t={}))B.call(t,r)&&H(e,r,t[r]);if(D)for(var r of D(t))G.call(t,r)&&H(e,r,t[r]);return e},z=(e,t)=>{var r={};for(var n in e)B.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&&G.call(e,n)&&(r[n]=e[n]);return r},J=(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 Q(e){return J(this,null,(function*(){const t=yield function(e){return J(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 $(void 0,t);case"ready":c=!0;break;case"cancelled":throw new S(t);case"failed":throw new N(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new $(void 0,t)}}return i}))}const W=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function K(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 X(e,t,r){return J(this,null,(function*(){var n;const i=M(M({},{method:"GET",credentials:"same-origin",headers:{}}),t),{withCredentials:s,responseType:a,cacheProps:c,cacheName:u,lunkwill:l,cache:h,query:f,json:p}=i,d=z(i,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);s&&(d.credentials="include");let y=!1;void 0!==l&&(l.toHeaders(d.headers||{}),y=!0);const m=!!d.lunkwillReturnUrl;if(delete d.lunkwillReturnUrl,void 0!==p){d.body=JSON.stringify(p);const e=d.headers||{};e["Content-Type"]="application/json",d.headers=e}const v=K(e,f),g=()=>J(this,null,(function*(){let e;try{e=yield fetch(v,d)}catch(o){if("AbortError"===o.name)throw o;throw new q}if(y&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield Q(e);if(m)return t;e=yield function(e){return J(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&&W.test(t);if("blob"===a||o)n=yield e.blob();else{if(!r)throw new A;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof A)throw o;throw new A}if(400<=e.status&&e.status<=599)throw new U(n);return n}));if(r&&h&&"GET"===(null==(n=d.method)?void 0:n.toUpperCase())){const e=c||M({},o({withCredentials:s,responseType:a}));return r.add(u||v,g,e,!1)}return g()}))}var V=Object.defineProperty,Y=Object.defineProperties,Z=Object.getOwnPropertyDescriptors,ee=Object.getOwnPropertySymbols,te=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable,ne=(e,t,r)=>t in e?V(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)=>{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},se=(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 ae(e,t,r,...n){const[o,...s]=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[s.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return i(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)})))}var ce=Object.defineProperty,ue=(e,t,r)=>((e,t,r)=>t in e?ce(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class le extends Error{constructor(e){super(),ue(this,"name","NetworkError"),Object.setPrototypeOf(this,le.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}function he(e){return"[object Object]"===Object.prototype.toString.call(e)}let fe;fe=(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 x(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 le(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 pe=/\{ *([\w_-]+) *\}/g;function de(e,t){return e.replace(pe,((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 ye=Object.defineProperty,me=Object.defineProperties,ve=Object.getOwnPropertyDescriptors,ge=Object.getOwnPropertySymbols,we=Object.prototype.hasOwnProperty,be=Object.prototype.propertyIsEnumerable,Oe=(e,t,r)=>t in e?ye(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,je=(e,t)=>{for(var r in t||(t={}))we.call(t,r)&&Oe(e,r,t[r]);if(ge)for(var r of ge(t))be.call(t,r)&&Oe(e,r,t[r]);return e},xe=(e,t,r)=>Oe(e,"symbol"!=typeof t?t+"":t,r),_e=(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 Pe=0,Ee=0;class Re{constructor(e){this.options=e,xe(this,"id",Pe++),xe(this,"emitter",new a),xe(this,"user"),xe(this,"cache"),xe(this,"withCredentials"),xe(this,"client",`NextGIS-NGW-Connector/${v}`),xe(this,"routeStr","/api/component/pyramid/route"),xe(this,"activeRequests",{}),xe(this,"requestTransform");const t=function(e){return g.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 m({namespace:e.cacheId}),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),w(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,w(this)}connect(){return _e(this,null,(function*(){const e=this.options.auth;if(e){const{login:t,password:r}=e;t&&r&&(yield this._login({login:t,password:r}))}const t=`${this.routeStr}?client=${this.client}`;return yield this.makeQuery(t,{},{cacheName:"route",cache:!0,cacheProps:{id:this.id,auth:e,baseUrl:this.options.baseUrl}})}))}login(e,t){return this.logout(),w(this),this._login(e,t)}logout(){this.abort(),function(e){const t=g.indexOf(e);-1!==t&&g.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return _e(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=je({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 je({},this.activeRequests)}route(e,...t){return function(e,t,...r){const n={url:n=>se(this,null,(function*(){var o;const i=yield t.connect();return K(ae(e,null!=(o=t.options.baseUrl)?o:"",i,...r),null==n?void 0:n.query)}))},i=["get","post","put","delete","patch"];for(const s of i){const i=n=>se(this,null,(function*(){var i;const a=n||{},{headers:c}=a,u=ie(a,["headers"]),l=yield t.connect(),h=ae(e,null!=(i=t.options.baseUrl)?i:"",l,...r),f=o(oe(oe({},t.getAuthorizationHeaders()),null!=c?c:{}));return X(h,(p=oe({headers:f},u),Y(p,Z({method:s}))),t.cache);var p}));n[s]=i}return n}(e,this,...t)}makeQuery(e,t){return _e(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)we.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&&be.call(e,n)&&(r[n]=e[n]);return r})(t,["paramList"]);e=de(e,r)}e=encodeURI(i(e)),r=je({withCredentials:this.withCredentials},r);const{cache:s,signal:a,method:c="GET",headers:u,cacheName:l,cacheProps:h,responseType:f,withCredentials:p}=r,d=new AbortController,y=d.signal;if(a){if(a.aborted)throw new b;a.addEventListener("abort",(()=>{d.abort()}))}r.signal=y;const m=()=>_e(this,null,(function*(){const t=Ee++;this.activeRequests[t]=d;try{return this._loadData(e,r)}finally{this._cleanActiveRequest(t)}}));if("GET"===c){const r=h||(v=je({},o({headers:u,withCredentials:p,responseType:f,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id})),me(v,ve({params:t})));return this.cache.add(l||e,m,{props:r,expirationTime:s?void 0:500})}var v;return m()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=je(je({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let i;fe(e,r,t,n,(e=>{i=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==i&&i(),n(new b)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return _e(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 x){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new T(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new E(e)}return e}}xe(Re,"errors",{NgwError:x,ResourceNotFoundError:T});const Ce=["description"];function Te(t,r=""){r=r?r+"__":"";const n={};for(const[o,i]of Object.entries(t))if(-1===Ce.indexOf(o))if(he(i))if("owner_user"===o){const e=Te(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 ke=Object.defineProperty,Ie=Object.getOwnPropertySymbols,Le=Object.prototype.hasOwnProperty,qe=Object.prototype.propertyIsEnumerable,Ae=(e,t,r)=>t in e?ke(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Ue=(e,t)=>{for(var r in t||(t={}))Le.call(t,r)&&Ae(e,r,t[r]);if(Ie)for(var r of Ie(t))qe.call(t,r)&&Ae(e,r,t[r]);return e},$e=(e,t,r)=>Ae(e,"symbol"!=typeof t?t+"":t,r);class Se{constructor({connector:e,cacheId:t}){$e(this,"cache"),$e(this,"connector"),this.connector=e,this.cache=new m({namespace:t})}getOne(e,t){const r=Ue({},t);return"string"==typeof e||"number"==typeof e||he(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):he(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 T}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||he(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,Te(e)),this.connector.get("resource.search",t,Ue({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=>e?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,r=[]){return this.connector.get("resource.collection",t,{parent:e}).then((e=>{const n=[];for(const o of e)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(o),{id:o.resource.id}),r.push(o),(null==t?void 0:t.recursive)&&o.resource.children&&n.push(this._getChildrenOf(o.resource.id,t,r));return n.length?Promise.all(n).then((()=>r)):r}))}_cleanResourceItemCache(e){return t=this,r=null,n=function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;["resource.item","resource"].includes(o.key)&&void 0!==r&&("number"==typeof r?r===e&&n.push(o):(yield this.getId(r))===e&&n.push(o))}for(const e of n)this.cache.delete(e)},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}_fetchResourceById(e,t){return this.connector.get("resource.item",t,{id:e})}_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 Ne=Object.defineProperty,Fe=Object.getOwnPropertySymbols,De=Object.prototype.hasOwnProperty,Be=Object.prototype.propertyIsEnumerable,Ge=(e,t,r)=>t in e?Ne(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function He(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=de(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={}))De.call(t,r)&&Ge(e,r,t[r]);if(Fe)for(var r of Fe(t))Be.call(t,r)&&Ge(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 Me=Object.defineProperty,ze=(e,t,r)=>((e,t,r)=>t in e?Me(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class Je extends Re{constructor(e){super(e),ze(this,"resources"),this.resources=new Se({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 He({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{Je as default};
|
|
2
2
|
//# sourceMappingURL=ngw-connector.esm-browser.prod.js.map
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.
|
|
1
|
+
/** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.8; author: NextGIS */
|
|
2
2
|
import { objectDeepEqual, objectRemoveEmpty, fixUrlStr, defined } from '@nextgis/utils';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
4
|
import Cache from '@nextgis/cache';
|
|
5
5
|
|
|
6
6
|
var pkg = {
|
|
7
7
|
name: "@nextgis/ngw-connector",
|
|
8
|
-
version: "3.0.0-alpha.
|
|
8
|
+
version: "3.0.0-alpha.8",
|
|
9
9
|
_priority: 12,
|
|
10
10
|
description: "A lightweight HTTP client optimized for use with NextGIS Web API",
|
|
11
11
|
main: "index.js",
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{objectDeepEqual as e,objectRemoveEmpty as t,fixUrlStr as r,defined as n}from"@nextgis/utils";import{EventEmitter as o}from"events";import s from"@nextgis/cache";var i="3.0.0-alpha.7";const c=[];function a(e){c.push(e)}class u extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}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 Error{constructor(e){super(),h(this,"name","NgwError"),h(this,"title"),h(this,"message"),h(this,"detail"),h(this,"exception"),h(this,"status_code"),h(this,"data"),h(this,"guru_meditation"),Object.assign(this,e),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);class y extends d{constructor(e){super(e),f(this,"name","InsufficientPermissionsError"),f(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,y.prototype)}}var m=Object.defineProperty,g=(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 b extends d{constructor(e){super(e),g(this,"name","ResourceNotFoundError"),g(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,b.prototype)}}var w=Object.defineProperty,v=(e,t,r)=>((e,t,r)=>t in e?w(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class O extends Error{constructor(e){super(e||"Something went wrong."),v(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,O)}}class P extends O{constructor(e){super(e||"There is no response from the server or problem connecting to server."),v(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class j extends O{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class x extends O{constructor(e){super(e.message),v(this,"detail"),v(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class R extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),v(this,"title"),v(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,R)}}class E extends R{constructor(e){super("Long-running request was cancelled.",e)}}class I extends R{constructor(e){super(void 0,e)}}var C=Object.defineProperty,T=Object.getOwnPropertySymbols,q=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable,_=(e,t,r)=>t in e?C(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,U=(e,t)=>{for(var r in t||(t={}))q.call(t,r)&&_(e,r,t[r]);if(T)for(var r of T(t))k.call(t,r)&&_(e,r,t[r]);return e},$=(e,t)=>{var r={};for(var n in e)q.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&T)for(var n of T(e))t.indexOf(n)<0&&k.call(e,n)&&(r[n]=e[n]);return r},A=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{c(r.next(e))}catch(t){o(t)}},i=e=>{try{c(r.throw(e))}catch(t){o(t)}},c=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);c((r=r.apply(e,t)).next())}));function N(e){return A(this,null,(function*(){const t=yield function(e){return A(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 c=!1,a=!1;for(;!a;){let e,t;yield i(c?n:r),c=!1;try{e=yield fetch(o,{credentials:"same-origin"}),t=yield e.json()}catch(u){c=!0;continue}switch(t.status){case void 0:throw new R(void 0,t);case"ready":a=!0;break;case"cancelled":throw new E(t);case"failed":throw new I(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new R(void 0,t)}}return s}))}const S=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function F(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 L(e,r,n){return A(this,null,(function*(){var o;const s=U(U({},{method:"GET",credentials:"same-origin",headers:{}}),r),{withCredentials:i,responseType:c,cacheProps:a,cacheName:u,lunkwill:l,cache:h,query:d,json:p}=s,f=$(s,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);i&&(f.credentials="include");let y=!1;void 0!==l&&(l.toHeaders(f.headers||{}),y=!0);const m=!!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 g=F(e,d),b=()=>A(this,null,(function*(){let e;try{e=yield fetch(g,f)}catch(o){if("AbortError"===o.name)throw o;throw new P}if(y&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield N(e);if(m)return t;e=yield function(e){return A(this,null,(function*(){try{return yield window.fetch(e,{credentials:"same-origin"})}catch(o){throw new P}}))}(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&&S.test(t);if("blob"===c||o)n=yield e.blob();else{if(!r)throw new j;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof j)throw o;throw new j}if(400<=e.status&&e.status<=599)throw new x(n);return n}));if(n&&h&&"GET"===(null==(o=f.method)?void 0:o.toUpperCase())){const e=a||U({},t({withCredentials:i,responseType:c}));return n.add(u||g,b,e,!1)}return b()}))}var B=Object.defineProperty,G=Object.defineProperties,H=Object.getOwnPropertyDescriptors,D=Object.getOwnPropertySymbols,z=Object.prototype.hasOwnProperty,M=Object.prototype.propertyIsEnumerable,Q=(e,t,r)=>t in e?B(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)=>{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},V=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{c(r.next(e))}catch(t){o(t)}},i=e=>{try{c(r.throw(e))}catch(t){o(t)}},c=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);c((r=r.apply(e,t)).next())}));function K(e,t,n,...o){const[s,...i]=n[e],c=o[0];let a;if(void 0===c)a=[];else if("object"==typeof c&&null!==c){if(o.length>1)throw new Error("Too many arguments for route(name, object)!");a=[];for(const[e,t]of Object.entries(c))a[i.indexOf(e)]=String(t)}else a=o.map((e=>String(e)));return r(t+s.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),n=a[r];if(void 0===n){throw new Error(`Undefined parameter ${r} in "${s}".`)}return String(n)})))}var X=Object.defineProperty,Y=(e,t,r)=>((e,t,r)=>t in e?X(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class Z extends Error{constructor(e){super(),Y(this,"name","NetworkError"),Object.setPrototypeOf(this,Z.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}function ee(e){return"[object Object]"===Object.prototype.toString.call(e)}let te;te=(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 d(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 Z(e):t)},s.upload.onprogress=function(e){if(e.lengthComputable){r.onProgress&&r.onProgress(e.loaded/e.total*100,e)}};const c=r.headers;if(c)for(const u in c){const e=c[u];"string"==typeof e&&s.setRequestHeader(u,e)}let a;if(void 0!==r.withCredentials&&(s.withCredentials=r.withCredentials),r.file){if(a=new FormData,a.append("file",r.file),r.data)for(const u in r.data)a.append(u,a[u])}else a=r.data?"string"==typeof r.data?r.data:JSON.stringify(r.data):null;o&&o((()=>{s.abort()})),s.send(a)};const re=/\{ *([\w_-]+) *\}/g;function ne(e,t){return e.replace(re,((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 oe=Object.defineProperty,se=Object.defineProperties,ie=Object.getOwnPropertyDescriptors,ce=Object.getOwnPropertySymbols,ae=Object.prototype.hasOwnProperty,ue=Object.prototype.propertyIsEnumerable,le=(e,t,r)=>t in e?oe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,he=(e,t)=>{for(var r in t||(t={}))ae.call(t,r)&&le(e,r,t[r]);if(ce)for(var r of ce(t))ue.call(t,r)&&le(e,r,t[r]);return e},de=(e,t,r)=>le(e,"symbol"!=typeof t?t+"":t,r),pe=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{c(r.next(e))}catch(t){o(t)}},i=e=>{try{c(r.throw(e))}catch(t){o(t)}},c=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);c((r=r.apply(e,t)).next())}));let fe=0,ye=0;class me{constructor(t){this.options=t,de(this,"id",fe++),de(this,"emitter",new o),de(this,"user"),de(this,"cache"),de(this,"withCredentials"),de(this,"client",`NextGIS-NGW-Connector/${i}`),de(this,"routeStr","/api/component/pyramid/route"),de(this,"activeRequests",{}),de(this,"requestTransform");const r=function(t){return c.find((r=>{if(r.options.baseUrl===t.baseUrl){if(!t.auth)return!0;if(r.options.auth)return e(r.options.auth,t.auth)}}))}(t);if(this.cache=new s({namespace:t.cacheId}),r)return r;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),a(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,a(this)}connect(){return pe(this,null,(function*(){const e=this.options.auth;if(e){const{login:t,password:r}=e;t&&r&&(yield this._login({login:t,password:r}))}const t=`${this.routeStr}?client=${this.client}`;return yield this.makeQuery(t,{},{cacheName:"route",cache:!0,cacheProps:{id:this.id,auth:e,baseUrl:this.options.baseUrl}})}))}login(e,t){return this.logout(),a(this),this._login(e,t)}logout(){this.abort(),function(e){const t=c.indexOf(e);-1!==t&&c.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return pe(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=he({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 he({},this.activeRequests)}route(e,...r){return function(e,r,...n){const o={url:t=>V(this,null,(function*(){var o;const s=yield r.connect();return F(K(e,null!=(o=r.options.baseUrl)?o:"",s,...n),null==t?void 0:t.query)}))},s=["get","post","put","delete","patch"];for(const i of s){const s=o=>V(this,null,(function*(){var s;const c=o||{},{headers:a}=c,u=W(c,["headers"]),l=yield r.connect(),h=K(e,null!=(s=r.options.baseUrl)?s:"",l,...n),d=t(J(J({},r.getAuthorizationHeaders()),null!=a?a:{}));return L(h,(p=J({headers:d},u),G(p,H({method:i}))),r.cache);var p}));o[i]=s}return o}(e,this,...r)}makeQuery(e,n){return pe(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)ae.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&ce)for(var n of ce(e))t.indexOf(n)<0&&ue.call(e,n)&&(r[n]=e[n]);return r})(n,["paramList"]);e=ne(e,t)}e=encodeURI(r(e)),o=he({withCredentials:this.withCredentials},o);const{cache:i,signal:c,method:a="GET",headers:l,cacheName:h,cacheProps:d,responseType:p,withCredentials:f}=o,y=new AbortController,m=y.signal;if(c){if(c.aborted)throw new u;c.addEventListener("abort",(()=>{y.abort()}))}o.signal=m;const g=()=>pe(this,null,(function*(){const t=ye++;this.activeRequests[t]=y;try{return this._loadData(e,o)}finally{this._cleanActiveRequest(t)}}));if("GET"===a){const r=d||(b=he({},t({headers:l,withCredentials:f,responseType:p,baseUrl:this.options.baseUrl,userId:null==(s=this.user)?void 0:s.id})),se(b,ie({params:n})));return this.cache.add(h||e,g,{props:r,expirationTime:i?void 0:500})}var b;return g()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=he(he({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let s;te(e,r,t,n,(e=>{s=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==s&&s(),n(new u)}))})).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 pe(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 d){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new b(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new y(e)}return e}}de(me,"errors",{NgwError:d,ResourceNotFoundError:b});const ge=["description"];function be(e,t=""){t=t?t+"__":"";const r={};for(const[o,s]of Object.entries(e))if(-1===ge.indexOf(o))if(ee(s))if("owner_user"===o){const e=be(s,o);Object.assign(r,e)}else"parent"===o&&"id"in s&&(r.parent_id=s.id);else n(s)&&(r[t+o]=s);return r}var we=Object.defineProperty,ve=Object.getOwnPropertySymbols,Oe=Object.prototype.hasOwnProperty,Pe=Object.prototype.propertyIsEnumerable,je=(e,t,r)=>t in e?we(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,xe=(e,t)=>{for(var r in t||(t={}))Oe.call(t,r)&&je(e,r,t[r]);if(ve)for(var r of ve(t))Pe.call(t,r)&&je(e,r,t[r]);return e},Re=(e,t,r)=>je(e,"symbol"!=typeof t?t+"":t,r);class Ee{constructor({connector:e,cacheId:t}){Re(this,"cache"),Re(this,"connector"),this.connector=e,this.cache=new s({namespace:t})}getOne(e,t){const r=xe({},t);return"string"==typeof e||"number"==typeof e||ee(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):ee(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 b}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||ee(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,be(e)),this.connector.get("resource.search",t,xe({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=>e?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,r=[]){return this.connector.get("resource.collection",t,{parent:e}).then((e=>{const n=[];for(const o of e)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(o),{id:o.resource.id}),r.push(o),(null==t?void 0:t.recursive)&&o.resource.children&&n.push(this._getChildrenOf(o.resource.id,t,r));return n.length?Promise.all(n).then((()=>r)):r}))}_cleanResourceItemCache(e){return t=this,r=null,n=function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;["resource.item","resource"].includes(o.key)&&void 0!==r&&("number"==typeof r?r===e&&n.push(o):(yield this.getId(r))===e&&n.push(o))}for(const e of n)this.cache.delete(e)},new Promise(((e,o)=>{var s=e=>{try{c(n.next(e))}catch(t){o(t)}},i=e=>{try{c(n.throw(e))}catch(t){o(t)}},c=t=>t.done?e(t.value):Promise.resolve(t.value).then(s,i);c((n=n.apply(t,r)).next())}));var t,r,n}_fetchResourceById(e,t){return this.connector.get("resource.item",t,{id:e})}_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:n(t.id)&&n(r.resource.id)?t.id===r.resource.id:e(t,r.resource)})),[])))}}var Ie=Object.defineProperty,Ce=Object.getOwnPropertySymbols,Te=Object.prototype.hasOwnProperty,qe=Object.prototype.propertyIsEnumerable,ke=(e,t,r)=>t in e?Ie(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function _e(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=ne(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={}))Te.call(t,r)&&ke(e,r,t[r]);if(Ce)for(var r of Ce(t))qe.call(t,r)&&ke(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{c(n.next(e))}catch(t){o(t)}},i=e=>{try{c(n.throw(e))}catch(t){o(t)}},c=t=>t.done?e(t.value):Promise.resolve(t.value).then(s,i);c((n=n.apply(t,r)).next())}));var t,r,n}var Ue=Object.defineProperty,$e=(e,t,r)=>((e,t,r)=>t in e?Ue(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class Ae extends me{constructor(e){super(e),$e(this,"resources"),this.resources=new Ee({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 _e({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{Ae as default};
|
|
1
|
+
import{objectDeepEqual as e,objectRemoveEmpty as t,fixUrlStr as r,defined as n}from"@nextgis/utils";import{EventEmitter as o}from"events";import s from"@nextgis/cache";var i="3.0.0-alpha.8";const c=[];function a(e){c.push(e)}class u extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}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 Error{constructor(e){super(),h(this,"name","NgwError"),h(this,"title"),h(this,"message"),h(this,"detail"),h(this,"exception"),h(this,"status_code"),h(this,"data"),h(this,"guru_meditation"),Object.assign(this,e),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);class y extends d{constructor(e){super(e),f(this,"name","InsufficientPermissionsError"),f(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,y.prototype)}}var m=Object.defineProperty,g=(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 b extends d{constructor(e){super(e),g(this,"name","ResourceNotFoundError"),g(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,b.prototype)}}var w=Object.defineProperty,v=(e,t,r)=>((e,t,r)=>t in e?w(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class O extends Error{constructor(e){super(e||"Something went wrong."),v(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,O)}}class P extends O{constructor(e){super(e||"There is no response from the server or problem connecting to server."),v(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class j extends O{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class x extends O{constructor(e){super(e.message),v(this,"detail"),v(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class R extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),v(this,"title"),v(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,R)}}class E extends R{constructor(e){super("Long-running request was cancelled.",e)}}class I extends R{constructor(e){super(void 0,e)}}var C=Object.defineProperty,T=Object.getOwnPropertySymbols,q=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable,_=(e,t,r)=>t in e?C(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,U=(e,t)=>{for(var r in t||(t={}))q.call(t,r)&&_(e,r,t[r]);if(T)for(var r of T(t))k.call(t,r)&&_(e,r,t[r]);return e},$=(e,t)=>{var r={};for(var n in e)q.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&T)for(var n of T(e))t.indexOf(n)<0&&k.call(e,n)&&(r[n]=e[n]);return r},A=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{c(r.next(e))}catch(t){o(t)}},i=e=>{try{c(r.throw(e))}catch(t){o(t)}},c=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);c((r=r.apply(e,t)).next())}));function N(e){return A(this,null,(function*(){const t=yield function(e){return A(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 c=!1,a=!1;for(;!a;){let e,t;yield i(c?n:r),c=!1;try{e=yield fetch(o,{credentials:"same-origin"}),t=yield e.json()}catch(u){c=!0;continue}switch(t.status){case void 0:throw new R(void 0,t);case"ready":a=!0;break;case"cancelled":throw new E(t);case"failed":throw new I(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new R(void 0,t)}}return s}))}const S=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function F(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 L(e,r,n){return A(this,null,(function*(){var o;const s=U(U({},{method:"GET",credentials:"same-origin",headers:{}}),r),{withCredentials:i,responseType:c,cacheProps:a,cacheName:u,lunkwill:l,cache:h,query:d,json:p}=s,f=$(s,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);i&&(f.credentials="include");let y=!1;void 0!==l&&(l.toHeaders(f.headers||{}),y=!0);const m=!!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 g=F(e,d),b=()=>A(this,null,(function*(){let e;try{e=yield fetch(g,f)}catch(o){if("AbortError"===o.name)throw o;throw new P}if(y&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield N(e);if(m)return t;e=yield function(e){return A(this,null,(function*(){try{return yield window.fetch(e,{credentials:"same-origin"})}catch(o){throw new P}}))}(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&&S.test(t);if("blob"===c||o)n=yield e.blob();else{if(!r)throw new j;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof j)throw o;throw new j}if(400<=e.status&&e.status<=599)throw new x(n);return n}));if(n&&h&&"GET"===(null==(o=f.method)?void 0:o.toUpperCase())){const e=a||U({},t({withCredentials:i,responseType:c}));return n.add(u||g,b,e,!1)}return b()}))}var B=Object.defineProperty,G=Object.defineProperties,H=Object.getOwnPropertyDescriptors,D=Object.getOwnPropertySymbols,z=Object.prototype.hasOwnProperty,M=Object.prototype.propertyIsEnumerable,Q=(e,t,r)=>t in e?B(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)=>{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},V=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{c(r.next(e))}catch(t){o(t)}},i=e=>{try{c(r.throw(e))}catch(t){o(t)}},c=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);c((r=r.apply(e,t)).next())}));function K(e,t,n,...o){const[s,...i]=n[e],c=o[0];let a;if(void 0===c)a=[];else if("object"==typeof c&&null!==c){if(o.length>1)throw new Error("Too many arguments for route(name, object)!");a=[];for(const[e,t]of Object.entries(c))a[i.indexOf(e)]=String(t)}else a=o.map((e=>String(e)));return r(t+s.replace(/\{(\w+)\}/g,(function(e,t){const r=parseInt(t),n=a[r];if(void 0===n){throw new Error(`Undefined parameter ${r} in "${s}".`)}return String(n)})))}var X=Object.defineProperty,Y=(e,t,r)=>((e,t,r)=>t in e?X(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class Z extends Error{constructor(e){super(),Y(this,"name","NetworkError"),Object.setPrototypeOf(this,Z.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}function ee(e){return"[object Object]"===Object.prototype.toString.call(e)}let te;te=(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 d(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 Z(e):t)},s.upload.onprogress=function(e){if(e.lengthComputable){r.onProgress&&r.onProgress(e.loaded/e.total*100,e)}};const c=r.headers;if(c)for(const u in c){const e=c[u];"string"==typeof e&&s.setRequestHeader(u,e)}let a;if(void 0!==r.withCredentials&&(s.withCredentials=r.withCredentials),r.file){if(a=new FormData,a.append("file",r.file),r.data)for(const u in r.data)a.append(u,a[u])}else a=r.data?"string"==typeof r.data?r.data:JSON.stringify(r.data):null;o&&o((()=>{s.abort()})),s.send(a)};const re=/\{ *([\w_-]+) *\}/g;function ne(e,t){return e.replace(re,((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 oe=Object.defineProperty,se=Object.defineProperties,ie=Object.getOwnPropertyDescriptors,ce=Object.getOwnPropertySymbols,ae=Object.prototype.hasOwnProperty,ue=Object.prototype.propertyIsEnumerable,le=(e,t,r)=>t in e?oe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,he=(e,t)=>{for(var r in t||(t={}))ae.call(t,r)&&le(e,r,t[r]);if(ce)for(var r of ce(t))ue.call(t,r)&&le(e,r,t[r]);return e},de=(e,t,r)=>le(e,"symbol"!=typeof t?t+"":t,r),pe=(e,t,r)=>new Promise(((n,o)=>{var s=e=>{try{c(r.next(e))}catch(t){o(t)}},i=e=>{try{c(r.throw(e))}catch(t){o(t)}},c=e=>e.done?n(e.value):Promise.resolve(e.value).then(s,i);c((r=r.apply(e,t)).next())}));let fe=0,ye=0;class me{constructor(t){this.options=t,de(this,"id",fe++),de(this,"emitter",new o),de(this,"user"),de(this,"cache"),de(this,"withCredentials"),de(this,"client",`NextGIS-NGW-Connector/${i}`),de(this,"routeStr","/api/component/pyramid/route"),de(this,"activeRequests",{}),de(this,"requestTransform");const r=function(t){return c.find((r=>{if(r.options.baseUrl===t.baseUrl){if(!t.auth)return!0;if(r.options.auth)return e(r.options.auth,t.auth)}}))}(t);if(this.cache=new s({namespace:t.cacheId}),r)return r;{const{route:e,requestTransform:t,withCredentials:r}=this.options;e&&(this.routeStr=e),t&&(this.requestTransform=t),void 0!==r&&(this.withCredentials=r),a(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,a(this)}connect(){return pe(this,null,(function*(){const e=this.options.auth;if(e){const{login:t,password:r}=e;t&&r&&(yield this._login({login:t,password:r}))}const t=`${this.routeStr}?client=${this.client}`;return yield this.makeQuery(t,{},{cacheName:"route",cache:!0,cacheProps:{id:this.id,auth:e,baseUrl:this.options.baseUrl}})}))}login(e,t){return this.logout(),a(this),this._login(e,t)}logout(){this.abort(),function(e){const t=c.indexOf(e);-1!==t&&c.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return pe(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=he({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 he({},this.activeRequests)}route(e,...r){return function(e,r,...n){const o={url:t=>V(this,null,(function*(){var o;const s=yield r.connect();return F(K(e,null!=(o=r.options.baseUrl)?o:"",s,...n),null==t?void 0:t.query)}))},s=["get","post","put","delete","patch"];for(const i of s){const s=o=>V(this,null,(function*(){var s;const c=o||{},{headers:a}=c,u=W(c,["headers"]),l=yield r.connect(),h=K(e,null!=(s=r.options.baseUrl)?s:"",l,...n),d=t(J(J({},r.getAuthorizationHeaders()),null!=a?a:{}));return L(h,(p=J({headers:d},u),G(p,H({method:i}))),r.cache);var p}));o[i]=s}return o}(e,this,...r)}makeQuery(e,n){return pe(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)ae.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&ce)for(var n of ce(e))t.indexOf(n)<0&&ue.call(e,n)&&(r[n]=e[n]);return r})(n,["paramList"]);e=ne(e,t)}e=encodeURI(r(e)),o=he({withCredentials:this.withCredentials},o);const{cache:i,signal:c,method:a="GET",headers:l,cacheName:h,cacheProps:d,responseType:p,withCredentials:f}=o,y=new AbortController,m=y.signal;if(c){if(c.aborted)throw new u;c.addEventListener("abort",(()=>{y.abort()}))}o.signal=m;const g=()=>pe(this,null,(function*(){const t=ye++;this.activeRequests[t]=y;try{return this._loadData(e,o)}finally{this._cleanActiveRequest(t)}}));if("GET"===a){const r=d||(b=he({},t({headers:l,withCredentials:f,responseType:p,baseUrl:this.options.baseUrl,userId:null==(s=this.user)?void 0:s.id})),se(b,ie({params:n})));return this.cache.add(h||e,g,{props:r,expirationTime:i?void 0:500})}var b;return g()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=he(he({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let s;te(e,r,t,n,(e=>{s=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==s&&s(),n(new u)}))})).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 pe(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 d){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new b(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new y(e)}return e}}de(me,"errors",{NgwError:d,ResourceNotFoundError:b});const ge=["description"];function be(e,t=""){t=t?t+"__":"";const r={};for(const[o,s]of Object.entries(e))if(-1===ge.indexOf(o))if(ee(s))if("owner_user"===o){const e=be(s,o);Object.assign(r,e)}else"parent"===o&&"id"in s&&(r.parent_id=s.id);else n(s)&&(r[t+o]=s);return r}var we=Object.defineProperty,ve=Object.getOwnPropertySymbols,Oe=Object.prototype.hasOwnProperty,Pe=Object.prototype.propertyIsEnumerable,je=(e,t,r)=>t in e?we(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,xe=(e,t)=>{for(var r in t||(t={}))Oe.call(t,r)&&je(e,r,t[r]);if(ve)for(var r of ve(t))Pe.call(t,r)&&je(e,r,t[r]);return e},Re=(e,t,r)=>je(e,"symbol"!=typeof t?t+"":t,r);class Ee{constructor({connector:e,cacheId:t}){Re(this,"cache"),Re(this,"connector"),this.connector=e,this.cache=new s({namespace:t})}getOne(e,t){const r=xe({},t);return"string"==typeof e||"number"==typeof e||ee(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):ee(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 b}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||ee(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,be(e)),this.connector.get("resource.search",t,xe({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=>e?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,r=[]){return this.connector.get("resource.collection",t,{parent:e}).then((e=>{const n=[];for(const o of e)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(o),{id:o.resource.id}),r.push(o),(null==t?void 0:t.recursive)&&o.resource.children&&n.push(this._getChildrenOf(o.resource.id,t,r));return n.length?Promise.all(n).then((()=>r)):r}))}_cleanResourceItemCache(e){return t=this,r=null,n=function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;["resource.item","resource"].includes(o.key)&&void 0!==r&&("number"==typeof r?r===e&&n.push(o):(yield this.getId(r))===e&&n.push(o))}for(const e of n)this.cache.delete(e)},new Promise(((e,o)=>{var s=e=>{try{c(n.next(e))}catch(t){o(t)}},i=e=>{try{c(n.throw(e))}catch(t){o(t)}},c=t=>t.done?e(t.value):Promise.resolve(t.value).then(s,i);c((n=n.apply(t,r)).next())}));var t,r,n}_fetchResourceById(e,t){return this.connector.get("resource.item",t,{id:e})}_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:n(t.id)&&n(r.resource.id)?t.id===r.resource.id:e(t,r.resource)})),[])))}}var Ie=Object.defineProperty,Ce=Object.getOwnPropertySymbols,Te=Object.prototype.hasOwnProperty,qe=Object.prototype.propertyIsEnumerable,ke=(e,t,r)=>t in e?Ie(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function _e(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=ne(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={}))Te.call(t,r)&&ke(e,r,t[r]);if(Ce)for(var r of Ce(t))qe.call(t,r)&&ke(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{c(n.next(e))}catch(t){o(t)}},i=e=>{try{c(n.throw(e))}catch(t){o(t)}},c=t=>t.done?e(t.value):Promise.resolve(t.value).then(s,i);c((n=n.apply(t,r)).next())}));var t,r,n}var Ue=Object.defineProperty,$e=(e,t,r)=>((e,t,r)=>t in e?Ue(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class Ae extends me{constructor(e){super(e),$e(this,"resources"),this.resources=new Ee({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 _e({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{Ae 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.
|
|
1
|
+
/** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.8; author: NextGIS */
|
|
2
2
|
var NgwConnector = (function () {
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
@@ -672,7 +672,7 @@ var NgwConnector = (function () {
|
|
|
672
672
|
|
|
673
673
|
var pkg = {
|
|
674
674
|
name: "@nextgis/ngw-connector",
|
|
675
|
-
version: "3.0.0-alpha.
|
|
675
|
+
version: "3.0.0-alpha.8",
|
|
676
676
|
_priority: 12,
|
|
677
677
|
description: "A lightweight HTTP client optimized for use with NextGIS Web API",
|
|
678
678
|
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 e.replace(/([^:]\/)\/+/g,"$1")}function s(){}function a(){a.init.call(this)}function c(e){return void 0===e._maxListeners?a.defaultMaxListeners:e._maxListeners}function u(e,t,r,n){var o,i,a,u;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),a=i[t]):(i=e._events=new s,e._eventsCount=0),a){if("function"==typeof a?a=i[t]=n?[r,a]:[a,r]:n?a.unshift(r):a.push(r),!a.warned&&(o=c(e))&&o>0&&a.length>o){a.warned=!0;var l=new Error("Possible EventEmitter memory leak detected. "+a.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");l.name="MaxListenersExceededWarning",l.emitter=e,l.type=t,l.count=a.length,u=l,"function"==typeof console.warn?console.warn(u):console.log(u)}}else a=i[t]=r,++e._eventsCount;return e}function l(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 h(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 f(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}s.prototype=Object.create(null),a.EventEmitter=a,a.usingDomains=!1,a.prototype.domain=void 0,a.prototype._events=void 0,a.prototype._maxListeners=void 0,a.defaultMaxListeners=10,a.init=function(){this.domain=null,a.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new s,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},a.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},a.prototype.getMaxListeners=function(){return c(this)},a.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=f(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=f(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=f(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=f(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=f(e,o),s=0;s<o;++s)i[s].apply(r,n)}(r,l,this,o)}return!0},a.prototype.on=a.prototype.addListener=function(e,t){return u(this,e,t,!1)},a.prototype.prependListener=function(e,t){return u(this,e,t,!0)},a.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,l(this,e,t)),this},a.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,l(this,e,t)),this},a.prototype.removeListener=function(e,t){var r,n,o,i,a;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 s:(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){a=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 s,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,a||t)}return this},a.prototype.off=function(e,t){return this.removeListener(e,t)},a.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new s,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new s: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 s,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},a.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):[]},a.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):h.call(e,t)},a.prototype.listenerCount=h,a.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var p=Object.defineProperty,d=(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);const y=class e{constructor(t={}){var r;d(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((t=>{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))}};d(y,"instance",{});let m=y;var v="3.0.0-alpha.7";const g=[];function w(e){g.push(e)}class b extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var O=Object.defineProperty,j=(e,t,r)=>((e,t,r)=>t in e?O(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class x extends Error{constructor(e){super(),j(this,"name","NgwError"),j(this,"title"),j(this,"message"),j(this,"detail"),j(this,"exception"),j(this,"status_code"),j(this,"data"),j(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,x.prototype)}}var _=Object.defineProperty,P=(e,t,r)=>((e,t,r)=>t in e?_(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class E extends x{constructor(e){super(e),P(this,"name","InsufficientPermissionsError"),P(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,E.prototype)}}var C=Object.defineProperty,R=(e,t,r)=>((e,t,r)=>t in e?C(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class T extends x{constructor(e){super(e),R(this,"name","ResourceNotFoundError"),R(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,T.prototype)}}var k=Object.defineProperty,I=(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 L extends Error{constructor(e){super(e||"Something went wrong."),I(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,L)}}class q extends L{constructor(e){super(e||"There is no response from the server or problem connecting to server."),I(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class A extends L{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class U extends L{constructor(e){super(e.message),I(this,"detail"),I(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class $ extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),I(this,"title"),I(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,$)}}class S extends ${constructor(e){super("Long-running request was cancelled.",e)}}class N extends ${constructor(e){super(void 0,e)}}var F=Object.defineProperty,D=Object.getOwnPropertySymbols,B=Object.prototype.hasOwnProperty,G=Object.prototype.propertyIsEnumerable,H=(e,t,r)=>t in e?F(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,M=(e,t)=>{for(var r in t||(t={}))B.call(t,r)&&H(e,r,t[r]);if(D)for(var r of D(t))G.call(t,r)&&H(e,r,t[r]);return e},z=(e,t)=>{var r={};for(var n in e)B.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&&G.call(e,n)&&(r[n]=e[n]);return r},J=(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 Q(e){return J(this,null,(function*(){const t=yield function(e){return J(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 $(void 0,t);case"ready":c=!0;break;case"cancelled":throw new S(t);case"failed":throw new N(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new $(void 0,t)}}return i}))}const W=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function K(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 X(e,t,r){return J(this,null,(function*(){var n;const i=M(M({},{method:"GET",credentials:"same-origin",headers:{}}),t),{withCredentials:s,responseType:a,cacheProps:c,cacheName:u,lunkwill:l,cache:h,query:f,json:p}=i,d=z(i,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);s&&(d.credentials="include");let y=!1;void 0!==l&&(l.toHeaders(d.headers||{}),y=!0);const m=!!d.lunkwillReturnUrl;if(delete d.lunkwillReturnUrl,void 0!==p){d.body=JSON.stringify(p);const e=d.headers||{};e["Content-Type"]="application/json",d.headers=e}const v=K(e,f),g=()=>J(this,null,(function*(){let e;try{e=yield fetch(v,d)}catch(o){if("AbortError"===o.name)throw o;throw new q}if(y&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield Q(e);if(m)return t;e=yield function(e){return J(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&&W.test(t);if("blob"===a||o)n=yield e.blob();else{if(!r)throw new A;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof A)throw o;throw new A}if(400<=e.status&&e.status<=599)throw new U(n);return n}));if(r&&h&&"GET"===(null==(n=d.method)?void 0:n.toUpperCase())){const e=c||M({},o({withCredentials:s,responseType:a}));return r.add(u||v,g,e,!1)}return g()}))}var V=Object.defineProperty,Y=Object.defineProperties,Z=Object.getOwnPropertyDescriptors,ee=Object.getOwnPropertySymbols,te=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable,ne=(e,t,r)=>t in e?V(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)=>{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},se=(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 ae(e,t,r,...n){const[o,...s]=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[s.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return i(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)})))}var ce=Object.defineProperty,ue=(e,t,r)=>((e,t,r)=>t in e?ce(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class le extends Error{constructor(e){super(),ue(this,"name","NetworkError"),Object.setPrototypeOf(this,le.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}function he(e){return"[object Object]"===Object.prototype.toString.call(e)}let fe;fe=(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 x(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 le(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 pe=/\{ *([\w_-]+) *\}/g;function de(e,t){return e.replace(pe,((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 ye=Object.defineProperty,me=Object.defineProperties,ve=Object.getOwnPropertyDescriptors,ge=Object.getOwnPropertySymbols,we=Object.prototype.hasOwnProperty,be=Object.prototype.propertyIsEnumerable,Oe=(e,t,r)=>t in e?ye(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,je=(e,t)=>{for(var r in t||(t={}))we.call(t,r)&&Oe(e,r,t[r]);if(ge)for(var r of ge(t))be.call(t,r)&&Oe(e,r,t[r]);return e},xe=(e,t,r)=>Oe(e,"symbol"!=typeof t?t+"":t,r),_e=(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 Pe=0,Ee=0;class Ce{constructor(e){this.options=e,xe(this,"id",Pe++),xe(this,"emitter",new a),xe(this,"user"),xe(this,"cache"),xe(this,"withCredentials"),xe(this,"client",`NextGIS-NGW-Connector/${v}`),xe(this,"routeStr","/api/component/pyramid/route"),xe(this,"activeRequests",{}),xe(this,"requestTransform");const t=function(e){return g.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 m({namespace:e.cacheId}),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),w(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,w(this)}connect(){return _e(this,null,(function*(){const e=this.options.auth;if(e){const{login:t,password:r}=e;t&&r&&(yield this._login({login:t,password:r}))}const t=`${this.routeStr}?client=${this.client}`;return yield this.makeQuery(t,{},{cacheName:"route",cache:!0,cacheProps:{id:this.id,auth:e,baseUrl:this.options.baseUrl}})}))}login(e,t){return this.logout(),w(this),this._login(e,t)}logout(){this.abort(),function(e){const t=g.indexOf(e);-1!==t&&g.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return _e(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=je({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 je({},this.activeRequests)}route(e,...t){return function(e,t,...r){const n={url:n=>se(this,null,(function*(){var o;const i=yield t.connect();return K(ae(e,null!=(o=t.options.baseUrl)?o:"",i,...r),null==n?void 0:n.query)}))},i=["get","post","put","delete","patch"];for(const s of i){const i=n=>se(this,null,(function*(){var i;const a=n||{},{headers:c}=a,u=ie(a,["headers"]),l=yield t.connect(),h=ae(e,null!=(i=t.options.baseUrl)?i:"",l,...r),f=o(oe(oe({},t.getAuthorizationHeaders()),null!=c?c:{}));return X(h,(p=oe({headers:f},u),Y(p,Z({method:s}))),t.cache);var p}));n[s]=i}return n}(e,this,...t)}makeQuery(e,t){return _e(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)we.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&&be.call(e,n)&&(r[n]=e[n]);return r})(t,["paramList"]);e=de(e,r)}e=encodeURI(i(e)),r=je({withCredentials:this.withCredentials},r);const{cache:s,signal:a,method:c="GET",headers:u,cacheName:l,cacheProps:h,responseType:f,withCredentials:p}=r,d=new AbortController,y=d.signal;if(a){if(a.aborted)throw new b;a.addEventListener("abort",(()=>{d.abort()}))}r.signal=y;const m=()=>_e(this,null,(function*(){const t=Ee++;this.activeRequests[t]=d;try{return this._loadData(e,r)}finally{this._cleanActiveRequest(t)}}));if("GET"===c){const r=h||(v=je({},o({headers:u,withCredentials:p,responseType:f,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id})),me(v,ve({params:t})));return this.cache.add(l||e,m,{props:r,expirationTime:s?void 0:500})}var v;return m()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=je(je({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let i;fe(e,r,t,n,(e=>{i=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==i&&i(),n(new b)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return _e(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 x){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new T(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new E(e)}return e}}xe(Ce,"errors",{NgwError:x,ResourceNotFoundError:T});const Re=["description"];function Te(t,r=""){r=r?r+"__":"";const n={};for(const[o,i]of Object.entries(t))if(-1===Re.indexOf(o))if(he(i))if("owner_user"===o){const e=Te(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 ke=Object.defineProperty,Ie=Object.getOwnPropertySymbols,Le=Object.prototype.hasOwnProperty,qe=Object.prototype.propertyIsEnumerable,Ae=(e,t,r)=>t in e?ke(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Ue=(e,t)=>{for(var r in t||(t={}))Le.call(t,r)&&Ae(e,r,t[r]);if(Ie)for(var r of Ie(t))qe.call(t,r)&&Ae(e,r,t[r]);return e},$e=(e,t,r)=>Ae(e,"symbol"!=typeof t?t+"":t,r);class Se{constructor({connector:e,cacheId:t}){$e(this,"cache"),$e(this,"connector"),this.connector=e,this.cache=new m({namespace:t})}getOne(e,t){const r=Ue({},t);return"string"==typeof e||"number"==typeof e||he(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):he(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 T}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||he(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,Te(e)),this.connector.get("resource.search",t,Ue({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=>e?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,r=[]){return this.connector.get("resource.collection",t,{parent:e}).then((e=>{const n=[];for(const o of e)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(o),{id:o.resource.id}),r.push(o),(null==t?void 0:t.recursive)&&o.resource.children&&n.push(this._getChildrenOf(o.resource.id,t,r));return n.length?Promise.all(n).then((()=>r)):r}))}_cleanResourceItemCache(e){return t=this,r=null,n=function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;["resource.item","resource"].includes(o.key)&&void 0!==r&&("number"==typeof r?r===e&&n.push(o):(yield this.getId(r))===e&&n.push(o))}for(const e of n)this.cache.delete(e)},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}_fetchResourceById(e,t){return this.connector.get("resource.item",t,{id:e})}_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 Ne=Object.defineProperty,Fe=Object.getOwnPropertySymbols,De=Object.prototype.hasOwnProperty,Be=Object.prototype.propertyIsEnumerable,Ge=(e,t,r)=>t in e?Ne(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function He(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=de(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={}))De.call(t,r)&&Ge(e,r,t[r]);if(Fe)for(var r of Fe(t))Be.call(t,r)&&Ge(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 Me=Object.defineProperty,ze=(e,t,r)=>((e,t,r)=>t in e?Me(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);return class extends Ce{constructor(e){super(e),ze(this,"resources"),this.resources=new Se({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 He({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 e.replace(/([^:]\/)\/+/g,"$1")}function s(){}function a(){a.init.call(this)}function c(e){return void 0===e._maxListeners?a.defaultMaxListeners:e._maxListeners}function u(e,t,r,n){var o,i,a,u;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),a=i[t]):(i=e._events=new s,e._eventsCount=0),a){if("function"==typeof a?a=i[t]=n?[r,a]:[a,r]:n?a.unshift(r):a.push(r),!a.warned&&(o=c(e))&&o>0&&a.length>o){a.warned=!0;var l=new Error("Possible EventEmitter memory leak detected. "+a.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");l.name="MaxListenersExceededWarning",l.emitter=e,l.type=t,l.count=a.length,u=l,"function"==typeof console.warn?console.warn(u):console.log(u)}}else a=i[t]=r,++e._eventsCount;return e}function l(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 h(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 f(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}s.prototype=Object.create(null),a.EventEmitter=a,a.usingDomains=!1,a.prototype.domain=void 0,a.prototype._events=void 0,a.prototype._maxListeners=void 0,a.defaultMaxListeners=10,a.init=function(){this.domain=null,a.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new s,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},a.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},a.prototype.getMaxListeners=function(){return c(this)},a.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=f(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=f(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=f(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=f(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=f(e,o),s=0;s<o;++s)i[s].apply(r,n)}(r,l,this,o)}return!0},a.prototype.on=a.prototype.addListener=function(e,t){return u(this,e,t,!1)},a.prototype.prependListener=function(e,t){return u(this,e,t,!0)},a.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,l(this,e,t)),this},a.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,l(this,e,t)),this},a.prototype.removeListener=function(e,t){var r,n,o,i,a;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 s:(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){a=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 s,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,a||t)}return this},a.prototype.off=function(e,t){return this.removeListener(e,t)},a.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new s,this._eventsCount=0):r[e]&&(0==--this._eventsCount?this._events=new s: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 s,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},a.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):[]},a.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):h.call(e,t)},a.prototype.listenerCount=h,a.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var p=Object.defineProperty,d=(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);const y=class e{constructor(t={}){var r;d(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((t=>{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))}};d(y,"instance",{});let m=y;var v="3.0.0-alpha.8";const g=[];function w(e){g.push(e)}class b extends Error{constructor(e="AbortError"){super(e),this.name="AbortError"}}var O=Object.defineProperty,j=(e,t,r)=>((e,t,r)=>t in e?O(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class x extends Error{constructor(e){super(),j(this,"name","NgwError"),j(this,"title"),j(this,"message"),j(this,"detail"),j(this,"exception"),j(this,"status_code"),j(this,"data"),j(this,"guru_meditation"),Object.assign(this,e),Object.setPrototypeOf(this,x.prototype)}}var _=Object.defineProperty,P=(e,t,r)=>((e,t,r)=>t in e?_(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class E extends x{constructor(e){super(e),P(this,"name","InsufficientPermissionsError"),P(this,"exception","nextgisweb.core.exception.InsufficientPermissions"),Object.setPrototypeOf(this,E.prototype)}}var C=Object.defineProperty,R=(e,t,r)=>((e,t,r)=>t in e?C(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,"symbol"!=typeof t?t+"":t,r);class T extends x{constructor(e){super(e),R(this,"name","ResourceNotFoundError"),R(this,"exception","nextgisweb.resource.exception.ResourceNotFound"),Object.setPrototypeOf(this,T.prototype)}}var k=Object.defineProperty,I=(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 L extends Error{constructor(e){super(e||"Something went wrong."),I(this,"title"),this.name="BaseAPIError",this.title="Unknown API error",Error.captureStackTrace&&Error.captureStackTrace(this,L)}}class q extends L{constructor(e){super(e||"There is no response from the server or problem connecting to server."),I(this,"detail"),this.title="Network error",this.detail="Check network connectivity and try again later."}}class A extends L{constructor(e){super(e||"Something went wrong."),this.title="Unexpected server response"}}class U extends L{constructor(e){super(e.message),I(this,"detail"),I(this,"data"),this.title=e.title||this.title,this.detail=e.detail||null,this.data=e}}class $ extends Error{constructor(e,t={}){super(e||"Unexpected error while processing long-running request."),I(this,"title"),I(this,"data"),this.name="LunkwillError",this.title="Long-running request error",this.data=t,Error.captureStackTrace&&Error.captureStackTrace(this,$)}}class S extends ${constructor(e){super("Long-running request was cancelled.",e)}}class N extends ${constructor(e){super(void 0,e)}}var F=Object.defineProperty,D=Object.getOwnPropertySymbols,B=Object.prototype.hasOwnProperty,G=Object.prototype.propertyIsEnumerable,H=(e,t,r)=>t in e?F(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,M=(e,t)=>{for(var r in t||(t={}))B.call(t,r)&&H(e,r,t[r]);if(D)for(var r of D(t))G.call(t,r)&&H(e,r,t[r]);return e},z=(e,t)=>{var r={};for(var n in e)B.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&&G.call(e,n)&&(r[n]=e[n]);return r},J=(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 Q(e){return J(this,null,(function*(){const t=yield function(e){return J(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 $(void 0,t);case"ready":c=!0;break;case"cancelled":throw new S(t);case"failed":throw new N(t);case"spooled":case"processing":case"buffering":r=t.delay_ms;break;default:throw new $(void 0,t)}}return i}))}const W=new RegExp(["application/pdf","image/png","image/jpeg","image/tiff","text/csv"].join("|"));function K(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 X(e,t,r){return J(this,null,(function*(){var n;const i=M(M({},{method:"GET",credentials:"same-origin",headers:{}}),t),{withCredentials:s,responseType:a,cacheProps:c,cacheName:u,lunkwill:l,cache:h,query:f,json:p}=i,d=z(i,["withCredentials","responseType","cacheProps","cacheName","lunkwill","cache","query","json"]);s&&(d.credentials="include");let y=!1;void 0!==l&&(l.toHeaders(d.headers||{}),y=!0);const m=!!d.lunkwillReturnUrl;if(delete d.lunkwillReturnUrl,void 0!==p){d.body=JSON.stringify(p);const e=d.headers||{};e["Content-Type"]="application/json",d.headers=e}const v=K(e,f),g=()=>J(this,null,(function*(){let e;try{e=yield fetch(v,d)}catch(o){if("AbortError"===o.name)throw o;throw new q}if(y&&function(e){const t=e.headers.get("content-type");return null!=t&&t.includes("application/vnd.lunkwill.request-summary+json")}(e)){const t=yield Q(e);if(m)return t;e=yield function(e){return J(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&&W.test(t);if("blob"===a||o)n=yield e.blob();else{if(!r)throw new A;n=yield e.json()}}catch(o){if("AbortError"===o.name||o instanceof A)throw o;throw new A}if(400<=e.status&&e.status<=599)throw new U(n);return n}));if(r&&h&&"GET"===(null==(n=d.method)?void 0:n.toUpperCase())){const e=c||M({},o({withCredentials:s,responseType:a}));return r.add(u||v,g,e,!1)}return g()}))}var V=Object.defineProperty,Y=Object.defineProperties,Z=Object.getOwnPropertyDescriptors,ee=Object.getOwnPropertySymbols,te=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable,ne=(e,t,r)=>t in e?V(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)=>{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},se=(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 ae(e,t,r,...n){const[o,...s]=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[s.indexOf(e)]=String(t)}else c=n.map((e=>String(e)));return i(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)})))}var ce=Object.defineProperty,ue=(e,t,r)=>((e,t,r)=>t in e?ce(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);class le extends Error{constructor(e){super(),ue(this,"name","NetworkError"),Object.setPrototypeOf(this,le.prototype),this.message=`Unable to request ${e}.\n Possibly invalid NGW URL entered or CORS not configured to get request from ${location.origin}`}}function he(e){return"[object Object]"===Object.prototype.toString.call(e)}let fe;fe=(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 x(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 le(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 pe=/\{ *([\w_-]+) *\}/g;function de(e,t){return e.replace(pe,((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 ye=Object.defineProperty,me=Object.defineProperties,ve=Object.getOwnPropertyDescriptors,ge=Object.getOwnPropertySymbols,we=Object.prototype.hasOwnProperty,be=Object.prototype.propertyIsEnumerable,Oe=(e,t,r)=>t in e?ye(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,je=(e,t)=>{for(var r in t||(t={}))we.call(t,r)&&Oe(e,r,t[r]);if(ge)for(var r of ge(t))be.call(t,r)&&Oe(e,r,t[r]);return e},xe=(e,t,r)=>Oe(e,"symbol"!=typeof t?t+"":t,r),_e=(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 Pe=0,Ee=0;class Ce{constructor(e){this.options=e,xe(this,"id",Pe++),xe(this,"emitter",new a),xe(this,"user"),xe(this,"cache"),xe(this,"withCredentials"),xe(this,"client",`NextGIS-NGW-Connector/${v}`),xe(this,"routeStr","/api/component/pyramid/route"),xe(this,"activeRequests",{}),xe(this,"requestTransform");const t=function(e){return g.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 m({namespace:e.cacheId}),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),w(this)}}clearCache(){this.cache.clean()}setRequestTransform(e){this.requestTransform=e}setNgw(e){this.logout(),this.options.baseUrl=e,w(this)}connect(){return _e(this,null,(function*(){const e=this.options.auth;if(e){const{login:t,password:r}=e;t&&r&&(yield this._login({login:t,password:r}))}const t=`${this.routeStr}?client=${this.client}`;return yield this.makeQuery(t,{},{cacheName:"route",cache:!0,cacheProps:{id:this.id,auth:e,baseUrl:this.options.baseUrl}})}))}login(e,t){return this.logout(),w(this),this._login(e,t)}logout(){this.abort(),function(e){const t=g.indexOf(e);-1!==t&&g.splice(t,1)}(this),this.options.auth=void 0,this.user=void 0,this.clearCache(),this.emitter.emit("logout")}getUserInfo(e,t){return _e(this,null,(function*(){if(this.user&&this.user.id)return this.user;e&&(this.options.auth=e);const r=je({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 je({},this.activeRequests)}route(e,...t){return function(e,t,...r){const n={url:n=>se(this,null,(function*(){var o;const i=yield t.connect();return K(ae(e,null!=(o=t.options.baseUrl)?o:"",i,...r),null==n?void 0:n.query)}))},i=["get","post","put","delete","patch"];for(const s of i){const i=n=>se(this,null,(function*(){var i;const a=n||{},{headers:c}=a,u=ie(a,["headers"]),l=yield t.connect(),h=ae(e,null!=(i=t.options.baseUrl)?i:"",l,...r),f=o(oe(oe({},t.getAuthorizationHeaders()),null!=c?c:{}));return X(h,(p=oe({headers:f},u),Y(p,Z({method:s}))),t.cache);var p}));n[s]=i}return n}(e,this,...t)}makeQuery(e,t){return _e(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)we.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&&be.call(e,n)&&(r[n]=e[n]);return r})(t,["paramList"]);e=de(e,r)}e=encodeURI(i(e)),r=je({withCredentials:this.withCredentials},r);const{cache:s,signal:a,method:c="GET",headers:u,cacheName:l,cacheProps:h,responseType:f,withCredentials:p}=r,d=new AbortController,y=d.signal;if(a){if(a.aborted)throw new b;a.addEventListener("abort",(()=>{d.abort()}))}r.signal=y;const m=()=>_e(this,null,(function*(){const t=Ee++;this.activeRequests[t]=d;try{return this._loadData(e,r)}finally{this._cleanActiveRequest(t)}}));if("GET"===c){const r=h||(v=je({},o({headers:u,withCredentials:p,responseType:f,baseUrl:this.options.baseUrl,userId:null==(n=this.user)?void 0:n.id})),me(v,ve({params:t})));return this.cache.add(l||e,m,{props:r,expirationTime:s?void 0:500})}var v;return m()}))}_loadData(e,t){return t.responseType=t.responseType||"json",new Promise(((r,n)=>{var o;if(this.user&&((t=t||{}).headers=je(je({},this.getAuthorizationHeaders()),t.headers)),this.requestTransform){const[r,n]=this.requestTransform(e,t);e=r,t=n}let i;fe(e,r,t,n,(e=>{i=e})),null==(o=t.signal)||o.addEventListener("abort",(()=>{void 0!==i&&i(),n(new b)}))})).catch((e=>{if("AbortError"!==e.name){const t=this._handleHttpError(e);if(t)throw t}throw e}))}_login(e,t){return _e(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 x){if("nextgisweb.resource.exception.ResourceNotFound"===e.exception)throw new T(e);if("nextgisweb.core.exception.InsufficientPermissions"===e.exception)throw new E(e)}return e}}xe(Ce,"errors",{NgwError:x,ResourceNotFoundError:T});const Re=["description"];function Te(t,r=""){r=r?r+"__":"";const n={};for(const[o,i]of Object.entries(t))if(-1===Re.indexOf(o))if(he(i))if("owner_user"===o){const e=Te(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 ke=Object.defineProperty,Ie=Object.getOwnPropertySymbols,Le=Object.prototype.hasOwnProperty,qe=Object.prototype.propertyIsEnumerable,Ae=(e,t,r)=>t in e?ke(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Ue=(e,t)=>{for(var r in t||(t={}))Le.call(t,r)&&Ae(e,r,t[r]);if(Ie)for(var r of Ie(t))qe.call(t,r)&&Ae(e,r,t[r]);return e},$e=(e,t,r)=>Ae(e,"symbol"!=typeof t?t+"":t,r);class Se{constructor({connector:e,cacheId:t}){$e(this,"cache"),$e(this,"connector"),this.connector=e,this.cache=new m({namespace:t})}getOne(e,t){const r=Ue({},t);return"string"==typeof e||"number"==typeof e||he(e),"string"==typeof e?this._fetchResourceBy({keyname:e},r):"number"==typeof e?this._fetchResourceById(e,r):he(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 T}))}getId(e,t){return"number"==typeof e?Promise.resolve(e):"string"==typeof e||he(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,Te(e)),this.connector.get("resource.search",t,Ue({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=>e?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,r=[]){return this.connector.get("resource.collection",t,{parent:e}).then((e=>{const n=[];for(const o of e)(null==t?void 0:t.cache)&&this.cache.add("resource.item",Promise.resolve(o),{id:o.resource.id}),r.push(o),(null==t?void 0:t.recursive)&&o.resource.children&&n.push(this._getChildrenOf(o.resource.id,t,r));return n.length?Promise.all(n).then((()=>r)):r}))}_cleanResourceItemCache(e){return t=this,r=null,n=function*(){var t;const r=this.cache.all(),n=[];for(const o of r){const r=null==(t=o.props)?void 0:t.id;["resource.item","resource"].includes(o.key)&&void 0!==r&&("number"==typeof r?r===e&&n.push(o):(yield this.getId(r))===e&&n.push(o))}for(const e of n)this.cache.delete(e)},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}_fetchResourceById(e,t){return this.connector.get("resource.item",t,{id:e})}_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 Ne=Object.defineProperty,Fe=Object.getOwnPropertySymbols,De=Object.prototype.hasOwnProperty,Be=Object.prototype.propertyIsEnumerable,Ge=(e,t,r)=>t in e?Ne(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;function He(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=de(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={}))De.call(t,r)&&Ge(e,r,t[r]);if(Fe)for(var r of Fe(t))Be.call(t,r)&&Ge(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 Me=Object.defineProperty,ze=(e,t,r)=>((e,t,r)=>t in e?Me(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r)(e,t+"",r);return class extends Ce{constructor(e){super(e),ze(this,"resources"),this.resources=new Se({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 He({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.
|
|
3
|
+
"version": "3.0.0-alpha.8",
|
|
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": "
|
|
57
|
+
"gitHead": "ef384211e9024fbf72594c5d34a5c2c554534b2c"
|
|
58
58
|
}
|