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