wjec-one 3.10.2 → 3.10.5
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/legacy/services/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).WJECOneServices={},e.React)}(this,(function(e,t){"use strict";function n(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function r(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?n(Object(r),!0).forEach((function(t){c(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):n(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function s(e,t,n,r,s,o,a){try{var i=e[o](a),c=i.value}catch(e){return void n(e)}i.done?t(c):Promise.resolve(c).then(r,s)}function o(e){return function(){var t=this,n=arguments;return new Promise((function(r,o){var a=e.apply(t,n);function i(e){s(a,r,o,i,c,"next",e)}function c(e){s(a,r,o,i,c,"throw",e)}i(void 0)}))}}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function c(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function u(e,t){if(null==e)return{};var n,r,s=function(e,t){if(null==e)return{};var n,r,s={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(s[n]=e[n]);return s}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(s[n]=e[n])}return s}function f(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==n)return;var r,s,o=[],a=!0,i=!1;try{for(n=n.call(e);!(a=(r=n.next()).done)&&(o.push(r.value),!t||o.length!==t);a=!0);}catch(e){i=!0,s=e}finally{try{a||null==n.return||n.return()}finally{if(i)throw s}}return o}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return l(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return l(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}var v={calls:{}},p=Date.now(),d=function(e){return v.calls[e]},h=function(e){return e.substr(0,e.indexOf("?")>0?e.indexOf("?"):e.length).replace(/\/+$/,"")},m=function(e){var t=e.error,n=e.serviceIdentifier,r=e.status,s=e.response,o=e.responseCode;v.calls[n]={error:t,response:s,responseCode:o,status:r},p=Date.now(),self.dispatchEvent(new CustomEvent("wjecOneUseServiceCallUpdateCache",{detail:{error:t,response:s,serviceIdentifier:n,status:r}}))},y=function(e){var t=e.error,n=void 0===t?"Error":t,r=e.message,s=e.path,o=e.status,a=e.timestamp;return{body:{error:n,message:r,path:s,status:o,timestamp:void 0===a?(new Date).toISOString():a},success:!1}},b=function(e){return!1===e.success},g=function(e){var t=e.authHeader,n=void 0===t?"Auth":t,s=e.id,a=void 0===s?"":s,i=e.method,u=void 0===i?"GET":i,f=e.idToken,l=void 0===f?"":f,v=e.params,p=e.service,d="".concat(p).concat(a?"/".concat(a):""),h={headers:r(r({},l?c({},n,l):{}),{},{"Content-Type":"application/json"}),method:u};if(v)if("GET"===u){var m="string"==typeof v?v:Object.keys(v).map((function(e){return"".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(v[e]))})).join("&");d="".concat(d,"?").concat(m)}else h.body=v&&JSON.stringify(v);return fetch(d,h).then(function(){var e=o(regeneratorRuntime.mark((function e(t){var n,r;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(n=t.headers.get("Content-Type"))||!n.includes("application/json")){e.next=12;break}return e.prev=2,e.next=5,t.json();case 5:r=e.sent,e.next=12;break;case 8:throw e.prev=8,e.t0=e.catch(2),{message:"Could not parse response JSON",path:d,status:t.status};case 12:if(2===Math.floor(t.status/100)){e.next=15;break}throw{message:r&&r.message||t.statusText,path:r&&r.path||d,status:r&&r.status||t.status};case 15:return e.abrupt("return",{body:r,status:t.status,success:!0});case 16:case"end":return e.stop()}}),e,null,[[2,8]])})));return function(t){return e.apply(this,arguments)}}()).catch((function(e){return y(e)}))},w=function(){function e(t,n){var r=this,s=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};a(this,e),this._consecutiveFailureLimit=s.refreshAfterConsecutiveFailures,this._invocationCountLimit=s.refreshAfterInvocations,this._refreshInterval=s.refreshAfterInterval,this._serviceIdentifier=n,this._wheelhouseUrl=t,this._fetchServiceInstances().then((function(){return r._selectInstance()}))}var t,n,s,c;return t=e,(n=[{key:"call",value:(c=o(regeneratorRuntime.mark((function e(){var t,n,s,o,a,i,c=arguments;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=c.length>0&&void 0!==c[0]?c[0]:{},o=s.resource,a=u(s,["resource"]),e.next=4,this._fetchInstancesPromise;case 4:if(this._isRefreshRequired()&&this._selectInstance(),void 0!==(null===(t=this._activeInstance)||void 0===t?void 0:t.url)){e.next=8;break}return e.abrupt("return",y({}));case 8:return this._activeInstance.invocationCount++,e.next=11,g(r(r({},a),{},{service:"".concat(null===(n=this._activeInstance)||void 0===n?void 0:n.url).concat(o?"/".concat(o):"")}));case 11:return(i=e.sent)&&i.success?this._activeInstance.consecutiveFailures=0:(this._activeInstance.consecutiveFailures++,b(i)&&void 0===i.body.status&&(this._activeInstance.status="failed")),e.abrupt("return",i);case 14:case"end":return e.stop()}}),e,this)}))),function(){return c.apply(this,arguments)})},{key:"_fetchServiceInstances",value:function(){var e=this;return this._fetchInstancesPromise=new Promise(function(){var t=o(regeneratorRuntime.mark((function t(n){var r,s,o,a,i;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,fetch("".concat(e._wheelhouseUrl,"/").concat(e._serviceIdentifier));case 2:return r=t.sent,t.next=5,r.json();case 5:s=t.sent,o=s.body,a=JSON.parse(o),i=a.Instances,e._instances=i.map((function(e){return{consecutiveFailures:0,id:e.InstanceId,invocationCount:0,status:"unused",url:e.Attributes.URL}})),n();case 10:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}())}},{key:"_isRefreshRequired",value:function(){var e=this._activeInstance,t=!1;return"failed"===(null==e?void 0:e.status)&&this._instances.some((function(e){return"failed"!==e.status}))&&(t=!0),void 0!==this._consecutiveFailureLimit&&(null==e?void 0:e.consecutiveFailures)>=this._consecutiveFailureLimit&&(t=!0),void 0!==this._invocationCountLimit&&(null==e?void 0:e.invocationCount)>=this._invocationCountLimit&&(t=!0),void 0!==this._refreshInterval&&this._refreshInterval>=Date.now()-this._lastRefreshed&&(t=!0),t}},{key:"_selectInstance",value:function(){var e=this,t=this._instances.filter((function(t){var n;return t.id!==(null===(n=e._activeInstance)||void 0===n?void 0:n.id)})),n=t.some((function(e){return"unused"===e.status}))?t.filter((function(e){return"unused"===e.status})):t.filter((function(e){return"failed"!==e.status}));if(n.length>0){var r=n[Math.floor(Math.random()*n.length)];r.consecutiveFailures=0,r.invocationCount=0,r.status="ok",this._activeInstance=r,this._lastRefreshed=Date.now()}}},{key:"url",get:function(){var e;return null===(e=this._activeInstance)||void 0===e?void 0:e.url}},{key:"consecutiveFailureLimit",set:function(e){this._consecutiveFailureLimit=e}},{key:"invocationCountLimit",set:function(e){this._invocationCountLimit=e}},{key:"refreshInterval",set:function(e){this._refreshInterval=e}}])&&i(t.prototype,n),s&&i(t,s),Object.defineProperty(t,"prototype",{writable:!1}),e}(),O=function(e){var t=e.error,n=void 0===t?"Error":t,r=e.message,s=e.path,o=e.status,a=e.timestamp;return{body:{error:n,message:r,path:s,status:o,timestamp:void 0===a?(new Date).toISOString():a},success:!1}},I=function(){var e=o(regeneratorRuntime.mark((function e(t,n){var r,s,o;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch(t);case 2:return r=e.sent,e.prev=3,e.next=6,r.json();case 6:s=e.sent,e.next=12;break;case 9:e.prev=9,e.t0=e.catch(3),s=[];case 12:if(o=s.map((function(e){return{description:e.Description,identifier:e.Name,type:e.Name.startsWith("api.")?"service":e.Name.startsWith("www.")?"frontEnd":void 0}})),!n){e.next=15;break}return e.abrupt("return",o.filter((function(e){return e.type===n})));case 15:return e.abrupt("return",o);case 16:case"end":return e.stop()}}),e,null,[[3,9]])})));return function(t,n){return e.apply(this,arguments)}}(),C={};e.callService=function(e){var t=e.authHeader,n=void 0===t?"Auth":t,s=e.id,a=void 0===s?"":s,i=e.method,u=void 0===i?"GET":i,f=e.idToken,l=void 0===f?"":f,v=e.params,p=e.service,d="".concat(p).concat(a?"/".concat(a):""),h={headers:r(r({},l?c({},n,l):{}),{},{"Content-Type":"application/json"}),method:u};if(v)if("GET"===u){var m="string"==typeof v?v:Object.keys(v).map((function(e){return"".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(v[e]))})).join("&");d="".concat(d,"?").concat(m)}else h.body=v&&JSON.stringify(v);return fetch(d,h).then(function(){var e=o(regeneratorRuntime.mark((function e(t){var n,r;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(n=t.headers.get("Content-Type"))||!n.includes("application/json")){e.next=12;break}return e.prev=2,e.next=5,t.json();case 5:r=e.sent,e.next=12;break;case 8:throw e.prev=8,e.t0=e.catch(2),{message:"Could not parse response JSON",path:d,status:t.status};case 12:if(2===Math.floor(t.status/100)){e.next=15;break}throw{message:r&&r.message||t.statusText,path:r&&r.path||d,status:r&&r.status||t.status};case 15:return e.abrupt("return",{body:r,status:t.status,success:!0});case 16:case"end":return e.stop()}}),e,null,[[2,8]])})));return function(t){return e.apply(this,arguments)}}()).catch((function(e){return O(e)}))},e.downloadFile=function(e){var t=e.autoSave,n=void 0===t||t,r=e.filename,s=e.onProgress,o=e.uri;return new Promise((function(e,t){var a=new XMLHttpRequest;a.open("GET","".concat(o),!0),a.onloadstart=function(){a.responseType="arraybuffer"},a.onprogress=function(e){var t=e.loaded,n=e.total,r=Math.round(t/n*100);s&&s(r)},a.onerror=function(){return t({message:a.responseText,path:"".concat(o),status:a.status})},a.onload=function(){if(200===a.status){var s,i=r,c=a.getResponseHeader("Content-Type");try{s=new Blob([new Uint8Array(a.response)],{type:c})}catch(e){if(self.MSBlobBuilder){var u=new self.MSBlobBuilder;u.append(a.response),s=u.getBlob(c)}else t(e)}i||(a.getAllResponseHeaders().match(/Content-Disposition/i)&&(i=a.getResponseHeader("Content-Disposition").match(/filename="(.*)"/)[1]),i||(i="file")),n&&function(e,t){if(self.navigator&&self.navigator.msSaveBlob)self.navigator.msSaveBlob(e,t);else{var n=document.createElement("a"),r=URL.createObjectURL(e);n.href=r,n.download=t,document.body.appendChild(n),n.click(),n.remove(),URL.revokeObjectURL(r)}}(s,i),e({body:s,status:200,success:!0})}else try{t(JSON.parse(String.fromCharCode.apply(null,new Uint8Array(a.response))))}catch(e){t({message:a.responseText,path:"".concat(o),status:a.status})}},a.send()})).catch((function(e){return O(e)}))},e.fetchJson=function(e,t){return fetch(e,t).then((function(e){return e.json()}))},e.fetchWheelhouseServices=I,e.generateServiceError=O,e.getWheelhouseServices=function(e,t,n){for(var r in C.hasOwnProperty(e)||(C[e]={}),t)void 0===C[e][r]&&(C[e][r]=new w(e,t[r],n));return Object.freeze(C[e])},e.isServiceError=function(e){return!1===e.success},e.uploadFile=function(e){var t=e.file,n=e.id,r=void 0===n?"":n,s=e.onProgress,o=e.service;return new Promise((function(e,n){var a=new XMLHttpRequest,i=new FormData,c="".concat(o).concat(r?"/".concat(r):"");i.append("file",t),a.upload.onprogress=function(e){var t=e.loaded,n=e.total,r=Math.round(t/n*100);s&&s(r)},a.onerror=function(){return n({message:a.responseText,path:c,status:a.status})},a.onload=function(){if(200===a.status)e({body:void 0,status:200,success:!0});else try{n(JSON.parse(a.response))}catch(e){n({message:a.responseText,path:c,status:a.status})}},a.open("POST",c,!0),a.send(i)})).catch((function(e){return O(e)}))},e.useServiceCall=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},s=t.useState(p),a=f(s,2),i=a[0],c=a[1],u=t.useState(e),l=f(u,1),v=l[0],y=t.useState(h(e)),w=f(y,1),O=w[0],I=t.useMemo((function(){return r({allowConcurrent:!1},n)}),[n]),C=t.useCallback(function(){var e=o(regeneratorRuntime.mark((function e(t){var n,s,o;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s=d(O),o=s.status,I.allowConcurrent||"loading"!==o){e.next=5;break}e.next=10;break;case 5:return m({serviceIdentifier:O,status:"loading"}),e.next=8,g(r(r({},t),{},{service:v}));case 8:n=e.sent,b(n)?m({error:n.body,responseCode:n.body.status,serviceIdentifier:O,status:"failed"}):m({response:n.body,responseCode:n.status,serviceIdentifier:O,status:"done"});case 10:return e.abrupt("return",!n||b(n)?void 0:n.body);case 11:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),[I]);return t.useEffect((function(){m({serviceIdentifier:O,status:"idle"})}),[]),t.useEffect((function(){}),[e]),t.useEffect((function(){p>i&&c(p)}),[p]),t.useEffect((function(){var e=function(){c(Date.now())};return self.addEventListener("wjecOneUseServiceCallUpdateCache",e),function(){self.removeEventListener("wjecOneUseServiceCallUpdateCache",e)}}),[]),r({call:C},d(O))},Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).WJECOneServices={},e.React)}(this,(function(e,t){"use strict";function n(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function r(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?n(Object(r),!0).forEach((function(t){c(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):n(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function s(e,t,n,r,s,a,o){try{var i=e[a](o),c=i.value}catch(e){return void n(e)}i.done?t(c):Promise.resolve(c).then(r,s)}function a(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var o=e.apply(t,n);function i(e){s(o,r,a,i,c,"next",e)}function c(e){s(o,r,a,i,c,"throw",e)}i(void 0)}))}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function c(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function u(e,t){if(null==e)return{};var n,r,s=function(e,t){if(null==e)return{};var n,r,s={},a=Object.keys(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||(s[n]=e[n]);return s}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(s[n]=e[n])}return s}function f(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==n)return;var r,s,a=[],o=!0,i=!1;try{for(n=n.call(e);!(o=(r=n.next()).done)&&(a.push(r.value),!t||a.length!==t);o=!0);}catch(e){i=!0,s=e}finally{try{o||null==n.return||n.return()}finally{if(i)throw s}}return a}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return l(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return l(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}var v={calls:{}},p=Date.now(),d=function(e){return v.calls[e]},h=function(e){return e.substr(0,e.indexOf("?")>0?e.indexOf("?"):e.length).replace(/\/+$/,"")},m=function(e){var t=e.error,n=e.serviceIdentifier,r=e.status,s=e.response,a=e.responseCode;v.calls[n]={error:t,response:s,responseCode:a,status:r},p=Date.now(),self.dispatchEvent(new CustomEvent("wjecOneUseServiceCallUpdateCache",{detail:{error:t,response:s,serviceIdentifier:n,status:r}}))},y=function(e){var t=e.error,n=void 0===t?"Error":t,r=e.message,s=e.path,a=e.status,o=e.timestamp;return{body:{error:n,message:r,path:s,status:a,timestamp:void 0===o?(new Date).toISOString():o},success:!1}},b=function(e){return!1===e.success},g=function(e){var t=e.authHeader,n=void 0===t?"Auth":t,s=e.id,o=void 0===s?"":s,i=e.method,u=void 0===i?"GET":i,f=e.idToken,l=void 0===f?"":f,v=e.params,p=e.service,d="".concat(p).concat(o?"/".concat(o):""),h={headers:r(r({},l?c({},n,l):{}),{},{"Content-Type":"application/json"}),method:u};if(v)if("GET"===u){var m="string"==typeof v?v:Object.keys(v).map((function(e){return"".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(v[e]))})).join("&");d="".concat(d,"?").concat(m)}else h.body=v&&JSON.stringify(v);return fetch(d,h).then(function(){var e=a(regeneratorRuntime.mark((function e(t){var n,r;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(n=t.headers.get("Content-Type"))||!n.includes("application/json")){e.next=12;break}return e.prev=2,e.next=5,t.json();case 5:r=e.sent,e.next=12;break;case 8:throw e.prev=8,e.t0=e.catch(2),{message:"Could not parse response JSON",path:d,status:t.status};case 12:if(2===Math.floor(t.status/100)){e.next=15;break}throw{message:r&&r.message||t.statusText,path:r&&r.path||d,status:r&&r.status||t.status};case 15:return e.abrupt("return",{body:r,status:t.status,success:!0});case 16:case"end":return e.stop()}}),e,null,[[2,8]])})));return function(t){return e.apply(this,arguments)}}()).catch((function(e){return y(e)}))},w=function(){function e(t,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};o(this,e),this._consecutiveFailureLimit=r.refreshAfterConsecutiveFailures,this._invocationCountLimit=r.refreshAfterInvocations,this._refreshInterval=r.refreshAfterInterval,this._serviceIdentifier=n,this._wheelhouseUrl=t}var t,n,s,c,f,l;return t=e,n=[{key:"call",value:(l=a(regeneratorRuntime.mark((function e(){var t,n,s,a,o,i,c=arguments;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return s=c.length>0&&void 0!==c[0]?c[0]:{},a=s.resource,o=u(s,["resource"]),e.next=4,this._verifyInstances();case 4:if(this._isRefreshRequired()&&this._selectInstance(),void 0!==(null===(t=this._activeInstance)||void 0===t?void 0:t.url)){e.next=8;break}return e.abrupt("return",y({message:"Missing service URL"}));case 8:return this._activeInstance.invocationCount++,e.next=11,g(r(r({},o),{},{service:"".concat(null===(n=this._activeInstance)||void 0===n?void 0:n.url).concat(a?"/".concat(a):"")}));case 11:return(i=e.sent)&&i.success?this._activeInstance.consecutiveFailures=0:(this._activeInstance.consecutiveFailures++,b(i)&&void 0===i.body.status&&(this._activeInstance.status="failed")),e.abrupt("return",i);case 14:case"end":return e.stop()}}),e,this)}))),function(){return l.apply(this,arguments)})},{key:"_isRefreshRequired",value:function(){var e=this._activeInstance,t=!1;return"failed"===(null==e?void 0:e.status)&&this._instances.some((function(e){return"failed"!==e.status}))&&(t=!0),void 0!==this._consecutiveFailureLimit&&(null==e?void 0:e.consecutiveFailures)>=this._consecutiveFailureLimit&&(t=!0),void 0!==this._invocationCountLimit&&(null==e?void 0:e.invocationCount)>=this._invocationCountLimit&&(t=!0),void 0!==this._refreshInterval&&this._refreshInterval>=Date.now()-this._lastRefreshed&&(t=!0),t}},{key:"_selectInstance",value:function(){var e=this,t=this._instances.filter((function(t){var n;return t.id!==(null===(n=e._activeInstance)||void 0===n?void 0:n.id)})),n=t.some((function(e){return"unused"===e.status}))?t.filter((function(e){return"unused"===e.status})):t.filter((function(e){return"failed"!==e.status}));if(n.length>0){var r=n[Math.floor(Math.random()*n.length)];r.consecutiveFailures=0,r.invocationCount=0,r.status="ok",this._activeInstance=r,this._lastRefreshed=Date.now()}}},{key:"_verifyInstances",value:(f=a(regeneratorRuntime.mark((function t(){var n=this;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(void 0!==this._instances){t.next=3;break}return t.next=3,e._fetchServiceInstances(this._wheelhouseUrl,this._serviceIdentifier).then((function(e){n._instances=e,n._selectInstance()}));case 3:return t.abrupt("return",Promise.resolve());case 4:case"end":return t.stop()}}),t,this)}))),function(){return f.apply(this,arguments)})},{key:"url",get:function(){var e;return null===(e=this._activeInstance)||void 0===e?void 0:e.url}},{key:"consecutiveFailureLimit",set:function(e){this._consecutiveFailureLimit=e}},{key:"invocationCountLimit",set:function(e){this._invocationCountLimit=e}},{key:"refreshInterval",set:function(e){this._refreshInterval=e}}],s=[{key:"build",value:(c=a(regeneratorRuntime.mark((function t(n,r){var s,a,o,i=arguments;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=i.length>2&&void 0!==i[2]?i[2]:{},t.next=3,e._fetchServiceInstances(n,r);case 3:return a=t.sent,(o=new e(n,r,s))._instances=a,o._selectInstance(),t.abrupt("return",o);case 8:case"end":return t.stop()}}),t)}))),function(e,t){return c.apply(this,arguments)})},{key:"_fetchServiceInstances",value:function(e,t){return new Promise(function(){var n=a(regeneratorRuntime.mark((function n(r){var s,a,o,i,c,u;return regeneratorRuntime.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return n.next=2,fetch("".concat(e,"/").concat(t));case 2:return s=n.sent,n.next=5,s.json();case 5:a=n.sent,o=a.body,i=JSON.parse(o),c=i.Instances,u=c.map((function(e){return{consecutiveFailures:0,id:e.InstanceId,invocationCount:0,status:"unused",url:e.Attributes.URL}})),r(u);case 10:case"end":return n.stop()}}),n)})));return function(e){return n.apply(this,arguments)}}())}}],n&&i(t.prototype,n),s&&i(t,s),Object.defineProperty(t,"prototype",{writable:!1}),e}(),O=function(e){var t=e.error,n=void 0===t?"Error":t,r=e.message,s=e.path,a=e.status,o=e.timestamp;return{body:{error:n,message:r,path:s,status:a,timestamp:void 0===o?(new Date).toISOString():o},success:!1}},x=function(){var e=a(regeneratorRuntime.mark((function e(t,n){var r,s,a;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch(t);case 2:return r=e.sent,e.prev=3,e.next=6,r.json();case 6:s=e.sent,e.next=12;break;case 9:e.prev=9,e.t0=e.catch(3),s=[];case 12:if(a=s.map((function(e){return{description:e.Description,identifier:e.Name,type:e.Name.startsWith("api.")?"service":e.Name.startsWith("www.")?"frontEnd":void 0}})),!n){e.next=15;break}return e.abrupt("return",a.filter((function(e){return e.type===n})));case 15:return e.abrupt("return",a);case 16:case"end":return e.stop()}}),e,null,[[3,9]])})));return function(t,n){return e.apply(this,arguments)}}(),I={},S=function(){var e=a(regeneratorRuntime.mark((function e(t,n,s){var a,o,i;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(o in a={},I.hasOwnProperty(t)||(I[t]={}),n)void 0===I[t][o]&&(a[o]=w.build(t,n[o],s));return e.next=5,Promise.all(Object.values(a));case 5:e.t0=regeneratorRuntime.keys(a);case 6:if((e.t1=e.t0()).done){e.next=13;break}return i=e.t1.value,e.next=10,a[i];case 10:I[t][i]=e.sent,e.next=6;break;case 13:return e.abrupt("return",Object.freeze(r({},I[t])));case 14:case"end":return e.stop()}}),e)})));return function(t,n,r){return e.apply(this,arguments)}}();e.WheelhouseService=w,e.callService=function(e){var t=e.authHeader,n=void 0===t?"Auth":t,s=e.id,o=void 0===s?"":s,i=e.method,u=void 0===i?"GET":i,f=e.idToken,l=void 0===f?"":f,v=e.params,p=e.service,d="".concat(p).concat(o?"/".concat(o):""),h={headers:r(r({},l?c({},n,l):{}),{},{"Content-Type":"application/json"}),method:u};if(v)if("GET"===u){var m="string"==typeof v?v:Object.keys(v).map((function(e){return"".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(v[e]))})).join("&");d="".concat(d,"?").concat(m)}else h.body=v&&JSON.stringify(v);return fetch(d,h).then(function(){var e=a(regeneratorRuntime.mark((function e(t){var n,r;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(n=t.headers.get("Content-Type"))||!n.includes("application/json")){e.next=12;break}return e.prev=2,e.next=5,t.json();case 5:r=e.sent,e.next=12;break;case 8:throw e.prev=8,e.t0=e.catch(2),{message:"Could not parse response JSON",path:d,status:t.status};case 12:if(2===Math.floor(t.status/100)){e.next=15;break}throw{message:r&&r.message||t.statusText,path:r&&r.path||d,status:r&&r.status||t.status};case 15:return e.abrupt("return",{body:r,status:t.status,success:!0});case 16:case"end":return e.stop()}}),e,null,[[2,8]])})));return function(t){return e.apply(this,arguments)}}()).catch((function(e){return O(e)}))},e.downloadFile=function(e){var t=e.autoSave,n=void 0===t||t,r=e.filename,s=e.onProgress,a=e.uri;return new Promise((function(e,t){var o=new XMLHttpRequest;o.open("GET","".concat(a),!0),o.onloadstart=function(){o.responseType="arraybuffer"},o.onprogress=function(e){var t=e.loaded,n=e.total,r=Math.round(t/n*100);s&&s(r)},o.onerror=function(){return t({message:o.responseText,path:"".concat(a),status:o.status})},o.onload=function(){if(200===o.status){var s,i=r,c=o.getResponseHeader("Content-Type");try{s=new Blob([new Uint8Array(o.response)],{type:c})}catch(e){if(self.MSBlobBuilder){var u=new self.MSBlobBuilder;u.append(o.response),s=u.getBlob(c)}else t(e)}i||(o.getAllResponseHeaders().match(/Content-Disposition/i)&&(i=o.getResponseHeader("Content-Disposition").match(/filename="(.*)"/)[1]),i||(i="file")),n&&function(e,t){if(self.navigator&&self.navigator.msSaveBlob)self.navigator.msSaveBlob(e,t);else{var n=document.createElement("a"),r=URL.createObjectURL(e);n.href=r,n.download=t,document.body.appendChild(n),n.click(),n.remove(),URL.revokeObjectURL(r)}}(s,i),e({body:s,status:200,success:!0})}else try{t(JSON.parse(String.fromCharCode.apply(null,new Uint8Array(o.response))))}catch(e){t({message:o.responseText,path:"".concat(a),status:o.status})}},o.send()})).catch((function(e){return O(e)}))},e.fetchJson=function(e,t){return fetch(e,t).then((function(e){return e.json()}))},e.fetchWheelhouseServices=x,e.generateServiceError=O,e.getWheelhouseServices=S,e.getWheelhouseServicesSync=function(e,t,n){for(var s in I.hasOwnProperty(e)||(I[e]={}),t)void 0===I[e][s]&&(I[e][s]=new w(e,t[s],n));return Object.freeze(r({},I[e]))},e.isServiceError=function(e){return!1===e.success},e.uploadFile=function(e){var t=e.file,n=e.id,r=void 0===n?"":n,s=e.onProgress,a=e.service;return new Promise((function(e,n){var o=new XMLHttpRequest,i=new FormData,c="".concat(a).concat(r?"/".concat(r):"");i.append("file",t),o.upload.onprogress=function(e){var t=e.loaded,n=e.total,r=Math.round(t/n*100);s&&s(r)},o.onerror=function(){return n({message:o.responseText,path:c,status:o.status})},o.onload=function(){if(200===o.status)e({body:void 0,status:200,success:!0});else try{n(JSON.parse(o.response))}catch(e){n({message:o.responseText,path:c,status:o.status})}},o.open("POST",c,!0),o.send(i)})).catch((function(e){return O(e)}))},e.useServiceCall=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},s=t.useState(p),o=f(s,2),i=o[0],c=o[1],u=t.useState(e),l=f(u,1),v=l[0],y=t.useState(h(e)),w=f(y,1),O=w[0],x=t.useMemo((function(){return r({allowConcurrent:!1},n)}),[n]),I=t.useCallback(function(){var e=a(regeneratorRuntime.mark((function e(t){var n,s,a;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s=d(O),a=s.status,x.allowConcurrent||"loading"!==a){e.next=5;break}e.next=10;break;case 5:return m({serviceIdentifier:O,status:"loading"}),e.next=8,g(r(r({},t),{},{service:v}));case 8:n=e.sent,b(n)?m({error:n.body,responseCode:n.body.status,serviceIdentifier:O,status:"failed"}):m({response:n.body,responseCode:n.status,serviceIdentifier:O,status:"done"});case 10:return e.abrupt("return",!n||b(n)?void 0:n.body);case 11:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),[x]);return t.useEffect((function(){m({serviceIdentifier:O,status:"idle"})}),[]),t.useEffect((function(){}),[e]),t.useEffect((function(){p>i&&c(p)}),[p]),t.useEffect((function(){var e=function(){c(Date.now())};return self.addEventListener("wjecOneUseServiceCallUpdateCache",e),function(){self.removeEventListener("wjecOneUseServiceCallUpdateCache",e)}}),[]),r({call:I},d(O))},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { callService } from './';
|
|
1
|
+
import { callService, ServiceCallResponse } from './';
|
|
2
2
|
declare type ServiceCallParams = Parameters<typeof callService>[0];
|
|
3
3
|
export declare type WheelhouseOptions = {
|
|
4
4
|
refreshAfterConsecutiveFailures?: number;
|
|
@@ -8,33 +8,21 @@ export declare type WheelhouseOptions = {
|
|
|
8
8
|
export default class WheelhouseService {
|
|
9
9
|
private _activeInstance;
|
|
10
10
|
private _consecutiveFailureLimit;
|
|
11
|
-
private _fetchInstancesPromise;
|
|
12
11
|
private _instances;
|
|
13
12
|
private _invocationCountLimit;
|
|
14
13
|
private _lastRefreshed;
|
|
15
14
|
private _refreshInterval;
|
|
16
15
|
private _serviceIdentifier;
|
|
17
16
|
private _wheelhouseUrl;
|
|
17
|
+
static build(wheelhouseEndpoint: string, service: string, options?: WheelhouseOptions): Promise<WheelhouseService>;
|
|
18
|
+
private static _fetchServiceInstances;
|
|
18
19
|
constructor(wheelhouseEndpoint: string, service: string, options?: WheelhouseOptions);
|
|
19
|
-
call<T
|
|
20
|
+
call<T>(params?: Omit<ServiceCallParams, 'service'> & {
|
|
20
21
|
resource?: string;
|
|
21
|
-
}): Promise<
|
|
22
|
-
body: {
|
|
23
|
-
error: string;
|
|
24
|
-
message: string;
|
|
25
|
-
path: string;
|
|
26
|
-
status: number;
|
|
27
|
-
timestamp: string;
|
|
28
|
-
};
|
|
29
|
-
success: false;
|
|
30
|
-
} | {
|
|
31
|
-
body: T;
|
|
32
|
-
status: number;
|
|
33
|
-
success: true;
|
|
34
|
-
}>;
|
|
35
|
-
private _fetchServiceInstances;
|
|
22
|
+
}): Promise<ServiceCallResponse<T>>;
|
|
36
23
|
private _isRefreshRequired;
|
|
37
24
|
private _selectInstance;
|
|
25
|
+
private _verifyInstances;
|
|
38
26
|
get url(): string;
|
|
39
27
|
set consecutiveFailureLimit(limit: number);
|
|
40
28
|
set invocationCountLimit(limit: number);
|
package/services/index.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ declare type ServiceCallParams = {
|
|
|
22
22
|
};
|
|
23
23
|
service: string;
|
|
24
24
|
};
|
|
25
|
-
declare type ServiceCallResponse<T> = ServiceError | ServiceResponse<T>;
|
|
25
|
+
export declare type ServiceCallResponse<T> = ServiceError | ServiceResponse<T>;
|
|
26
26
|
declare type ServiceError = {
|
|
27
27
|
body: {
|
|
28
28
|
error: string;
|
|
@@ -58,14 +58,20 @@ export declare const generateServiceError: ({ error, message, path, status, time
|
|
|
58
58
|
timestamp: string;
|
|
59
59
|
}>) => ServiceError;
|
|
60
60
|
export declare const isServiceError: (serviceResponse: ServiceCallResponse<any>) => serviceResponse is ServiceError;
|
|
61
|
-
export declare const callService: <T
|
|
61
|
+
export declare const callService: <T>({ authHeader, id, method, idToken, params, service }: ServiceCallParams) => Promise<ServiceCallResponse<T>>;
|
|
62
62
|
export declare const downloadFile: ({ autoSave, filename, onProgress, uri }: DownloadFileParams) => Promise<ServiceCallResponse<Blob>>;
|
|
63
63
|
export declare const fetchJson: (url: string, requestParams?: RequestInit) => Promise<any>;
|
|
64
64
|
export declare const fetchWheelhouseServices: (wheelhouseEndpoint: string, type?: WheelhouseServiceType) => Promise<WheelhouseServiceSummary[]>;
|
|
65
65
|
export declare const getWheelhouseServices: <T extends {
|
|
66
66
|
[key: string]: string;
|
|
67
|
+
}>(wheelhouseEndpoint: string, services: T, options?: WheelhouseOptions) => Promise<Readonly<{
|
|
68
|
+
[x: string]: WheelhouseService;
|
|
69
|
+
}>>;
|
|
70
|
+
export declare const getWheelhouseServicesSync: <T extends {
|
|
71
|
+
[key: string]: string;
|
|
67
72
|
}>(wheelhouseEndpoint: string, services: T, options?: WheelhouseOptions) => Readonly<{
|
|
68
|
-
[
|
|
73
|
+
[x: string]: WheelhouseService;
|
|
69
74
|
}>;
|
|
70
75
|
export declare const uploadFile: ({ file, id, onProgress, service }: UploadFileParams) => Promise<ServiceCallResponse<undefined>>;
|
|
71
76
|
export { default as useServiceCall } from './useServiceCall';
|
|
77
|
+
export { default as WheelhouseService } from './WheelhouseService';
|
package/services/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useState as e,useMemo as t,useCallback as n,useEffect as r}from"react";function s(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?s(Object(n),!0).forEach((function(t){f(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):s(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function o(e,t,n,r,s,a,o){try{var i=e[a](o),c=i.value}catch(e){return void n(e)}i.done?t(c):Promise.resolve(c).then(r,s)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,s){var a=e.apply(t,n);function i(e){o(a,r,s,i,c,"next",e)}function c(e){o(a,r,s,i,c,"throw",e)}i(void 0)}))}}function c(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function f(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function l(e,t){if(null==e)return{};var n,r,s=function(e,t){if(null==e)return{};var n,r,s={},a=Object.keys(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||(s[n]=e[n]);return s}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(s[n]=e[n])}return s}function v(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==n)return;var r,s,a=[],o=!0,i=!1;try{for(n=n.call(e);!(o=(r=n.next()).done)&&(a.push(r.value),!t||a.length!==t);o=!0);}catch(e){i=!0,s=e}finally{try{o||null==n.return||n.return()}finally{if(i)throw s}}return a}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return p(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return p(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function p(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}var d={calls:{}},h=Date.now(),m=function(e){return d.calls[e]},y=function(e){return e.substr(0,e.indexOf("?")>0?e.indexOf("?"):e.length).replace(/\/+$/,"")},b=function(e){var t=e.error,n=e.serviceIdentifier,r=e.status,s=e.response,a=e.responseCode;d.calls[n]={error:t,response:s,responseCode:a,status:r},h=Date.now(),self.dispatchEvent(new CustomEvent("wjecOneUseServiceCallUpdateCache",{detail:{error:t,response:s,serviceIdentifier:n,status:r}}))},g=function(s){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},c=e(h),u=v(c,2),f=u[0],l=u[1],p=e(s),d=v(p,1),g=d[0],w=e(y(s)),C=v(w,1),j=C[0],_=t((function(){return a({allowConcurrent:!1},o)}),[o]),x=n(function(){var e=i(regeneratorRuntime.mark((function e(t){var n,r,s;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=m(j),s=r.status,_.allowConcurrent||"loading"!==s){e.next=5;break}e.next=10;break;case 5:return b({serviceIdentifier:j,status:"loading"}),e.next=8,I(a(a({},t),{},{service:g}));case 8:n=e.sent,O(n)?b({error:n.body,responseCode:n.body.status,serviceIdentifier:j,status:"failed"}):b({response:n.body,responseCode:n.status,serviceIdentifier:j,status:"done"});case 10:return e.abrupt("return",!n||O(n)?void 0:n.body);case 11:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),[_]);return r((function(){b({serviceIdentifier:j,status:"idle"})}),[]),r((function(){}),[s]),r((function(){h>f&&l(h)}),[h]),r((function(){var e=function(){l(Date.now())};return self.addEventListener("wjecOneUseServiceCallUpdateCache",e),function(){self.removeEventListener("wjecOneUseServiceCallUpdateCache",e)}}),[]),a({call:x},m(j))},w=function(e){var t=e.error,n=void 0===t?"Error":t,r=e.message,s=e.path,a=e.status,o=e.timestamp;return{body:{error:n,message:r,path:s,status:a,timestamp:void 0===o?(new Date).toISOString():o},success:!1}},O=function(e){return!1===e.success},I=function(e){var t=e.authHeader,n=void 0===t?"Auth":t,r=e.id,s=void 0===r?"":r,o=e.method,c=void 0===o?"GET":o,u=e.idToken,l=void 0===u?"":u,v=e.params,p=e.service,d="".concat(p).concat(s?"/".concat(s):""),h={headers:a(a({},l?f({},n,l):{}),{},{"Content-Type":"application/json"}),method:c};if(v)if("GET"===c){var m="string"==typeof v?v:Object.keys(v).map((function(e){return"".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(v[e]))})).join("&");d="".concat(d,"?").concat(m)}else h.body=v&&JSON.stringify(v);return fetch(d,h).then(function(){var e=i(regeneratorRuntime.mark((function e(t){var n,r;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(n=t.headers.get("Content-Type"))||!n.includes("application/json")){e.next=12;break}return e.prev=2,e.next=5,t.json();case 5:r=e.sent,e.next=12;break;case 8:throw e.prev=8,e.t0=e.catch(2),{message:"Could not parse response JSON",path:d,status:t.status};case 12:if(2===Math.floor(t.status/100)){e.next=15;break}throw{message:r&&r.message||t.statusText,path:r&&r.path||d,status:r&&r.status||t.status};case 15:return e.abrupt("return",{body:r,status:t.status,success:!0});case 16:case"end":return e.stop()}}),e,null,[[2,8]])})));return function(t){return e.apply(this,arguments)}}()).catch((function(e){return w(e)}))},C=function(){function e(t,n){var r=this,s=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};c(this,e),this._consecutiveFailureLimit=s.refreshAfterConsecutiveFailures,this._invocationCountLimit=s.refreshAfterInvocations,this._refreshInterval=s.refreshAfterInterval,this._serviceIdentifier=n,this._wheelhouseUrl=t,this._fetchServiceInstances().then((function(){return r._selectInstance()}))}var t,n,r,s;return t=e,(n=[{key:"call",value:(s=i(regeneratorRuntime.mark((function e(){var t,n,r,s,o,i,c=arguments;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=c.length>0&&void 0!==c[0]?c[0]:{},s=r.resource,o=l(r,["resource"]),e.next=4,this._fetchInstancesPromise;case 4:if(this._isRefreshRequired()&&this._selectInstance(),void 0!==(null===(t=this._activeInstance)||void 0===t?void 0:t.url)){e.next=8;break}return e.abrupt("return",w({}));case 8:return this._activeInstance.invocationCount++,e.next=11,I(a(a({},o),{},{service:"".concat(null===(n=this._activeInstance)||void 0===n?void 0:n.url).concat(s?"/".concat(s):"")}));case 11:return(i=e.sent)&&i.success?this._activeInstance.consecutiveFailures=0:(this._activeInstance.consecutiveFailures++,O(i)&&void 0===i.body.status&&(this._activeInstance.status="failed")),e.abrupt("return",i);case 14:case"end":return e.stop()}}),e,this)}))),function(){return s.apply(this,arguments)})},{key:"_fetchServiceInstances",value:function(){var e=this;return this._fetchInstancesPromise=new Promise(function(){var t=i(regeneratorRuntime.mark((function t(n){var r,s,a,o,i;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,fetch("".concat(e._wheelhouseUrl,"/").concat(e._serviceIdentifier));case 2:return r=t.sent,t.next=5,r.json();case 5:s=t.sent,a=s.body,o=JSON.parse(a),i=o.Instances,e._instances=i.map((function(e){return{consecutiveFailures:0,id:e.InstanceId,invocationCount:0,status:"unused",url:e.Attributes.URL}})),n();case 10:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}())}},{key:"_isRefreshRequired",value:function(){var e=this._activeInstance,t=!1;return"failed"===(null==e?void 0:e.status)&&this._instances.some((function(e){return"failed"!==e.status}))&&(t=!0),void 0!==this._consecutiveFailureLimit&&(null==e?void 0:e.consecutiveFailures)>=this._consecutiveFailureLimit&&(t=!0),void 0!==this._invocationCountLimit&&(null==e?void 0:e.invocationCount)>=this._invocationCountLimit&&(t=!0),void 0!==this._refreshInterval&&this._refreshInterval>=Date.now()-this._lastRefreshed&&(t=!0),t}},{key:"_selectInstance",value:function(){var e=this,t=this._instances.filter((function(t){var n;return t.id!==(null===(n=e._activeInstance)||void 0===n?void 0:n.id)})),n=t.some((function(e){return"unused"===e.status}))?t.filter((function(e){return"unused"===e.status})):t.filter((function(e){return"failed"!==e.status}));if(n.length>0){var r=n[Math.floor(Math.random()*n.length)];r.consecutiveFailures=0,r.invocationCount=0,r.status="ok",this._activeInstance=r,this._lastRefreshed=Date.now()}}},{key:"url",get:function(){var e;return null===(e=this._activeInstance)||void 0===e?void 0:e.url}},{key:"consecutiveFailureLimit",set:function(e){this._consecutiveFailureLimit=e}},{key:"invocationCountLimit",set:function(e){this._invocationCountLimit=e}},{key:"refreshInterval",set:function(e){this._refreshInterval=e}}])&&u(t.prototype,n),r&&u(t,r),Object.defineProperty(t,"prototype",{writable:!1}),e}(),j=function(e){var t=e.error,n=void 0===t?"Error":t,r=e.message,s=e.path,a=e.status,o=e.timestamp;return{body:{error:n,message:r,path:s,status:a,timestamp:void 0===o?(new Date).toISOString():o},success:!1}},_=function(e){return!1===e.success},x=function(e){var t=e.authHeader,n=void 0===t?"Auth":t,r=e.id,s=void 0===r?"":r,o=e.method,c=void 0===o?"GET":o,u=e.idToken,l=void 0===u?"":u,v=e.params,p=e.service,d="".concat(p).concat(s?"/".concat(s):""),h={headers:a(a({},l?f({},n,l):{}),{},{"Content-Type":"application/json"}),method:c};if(v)if("GET"===c){var m="string"==typeof v?v:Object.keys(v).map((function(e){return"".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(v[e]))})).join("&");d="".concat(d,"?").concat(m)}else h.body=v&&JSON.stringify(v);return fetch(d,h).then(function(){var e=i(regeneratorRuntime.mark((function e(t){var n,r;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(n=t.headers.get("Content-Type"))||!n.includes("application/json")){e.next=12;break}return e.prev=2,e.next=5,t.json();case 5:r=e.sent,e.next=12;break;case 8:throw e.prev=8,e.t0=e.catch(2),{message:"Could not parse response JSON",path:d,status:t.status};case 12:if(2===Math.floor(t.status/100)){e.next=15;break}throw{message:r&&r.message||t.statusText,path:r&&r.path||d,status:r&&r.status||t.status};case 15:return e.abrupt("return",{body:r,status:t.status,success:!0});case 16:case"end":return e.stop()}}),e,null,[[2,8]])})));return function(t){return e.apply(this,arguments)}}()).catch((function(e){return j(e)}))},k=function(e){var t=e.autoSave,n=void 0===t||t,r=e.filename,s=e.onProgress,a=e.uri;return new Promise((function(e,t){var o=new XMLHttpRequest;o.open("GET","".concat(a),!0),o.onloadstart=function(){o.responseType="arraybuffer"},o.onprogress=function(e){var t=e.loaded,n=e.total,r=Math.round(t/n*100);s&&s(r)},o.onerror=function(){return t({message:o.responseText,path:"".concat(a),status:o.status})},o.onload=function(){if(200===o.status){var s,i=r,c=o.getResponseHeader("Content-Type");try{s=new Blob([new Uint8Array(o.response)],{type:c})}catch(e){if(self.MSBlobBuilder){var u=new self.MSBlobBuilder;u.append(o.response),s=u.getBlob(c)}else t(e)}i||(o.getAllResponseHeaders().match(/Content-Disposition/i)&&(i=o.getResponseHeader("Content-Disposition").match(/filename="(.*)"/)[1]),i||(i="file")),n&&function(e,t){if(self.navigator&&self.navigator.msSaveBlob)self.navigator.msSaveBlob(e,t);else{var n=document.createElement("a"),r=URL.createObjectURL(e);n.href=r,n.download=t,document.body.appendChild(n),n.click(),n.remove(),URL.revokeObjectURL(r)}}(s,i),e({body:s,status:200,success:!0})}else try{t(JSON.parse(String.fromCharCode.apply(null,new Uint8Array(o.response))))}catch(e){t({message:o.responseText,path:"".concat(a),status:o.status})}},o.send()})).catch((function(e){return j(e)}))},S=function(e,t){return fetch(e,t).then((function(e){return e.json()}))},R=function(){var e=i(regeneratorRuntime.mark((function e(t,n){var r,s,a;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch(t);case 2:return r=e.sent,e.prev=3,e.next=6,r.json();case 6:s=e.sent,e.next=12;break;case 9:e.prev=9,e.t0=e.catch(3),s=[];case 12:if(a=s.map((function(e){return{description:e.Description,identifier:e.Name,type:e.Name.startsWith("api.")?"service":e.Name.startsWith("www.")?"frontEnd":void 0}})),!n){e.next=15;break}return e.abrupt("return",a.filter((function(e){return e.type===n})));case 15:return e.abrupt("return",a);case 16:case"end":return e.stop()}}),e,null,[[3,9]])})));return function(t,n){return e.apply(this,arguments)}}(),P={},T=function(e,t,n){for(var r in P.hasOwnProperty(e)||(P[e]={}),t)void 0===P[e][r]&&(P[e][r]=new C(e,t[r],n));return Object.freeze(P[e])},E=function(e){var t=e.file,n=e.id,r=void 0===n?"":n,s=e.onProgress,a=e.service;return new Promise((function(e,n){var o=new XMLHttpRequest,i=new FormData,c="".concat(a).concat(r?"/".concat(r):"");i.append("file",t),o.upload.onprogress=function(e){var t=e.loaded,n=e.total,r=Math.round(t/n*100);s&&s(r)},o.onerror=function(){return n({message:o.responseText,path:c,status:o.status})},o.onload=function(){if(200===o.status)e({body:void 0,status:200,success:!0});else try{n(JSON.parse(o.response))}catch(e){n({message:o.responseText,path:c,status:o.status})}},o.open("POST",c,!0),o.send(i)})).catch((function(e){return j(e)}))};export{x as callService,k as downloadFile,S as fetchJson,R as fetchWheelhouseServices,j as generateServiceError,T as getWheelhouseServices,_ as isServiceError,E as uploadFile,g as useServiceCall};
|
|
1
|
+
import{useState as e,useMemo as t,useCallback as n,useEffect as r}from"react";function s(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?s(Object(n),!0).forEach((function(t){f(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):s(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function o(e,t,n,r,s,a,o){try{var i=e[a](o),c=i.value}catch(e){return void n(e)}i.done?t(c):Promise.resolve(c).then(r,s)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,s){var a=e.apply(t,n);function i(e){o(a,r,s,i,c,"next",e)}function c(e){o(a,r,s,i,c,"throw",e)}i(void 0)}))}}function c(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function f(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function l(e,t){if(null==e)return{};var n,r,s=function(e,t){if(null==e)return{};var n,r,s={},a=Object.keys(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||(s[n]=e[n]);return s}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(s[n]=e[n])}return s}function v(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==n)return;var r,s,a=[],o=!0,i=!1;try{for(n=n.call(e);!(o=(r=n.next()).done)&&(a.push(r.value),!t||a.length!==t);o=!0);}catch(e){i=!0,s=e}finally{try{o||null==n.return||n.return()}finally{if(i)throw s}}return a}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return p(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return p(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function p(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}var d={calls:{}},h=Date.now(),m=function(e){return d.calls[e]},y=function(e){return e.substr(0,e.indexOf("?")>0?e.indexOf("?"):e.length).replace(/\/+$/,"")},b=function(e){var t=e.error,n=e.serviceIdentifier,r=e.status,s=e.response,a=e.responseCode;d.calls[n]={error:t,response:s,responseCode:a,status:r},h=Date.now(),self.dispatchEvent(new CustomEvent("wjecOneUseServiceCallUpdateCache",{detail:{error:t,response:s,serviceIdentifier:n,status:r}}))},g=function(s){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},c=e(h),u=v(c,2),f=u[0],l=u[1],p=e(s),d=v(p,1),g=d[0],w=e(y(s)),I=v(w,1),k=I[0],_=t((function(){return a({allowConcurrent:!1},o)}),[o]),j=n(function(){var e=i(regeneratorRuntime.mark((function e(t){var n,r,s;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=m(k),s=r.status,_.allowConcurrent||"loading"!==s){e.next=5;break}e.next=10;break;case 5:return b({serviceIdentifier:k,status:"loading"}),e.next=8,x(a(a({},t),{},{service:g}));case 8:n=e.sent,O(n)?b({error:n.body,responseCode:n.body.status,serviceIdentifier:k,status:"failed"}):b({response:n.body,responseCode:n.status,serviceIdentifier:k,status:"done"});case 10:return e.abrupt("return",!n||O(n)?void 0:n.body);case 11:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),[_]);return r((function(){b({serviceIdentifier:k,status:"idle"})}),[]),r((function(){}),[s]),r((function(){h>f&&l(h)}),[h]),r((function(){var e=function(){l(Date.now())};return self.addEventListener("wjecOneUseServiceCallUpdateCache",e),function(){self.removeEventListener("wjecOneUseServiceCallUpdateCache",e)}}),[]),a({call:j},m(k))},w=function(e){var t=e.error,n=void 0===t?"Error":t,r=e.message,s=e.path,a=e.status,o=e.timestamp;return{body:{error:n,message:r,path:s,status:a,timestamp:void 0===o?(new Date).toISOString():o},success:!1}},O=function(e){return!1===e.success},x=function(e){var t=e.authHeader,n=void 0===t?"Auth":t,r=e.id,s=void 0===r?"":r,o=e.method,c=void 0===o?"GET":o,u=e.idToken,l=void 0===u?"":u,v=e.params,p=e.service,d="".concat(p).concat(s?"/".concat(s):""),h={headers:a(a({},l?f({},n,l):{}),{},{"Content-Type":"application/json"}),method:c};if(v)if("GET"===c){var m="string"==typeof v?v:Object.keys(v).map((function(e){return"".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(v[e]))})).join("&");d="".concat(d,"?").concat(m)}else h.body=v&&JSON.stringify(v);return fetch(d,h).then(function(){var e=i(regeneratorRuntime.mark((function e(t){var n,r;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(n=t.headers.get("Content-Type"))||!n.includes("application/json")){e.next=12;break}return e.prev=2,e.next=5,t.json();case 5:r=e.sent,e.next=12;break;case 8:throw e.prev=8,e.t0=e.catch(2),{message:"Could not parse response JSON",path:d,status:t.status};case 12:if(2===Math.floor(t.status/100)){e.next=15;break}throw{message:r&&r.message||t.statusText,path:r&&r.path||d,status:r&&r.status||t.status};case 15:return e.abrupt("return",{body:r,status:t.status,success:!0});case 16:case"end":return e.stop()}}),e,null,[[2,8]])})));return function(t){return e.apply(this,arguments)}}()).catch((function(e){return w(e)}))},I=function(){function e(t,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};c(this,e),this._consecutiveFailureLimit=r.refreshAfterConsecutiveFailures,this._invocationCountLimit=r.refreshAfterInvocations,this._refreshInterval=r.refreshAfterInterval,this._serviceIdentifier=n,this._wheelhouseUrl=t}var t,n,r,s,o,f;return t=e,n=[{key:"call",value:(f=i(regeneratorRuntime.mark((function e(){var t,n,r,s,o,i,c=arguments;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=c.length>0&&void 0!==c[0]?c[0]:{},s=r.resource,o=l(r,["resource"]),e.next=4,this._verifyInstances();case 4:if(this._isRefreshRequired()&&this._selectInstance(),void 0!==(null===(t=this._activeInstance)||void 0===t?void 0:t.url)){e.next=8;break}return e.abrupt("return",w({message:"Missing service URL"}));case 8:return this._activeInstance.invocationCount++,e.next=11,x(a(a({},o),{},{service:"".concat(null===(n=this._activeInstance)||void 0===n?void 0:n.url).concat(s?"/".concat(s):"")}));case 11:return(i=e.sent)&&i.success?this._activeInstance.consecutiveFailures=0:(this._activeInstance.consecutiveFailures++,O(i)&&void 0===i.body.status&&(this._activeInstance.status="failed")),e.abrupt("return",i);case 14:case"end":return e.stop()}}),e,this)}))),function(){return f.apply(this,arguments)})},{key:"_isRefreshRequired",value:function(){var e=this._activeInstance,t=!1;return"failed"===(null==e?void 0:e.status)&&this._instances.some((function(e){return"failed"!==e.status}))&&(t=!0),void 0!==this._consecutiveFailureLimit&&(null==e?void 0:e.consecutiveFailures)>=this._consecutiveFailureLimit&&(t=!0),void 0!==this._invocationCountLimit&&(null==e?void 0:e.invocationCount)>=this._invocationCountLimit&&(t=!0),void 0!==this._refreshInterval&&this._refreshInterval>=Date.now()-this._lastRefreshed&&(t=!0),t}},{key:"_selectInstance",value:function(){var e=this,t=this._instances.filter((function(t){var n;return t.id!==(null===(n=e._activeInstance)||void 0===n?void 0:n.id)})),n=t.some((function(e){return"unused"===e.status}))?t.filter((function(e){return"unused"===e.status})):t.filter((function(e){return"failed"!==e.status}));if(n.length>0){var r=n[Math.floor(Math.random()*n.length)];r.consecutiveFailures=0,r.invocationCount=0,r.status="ok",this._activeInstance=r,this._lastRefreshed=Date.now()}}},{key:"_verifyInstances",value:(o=i(regeneratorRuntime.mark((function t(){var n=this;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(void 0!==this._instances){t.next=3;break}return t.next=3,e._fetchServiceInstances(this._wheelhouseUrl,this._serviceIdentifier).then((function(e){n._instances=e,n._selectInstance()}));case 3:return t.abrupt("return",Promise.resolve());case 4:case"end":return t.stop()}}),t,this)}))),function(){return o.apply(this,arguments)})},{key:"url",get:function(){var e;return null===(e=this._activeInstance)||void 0===e?void 0:e.url}},{key:"consecutiveFailureLimit",set:function(e){this._consecutiveFailureLimit=e}},{key:"invocationCountLimit",set:function(e){this._invocationCountLimit=e}},{key:"refreshInterval",set:function(e){this._refreshInterval=e}}],r=[{key:"build",value:(s=i(regeneratorRuntime.mark((function t(n,r){var s,a,o,i=arguments;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=i.length>2&&void 0!==i[2]?i[2]:{},t.next=3,e._fetchServiceInstances(n,r);case 3:return a=t.sent,(o=new e(n,r,s))._instances=a,o._selectInstance(),t.abrupt("return",o);case 8:case"end":return t.stop()}}),t)}))),function(e,t){return s.apply(this,arguments)})},{key:"_fetchServiceInstances",value:function(e,t){return new Promise(function(){var n=i(regeneratorRuntime.mark((function n(r){var s,a,o,i,c,u;return regeneratorRuntime.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return n.next=2,fetch("".concat(e,"/").concat(t));case 2:return s=n.sent,n.next=5,s.json();case 5:a=n.sent,o=a.body,i=JSON.parse(o),c=i.Instances,u=c.map((function(e){return{consecutiveFailures:0,id:e.InstanceId,invocationCount:0,status:"unused",url:e.Attributes.URL}})),r(u);case 10:case"end":return n.stop()}}),n)})));return function(e){return n.apply(this,arguments)}}())}}],n&&u(t.prototype,n),r&&u(t,r),Object.defineProperty(t,"prototype",{writable:!1}),e}(),k=function(e){var t=e.error,n=void 0===t?"Error":t,r=e.message,s=e.path,a=e.status,o=e.timestamp;return{body:{error:n,message:r,path:s,status:a,timestamp:void 0===o?(new Date).toISOString():o},success:!1}},_=function(e){return!1===e.success},j=function(e){var t=e.authHeader,n=void 0===t?"Auth":t,r=e.id,s=void 0===r?"":r,o=e.method,c=void 0===o?"GET":o,u=e.idToken,l=void 0===u?"":u,v=e.params,p=e.service,d="".concat(p).concat(s?"/".concat(s):""),h={headers:a(a({},l?f({},n,l):{}),{},{"Content-Type":"application/json"}),method:c};if(v)if("GET"===c){var m="string"==typeof v?v:Object.keys(v).map((function(e){return"".concat(encodeURIComponent(e),"=").concat(encodeURIComponent(v[e]))})).join("&");d="".concat(d,"?").concat(m)}else h.body=v&&JSON.stringify(v);return fetch(d,h).then(function(){var e=i(regeneratorRuntime.mark((function e(t){var n,r;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(n=t.headers.get("Content-Type"))||!n.includes("application/json")){e.next=12;break}return e.prev=2,e.next=5,t.json();case 5:r=e.sent,e.next=12;break;case 8:throw e.prev=8,e.t0=e.catch(2),{message:"Could not parse response JSON",path:d,status:t.status};case 12:if(2===Math.floor(t.status/100)){e.next=15;break}throw{message:r&&r.message||t.statusText,path:r&&r.path||d,status:r&&r.status||t.status};case 15:return e.abrupt("return",{body:r,status:t.status,success:!0});case 16:case"end":return e.stop()}}),e,null,[[2,8]])})));return function(t){return e.apply(this,arguments)}}()).catch((function(e){return k(e)}))},C=function(e){var t=e.autoSave,n=void 0===t||t,r=e.filename,s=e.onProgress,a=e.uri;return new Promise((function(e,t){var o=new XMLHttpRequest;o.open("GET","".concat(a),!0),o.onloadstart=function(){o.responseType="arraybuffer"},o.onprogress=function(e){var t=e.loaded,n=e.total,r=Math.round(t/n*100);s&&s(r)},o.onerror=function(){return t({message:o.responseText,path:"".concat(a),status:o.status})},o.onload=function(){if(200===o.status){var s,i=r,c=o.getResponseHeader("Content-Type");try{s=new Blob([new Uint8Array(o.response)],{type:c})}catch(e){if(self.MSBlobBuilder){var u=new self.MSBlobBuilder;u.append(o.response),s=u.getBlob(c)}else t(e)}i||(o.getAllResponseHeaders().match(/Content-Disposition/i)&&(i=o.getResponseHeader("Content-Disposition").match(/filename="(.*)"/)[1]),i||(i="file")),n&&function(e,t){if(self.navigator&&self.navigator.msSaveBlob)self.navigator.msSaveBlob(e,t);else{var n=document.createElement("a"),r=URL.createObjectURL(e);n.href=r,n.download=t,document.body.appendChild(n),n.click(),n.remove(),URL.revokeObjectURL(r)}}(s,i),e({body:s,status:200,success:!0})}else try{t(JSON.parse(String.fromCharCode.apply(null,new Uint8Array(o.response))))}catch(e){t({message:o.responseText,path:"".concat(a),status:o.status})}},o.send()})).catch((function(e){return k(e)}))},R=function(e,t){return fetch(e,t).then((function(e){return e.json()}))},S=function(){var e=i(regeneratorRuntime.mark((function e(t,n){var r,s,a;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch(t);case 2:return r=e.sent,e.prev=3,e.next=6,r.json();case 6:s=e.sent,e.next=12;break;case 9:e.prev=9,e.t0=e.catch(3),s=[];case 12:if(a=s.map((function(e){return{description:e.Description,identifier:e.Name,type:e.Name.startsWith("api.")?"service":e.Name.startsWith("www.")?"frontEnd":void 0}})),!n){e.next=15;break}return e.abrupt("return",a.filter((function(e){return e.type===n})));case 15:return e.abrupt("return",a);case 16:case"end":return e.stop()}}),e,null,[[3,9]])})));return function(t,n){return e.apply(this,arguments)}}(),P={},T=function(){var e=i(regeneratorRuntime.mark((function e(t,n,r){var s,o,i;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(o in s={},P.hasOwnProperty(t)||(P[t]={}),n)void 0===P[t][o]&&(s[o]=I.build(t,n[o],r));return e.next=5,Promise.all(Object.values(s));case 5:e.t0=regeneratorRuntime.keys(s);case 6:if((e.t1=e.t0()).done){e.next=13;break}return i=e.t1.value,e.next=10,s[i];case 10:P[t][i]=e.sent,e.next=6;break;case 13:return e.abrupt("return",Object.freeze(a({},P[t])));case 14:case"end":return e.stop()}}),e)})));return function(t,n,r){return e.apply(this,arguments)}}(),U=function(e,t,n){for(var r in P.hasOwnProperty(e)||(P[e]={}),t)void 0===P[e][r]&&(P[e][r]=new I(e,t[r],n));return Object.freeze(a({},P[e]))},E=function(e){var t=e.file,n=e.id,r=void 0===n?"":n,s=e.onProgress,a=e.service;return new Promise((function(e,n){var o=new XMLHttpRequest,i=new FormData,c="".concat(a).concat(r?"/".concat(r):"");i.append("file",t),o.upload.onprogress=function(e){var t=e.loaded,n=e.total,r=Math.round(t/n*100);s&&s(r)},o.onerror=function(){return n({message:o.responseText,path:c,status:o.status})},o.onload=function(){if(200===o.status)e({body:void 0,status:200,success:!0});else try{n(JSON.parse(o.response))}catch(e){n({message:o.responseText,path:c,status:o.status})}},o.open("POST",c,!0),o.send(i)})).catch((function(e){return k(e)}))};export{I as WheelhouseService,j as callService,C as downloadFile,R as fetchJson,S as fetchWheelhouseServices,k as generateServiceError,T as getWheelhouseServices,U as getWheelhouseServicesSync,_ as isServiceError,E as uploadFile,g as useServiceCall};
|