@unifiedsoftware/react-plugin-remote 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- 'use strict';var H=require('react'),X=require('@unifiedsoftware/http-client'),jsxRuntime=require('react/jsx-runtime');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var H__default=/*#__PURE__*/_interopDefault(H);var X__namespace=/*#__PURE__*/_interopNamespace(X);var $=Object.defineProperty;var z=(e,t)=>{for(var r in t)$(e,r,{get:t[r],enumerable:true});};var N=(a=>(a.INTERNAL="internal",a.MICROFRONTEND="microfrontend",a.IFRAME="iframe",a))(N||{}),K=(e=>e)(K||{});function Q(e){return ()=>({manifest:e.manifest,initialize:e.initialize,activate:e.activate,deactivate:e.deactivate,dispose:e.dispose,components:e.components})}function re(e){return e}function ae(e){return e}function j(e){return e}function q(e,t){let r=e.id,a=e.apis||{},n={};for(let[i,c]of Object.entries(a)){let h=c.endpoints||c.allowedEndpoints||{};n[i]={};for(let[s,o]of Object.entries(h))n[i][s]=async(p={})=>{let{params:d={},data:f,headers:y}=p,u=o.path,l={},g=/{([^}]+)}/g,m;for(;(m=g.exec(o.path))!==null;){let x=m[1];d[x]&&(l[x]=d[x],u=u.replace(`{${x}}`,d[x]));}let T={...d};for(let x of Object.keys(l))delete T[x];let C=o.method||o.methods&&o.methods[0]||"GET";return t.request({pluginId:r,apiName:i,endpoint:s,method:C,params:Object.keys(T).length>0?T:void 0,data:f,headers:y})};}return n}function J(e,t,r,a,n,i){let[c,h]=H.useState(null),[s,o]=H.useState(false),[p,d]=H.useState(null),f=i?.enabled!==false,y=i?.refetchInterval,u=H.useRef(null),l=H.useCallback(async()=>{if(f){o(true),d(null);try{let g=await e.capabilities.api.request({pluginId:t,apiName:r,endpoint:a,method:"GET",...n});h(g),o(!1),i?.onSuccess&&i.onSuccess(g);}catch(g){let m=g instanceof Error?g:new Error(String(g));d(m),o(false),i?.onError&&i.onError(m);}}},[t,r,a,f,JSON.stringify(n)]);return H.useEffect(()=>{f&&l();},[l,f]),H.useEffect(()=>{if(y&&f)return u.current=setInterval(l,y),()=>{u.current&&clearInterval(u.current);}},[y,l,f]),{data:c,loading:s,error:p,refetch:l,isSuccess:!s&&!p&&c!==null,isError:!s&&p!==null}}function _(e,t,r,a,n="POST",i){let[c,h]=H.useState(null),[s,o]=H.useState(false),[p,d]=H.useState(null),f=H.useCallback(async u=>{o(true),d(null);try{let l=await e.capabilities.api.request({pluginId:t,apiName:r,endpoint:a,method:n,...u});return h(l),o(!1),i?.onSuccess&&i.onSuccess(l,u),l}catch(l){let g=l instanceof Error?l:new Error(String(l));throw d(g),o(false),i?.onError&&i.onError(g,u),g}},[t,r,a,n,i]),y=H.useCallback(()=>{h(null),d(null),o(false);},[]);return {data:c,loading:s,error:p,mutate:f,reset:y,isSuccess:!s&&!p&&c!==null,isError:!s&&p!==null}}function O(e,t,r){let a=r.apis||{},n={};for(let[i,c]of Object.entries(a)){n[i]={};let h=c.endpoints||c.allowedEndpoints||{};for(let[s,o]of Object.entries(h)){let p=o.method||o.methods&&o.methods[0]||"GET",d=`use${s.charAt(0).toUpperCase()}${s.slice(1)}`;p==="GET"?n[i][d]=(f,y)=>J(e,t,i,s,f,y):n[i][d]=f=>_(e,t,i,s,p,f);}}return n}function k(e,t){let r=e.id,a=e.apis||{},n={};for(let[i,c]of Object.entries(a)){if(!c.graphql)continue;let h=c.graphql,s=h.operations||{};n[i]={query:async(o,p)=>{let d=s[o];if(!d)throw new Error(`GraphQL operation '${o}' not found in manifest`);if(d.type!=="query")throw new Error(`Operation '${o}' is not a query`);let f=await t.graphql({pluginId:r,apiName:i,query:d.query,variables:p||d.variables||{},operationName:o,headers:h.headers});if(f.errors&&f.errors.length>0)throw new Error(f.errors[0].message);return f.data},mutate:async(o,p)=>{let d=s[o];if(!d)throw new Error(`GraphQL operation '${o}' not found in manifest`);if(d.type!=="mutation")throw new Error(`Operation '${o}' is not a mutation`);let f=await t.graphql({pluginId:r,apiName:i,query:d.query,variables:p||d.variables||{},operationName:o,headers:h.headers});if(f.errors&&f.errors.length>0)throw new Error(f.errors[0].message);return f.data},subscribe:(o,p,d)=>(console.warn("GraphQL subscriptions not yet implemented"),()=>{})};}return n}function S(e,t,r){return {async query(a,n,i){let c=await r.graphql({pluginId:e,apiName:t,query:a,variables:n||{},operationName:i});if(c.errors&&c.errors.length>0)throw new Error(c.errors[0].message);return c.data},async mutate(a,n,i){let c=await r.graphql({pluginId:e,apiName:t,query:a,variables:n||{},operationName:i});if(c.errors&&c.errors.length>0)throw new Error(c.errors[0].message);return c.data}}}function de(e,t,r={}){let{enabled:a=true,refetchOnMount:n=true,refetchInterval:i,onSuccess:c,onError:h,cacheTime:s=300*1e3}=r,[o,p]=H.useState(void 0),[d,f]=H.useState(a),[y,u]=H.useState(void 0),l=H.useRef(true),g=H.useRef(null),m=H.useCallback(async()=>{if(a){if(g.current&&Date.now()-g.current.timestamp<s){p(g.current.data),f(false);return}f(true),u(void 0);try{let T=await e.graphql(t);if(!l.current)return;if(T.errors&&T.errors.length>0){let C=new Error(T.errors[0].message);u(C),h?.(C);}else T.data&&(p(T.data),g.current={data:T.data,timestamp:Date.now()},c?.(T.data));}catch(T){if(!l.current)return;let C=T instanceof Error?T:new Error(String(T));u(C),h?.(C);}finally{l.current&&f(false);}}},[e,t,a,s,c,h]);return H.useEffect(()=>{n&&m();},[m,n]),H.useEffect(()=>{if(!i||!a)return;let T=setInterval(()=>{m();},i);return ()=>clearInterval(T)},[i,a,m]),H.useEffect(()=>()=>{l.current=false;},[]),{data:o,loading:d,error:y,refetch:m}}function ue(e,t,r={}){let{onSuccess:a,onError:n}=r,[i,c]=H.useState(void 0),[h,s]=H.useState(false),[o,p]=H.useState(void 0),d=H.useRef(true);H.useEffect(()=>()=>{d.current=false;},[]);let f=H.useCallback(async u=>{s(true),p(void 0);try{let l=await e.graphql({...t,variables:u});if(!d.current)throw new Error("Component unmounted");if(l.errors&&l.errors.length>0){let g=new Error(l.errors[0].message);throw p(g),n?.(g,u),g}if(l.data)return c(l.data),a?.(l.data,u),l.data;throw new Error("No data returned from mutation")}catch(l){if(!d.current)throw l;let g=l instanceof Error?l:new Error(String(l));throw p(g),n?.(g,u),g}finally{d.current&&s(false);}},[e,t,a,n]),y=H.useCallback(()=>{c(void 0),p(void 0),s(false);},[]);return {mutate:f,data:i,loading:h,error:o,reset:y}}function fe(e,t,r={}){return console.warn("GraphQL subscriptions not yet implemented"),{data:void 0,loading:false,error:new Error("Subscriptions not yet implemented"),refetch:async()=>{}}}var M=class{constructor(t,r){this.context=t;this.pluginId=r;}async request(t){let{url:r,method:a,headers:n,body:i,params:c}=t,h=r.split("/").pop()?.split("?")[0]??"";try{let s=await this.context.capabilities.api.request({pluginId:this.pluginId,endpoint:h,method:a,data:i,params:c,headers:n});return {data:s&&typeof s=="object"&&"ObjOptional"in s?s.ObjOptional:s,status:200,statusText:"OK",headers:{}}}catch(s){throw {message:s.message||"Plugin API Request Failed",status:s.status||500,details:s}}}};function U(e,t){return new M(e,t)}var L={};z(L,{GraphQLCache:()=>I,GraphQLClient:()=>F,GraphQLError:()=>v,createGraphQLClient:()=>W});var I=class{cache=new Map;defaultTTL;constructor(e=3e5){this.defaultTTL=e;}generateKey(e,t){let r=t?JSON.stringify(t):"";return `${e}:${r}`}isValid(e){return Date.now()-e.timestamp<e.ttl}get(e,t){let r=this.generateKey(e,t),a=this.cache.get(r);return a?this.isValid(a)?a.data:(this.cache.delete(r),null):null}set(e,t,r,a){let n=this.generateKey(e,r);this.cache.set(n,{data:t,timestamp:Date.now(),ttl:a||this.defaultTTL});}clear(e,t){let r=this.generateKey(e,t);this.cache.delete(r);}clearAll(){this.cache.clear();}get size(){return this.cache.size}},v=class extends Error{constructor(e,t,r){super(e),this.errors=t,this.response=r,this.name="GraphQLError";}get firstError(){var e;return ((e=this.errors[0])==null?void 0:e.message)||this.message}get messages(){return this.errors.map(e=>e.message)}},F=class{httpClient;endpoint;defaultHeaders;cache;constructor(e,t){this.httpClient=e,this.endpoint=t.endpoint,this.defaultHeaders={"Content-Type":"application/json",...t.headers},t.cache&&(this.cache=new I(t.cacheTTL));}async query(e,t,r){if(this.cache){let i=this.cache.get(e,t);if(i!==null)return i}let a={query:e,variables:t,operationName:r},n=await this.httpClient.post(this.endpoint,a,{headers:this.defaultHeaders});if(n.errors&&n.errors.length>0)throw new v(n.errors[0].message,n.errors,n);if(!n.data)throw new v("No data returned from GraphQL query",[],n);return this.cache&&this.cache.set(e,n.data,t),n.data}async mutate(e,t,r){let a={query:e,variables:t,operationName:r},n=await this.httpClient.post(this.endpoint,a,{headers:this.defaultHeaders});if(n.errors&&n.errors.length>0)throw new v(n.errors[0].message,n.errors,n);if(!n.data)throw new v("No data returned from GraphQL mutation",[],n);return n.data}async request(e){return await this.httpClient.post(this.endpoint,e,{headers:this.defaultHeaders})}clearCache(e,t){this.cache&&(e?this.cache.clear(e,t):this.cache.clearAll());}getHttpClient(){return this.httpClient}};function W(e,t){return new F(e,t)}var{HttpClient:B}=X__namespace,{createGraphQLClient:xe}=L;function De(e){let t,r,a,n,i={};return {PluginProvider:({children:y})=>{if(!(Object.keys(i).length>0))return null;let l=e.providers.reduceRight((g,m)=>{let{provider:T}=m,C;return "clientKey"in m&&m.clientKey?C=i[m.clientKey]:"value"in m&&(typeof m.value=="function"?C=m.value(i):C=m.value),H__default.default.createElement(T,{value:C},g)},y);return H__default.default.createElement(H__default.default.Fragment,null,l)},initialize:y=>{t=y;let u=y.manifest||e.manifest;console.log(`[${u.id}] Context Injected`),r=q(u,y.capabilities.api),a=O(y,u.id,u),n=k(u,y.capabilities.api);let m={httpClient:new B({adapter:U(y,u.id)}),useGraphQL:C=>S(u.id,C,y.capabilities.api),manifest:u,hostContext:y},T=e.configureServices(m);Object.assign(i,T);},useHostContext:()=>{if(!t)throw new Error("HostContext not initialized");return t},useApi:()=>{if(!r)throw new Error("ApiClient not initialized");return r},usePluginHooks:()=>{if(!a)throw new Error("ApiHooks not initialized");return a},useGraphQL:()=>{if(!n)throw new Error("GraphQLClient not initialized");return n},getClient:y=>{let u=i[y];if(!u)throw new Error(`Client ${String(y)} not initialized`);return u}}}function Me(e){let{integrations:t,extensions:r=[],extraCapabilities:a=[],...n}=e,i={},c={},h=new Set(["notification","storage","events",...a]);return t&&(h.add("api_proxy"),Object.entries(t).forEach(([o,p])=>{i[o]={baseUrl:p.baseUrl,graphqlUrl:p.graphql?.url},c[o]={strategy:"proxy",baseUrl:p.baseUrl,auth:"inherit",...p.graphql&&{graphql:{url:p.graphql.url,auth:"inherit",operations:p.graphql.operations,headers:{"Content-Type":"application/json"},cache:{ttl:3e5,enabled:true}}}};})),{manifest:{version:"1.0.0",author:"Unified Software",icon:"extension",...n,remote:{url:"https://localhost:4173/assets/remoteEntry.js",scope:"portalWeb",module:`./${e.id}`},capabilities:Array.from(h),extensionPoints:r,apis:c},endpoints:i}}function Le(e){return Q({manifest:e.manifest,components:e.components,async initialize(t){if(console.log(`[${e.manifest.id}] Initializing (Framework)...`),e.onContextInit&&e.onContextInit(t),e.settings?.defaults)try{let r=`${e.manifest.id}-settings`;await t.capabilities.storage.get(r)||await t.capabilities.storage.set(r,e.settings.defaults);}catch(r){console.error(`[${e.manifest.id}] Failed to init settings:`,r);}e.events&&Object.entries(e.events).forEach(([r,a])=>{t.capabilities.events.on(r,n=>a(t,n));}),e.onInitialize&&await e.onInitialize(t);},async activate(t){console.log(`[${e.manifest.id}] Activating (Framework)...`),t.capabilities.events.emit("plugin:activated",{pluginId:e.manifest.id}),e.onActivate&&await e.onActivate(t);},async deactivate(){e.onDeactivate&&await e.onDeactivate();},async dispose(){e.onDispose&&await e.onDispose();}})}var ee=()=>jsxRuntime.jsx("div",{style:{display:"flex",justifyContent:"center",alignItems:"center",padding:"1rem",width:"100%",color:"#6c757d"},children:"Loading..."});function Ge(e,t=ee){let r=H__default.default.lazy(e);return a=>jsxRuntime.jsx(H.Suspense,{fallback:jsxRuntime.jsx(t,{}),children:jsxRuntime.jsx(r,{...a})})}
1
+ 'use strict';var H=require('react'),X=require('@unifiedsoftware/http-client'),jsxRuntime=require('react/jsx-runtime');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var H__default=/*#__PURE__*/_interopDefault(H);var X__namespace=/*#__PURE__*/_interopNamespace(X);var $=Object.defineProperty;var z=(e,t)=>{for(var r in t)$(e,r,{get:t[r],enumerable:true});};var N=(a=>(a.INTERNAL="internal",a.MICROFRONTEND="microfrontend",a.IFRAME="iframe",a))(N||{}),K=(e=>e)(K||{});function Q(e){return ()=>({manifest:e.manifest,initialize:e.initialize,activate:e.activate,deactivate:e.deactivate,dispose:e.dispose,components:e.components})}function re(e){return e}function ae(e){return e}function j(e){return e}function q(e,t){let r=e.id,a=e.apis||{},n={};for(let[i,c]of Object.entries(a)){let h=c.endpoints||c.allowedEndpoints||{};n[i]={};for(let[s,p]of Object.entries(h))n[i][s]=async(f={})=>{let{params:o={},data:u,headers:y}=f,d=p.path,l={},g=/{([^}]+)}/g,m;for(;(m=g.exec(p.path))!==null;){let x=m[1];o[x]&&(l[x]=o[x],d=d.replace(`{${x}}`,o[x]));}let T={...o};for(let x of Object.keys(l))delete T[x];let C=p.method||p.methods&&p.methods[0]||"GET";return t.request({pluginId:r,apiName:i,endpoint:s,method:C,params:Object.keys(T).length>0?T:void 0,data:u,headers:y})};}return n}function J(e,t,r,a,n,i){let[c,h]=H.useState(null),[s,p]=H.useState(false),[f,o]=H.useState(null),u=i?.enabled!==false,y=i?.refetchInterval,d=H.useRef(null),l=H.useCallback(async()=>{if(u){p(true),o(null);try{let g=await e.capabilities.api.request({pluginId:t,apiName:r,endpoint:a,method:"GET",...n});h(g),p(!1),i?.onSuccess&&i.onSuccess(g);}catch(g){let m=g instanceof Error?g:new Error(String(g));o(m),p(false),i?.onError&&i.onError(m);}}},[t,r,a,u,JSON.stringify(n)]);return H.useEffect(()=>{u&&l();},[l,u]),H.useEffect(()=>{if(y&&u)return d.current=setInterval(l,y),()=>{d.current&&clearInterval(d.current);}},[y,l,u]),{data:c,loading:s,error:f,refetch:l,isSuccess:!s&&!f&&c!==null,isError:!s&&f!==null}}function _(e,t,r,a,n="POST",i){let[c,h]=H.useState(null),[s,p]=H.useState(false),[f,o]=H.useState(null),u=H.useCallback(async d=>{p(true),o(null);try{let l=await e.capabilities.api.request({pluginId:t,apiName:r,endpoint:a,method:n,...d});return h(l),p(!1),i?.onSuccess&&i.onSuccess(l,d),l}catch(l){let g=l instanceof Error?l:new Error(String(l));throw o(g),p(false),i?.onError&&i.onError(g,d),g}},[t,r,a,n,i]),y=H.useCallback(()=>{h(null),o(null),p(false);},[]);return {data:c,loading:s,error:f,mutate:u,reset:y,isSuccess:!s&&!f&&c!==null,isError:!s&&f!==null}}function O(e,t,r){let a=r.apis||{},n={};for(let[i,c]of Object.entries(a)){n[i]={};let h=c.endpoints||c.allowedEndpoints||{};for(let[s,p]of Object.entries(h)){let f=p.method||p.methods&&p.methods[0]||"GET",o=`use${s.charAt(0).toUpperCase()}${s.slice(1)}`;f==="GET"?n[i][o]=(u,y)=>J(e,t,i,s,u,y):n[i][o]=u=>_(e,t,i,s,f,u);}}return n}function k(e,t){let r=e.id,a=e.apis||{},n={};for(let[i,c]of Object.entries(a)){if(!c.graphql)continue;let h=c.graphql,s=h.operations||{};n[i]={query:async(p,f)=>{let o=s[p];if(!o)throw new Error(`GraphQL operation '${p}' not found in manifest`);if(o.type!=="query")throw new Error(`Operation '${p}' is not a query`);let u=await t.graphql({pluginId:r,apiName:i,query:o.query,variables:f||o.variables||{},operationName:p,headers:h.headers});if(u.errors&&u.errors.length>0)throw new Error(u.errors[0].message);return u.data},mutate:async(p,f)=>{let o=s[p];if(!o)throw new Error(`GraphQL operation '${p}' not found in manifest`);if(o.type!=="mutation")throw new Error(`Operation '${p}' is not a mutation`);let u=await t.graphql({pluginId:r,apiName:i,query:o.query,variables:f||o.variables||{},operationName:p,headers:h.headers});if(u.errors&&u.errors.length>0)throw new Error(u.errors[0].message);return u.data},subscribe:(p,f,o)=>(console.warn("GraphQL subscriptions not yet implemented"),()=>{})};}return n}function S(e,t,r){return {async query(a,n,i){let c=await r.graphql({pluginId:e,apiName:t,query:a,variables:n||{},operationName:i});if(c.errors&&c.errors.length>0)throw new Error(c.errors[0].message);return c.data},async mutate(a,n,i){let c=await r.graphql({pluginId:e,apiName:t,query:a,variables:n||{},operationName:i});if(c.errors&&c.errors.length>0)throw new Error(c.errors[0].message);return c.data}}}function de(e,t,r={}){let{enabled:a=true,refetchOnMount:n=true,refetchInterval:i,onSuccess:c,onError:h,cacheTime:s=300*1e3}=r,[p,f]=H.useState(void 0),[o,u]=H.useState(a),[y,d]=H.useState(void 0),l=H.useRef(true),g=H.useRef(null),m=H.useCallback(async()=>{if(a){if(g.current&&Date.now()-g.current.timestamp<s){f(g.current.data),u(false);return}u(true),d(void 0);try{let T=await e.graphql(t);if(!l.current)return;if(T.errors&&T.errors.length>0){let C=new Error(T.errors[0].message);d(C),h?.(C);}else T.data&&(f(T.data),g.current={data:T.data,timestamp:Date.now()},c?.(T.data));}catch(T){if(!l.current)return;let C=T instanceof Error?T:new Error(String(T));d(C),h?.(C);}finally{l.current&&u(false);}}},[e,t,a,s,c,h]);return H.useEffect(()=>{n&&m();},[m,n]),H.useEffect(()=>{if(!i||!a)return;let T=setInterval(()=>{m();},i);return ()=>clearInterval(T)},[i,a,m]),H.useEffect(()=>()=>{l.current=false;},[]),{data:p,loading:o,error:y,refetch:m}}function ue(e,t,r={}){let{onSuccess:a,onError:n}=r,[i,c]=H.useState(void 0),[h,s]=H.useState(false),[p,f]=H.useState(void 0),o=H.useRef(true);H.useEffect(()=>()=>{o.current=false;},[]);let u=H.useCallback(async d=>{s(true),f(void 0);try{let l=await e.graphql({...t,variables:d});if(!o.current)throw new Error("Component unmounted");if(l.errors&&l.errors.length>0){let g=new Error(l.errors[0].message);throw f(g),n?.(g,d),g}if(l.data)return c(l.data),a?.(l.data,d),l.data;throw new Error("No data returned from mutation")}catch(l){if(!o.current)throw l;let g=l instanceof Error?l:new Error(String(l));throw f(g),n?.(g,d),g}finally{o.current&&s(false);}},[e,t,a,n]),y=H.useCallback(()=>{c(void 0),f(void 0),s(false);},[]);return {mutate:u,data:i,loading:h,error:p,reset:y}}function fe(e,t,r={}){return console.warn("GraphQL subscriptions not yet implemented"),{data:void 0,loading:false,error:new Error("Subscriptions not yet implemented"),refetch:async()=>{}}}var M=class{constructor(t,r){this.context=t;this.pluginId=r;}async request(t){let{url:r,method:a,headers:n,body:i,params:c}=t,h=r.split("/").pop()?.split("?")[0]??"";try{let s=await this.context.capabilities.api.request({pluginId:this.pluginId,endpoint:h,method:a,data:i,params:c,headers:n});return {data:s&&typeof s=="object"&&"ObjOptional"in s?s.ObjOptional:s,status:200,statusText:"OK",headers:{}}}catch(s){throw {message:s.message||"Plugin API Request Failed",status:s.status||500,details:s}}}};function U(e,t){return new M(e,t)}var L={};z(L,{GraphQLCache:()=>I,GraphQLClient:()=>F,GraphQLError:()=>v,createGraphQLClient:()=>W});var I=class{cache=new Map;defaultTTL;constructor(e=3e5){this.defaultTTL=e;}generateKey(e,t){let r=t?JSON.stringify(t):"";return `${e}:${r}`}isValid(e){return Date.now()-e.timestamp<e.ttl}get(e,t){let r=this.generateKey(e,t),a=this.cache.get(r);return a?this.isValid(a)?a.data:(this.cache.delete(r),null):null}set(e,t,r,a){let n=this.generateKey(e,r);this.cache.set(n,{data:t,timestamp:Date.now(),ttl:a||this.defaultTTL});}clear(e,t){let r=this.generateKey(e,t);this.cache.delete(r);}clearAll(){this.cache.clear();}get size(){return this.cache.size}},v=class extends Error{constructor(e,t,r){super(e),this.errors=t,this.response=r,this.name="GraphQLError";}get firstError(){var e;return ((e=this.errors[0])==null?void 0:e.message)||this.message}get messages(){return this.errors.map(e=>e.message)}},F=class{httpClient;endpoint;defaultHeaders;cache;constructor(e,t){this.httpClient=e,this.endpoint=t.endpoint,this.defaultHeaders={"Content-Type":"application/json",...t.headers},t.cache&&(this.cache=new I(t.cacheTTL));}async query(e,t,r){if(this.cache){let i=this.cache.get(e,t);if(i!==null)return i}let a={query:e,variables:t,operationName:r},n=await this.httpClient.post(this.endpoint,a,{headers:this.defaultHeaders});if(n.errors&&n.errors.length>0)throw new v(n.errors[0].message,n.errors,n);if(!n.data)throw new v("No data returned from GraphQL query",[],n);return this.cache&&this.cache.set(e,n.data,t),n.data}async mutate(e,t,r){let a={query:e,variables:t,operationName:r},n=await this.httpClient.post(this.endpoint,a,{headers:this.defaultHeaders});if(n.errors&&n.errors.length>0)throw new v(n.errors[0].message,n.errors,n);if(!n.data)throw new v("No data returned from GraphQL mutation",[],n);return n.data}async request(e){return await this.httpClient.post(this.endpoint,e,{headers:this.defaultHeaders})}clearCache(e,t){this.cache&&(e?this.cache.clear(e,t):this.cache.clearAll());}getHttpClient(){return this.httpClient}};function W(e,t){return new F(e,t)}var{HttpClient:B}=X__namespace,{createGraphQLClient:xe}=L;function De(e){let t,r,a,n,i={};return {PluginProvider:({children:y})=>{if(!(Object.keys(i).length>0))return null;let l=e.providers.reduceRight((g,m)=>{let{provider:T}=m,C;return "clientKey"in m&&m.clientKey?C=i[m.clientKey]:"value"in m&&(typeof m.value=="function"?C=m.value(i):C=m.value),H__default.default.createElement(T,{value:C},g)},y);return H__default.default.createElement(H__default.default.Fragment,null,l)},initialize:y=>{t=y;let d=y.manifest||e.manifest;console.log(`[${d.id}] Context Injected`),r=q(d,y.capabilities.api),a=O(y,d.id,d),n=k(d,y.capabilities.api);let m={httpClient:new B({adapter:U(y,d.id)}),useGraphQL:C=>S(d.id,C,y.capabilities.api),manifest:d,hostContext:y},T=e.configureServices(m);Object.assign(i,T);},useHostContext:()=>{if(!t)throw new Error("HostContext not initialized");return t},useApi:()=>{if(!r)throw new Error("ApiClient not initialized");return r},usePluginHooks:()=>{if(!a)throw new Error("ApiHooks not initialized");return a},useGraphQL:()=>{if(!n)throw new Error("GraphQLClient not initialized");return n},getClient:y=>{let d=i[y];if(!d)throw new Error(`Client ${String(y)} not initialized`);return d}}}function Me(e){let{integrations:t,extensions:r=[],extraCapabilities:a=[],remote:n,...i}=e,c={},h={},s=new Set(["notification","storage","events",...a]);return t&&(s.add("api_proxy"),Object.entries(t).forEach(([f,o])=>{c[f]={baseUrl:o.baseUrl,graphqlUrl:o.graphql?.url},h[f]={strategy:"proxy",baseUrl:o.baseUrl,auth:"inherit",...o.graphql&&{graphql:{url:o.graphql.url,auth:"inherit",operations:o.graphql.operations,headers:{"Content-Type":"application/json"},cache:{ttl:3e5,enabled:true}}}};})),{manifest:{version:"1.0.0",author:"Unified Software",icon:"extension",...i,remote:e.type==="microfrontend"?{url:n?.url||"https://localhost:4173/assets/remoteEntry.js",scope:n?.scope||"portalWeb",module:n?.module||`./${e.id}`}:void 0,capabilities:Array.from(s),extensionPoints:r,apis:h},endpoints:c}}function Le(e){return Q({manifest:e.manifest,components:e.components,async initialize(t){if(console.log(`[${e.manifest.id}] Initializing (Framework)...`),e.onContextInit&&e.onContextInit(t),e.settings?.defaults)try{let r=`${e.manifest.id}-settings`;await t.capabilities.storage.get(r)||await t.capabilities.storage.set(r,e.settings.defaults);}catch(r){console.error(`[${e.manifest.id}] Failed to init settings:`,r);}e.events&&Object.entries(e.events).forEach(([r,a])=>{t.capabilities.events.on(r,n=>a(t,n));}),e.onInitialize&&await e.onInitialize(t);},async activate(t){console.log(`[${e.manifest.id}] Activating (Framework)...`),t.capabilities.events.emit("plugin:activated",{pluginId:e.manifest.id}),e.onActivate&&await e.onActivate(t);},async deactivate(){e.onDeactivate&&await e.onDeactivate();},async dispose(){e.onDispose&&await e.onDispose();}})}var ee=()=>jsxRuntime.jsx("div",{style:{display:"flex",justifyContent:"center",alignItems:"center",padding:"1rem",width:"100%",color:"#6c757d"},children:"Loading..."});function Ge(e,t=ee){let r=H__default.default.lazy(e);return a=>jsxRuntime.jsx(H.Suspense,{fallback:jsxRuntime.jsx(t,{}),children:jsxRuntime.jsx(r,{...a})})}
2
2
  exports.ExtensionPoint=K;exports.LoadingFallback=ee;exports.PluginHostAdapter=M;exports.PluginType=N;exports.createGraphQLClient=k;exports.createLazyComponent=Ge;exports.createManifest=Me;exports.createPlugin=Le;exports.createPluginApi=q;exports.createPluginApiHooks=O;exports.createPluginHostAdapter=U;exports.createRawGraphQLClient=S;exports.createRuntime=De;exports.defineApiConfig=j;exports.defineEndpoints=ae;exports.defineManifest=re;exports.definePlugin=Q;exports.useApiMutation=_;exports.useApiQuery=J;exports.useGraphQLMutation=ue;exports.useGraphQLQuery=de;exports.useGraphQLSubscription=fe;
package/dist/index.d.cts CHANGED
@@ -641,6 +641,11 @@ interface FrameworkManifestConfig {
641
641
  author?: string;
642
642
  icon?: string;
643
643
  type: 'internal' | 'microfrontend';
644
+ remote?: {
645
+ url?: string;
646
+ scope?: string;
647
+ module?: string;
648
+ };
644
649
  /**
645
650
  * Define custom integrations/APIs.
646
651
  * Key: API Name (e.g., 'payoffDemandApi', 'portalApi')
@@ -663,10 +668,10 @@ interface FrameworkManifestConfig {
663
668
  declare function createManifest(config: FrameworkManifestConfig): {
664
669
  manifest: {
665
670
  readonly remote: {
666
- readonly url: "https://localhost:4173/assets/remoteEntry.js";
667
- readonly scope: "portalWeb";
668
- readonly module: `./${string}`;
669
- };
671
+ url: string;
672
+ scope: string;
673
+ module: string;
674
+ } | undefined;
670
675
  readonly capabilities: string[];
671
676
  readonly extensionPoints: string[];
672
677
  readonly apis: any;
package/dist/index.d.ts CHANGED
@@ -641,6 +641,11 @@ interface FrameworkManifestConfig {
641
641
  author?: string;
642
642
  icon?: string;
643
643
  type: 'internal' | 'microfrontend';
644
+ remote?: {
645
+ url?: string;
646
+ scope?: string;
647
+ module?: string;
648
+ };
644
649
  /**
645
650
  * Define custom integrations/APIs.
646
651
  * Key: API Name (e.g., 'payoffDemandApi', 'portalApi')
@@ -663,10 +668,10 @@ interface FrameworkManifestConfig {
663
668
  declare function createManifest(config: FrameworkManifestConfig): {
664
669
  manifest: {
665
670
  readonly remote: {
666
- readonly url: "https://localhost:4173/assets/remoteEntry.js";
667
- readonly scope: "portalWeb";
668
- readonly module: `./${string}`;
669
- };
671
+ url: string;
672
+ scope: string;
673
+ module: string;
674
+ } | undefined;
670
675
  readonly capabilities: string[];
671
676
  readonly extensionPoints: string[];
672
677
  readonly apis: any;
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import H,{useState,useRef,useCallback,useEffect,Suspense}from'react';import*as X from'@unifiedsoftware/http-client';import {jsx}from'react/jsx-runtime';var $=Object.defineProperty;var z=(e,t)=>{for(var r in t)$(e,r,{get:t[r],enumerable:true});};var N=(a=>(a.INTERNAL="internal",a.MICROFRONTEND="microfrontend",a.IFRAME="iframe",a))(N||{}),K=(e=>e)(K||{});function Q(e){return ()=>({manifest:e.manifest,initialize:e.initialize,activate:e.activate,deactivate:e.deactivate,dispose:e.dispose,components:e.components})}function re(e){return e}function ae(e){return e}function j(e){return e}function q(e,t){let r=e.id,a=e.apis||{},n={};for(let[i,c]of Object.entries(a)){let h=c.endpoints||c.allowedEndpoints||{};n[i]={};for(let[s,o]of Object.entries(h))n[i][s]=async(p={})=>{let{params:d={},data:f,headers:y}=p,u=o.path,l={},g=/{([^}]+)}/g,m;for(;(m=g.exec(o.path))!==null;){let x=m[1];d[x]&&(l[x]=d[x],u=u.replace(`{${x}}`,d[x]));}let T={...d};for(let x of Object.keys(l))delete T[x];let C=o.method||o.methods&&o.methods[0]||"GET";return t.request({pluginId:r,apiName:i,endpoint:s,method:C,params:Object.keys(T).length>0?T:void 0,data:f,headers:y})};}return n}function J(e,t,r,a,n,i){let[c,h]=useState(null),[s,o]=useState(false),[p,d]=useState(null),f=i?.enabled!==false,y=i?.refetchInterval,u=useRef(null),l=useCallback(async()=>{if(f){o(true),d(null);try{let g=await e.capabilities.api.request({pluginId:t,apiName:r,endpoint:a,method:"GET",...n});h(g),o(!1),i?.onSuccess&&i.onSuccess(g);}catch(g){let m=g instanceof Error?g:new Error(String(g));d(m),o(false),i?.onError&&i.onError(m);}}},[t,r,a,f,JSON.stringify(n)]);return useEffect(()=>{f&&l();},[l,f]),useEffect(()=>{if(y&&f)return u.current=setInterval(l,y),()=>{u.current&&clearInterval(u.current);}},[y,l,f]),{data:c,loading:s,error:p,refetch:l,isSuccess:!s&&!p&&c!==null,isError:!s&&p!==null}}function _(e,t,r,a,n="POST",i){let[c,h]=useState(null),[s,o]=useState(false),[p,d]=useState(null),f=useCallback(async u=>{o(true),d(null);try{let l=await e.capabilities.api.request({pluginId:t,apiName:r,endpoint:a,method:n,...u});return h(l),o(!1),i?.onSuccess&&i.onSuccess(l,u),l}catch(l){let g=l instanceof Error?l:new Error(String(l));throw d(g),o(false),i?.onError&&i.onError(g,u),g}},[t,r,a,n,i]),y=useCallback(()=>{h(null),d(null),o(false);},[]);return {data:c,loading:s,error:p,mutate:f,reset:y,isSuccess:!s&&!p&&c!==null,isError:!s&&p!==null}}function O(e,t,r){let a=r.apis||{},n={};for(let[i,c]of Object.entries(a)){n[i]={};let h=c.endpoints||c.allowedEndpoints||{};for(let[s,o]of Object.entries(h)){let p=o.method||o.methods&&o.methods[0]||"GET",d=`use${s.charAt(0).toUpperCase()}${s.slice(1)}`;p==="GET"?n[i][d]=(f,y)=>J(e,t,i,s,f,y):n[i][d]=f=>_(e,t,i,s,p,f);}}return n}function k(e,t){let r=e.id,a=e.apis||{},n={};for(let[i,c]of Object.entries(a)){if(!c.graphql)continue;let h=c.graphql,s=h.operations||{};n[i]={query:async(o,p)=>{let d=s[o];if(!d)throw new Error(`GraphQL operation '${o}' not found in manifest`);if(d.type!=="query")throw new Error(`Operation '${o}' is not a query`);let f=await t.graphql({pluginId:r,apiName:i,query:d.query,variables:p||d.variables||{},operationName:o,headers:h.headers});if(f.errors&&f.errors.length>0)throw new Error(f.errors[0].message);return f.data},mutate:async(o,p)=>{let d=s[o];if(!d)throw new Error(`GraphQL operation '${o}' not found in manifest`);if(d.type!=="mutation")throw new Error(`Operation '${o}' is not a mutation`);let f=await t.graphql({pluginId:r,apiName:i,query:d.query,variables:p||d.variables||{},operationName:o,headers:h.headers});if(f.errors&&f.errors.length>0)throw new Error(f.errors[0].message);return f.data},subscribe:(o,p,d)=>(console.warn("GraphQL subscriptions not yet implemented"),()=>{})};}return n}function S(e,t,r){return {async query(a,n,i){let c=await r.graphql({pluginId:e,apiName:t,query:a,variables:n||{},operationName:i});if(c.errors&&c.errors.length>0)throw new Error(c.errors[0].message);return c.data},async mutate(a,n,i){let c=await r.graphql({pluginId:e,apiName:t,query:a,variables:n||{},operationName:i});if(c.errors&&c.errors.length>0)throw new Error(c.errors[0].message);return c.data}}}function de(e,t,r={}){let{enabled:a=true,refetchOnMount:n=true,refetchInterval:i,onSuccess:c,onError:h,cacheTime:s=300*1e3}=r,[o,p]=useState(void 0),[d,f]=useState(a),[y,u]=useState(void 0),l=useRef(true),g=useRef(null),m=useCallback(async()=>{if(a){if(g.current&&Date.now()-g.current.timestamp<s){p(g.current.data),f(false);return}f(true),u(void 0);try{let T=await e.graphql(t);if(!l.current)return;if(T.errors&&T.errors.length>0){let C=new Error(T.errors[0].message);u(C),h?.(C);}else T.data&&(p(T.data),g.current={data:T.data,timestamp:Date.now()},c?.(T.data));}catch(T){if(!l.current)return;let C=T instanceof Error?T:new Error(String(T));u(C),h?.(C);}finally{l.current&&f(false);}}},[e,t,a,s,c,h]);return useEffect(()=>{n&&m();},[m,n]),useEffect(()=>{if(!i||!a)return;let T=setInterval(()=>{m();},i);return ()=>clearInterval(T)},[i,a,m]),useEffect(()=>()=>{l.current=false;},[]),{data:o,loading:d,error:y,refetch:m}}function ue(e,t,r={}){let{onSuccess:a,onError:n}=r,[i,c]=useState(void 0),[h,s]=useState(false),[o,p]=useState(void 0),d=useRef(true);useEffect(()=>()=>{d.current=false;},[]);let f=useCallback(async u=>{s(true),p(void 0);try{let l=await e.graphql({...t,variables:u});if(!d.current)throw new Error("Component unmounted");if(l.errors&&l.errors.length>0){let g=new Error(l.errors[0].message);throw p(g),n?.(g,u),g}if(l.data)return c(l.data),a?.(l.data,u),l.data;throw new Error("No data returned from mutation")}catch(l){if(!d.current)throw l;let g=l instanceof Error?l:new Error(String(l));throw p(g),n?.(g,u),g}finally{d.current&&s(false);}},[e,t,a,n]),y=useCallback(()=>{c(void 0),p(void 0),s(false);},[]);return {mutate:f,data:i,loading:h,error:o,reset:y}}function fe(e,t,r={}){return console.warn("GraphQL subscriptions not yet implemented"),{data:void 0,loading:false,error:new Error("Subscriptions not yet implemented"),refetch:async()=>{}}}var M=class{constructor(t,r){this.context=t;this.pluginId=r;}async request(t){let{url:r,method:a,headers:n,body:i,params:c}=t,h=r.split("/").pop()?.split("?")[0]??"";try{let s=await this.context.capabilities.api.request({pluginId:this.pluginId,endpoint:h,method:a,data:i,params:c,headers:n});return {data:s&&typeof s=="object"&&"ObjOptional"in s?s.ObjOptional:s,status:200,statusText:"OK",headers:{}}}catch(s){throw {message:s.message||"Plugin API Request Failed",status:s.status||500,details:s}}}};function U(e,t){return new M(e,t)}var L={};z(L,{GraphQLCache:()=>I,GraphQLClient:()=>F,GraphQLError:()=>v,createGraphQLClient:()=>W});var I=class{cache=new Map;defaultTTL;constructor(e=3e5){this.defaultTTL=e;}generateKey(e,t){let r=t?JSON.stringify(t):"";return `${e}:${r}`}isValid(e){return Date.now()-e.timestamp<e.ttl}get(e,t){let r=this.generateKey(e,t),a=this.cache.get(r);return a?this.isValid(a)?a.data:(this.cache.delete(r),null):null}set(e,t,r,a){let n=this.generateKey(e,r);this.cache.set(n,{data:t,timestamp:Date.now(),ttl:a||this.defaultTTL});}clear(e,t){let r=this.generateKey(e,t);this.cache.delete(r);}clearAll(){this.cache.clear();}get size(){return this.cache.size}},v=class extends Error{constructor(e,t,r){super(e),this.errors=t,this.response=r,this.name="GraphQLError";}get firstError(){var e;return ((e=this.errors[0])==null?void 0:e.message)||this.message}get messages(){return this.errors.map(e=>e.message)}},F=class{httpClient;endpoint;defaultHeaders;cache;constructor(e,t){this.httpClient=e,this.endpoint=t.endpoint,this.defaultHeaders={"Content-Type":"application/json",...t.headers},t.cache&&(this.cache=new I(t.cacheTTL));}async query(e,t,r){if(this.cache){let i=this.cache.get(e,t);if(i!==null)return i}let a={query:e,variables:t,operationName:r},n=await this.httpClient.post(this.endpoint,a,{headers:this.defaultHeaders});if(n.errors&&n.errors.length>0)throw new v(n.errors[0].message,n.errors,n);if(!n.data)throw new v("No data returned from GraphQL query",[],n);return this.cache&&this.cache.set(e,n.data,t),n.data}async mutate(e,t,r){let a={query:e,variables:t,operationName:r},n=await this.httpClient.post(this.endpoint,a,{headers:this.defaultHeaders});if(n.errors&&n.errors.length>0)throw new v(n.errors[0].message,n.errors,n);if(!n.data)throw new v("No data returned from GraphQL mutation",[],n);return n.data}async request(e){return await this.httpClient.post(this.endpoint,e,{headers:this.defaultHeaders})}clearCache(e,t){this.cache&&(e?this.cache.clear(e,t):this.cache.clearAll());}getHttpClient(){return this.httpClient}};function W(e,t){return new F(e,t)}var{HttpClient:B}=X,{createGraphQLClient:xe}=L;function De(e){let t,r,a,n,i={};return {PluginProvider:({children:y})=>{if(!(Object.keys(i).length>0))return null;let l=e.providers.reduceRight((g,m)=>{let{provider:T}=m,C;return "clientKey"in m&&m.clientKey?C=i[m.clientKey]:"value"in m&&(typeof m.value=="function"?C=m.value(i):C=m.value),H.createElement(T,{value:C},g)},y);return H.createElement(H.Fragment,null,l)},initialize:y=>{t=y;let u=y.manifest||e.manifest;console.log(`[${u.id}] Context Injected`),r=q(u,y.capabilities.api),a=O(y,u.id,u),n=k(u,y.capabilities.api);let m={httpClient:new B({adapter:U(y,u.id)}),useGraphQL:C=>S(u.id,C,y.capabilities.api),manifest:u,hostContext:y},T=e.configureServices(m);Object.assign(i,T);},useHostContext:()=>{if(!t)throw new Error("HostContext not initialized");return t},useApi:()=>{if(!r)throw new Error("ApiClient not initialized");return r},usePluginHooks:()=>{if(!a)throw new Error("ApiHooks not initialized");return a},useGraphQL:()=>{if(!n)throw new Error("GraphQLClient not initialized");return n},getClient:y=>{let u=i[y];if(!u)throw new Error(`Client ${String(y)} not initialized`);return u}}}function Me(e){let{integrations:t,extensions:r=[],extraCapabilities:a=[],...n}=e,i={},c={},h=new Set(["notification","storage","events",...a]);return t&&(h.add("api_proxy"),Object.entries(t).forEach(([o,p])=>{i[o]={baseUrl:p.baseUrl,graphqlUrl:p.graphql?.url},c[o]={strategy:"proxy",baseUrl:p.baseUrl,auth:"inherit",...p.graphql&&{graphql:{url:p.graphql.url,auth:"inherit",operations:p.graphql.operations,headers:{"Content-Type":"application/json"},cache:{ttl:3e5,enabled:true}}}};})),{manifest:{version:"1.0.0",author:"Unified Software",icon:"extension",...n,remote:{url:"https://localhost:4173/assets/remoteEntry.js",scope:"portalWeb",module:`./${e.id}`},capabilities:Array.from(h),extensionPoints:r,apis:c},endpoints:i}}function Le(e){return Q({manifest:e.manifest,components:e.components,async initialize(t){if(console.log(`[${e.manifest.id}] Initializing (Framework)...`),e.onContextInit&&e.onContextInit(t),e.settings?.defaults)try{let r=`${e.manifest.id}-settings`;await t.capabilities.storage.get(r)||await t.capabilities.storage.set(r,e.settings.defaults);}catch(r){console.error(`[${e.manifest.id}] Failed to init settings:`,r);}e.events&&Object.entries(e.events).forEach(([r,a])=>{t.capabilities.events.on(r,n=>a(t,n));}),e.onInitialize&&await e.onInitialize(t);},async activate(t){console.log(`[${e.manifest.id}] Activating (Framework)...`),t.capabilities.events.emit("plugin:activated",{pluginId:e.manifest.id}),e.onActivate&&await e.onActivate(t);},async deactivate(){e.onDeactivate&&await e.onDeactivate();},async dispose(){e.onDispose&&await e.onDispose();}})}var ee=()=>jsx("div",{style:{display:"flex",justifyContent:"center",alignItems:"center",padding:"1rem",width:"100%",color:"#6c757d"},children:"Loading..."});function Ge(e,t=ee){let r=H.lazy(e);return a=>jsx(Suspense,{fallback:jsx(t,{}),children:jsx(r,{...a})})}
1
+ import H,{useState,useRef,useCallback,useEffect,Suspense}from'react';import*as X from'@unifiedsoftware/http-client';import {jsx}from'react/jsx-runtime';var $=Object.defineProperty;var z=(e,t)=>{for(var r in t)$(e,r,{get:t[r],enumerable:true});};var N=(a=>(a.INTERNAL="internal",a.MICROFRONTEND="microfrontend",a.IFRAME="iframe",a))(N||{}),K=(e=>e)(K||{});function Q(e){return ()=>({manifest:e.manifest,initialize:e.initialize,activate:e.activate,deactivate:e.deactivate,dispose:e.dispose,components:e.components})}function re(e){return e}function ae(e){return e}function j(e){return e}function q(e,t){let r=e.id,a=e.apis||{},n={};for(let[i,c]of Object.entries(a)){let h=c.endpoints||c.allowedEndpoints||{};n[i]={};for(let[s,p]of Object.entries(h))n[i][s]=async(f={})=>{let{params:o={},data:u,headers:y}=f,d=p.path,l={},g=/{([^}]+)}/g,m;for(;(m=g.exec(p.path))!==null;){let x=m[1];o[x]&&(l[x]=o[x],d=d.replace(`{${x}}`,o[x]));}let T={...o};for(let x of Object.keys(l))delete T[x];let C=p.method||p.methods&&p.methods[0]||"GET";return t.request({pluginId:r,apiName:i,endpoint:s,method:C,params:Object.keys(T).length>0?T:void 0,data:u,headers:y})};}return n}function J(e,t,r,a,n,i){let[c,h]=useState(null),[s,p]=useState(false),[f,o]=useState(null),u=i?.enabled!==false,y=i?.refetchInterval,d=useRef(null),l=useCallback(async()=>{if(u){p(true),o(null);try{let g=await e.capabilities.api.request({pluginId:t,apiName:r,endpoint:a,method:"GET",...n});h(g),p(!1),i?.onSuccess&&i.onSuccess(g);}catch(g){let m=g instanceof Error?g:new Error(String(g));o(m),p(false),i?.onError&&i.onError(m);}}},[t,r,a,u,JSON.stringify(n)]);return useEffect(()=>{u&&l();},[l,u]),useEffect(()=>{if(y&&u)return d.current=setInterval(l,y),()=>{d.current&&clearInterval(d.current);}},[y,l,u]),{data:c,loading:s,error:f,refetch:l,isSuccess:!s&&!f&&c!==null,isError:!s&&f!==null}}function _(e,t,r,a,n="POST",i){let[c,h]=useState(null),[s,p]=useState(false),[f,o]=useState(null),u=useCallback(async d=>{p(true),o(null);try{let l=await e.capabilities.api.request({pluginId:t,apiName:r,endpoint:a,method:n,...d});return h(l),p(!1),i?.onSuccess&&i.onSuccess(l,d),l}catch(l){let g=l instanceof Error?l:new Error(String(l));throw o(g),p(false),i?.onError&&i.onError(g,d),g}},[t,r,a,n,i]),y=useCallback(()=>{h(null),o(null),p(false);},[]);return {data:c,loading:s,error:f,mutate:u,reset:y,isSuccess:!s&&!f&&c!==null,isError:!s&&f!==null}}function O(e,t,r){let a=r.apis||{},n={};for(let[i,c]of Object.entries(a)){n[i]={};let h=c.endpoints||c.allowedEndpoints||{};for(let[s,p]of Object.entries(h)){let f=p.method||p.methods&&p.methods[0]||"GET",o=`use${s.charAt(0).toUpperCase()}${s.slice(1)}`;f==="GET"?n[i][o]=(u,y)=>J(e,t,i,s,u,y):n[i][o]=u=>_(e,t,i,s,f,u);}}return n}function k(e,t){let r=e.id,a=e.apis||{},n={};for(let[i,c]of Object.entries(a)){if(!c.graphql)continue;let h=c.graphql,s=h.operations||{};n[i]={query:async(p,f)=>{let o=s[p];if(!o)throw new Error(`GraphQL operation '${p}' not found in manifest`);if(o.type!=="query")throw new Error(`Operation '${p}' is not a query`);let u=await t.graphql({pluginId:r,apiName:i,query:o.query,variables:f||o.variables||{},operationName:p,headers:h.headers});if(u.errors&&u.errors.length>0)throw new Error(u.errors[0].message);return u.data},mutate:async(p,f)=>{let o=s[p];if(!o)throw new Error(`GraphQL operation '${p}' not found in manifest`);if(o.type!=="mutation")throw new Error(`Operation '${p}' is not a mutation`);let u=await t.graphql({pluginId:r,apiName:i,query:o.query,variables:f||o.variables||{},operationName:p,headers:h.headers});if(u.errors&&u.errors.length>0)throw new Error(u.errors[0].message);return u.data},subscribe:(p,f,o)=>(console.warn("GraphQL subscriptions not yet implemented"),()=>{})};}return n}function S(e,t,r){return {async query(a,n,i){let c=await r.graphql({pluginId:e,apiName:t,query:a,variables:n||{},operationName:i});if(c.errors&&c.errors.length>0)throw new Error(c.errors[0].message);return c.data},async mutate(a,n,i){let c=await r.graphql({pluginId:e,apiName:t,query:a,variables:n||{},operationName:i});if(c.errors&&c.errors.length>0)throw new Error(c.errors[0].message);return c.data}}}function de(e,t,r={}){let{enabled:a=true,refetchOnMount:n=true,refetchInterval:i,onSuccess:c,onError:h,cacheTime:s=300*1e3}=r,[p,f]=useState(void 0),[o,u]=useState(a),[y,d]=useState(void 0),l=useRef(true),g=useRef(null),m=useCallback(async()=>{if(a){if(g.current&&Date.now()-g.current.timestamp<s){f(g.current.data),u(false);return}u(true),d(void 0);try{let T=await e.graphql(t);if(!l.current)return;if(T.errors&&T.errors.length>0){let C=new Error(T.errors[0].message);d(C),h?.(C);}else T.data&&(f(T.data),g.current={data:T.data,timestamp:Date.now()},c?.(T.data));}catch(T){if(!l.current)return;let C=T instanceof Error?T:new Error(String(T));d(C),h?.(C);}finally{l.current&&u(false);}}},[e,t,a,s,c,h]);return useEffect(()=>{n&&m();},[m,n]),useEffect(()=>{if(!i||!a)return;let T=setInterval(()=>{m();},i);return ()=>clearInterval(T)},[i,a,m]),useEffect(()=>()=>{l.current=false;},[]),{data:p,loading:o,error:y,refetch:m}}function ue(e,t,r={}){let{onSuccess:a,onError:n}=r,[i,c]=useState(void 0),[h,s]=useState(false),[p,f]=useState(void 0),o=useRef(true);useEffect(()=>()=>{o.current=false;},[]);let u=useCallback(async d=>{s(true),f(void 0);try{let l=await e.graphql({...t,variables:d});if(!o.current)throw new Error("Component unmounted");if(l.errors&&l.errors.length>0){let g=new Error(l.errors[0].message);throw f(g),n?.(g,d),g}if(l.data)return c(l.data),a?.(l.data,d),l.data;throw new Error("No data returned from mutation")}catch(l){if(!o.current)throw l;let g=l instanceof Error?l:new Error(String(l));throw f(g),n?.(g,d),g}finally{o.current&&s(false);}},[e,t,a,n]),y=useCallback(()=>{c(void 0),f(void 0),s(false);},[]);return {mutate:u,data:i,loading:h,error:p,reset:y}}function fe(e,t,r={}){return console.warn("GraphQL subscriptions not yet implemented"),{data:void 0,loading:false,error:new Error("Subscriptions not yet implemented"),refetch:async()=>{}}}var M=class{constructor(t,r){this.context=t;this.pluginId=r;}async request(t){let{url:r,method:a,headers:n,body:i,params:c}=t,h=r.split("/").pop()?.split("?")[0]??"";try{let s=await this.context.capabilities.api.request({pluginId:this.pluginId,endpoint:h,method:a,data:i,params:c,headers:n});return {data:s&&typeof s=="object"&&"ObjOptional"in s?s.ObjOptional:s,status:200,statusText:"OK",headers:{}}}catch(s){throw {message:s.message||"Plugin API Request Failed",status:s.status||500,details:s}}}};function U(e,t){return new M(e,t)}var L={};z(L,{GraphQLCache:()=>I,GraphQLClient:()=>F,GraphQLError:()=>v,createGraphQLClient:()=>W});var I=class{cache=new Map;defaultTTL;constructor(e=3e5){this.defaultTTL=e;}generateKey(e,t){let r=t?JSON.stringify(t):"";return `${e}:${r}`}isValid(e){return Date.now()-e.timestamp<e.ttl}get(e,t){let r=this.generateKey(e,t),a=this.cache.get(r);return a?this.isValid(a)?a.data:(this.cache.delete(r),null):null}set(e,t,r,a){let n=this.generateKey(e,r);this.cache.set(n,{data:t,timestamp:Date.now(),ttl:a||this.defaultTTL});}clear(e,t){let r=this.generateKey(e,t);this.cache.delete(r);}clearAll(){this.cache.clear();}get size(){return this.cache.size}},v=class extends Error{constructor(e,t,r){super(e),this.errors=t,this.response=r,this.name="GraphQLError";}get firstError(){var e;return ((e=this.errors[0])==null?void 0:e.message)||this.message}get messages(){return this.errors.map(e=>e.message)}},F=class{httpClient;endpoint;defaultHeaders;cache;constructor(e,t){this.httpClient=e,this.endpoint=t.endpoint,this.defaultHeaders={"Content-Type":"application/json",...t.headers},t.cache&&(this.cache=new I(t.cacheTTL));}async query(e,t,r){if(this.cache){let i=this.cache.get(e,t);if(i!==null)return i}let a={query:e,variables:t,operationName:r},n=await this.httpClient.post(this.endpoint,a,{headers:this.defaultHeaders});if(n.errors&&n.errors.length>0)throw new v(n.errors[0].message,n.errors,n);if(!n.data)throw new v("No data returned from GraphQL query",[],n);return this.cache&&this.cache.set(e,n.data,t),n.data}async mutate(e,t,r){let a={query:e,variables:t,operationName:r},n=await this.httpClient.post(this.endpoint,a,{headers:this.defaultHeaders});if(n.errors&&n.errors.length>0)throw new v(n.errors[0].message,n.errors,n);if(!n.data)throw new v("No data returned from GraphQL mutation",[],n);return n.data}async request(e){return await this.httpClient.post(this.endpoint,e,{headers:this.defaultHeaders})}clearCache(e,t){this.cache&&(e?this.cache.clear(e,t):this.cache.clearAll());}getHttpClient(){return this.httpClient}};function W(e,t){return new F(e,t)}var{HttpClient:B}=X,{createGraphQLClient:xe}=L;function De(e){let t,r,a,n,i={};return {PluginProvider:({children:y})=>{if(!(Object.keys(i).length>0))return null;let l=e.providers.reduceRight((g,m)=>{let{provider:T}=m,C;return "clientKey"in m&&m.clientKey?C=i[m.clientKey]:"value"in m&&(typeof m.value=="function"?C=m.value(i):C=m.value),H.createElement(T,{value:C},g)},y);return H.createElement(H.Fragment,null,l)},initialize:y=>{t=y;let d=y.manifest||e.manifest;console.log(`[${d.id}] Context Injected`),r=q(d,y.capabilities.api),a=O(y,d.id,d),n=k(d,y.capabilities.api);let m={httpClient:new B({adapter:U(y,d.id)}),useGraphQL:C=>S(d.id,C,y.capabilities.api),manifest:d,hostContext:y},T=e.configureServices(m);Object.assign(i,T);},useHostContext:()=>{if(!t)throw new Error("HostContext not initialized");return t},useApi:()=>{if(!r)throw new Error("ApiClient not initialized");return r},usePluginHooks:()=>{if(!a)throw new Error("ApiHooks not initialized");return a},useGraphQL:()=>{if(!n)throw new Error("GraphQLClient not initialized");return n},getClient:y=>{let d=i[y];if(!d)throw new Error(`Client ${String(y)} not initialized`);return d}}}function Me(e){let{integrations:t,extensions:r=[],extraCapabilities:a=[],remote:n,...i}=e,c={},h={},s=new Set(["notification","storage","events",...a]);return t&&(s.add("api_proxy"),Object.entries(t).forEach(([f,o])=>{c[f]={baseUrl:o.baseUrl,graphqlUrl:o.graphql?.url},h[f]={strategy:"proxy",baseUrl:o.baseUrl,auth:"inherit",...o.graphql&&{graphql:{url:o.graphql.url,auth:"inherit",operations:o.graphql.operations,headers:{"Content-Type":"application/json"},cache:{ttl:3e5,enabled:true}}}};})),{manifest:{version:"1.0.0",author:"Unified Software",icon:"extension",...i,remote:e.type==="microfrontend"?{url:n?.url||"https://localhost:4173/assets/remoteEntry.js",scope:n?.scope||"portalWeb",module:n?.module||`./${e.id}`}:void 0,capabilities:Array.from(s),extensionPoints:r,apis:h},endpoints:c}}function Le(e){return Q({manifest:e.manifest,components:e.components,async initialize(t){if(console.log(`[${e.manifest.id}] Initializing (Framework)...`),e.onContextInit&&e.onContextInit(t),e.settings?.defaults)try{let r=`${e.manifest.id}-settings`;await t.capabilities.storage.get(r)||await t.capabilities.storage.set(r,e.settings.defaults);}catch(r){console.error(`[${e.manifest.id}] Failed to init settings:`,r);}e.events&&Object.entries(e.events).forEach(([r,a])=>{t.capabilities.events.on(r,n=>a(t,n));}),e.onInitialize&&await e.onInitialize(t);},async activate(t){console.log(`[${e.manifest.id}] Activating (Framework)...`),t.capabilities.events.emit("plugin:activated",{pluginId:e.manifest.id}),e.onActivate&&await e.onActivate(t);},async deactivate(){e.onDeactivate&&await e.onDeactivate();},async dispose(){e.onDispose&&await e.onDispose();}})}var ee=()=>jsx("div",{style:{display:"flex",justifyContent:"center",alignItems:"center",padding:"1rem",width:"100%",color:"#6c757d"},children:"Loading..."});function Ge(e,t=ee){let r=H.lazy(e);return a=>jsx(Suspense,{fallback:jsx(t,{}),children:jsx(r,{...a})})}
2
2
  export{K as ExtensionPoint,ee as LoadingFallback,M as PluginHostAdapter,N as PluginType,k as createGraphQLClient,Ge as createLazyComponent,Me as createManifest,Le as createPlugin,q as createPluginApi,O as createPluginApiHooks,U as createPluginHostAdapter,S as createRawGraphQLClient,De as createRuntime,j as defineApiConfig,ae as defineEndpoints,re as defineManifest,Q as definePlugin,_ as useApiMutation,J as useApiQuery,ue as useGraphQLMutation,de as useGraphQLQuery,fe as useGraphQLSubscription};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unifiedsoftware/react-plugin-remote",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",