@dianamics/pcf-utils
Version:
This npm package is designed for Power Apps Component Framework (PCF). It provides utilities for working with PCF, most of them are React Hooks, which needs to be used in function components.
3 lines (2 loc) • 12.8 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function t(e,t,r,n,o,a,i){try{var u=e[a](i),c=u.value}catch(e){return void r(e)}u.done?t(c):Promise.resolve(c).then(n,o)}function r(e){return function(){var r=this,n=arguments;return new Promise((function(o,a){var i=e.apply(r,n);function u(e){t(i,o,a,u,c,"next",e)}function c(e){t(i,o,a,u,c,"throw",e)}u(void 0)}))}}var n,o,a=(function(e){var t=function(e){var t=Object.prototype,r=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function u(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{u({},"")}catch(e){u=function(e,t,r){return e[t]=r}}function c(e,t,r,n){var o=Object.create((t&&t.prototype instanceof f?t:f).prototype),a=new S(n||[]);return o._invoke=function(e,t,r){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var u=b(i,r);if(u){if(u===l)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=s(e,t,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}(e,r,a),o}function s(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=c;var l={};function f(){}function v(){}function p(){}var h={};h[o]=function(){return this};var d=Object.getPrototypeOf,m=d&&d(d(L([])));m&&m!==t&&r.call(m,o)&&(h=m);var g=p.prototype=f.prototype=Object.create(h);function y(e){["next","throw","return"].forEach((function(t){u(e,t,(function(e){return this._invoke(t,e)}))}))}function x(e,t){var n;this._invoke=function(o,a){function i(){return new t((function(n,i){!function n(o,a,i,u){var c=s(e[o],e,a);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==typeof f&&r.call(f,"__await")?t.resolve(f.__await).then((function(e){n("next",e,i,u)}),(function(e){n("throw",e,i,u)})):t.resolve(f).then((function(e){l.value=e,i(l)}),(function(e){return n("throw",e,i,u)}))}u(c.arg)}(o,a,n,i)}))}return n=n?n.then(i,i):i()}}function b(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,b(e,t),"throw"===t.method))return l;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=s(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,l;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,l):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,l)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function S(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,a=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return a.next=a}}return{next:N}}function N(){return{value:void 0,done:!0}}return v.prototype=g.constructor=p,p.constructor=v,v.displayName=u(p,i,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===v||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,p):(e.__proto__=p,u(e,i,"GeneratorFunction")),e.prototype=Object.create(g),e},e.awrap=function(e){return{__await:e}},y(x.prototype),x.prototype[a]=function(){return this},e.AsyncIterator=x,e.async=function(t,r,n,o,a){void 0===a&&(a=Promise);var i=new x(c(t,r,n,o),a);return e.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},y(g),u(g,i,"Generator"),g[o]=function(){return this},g.toString=function(){return"[object Generator]"},e.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},e.values=L,S.prototype={constructor:S,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return i.type="throw",i.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=r.call(a,"catchLoc"),c=r.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=t&&t<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=t,a?(this.method="next",this.next=a.finallyLoc,l):this.complete(i)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),l},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),E(r),l}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;E(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:L(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},e}(e.exports);try{regeneratorRuntime=t}catch(e){Function("r","regeneratorRuntime = r")(t)}}(o={exports:{}}),o.exports),i={useStorageCache:!0,STORAGE_PREFIX:"Dianamics.EnvironmentVariables"};(n=exports.EnvironmentVariableTypes||(exports.EnvironmentVariableTypes={}))[n.String=1e8]="String",n[n.Number=100000001]="Number",n[n.Boolean=100000002]="Boolean",n[n.JSON=100000003]="JSON",n[n.DataSource=100000004]="DataSource";var u={},c=function(){var e=r(a.mark((function e(t,r,n){var o,c,s,l,f,v,p,h,d,m;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==(s=u[r])){e.next=3;break}return e.abrupt("return",Promise.resolve(JSON.parse(s)));case 3:if(!0!==i.useStorageCache){e.next=7;break}if(null==(s=sessionStorage.getItem("[Dianamics.EnvironmentVariables] "+r))){e.next=7;break}return e.abrupt("return",Promise.resolve(JSON.parse(s)));case 7:return l=[void 0!==r?"schemaname eq '"+r+"'":void 0,void 0!==n?"type eq "+n:void 0].filter(Boolean).join(" and "),f=["?$select=","schemaname,defaultvalue,displayname","&$expand=environmentvariabledefinition_environmentvariablevalue($select=value)",""!==l?"&$filter="+l:""].join(""),e.next=11,t.retrieveMultipleRecords("environmentvariabledefinition",f).catch(console.error);case 11:if(null!=(p=null==(v=e.sent)?void 0:v.entities[0])){e.next=15;break}return e.abrupt("return",{value:void 0,defaultValue:void 0});case 15:return h=p.defaultvalue,d=null==(o=p.environmentvariabledefinition_environmentvariablevalue)||null==(c=o[0])?void 0:c.value,m={value:null!=d?d:h,defaultValue:h},u[r]=JSON.stringify(m),null!=r&&""!=r&&null!=m&&!0===i.useStorageCache&&sessionStorage.setItem("[Dianamics.EnvironmentVariables] "+r,JSON.stringify(m)),e.abrupt("return",m);case 21:case"end":return e.stop()}}),e)})));return function(t,r,n){return e.apply(this,arguments)}}(),s=function(){var e=r(a.mark((function e(t,r){var n;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,c(t,null==r?void 0:r.toLowerCase(),exports.EnvironmentVariableTypes.String);case 2:return e.abrupt("return",null==(n=e.sent)?void 0:n.value);case 4:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}(),l=function(){var e=r(a.mark((function e(t,r){var n,o;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,c(t,null==r?void 0:r.toLowerCase(),exports.EnvironmentVariableTypes.JSON);case 2:return o=null==(n=e.sent)?void 0:n.value,e.prev=4,e.abrupt("return",null!=o?JSON.parse(o):void 0);case 8:return e.prev=8,e.t0=e.catch(4),e.abrupt("return",void 0);case 11:case"end":return e.stop()}}),e,null,[[4,8]])})));return function(t,r){return e.apply(this,arguments)}}(),f=function(){var e=r(a.mark((function e(t,r){var n,o;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,c(t,null==r?void 0:r.toLowerCase(),exports.EnvironmentVariableTypes.Number);case 2:return e.abrupt("return",null!=(o=null==(n=e.sent)?void 0:n.value)?Number.parseFloat(o):void 0);case 5:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}(),v=function(){var e=r(a.mark((function e(t,r){var n,o;return a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,c(t,null==r?void 0:r.toLowerCase(),exports.EnvironmentVariableTypes.Boolean);case 2:return e.abrupt("return",null!=(o=null==(n=e.sent)?void 0:n.value)?new Boolean(o):void 0);case 5:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}(),p=function(e,t,r){return new Promise((function(n,o){e.getResource(t,(function(e){n("data:image/"+("svg"===r?"svg+xml":r)+";base64,"+e)}),o)}))};exports.clearCache=function(){u={}},exports.get=c,exports.getBoolean=v,exports.getJSON=l,exports.getNumber=f,exports.getResourceImagePromise=p,exports.getString=s,exports.setupEnvironmentVariable=function(e,t){i.useStorageCache=e,i.STORAGE_PREFIX=null!=t?t:i.STORAGE_PREFIX},exports.useEnvironmentVariable=function(t,r,n){var o=e.useState(),a=o[0],i=o[1],u=e.useState(!1),c=u[0],p=u[1],h=e.useState(),d=h[0],m=h[1],g=function(){p(!1)},y=function(e){var t;p(!1),m(null!=(t=null==e?void 0:e.message)?t:e)};return e.useEffect((function(){p(!0),m(void 0),n!==exports.EnvironmentVariableTypes.String?n!==exports.EnvironmentVariableTypes.Number?n!==exports.EnvironmentVariableTypes.Boolean?n!==exports.EnvironmentVariableTypes.JSON?n!==exports.EnvironmentVariableTypes.DataSource||s(t,r).then((function(e){return i(e)})).then(g).catch(y):l(t,r).then((function(e){return i(e)})).then(g).catch(y):v(t,r).then((function(e){return i(e)})).then(g).catch(y):f(t,r).then((function(e){return i(e)})).then(g).catch(y):s(t,r).then((function(e){return i(e)})).then(g).catch(y)}),[r]),{value:a,isLoading:c,errorMessage:d}},exports.usePaging=function(t){var r=e.useState(0),n=r[0],o=r[1],a=e.useState(),i=a[0],u=a[1],c=e.useState(),s=c[0],l=c[1],f=e.useState(0),v=f[0],p=f[1],h=e.useState(0),d=h[0],m=h[1];return e.useEffect((function(){t.paging.hasPreviousPage||(m(t.sortedRecordIds.length),p(1),l(t.paging.totalResultCount)),o((v-1)*d+1),u((v-1)*d+t.sortedRecordIds.length)}),[t]),{currentPage:v,firstItemNumber:n,lastItemNumber:i,totalRecords:s,moveToFirst:function(){p(1),t.paging.loadExactPage(1)},movePrevious:function(){var e=v-1;p(e),t.paging.loadExactPage(e)},moveNext:function(){var e=v+1;p(e),t.paging.loadExactPage(e)}}},exports.useResourceImage=function(t,r,n){var o=e.useState(),a=o[0],i=o[1],u=e.useState(),c=u[0],s=u[1],l=e.useState(),f=l[0],v=l[1];return e.useEffect((function(){null!=r&&""!==r&&(s(!0),v(void 0),p(t,r,n).then((function(e){i(e),s(!1)}),(function(e){var t;console.error(e),s(!1),v(null!=(t=null==e?void 0:e.message)?t:e)})))}),[r,n]),{src:a,isLoading:c,errorMessage:f}},exports.useResourceImages=function(t,r,n){var o=e.useState([]),a=o[0],i=o[1];return e.useEffect((function(){Promise.all(r.map((function(e){return p(t,e,n)}))).then(i).catch(console.error)}),[].concat(r)),a};
//# sourceMappingURL=pcf-utils.cjs.production.min.js.map