@uniformdev/mesh-sdk
Advanced tools
Comparing version
@@ -0,7 +1,80 @@ | ||
interface ContextData { | ||
locationKey: string; | ||
locationValue: any; | ||
locationMetadata: any; | ||
uniformApiVersion: string; | ||
dialogContext?: DialogContext; | ||
} | ||
interface DialogContext { | ||
dialogId: string; | ||
contentHeight?: CSSHeight; | ||
} | ||
interface DialogResponseHandlers { | ||
[dialogId: string]: DialogResponseHandler; | ||
} | ||
interface DialogResponseHandler { | ||
resolve: (value: Pick<DialogResponseData, 'value' | 'dialogId'>) => void; | ||
reject: (reason?: unknown) => void; | ||
} | ||
interface DialogResponseData { | ||
value?: unknown; | ||
dialogId: string; | ||
error?: string; | ||
} | ||
declare type DialogType = 'location' | 'confirm'; | ||
interface DialogOptions { | ||
/** | ||
* By default, dialogs will be closed when they set a value. | ||
* You can disable this behavior by setting the `disableCloseDialogOnSetValue` | ||
* property to true. | ||
* You'll still be able to close a dialog via the `closeDialog` method | ||
* that is returned from the `openLocationDialog` method. | ||
*/ | ||
disableCloseDialogOnSetValue?: boolean; | ||
/** | ||
* Options for setting the max width of the dialog. | ||
*/ | ||
maxWidth?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | '5xl' | '6xl' | 'full'; | ||
/** | ||
* Parameters to pass to the dialog, which will be available in the dialog via | ||
* the `metadata` object for the dialog location, e.g. `metadata.dialogParams`. | ||
* Parameters should be simple (serializable) types, not functions or DOM | ||
* elements or similar non-serializable objects. | ||
*/ | ||
params?: DialogParams; | ||
/** | ||
* Allows you to specify the height of the dialog content upon open. Note: the | ||
* dialog itself will always be full height to match the Uniform dashboard UI. | ||
* However, the content container within the dialog is, by default, auto-sized to | ||
* the calculated height of the rendered content of the dialog. | ||
* Setting the `contentHeight` option disables auto-sizing and allows you to specify | ||
* your own height for the dialog content. | ||
* `contentHeight` option can be specific lengths, e.g. 100vh, 500px, etc... as | ||
* well as other standard CSS height options. | ||
*/ | ||
contentHeight?: CSSHeight; | ||
} | ||
declare type DialogParamValue = string | number | boolean | null | DialogParamValue[] | { | ||
[key: string]: DialogParamValue; | ||
}; | ||
declare type DialogParams = { | ||
[paramName: string]: DialogParamValue; | ||
}; | ||
declare type CSSHeight = '-moz-initial' | 'inherit' | 'initial' | 'revert' | 'unset' | '-moz-max-content' | '-moz-min-content' | '-webkit-fit-content' | 'auto' | 'fit-content' | 'max-content' | 'min-content' | `${number}${'px' | 'em' | 'rem' | 'vh'}`; | ||
interface SdkWindow { | ||
/** The current window object. */ | ||
instance: Window; | ||
height?: number; | ||
/** The numerical height of the current window. */ | ||
height: number; | ||
/** The MutationObserver object used to detect content height changes. | ||
* Note: this property is only defined if `enableAutoResizing` is true. */ | ||
observer?: MutationObserver; | ||
/** Enables auto-resizing of the window (iframe) based on the size of the content. */ | ||
enableAutoResizing: () => void; | ||
/** Disables auto-resizing of the window (iframe) based on the size of the content. */ | ||
disableAutoResizing: () => void; | ||
/** `height` argument can be specific lengths, e.g. 100vh, 300px, etc... | ||
* as well as other standard CSS height options */ | ||
updateHeight: (height: CSSHeight) => void; | ||
} | ||
@@ -13,3 +86,34 @@ | ||
version: string; | ||
openLocationDialog: <TDialogValue>({ locationKey, options, }: { | ||
locationKey: string; | ||
options?: DialogOptions; | ||
}) => Promise<LocationDialogResponse<TDialogValue> | undefined>; | ||
closeLocationDialog: ({ dialogId, }: { | ||
/** Id of the dialog to close. If no dialog with the given id exists, nothing will happen. */ | ||
dialogId: string; | ||
}) => Promise<void>; | ||
openConfirmationDialog({ titleText, bodyText, options, }: { | ||
titleText: string; | ||
bodyText: string; | ||
options?: DialogOptions; | ||
}): Promise<{ | ||
value: 'confirm' | 'cancel'; | ||
closeDialog: () => Promise<void>; | ||
} | undefined>; | ||
closeConfirmationDialog(): Promise<void>; | ||
openCurrentLocationDialog<TDialogValue>({ options, }: { | ||
options?: DialogOptions; | ||
}): Promise<LocationDialogResponse<TDialogValue> | undefined>; | ||
closeCurrentLocationDialog(): Promise<void>; | ||
dialogContext?: DialogContext; | ||
} | ||
interface LocationDialogResponse<TDialogValue> { | ||
/** Generated id for the dialog. */ | ||
dialogId: string; | ||
/** The value set by the dialog. */ | ||
value: TDialogValue; | ||
/** Allows for closing the dialog manually. | ||
* Typically used when `closeDialogOnSetValue` is false. */ | ||
closeDialog: () => Promise<void>; | ||
} | ||
declare global { | ||
@@ -21,5 +125,5 @@ interface Window { | ||
/** Defines methods used for interacting with a Mesh location */ | ||
interface MeshLocation<TValue = unknown, TMetadata = unknown> { | ||
interface MeshLocation<TValue = unknown, TMetadata = unknown, TSetValue = TValue> { | ||
getValue: () => TValue; | ||
setValue: (value: TValue) => Promise<void>; | ||
setValue: (value: TSetValue) => Promise<void>; | ||
getMetadata: () => TMetadata; | ||
@@ -35,2 +139,2 @@ } | ||
export { MeshLocation, UniformMeshSDK, initializeUniformMeshSDK }; | ||
export { CSSHeight, ContextData, DialogContext, DialogOptions, DialogParamValue, DialogParams, DialogResponseData, DialogResponseHandler, DialogResponseHandlers, DialogType, LocationDialogResponse, MeshLocation, SdkWindow, UniformMeshSDK, initializeUniformMeshSDK }; |
@@ -1,1 +0,1 @@ | ||
var u,c=({onHeightChange:t,autoResizingDisabled:e})=>{if(typeof window=="undefined")return;let a=window,i=()=>{let o=Math.ceil(a.document.documentElement.getBoundingClientRect().height);o!==u&&(u=o,t==null||t(o))},n=e?void 0:new MutationObserver(i),r={instance:a,observer:n,height:0,enableAutoResizing:()=>{n==null||n.observe(document.body,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),a.addEventListener("resize",i)},disableAutoResizing:()=>{n==null||n.disconnect(),a.removeEventListener("resize",i)}};return e||r.enableAutoResizing(),r};var w=()=>import("post-robot");async function p({autoResizingDisabled:t}={}){if(typeof window!="undefined"&&typeof window.UniformMeshSDK=="undefined"){if(typeof window.parent=="undefined"||window.parent==window)throw new Error("It appears you are trying to connect with the Uniform Mesh SDK outside of an iframe. Be sure you are loading your app via a location URL configured in the Uniform dashboard.");let e=await l(),a=await e.initialize(),i={getCurrentLocation:()=>({getValue:()=>a.locationValue,setValue:async n=>{await e.setValue(n),a.locationValue=n},getMetadata:()=>a.locationMetadata}),currentWindow:c({onHeightChange:n=>{e.resize(n)},autoResizingDisabled:t}),version:a.uniformApiVersion};return window.UniformMeshSDK=i,i}}async function l(){let t=await w();return{initialize:async()=>(await t.send(window.parent,"initialize")).data,resize:async e=>{t.send(window.parent,"resize",{height:e})},getValue:async()=>{let e=await t.send(window.parent,"getValue");return e==null?void 0:e.data},setValue:async e=>{await t.send(window.parent,"setValue",e)},getMetadata:async()=>{let e=await t.send(window.parent,"getMetadata");return e==null?void 0:e.data}}}export{p as initializeUniformMeshSDK}; | ||
var R=(n,e)=>e?{log(o,...t){return console.log(`${n}: ${o}`,...t)},error(o,...t){return console.error(`${n}: ${o}`,...t)}}:{log(){},error(){}};var T=5e3;var c=class extends Error{constructor(){super("Request timed out");Object.setPrototypeOf(this,c.prototype),this.name="RequestTimeoutError"}},u=class extends Error{constructor(){super("Client destroyed");Object.setPrototypeOf(this,u.prototype),this.name="ClientDestroyedError"}};var b=()=>{let n=()=>{},e=()=>{},o=new Promise((t,i)=>{n=t,e=i});return{resolve:n,reject:e,promise:o}},m=(n=16)=>[...Array(n)].map(()=>(~~(Math.random()*36)).toString(36)).join(""),h=(n,e)=>{let{[e]:o,...t}=n;return t},k=n=>({message:n.message,name:n.name,stack:n.stack}),L=({name:n,message:e,stack:o})=>{switch(n){case c.name:return new c;default:{let t=new Error(e);return t.name=n,t.stack=o,t}}},w=n=>{let e=n.data,o="none";return e instanceof Error&&(o="error",e=k(e)),{...n,serialization:o,data:e}},v=n=>n.serialization==="error"?{...n,data:L(n.data)}:n;var D=class{constructor({debug:e=!1,requestTimeout:o=T}={}){this.debug=e,this.requestTimeout=o,this.channel=b(),this.eventSubscriptions={},this.responseSubscriptions={},this.requestSubscriptions={},this.onDestroyRequestHandlers={},this.destroyed=!1,this.logger=this.getLogger(),this.getChannel().then(()=>{this.logger.log("Secure parent <-> child channel established")}).catch(t=>{this.logger.log(t)})}async send(e,o){return this.postMessage("event",e,o)}on(e,o){this.eventSubscriptions[e]||(this.eventSubscriptions[e]={});let t=m(8);return this.eventSubscriptions[e][t]=o,this.logger.log(`Registered handler for event "${e}"`),()=>{this.eventSubscriptions[e]=h(this.eventSubscriptions[e],t),this.logger.log(`Unsubscribed handler for event ${e}`)}}async request(e,o,t={}){let r=(await this.postMessage("request",e,o)).id,s=()=>{this.responseSubscriptions=h(this.responseSubscriptions,r)},a=()=>{this.onDestroyRequestHandlers=h(this.responseSubscriptions,r)};return new Promise((l,p)=>{let y,x=(S,O)=>{clearTimeout(y),s(),a(),O.type==="error_response"?p(S):l(S)},I=()=>{clearTimeout(y),p(new u)};this.responseSubscriptions[r]=x,this.onDestroyRequestHandlers[r]=I,y=setTimeout(()=>{s(),a(),p(new c)},t.timeout||this.requestTimeout)})}onRequest(e,o){let t=async(i,r)=>{try{let s=await o(i,r);this.postMessage("response",e,s,r.id)}catch(s){this.postMessage("error_response",e,s,r.id)}};return this.requestSubscriptions[e]=t,()=>{this.requestSubscriptions=h(this.requestSubscriptions,e)}}async getChannel(){if(await this.channel.promise,this.destroyed)throw new u;return this.channel.promise}async messageListener(e){try{await this.getChannel();let o=this.isValidMessage(e),t=v(e.data);if(o)switch(t.type){case"event":{this.handleEvent(t);break}case"request":{this.handleRequest(t);break}case"error_response":case"response":{this.handleResponse(t);break}}else this.logger.error("Invalid message format. Skipping.")}catch(o){this.logger.error(o)}}handleEvent(e){let o=this.eventSubscriptions[e.key];o&&Object.values(o).forEach(t=>t(e.data,e))}handleRequest(e){let o=this.requestSubscriptions[e.key];o&&(o(e.data,e),this.logger.log(`Handled request type ${e.key}`))}handleResponse(e){let o=e.requestId,t=o&&this.responseSubscriptions[o];t&&t(e.data,e)}async postMessage(e,o,t,i){let{port:r}=await this.getChannel();if(this.destroyed)throw new u;let s=w({type:e,apiVersion:"framepost/v1",key:o,data:t,id:m(),requestId:i});return this.logger.log("posting message from child to parent",s),r.postMessage(s),s}initListener(e){this.isInitMessage(e)?(this.onChannelInit(e),this.messagePort&&(this.messagePort.onmessage=this.messageListener.bind(this)),this.resolveChannel(e)):this.logger.error("Invalid message format. Skipping.")}isValidMessage(e){let o=e.data;return o.type&&o.id&&o.apiVersion==="framepost/v1"}isInitMessage(e){return this.isValidMessage(e)&&e.data.type==="channel_init"}resolveChannel(e){if(this.messagePort){let o={port:this.messagePort,origin:e.origin,context:e.data.data};this.channel.resolve(o)}}getInitMessage(e){return w({type:"channel_init",apiVersion:"framepost/v1",key:"",data:e,id:m()})}destroy(){this.destroyed=!0,this.channel.reject(new u),this.messagePort&&this.messagePort.close(),Object.values(this.onDestroyRequestHandlers).forEach(e=>e())}};var E=class extends D{constructor(e={}){super(e);this.context=e.context||null,this.initListener=this.initListener.bind(this),window.addEventListener("message",this.initListener)}getLogger(){return R("child-client",this.debug)}onChannelInit(e){window.removeEventListener("message",this.initListener),this.messagePort=e.ports[0];let o=this.getInitMessage(this.context);this.logger.log("channel init, posting init message from child to parent",o),this.messagePort.postMessage(o)}destroy(){super.destroy(),window.removeEventListener("message",this.initListener)}};async function M({dialogResponseHandlers:n}){let e=new E({debug:!1});window.parent.postMessage("parents just don't understand","*");let o=await e.request("initialize");return e.onRequest("dialog-value",async t=>{let i=n[t.dialogId];!i||(t.value?i.resolve({value:t.value,dialogId:t.dialogId}):t.error&&i.reject(t.error),delete n[t.dialogId])}),{initData:o,parent:{resize:async t=>{e.request("resize",{height:t})},getValue:async()=>{let t=await e.request("getValue");return t==null?void 0:t.data},setValue:async t=>{await e.request("setValue",t)},getMetadata:async()=>{let t=await e.request("getMetadata");return t==null?void 0:t.data},openDialog:async({dialogType:t,data:i,options:r})=>{if(V(r==null?void 0:r.params)>1e5)throw new Error("Dialog parameters object is too large, maximum size is 100KB");let s=await e.request("openDialog",{dialogType:t,dialogData:i,options:r}),a=s==null?void 0:s.dialogId;if(!!a)return new Promise((l,p)=>{n[a]={resolve:l,reject:p}})},closeDialog:async({dialogId:t,dialogType:i,dialogData:r})=>{await e.request("closeDialog",{dialogId:t,dialogType:i,dialogData:r})}}}}function V(n){if(!n||typeof Blob=="undefined")return 0;try{let e=JSON.stringify(n);return new Blob([e]).size}catch(e){throw new Error("Error calculating object size: "+e.message)}}var f,C=({onHeightChange:n,autoResizingDisabled:e})=>{if(typeof window=="undefined")return;let o=window,t=a=>{if(a&&a!==f){f=a,n==null||n(a);return}let l=`${Math.ceil(o.document.documentElement.getBoundingClientRect().height)}px`;l!==f&&(f=l,n==null||n(l))},i=()=>{t()},r=e?void 0:new MutationObserver(i),s={instance:o,height:o.document.documentElement.getBoundingClientRect().height,observer:r,enableAutoResizing:()=>{r==null||r.observe(document.body,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),o.addEventListener("resize",i)},disableAutoResizing:()=>{r==null||r.disconnect(),o.removeEventListener("resize",i)},updateHeight:t};return e||s.enableAutoResizing(),s};var P={};async function oe({autoResizingDisabled:n}={}){var e;if(typeof window!="undefined"&&typeof window.UniformMeshSDK=="undefined"){if(typeof window.parent=="undefined"||window.parent==window)throw new Error("It appears you are trying to connect with the Uniform Mesh SDK outside of an iframe. Be sure you are loading your app via a location URL configured in the Uniform dashboard.");let o=await M({dialogResponseHandlers:P}),{initData:t,parent:i}=o,r={getCurrentLocation:()=>({getValue:()=>t.locationValue,setValue:async s=>{await i.setValue(s),t.locationValue=s},getMetadata:()=>t.locationMetadata}),currentWindow:C({onHeightChange:s=>{i.resize(s)},autoResizingDisabled:(e=t.dialogContext)!=null&&e.contentHeight?!0:n}),version:t.uniformApiVersion,openLocationDialog:async({locationKey:s,options:a})=>{let l=await i.openDialog({dialogType:"location",data:{locationKey:s},options:a});if(!!l)return{dialogId:l.dialogId,value:l.value,closeDialog:async()=>{await i.closeDialog({dialogId:l.dialogId,dialogType:"location"})}}},closeLocationDialog:async({dialogId:s})=>{await i.closeDialog({dialogId:s,dialogType:"location"})},openConfirmationDialog:async({titleText:s,bodyText:a})=>{let l=await i.openDialog({dialogType:"confirm",data:{titleText:s,bodyText:a}});if(!!l)return{dialogId:l.dialogId,value:l.value,closeDialog:async()=>{await i.closeDialog({dialogId:void 0,dialogType:"confirm"})}}},closeConfirmationDialog:async()=>{await i.closeDialog({dialogId:void 0,dialogType:"confirm"})},openCurrentLocationDialog:async({options:s})=>{let a=await i.openDialog({dialogType:"location",data:{},options:s});if(!!a)return{dialogId:a.dialogId,value:a.value,closeDialog:async()=>{await i.closeDialog({dialogId:void 0,dialogType:"location"})}}},closeCurrentLocationDialog:async()=>{await i.closeDialog({dialogId:void 0,dialogType:"location"})},dialogContext:t.dialogContext};return window.UniformMeshSDK=r,r}}export{oe as initializeUniformMeshSDK}; |
@@ -1,1 +0,1 @@ | ||
var l=Object.create;var o=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var g=Object.getPrototypeOf,p=Object.prototype.hasOwnProperty;var s=e=>o(e,"__esModule",{value:!0});var h=(e,n)=>{s(e);for(var t in n)o(e,t,{get:n[t],enumerable:!0})},M=(e,n,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of m(n))!p.call(e,a)&&a!=="default"&&o(e,a,{get:()=>n[a],enumerable:!(t=f(n,a))||t.enumerable});return e},y=e=>M(s(o(e!=null?l(g(e)):{},"default",e&&e.__esModule&&"default"in e?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e);h(exports,{initializeUniformMeshSDK:()=>b});var c,w=({onHeightChange:e,autoResizingDisabled:n})=>{if(typeof window=="undefined")return;let t=window,a=()=>{let r=Math.ceil(t.document.documentElement.getBoundingClientRect().height);r!==c&&(c=r,e==null||e(r))},i=n?void 0:new MutationObserver(a),d={instance:t,observer:i,height:0,enableAutoResizing:()=>{i==null||i.observe(document.body,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),t.addEventListener("resize",a)},disableAutoResizing:()=>{i==null||i.disconnect(),t.removeEventListener("resize",a)}};return n||d.enableAutoResizing(),d};var V=()=>import("post-robot");async function b({autoResizingDisabled:e}={}){if(typeof window!="undefined"&&typeof window.UniformMeshSDK=="undefined"){if(typeof window.parent=="undefined"||window.parent==window)throw new Error("It appears you are trying to connect with the Uniform Mesh SDK outside of an iframe. Be sure you are loading your app via a location URL configured in the Uniform dashboard.");let n=await k(),t=await n.initialize(),a={getCurrentLocation:()=>({getValue:()=>t.locationValue,setValue:async i=>{await n.setValue(i),t.locationValue=i},getMetadata:()=>t.locationMetadata}),currentWindow:w({onHeightChange:i=>{n.resize(i)},autoResizingDisabled:e}),version:t.uniformApiVersion};return window.UniformMeshSDK=a,a}}async function k(){let e=await V();return{initialize:async()=>(await e.send(window.parent,"initialize")).data,resize:async n=>{e.send(window.parent,"resize",{height:n})},getValue:async()=>{let n=await e.send(window.parent,"getValue");return n==null?void 0:n.data},setValue:async n=>{await e.send(window.parent,"setValue",n)},getMetadata:async()=>{let n=await e.send(window.parent,"getMetadata");return n==null?void 0:n.data}}}0&&(module.exports={initializeUniformMeshSDK}); | ||
var y=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var P=n=>y(n,"__esModule",{value:!0});var q=(n,e)=>{for(var o in e)y(n,o,{get:e[o],enumerable:!0})},z=(n,e,o,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of L(e))!V.call(n,i)&&(o||i!=="default")&&y(n,i,{get:()=>e[i],enumerable:!(t=k(e,i))||t.enumerable});return n};var H=(n=>(e,o)=>n&&n.get(e)||(o=z(P({}),e,1),n&&n.set(e,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var _={};q(_,{initializeUniformMeshSDK:()=>A});var R=(n,e)=>e?{log(o,...t){return console.log(`${n}: ${o}`,...t)},error(o,...t){return console.error(`${n}: ${o}`,...t)}}:{log(){},error(){}};var T=5e3;var g=class extends Error{constructor(){super("Request timed out");Object.setPrototypeOf(this,g.prototype),this.name="RequestTimeoutError"}},c=class extends Error{constructor(){super("Client destroyed");Object.setPrototypeOf(this,c.prototype),this.name="ClientDestroyedError"}};var b=()=>{let n=()=>{},e=()=>{},o=new Promise((t,i)=>{n=t,e=i});return{resolve:n,reject:e,promise:o}},h=(n=16)=>[...Array(n)].map(()=>(~~(Math.random()*36)).toString(36)).join(""),p=(n,e)=>{let{[e]:o,...t}=n;return t},N=n=>({message:n.message,name:n.name,stack:n.stack}),U=({name:n,message:e,stack:o})=>{switch(n){case g.name:return new g;default:{let t=new Error(e);return t.name=n,t.stack=o,t}}},w=n=>{let e=n.data,o="none";return e instanceof Error&&(o="error",e=N(e)),{...n,serialization:o,data:e}},v=n=>n.serialization==="error"?{...n,data:U(n.data)}:n;var D=class{constructor({debug:e=!1,requestTimeout:o=T}={}){this.debug=e,this.requestTimeout=o,this.channel=b(),this.eventSubscriptions={},this.responseSubscriptions={},this.requestSubscriptions={},this.onDestroyRequestHandlers={},this.destroyed=!1,this.logger=this.getLogger(),this.getChannel().then(()=>{this.logger.log("Secure parent <-> child channel established")}).catch(t=>{this.logger.log(t)})}async send(e,o){return this.postMessage("event",e,o)}on(e,o){this.eventSubscriptions[e]||(this.eventSubscriptions[e]={});let t=h(8);return this.eventSubscriptions[e][t]=o,this.logger.log(`Registered handler for event "${e}"`),()=>{this.eventSubscriptions[e]=p(this.eventSubscriptions[e],t),this.logger.log(`Unsubscribed handler for event ${e}`)}}async request(e,o,t={}){let r=(await this.postMessage("request",e,o)).id,s=()=>{this.responseSubscriptions=p(this.responseSubscriptions,r)},a=()=>{this.onDestroyRequestHandlers=p(this.responseSubscriptions,r)};return new Promise((l,u)=>{let f,x=(S,O)=>{clearTimeout(f),s(),a(),O.type==="error_response"?u(S):l(S)},I=()=>{clearTimeout(f),u(new c)};this.responseSubscriptions[r]=x,this.onDestroyRequestHandlers[r]=I,f=setTimeout(()=>{s(),a(),u(new g)},t.timeout||this.requestTimeout)})}onRequest(e,o){let t=async(i,r)=>{try{let s=await o(i,r);this.postMessage("response",e,s,r.id)}catch(s){this.postMessage("error_response",e,s,r.id)}};return this.requestSubscriptions[e]=t,()=>{this.requestSubscriptions=p(this.requestSubscriptions,e)}}async getChannel(){if(await this.channel.promise,this.destroyed)throw new c;return this.channel.promise}async messageListener(e){try{await this.getChannel();let o=this.isValidMessage(e),t=v(e.data);if(o)switch(t.type){case"event":{this.handleEvent(t);break}case"request":{this.handleRequest(t);break}case"error_response":case"response":{this.handleResponse(t);break}}else this.logger.error("Invalid message format. Skipping.")}catch(o){this.logger.error(o)}}handleEvent(e){let o=this.eventSubscriptions[e.key];o&&Object.values(o).forEach(t=>t(e.data,e))}handleRequest(e){let o=this.requestSubscriptions[e.key];o&&(o(e.data,e),this.logger.log(`Handled request type ${e.key}`))}handleResponse(e){let o=e.requestId,t=o&&this.responseSubscriptions[o];t&&t(e.data,e)}async postMessage(e,o,t,i){let{port:r}=await this.getChannel();if(this.destroyed)throw new c;let s=w({type:e,apiVersion:"framepost/v1",key:o,data:t,id:h(),requestId:i});return this.logger.log("posting message from child to parent",s),r.postMessage(s),s}initListener(e){this.isInitMessage(e)?(this.onChannelInit(e),this.messagePort&&(this.messagePort.onmessage=this.messageListener.bind(this)),this.resolveChannel(e)):this.logger.error("Invalid message format. Skipping.")}isValidMessage(e){let o=e.data;return o.type&&o.id&&o.apiVersion==="framepost/v1"}isInitMessage(e){return this.isValidMessage(e)&&e.data.type==="channel_init"}resolveChannel(e){if(this.messagePort){let o={port:this.messagePort,origin:e.origin,context:e.data.data};this.channel.resolve(o)}}getInitMessage(e){return w({type:"channel_init",apiVersion:"framepost/v1",key:"",data:e,id:h()})}destroy(){this.destroyed=!0,this.channel.reject(new c),this.messagePort&&this.messagePort.close(),Object.values(this.onDestroyRequestHandlers).forEach(e=>e())}};var E=class extends D{constructor(e={}){super(e);this.context=e.context||null,this.initListener=this.initListener.bind(this),window.addEventListener("message",this.initListener)}getLogger(){return R("child-client",this.debug)}onChannelInit(e){window.removeEventListener("message",this.initListener),this.messagePort=e.ports[0];let o=this.getInitMessage(this.context);this.logger.log("channel init, posting init message from child to parent",o),this.messagePort.postMessage(o)}destroy(){super.destroy(),window.removeEventListener("message",this.initListener)}};async function M({dialogResponseHandlers:n}){let e=new E({debug:!1});window.parent.postMessage("parents just don't understand","*");let o=await e.request("initialize");return e.onRequest("dialog-value",async t=>{let i=n[t.dialogId];!i||(t.value?i.resolve({value:t.value,dialogId:t.dialogId}):t.error&&i.reject(t.error),delete n[t.dialogId])}),{initData:o,parent:{resize:async t=>{e.request("resize",{height:t})},getValue:async()=>{let t=await e.request("getValue");return t==null?void 0:t.data},setValue:async t=>{await e.request("setValue",t)},getMetadata:async()=>{let t=await e.request("getMetadata");return t==null?void 0:t.data},openDialog:async({dialogType:t,data:i,options:r})=>{if(W(r==null?void 0:r.params)>1e5)throw new Error("Dialog parameters object is too large, maximum size is 100KB");let s=await e.request("openDialog",{dialogType:t,dialogData:i,options:r}),a=s==null?void 0:s.dialogId;if(!!a)return new Promise((l,u)=>{n[a]={resolve:l,reject:u}})},closeDialog:async({dialogId:t,dialogType:i,dialogData:r})=>{await e.request("closeDialog",{dialogId:t,dialogType:i,dialogData:r})}}}}function W(n){if(!n||typeof Blob=="undefined")return 0;try{let e=JSON.stringify(n);return new Blob([e]).size}catch(e){throw new Error("Error calculating object size: "+e.message)}}var m,C=({onHeightChange:n,autoResizingDisabled:e})=>{if(typeof window=="undefined")return;let o=window,t=a=>{if(a&&a!==m){m=a,n==null||n(a);return}let l=`${Math.ceil(o.document.documentElement.getBoundingClientRect().height)}px`;l!==m&&(m=l,n==null||n(l))},i=()=>{t()},r=e?void 0:new MutationObserver(i),s={instance:o,height:o.document.documentElement.getBoundingClientRect().height,observer:r,enableAutoResizing:()=>{r==null||r.observe(document.body,{attributes:!0,childList:!0,subtree:!0,characterData:!0}),o.addEventListener("resize",i)},disableAutoResizing:()=>{r==null||r.disconnect(),o.removeEventListener("resize",i)},updateHeight:t};return e||s.enableAutoResizing(),s};var Q={};async function A({autoResizingDisabled:n}={}){var e;if(typeof window!="undefined"&&typeof window.UniformMeshSDK=="undefined"){if(typeof window.parent=="undefined"||window.parent==window)throw new Error("It appears you are trying to connect with the Uniform Mesh SDK outside of an iframe. Be sure you are loading your app via a location URL configured in the Uniform dashboard.");let o=await M({dialogResponseHandlers:Q}),{initData:t,parent:i}=o,r={getCurrentLocation:()=>({getValue:()=>t.locationValue,setValue:async s=>{await i.setValue(s),t.locationValue=s},getMetadata:()=>t.locationMetadata}),currentWindow:C({onHeightChange:s=>{i.resize(s)},autoResizingDisabled:(e=t.dialogContext)!=null&&e.contentHeight?!0:n}),version:t.uniformApiVersion,openLocationDialog:async({locationKey:s,options:a})=>{let l=await i.openDialog({dialogType:"location",data:{locationKey:s},options:a});if(!!l)return{dialogId:l.dialogId,value:l.value,closeDialog:async()=>{await i.closeDialog({dialogId:l.dialogId,dialogType:"location"})}}},closeLocationDialog:async({dialogId:s})=>{await i.closeDialog({dialogId:s,dialogType:"location"})},openConfirmationDialog:async({titleText:s,bodyText:a})=>{let l=await i.openDialog({dialogType:"confirm",data:{titleText:s,bodyText:a}});if(!!l)return{dialogId:l.dialogId,value:l.value,closeDialog:async()=>{await i.closeDialog({dialogId:void 0,dialogType:"confirm"})}}},closeConfirmationDialog:async()=>{await i.closeDialog({dialogId:void 0,dialogType:"confirm"})},openCurrentLocationDialog:async({options:s})=>{let a=await i.openDialog({dialogType:"location",data:{},options:s});if(!!a)return{dialogId:a.dialogId,value:a.value,closeDialog:async()=>{await i.closeDialog({dialogId:void 0,dialogType:"location"})}}},closeCurrentLocationDialog:async()=>{await i.closeDialog({dialogId:void 0,dialogType:"location"})},dialogContext:t.dialogContext};return window.UniformMeshSDK=r,r}}module.exports=H(_);0&&(module.exports={initializeUniformMeshSDK}); |
{ | ||
"name": "@uniformdev/mesh-sdk", | ||
"version": "13.0.1-alpha.83+ca85e1db", | ||
"version": "13.0.1-alpha.96+63b9d6697", | ||
"description": "Uniform Mesh Framework SDK", | ||
@@ -18,3 +18,3 @@ "license": "SEE LICENSE IN LICENSE.txt", | ||
"scripts": { | ||
"build": "tsup", | ||
"build": "tsup --minify", | ||
"dev": "tsup --watch", | ||
@@ -24,24 +24,11 @@ "clean": "rimraf dist", | ||
"lint": "eslint \"src/**/*.{js,ts,tsx}\"", | ||
"format": "prettier --write \"src/**/*.{js,ts,tsx}\"", | ||
"ci:verify": "run-p lint test", | ||
"ci:build": "tsup --minify --clean" | ||
"format": "prettier --write \"src/**/*.{js,ts,tsx}\"" | ||
}, | ||
"dependencies": { | ||
"post-robot": "10.0.44" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "27.0.3", | ||
"@types/node": "16.11.12", | ||
"@types/post-robot": "10.0.3", | ||
"eslint": "8.4.1", | ||
"jest": "27.4.5", | ||
"npm-run-all": "4.1.5", | ||
"rimraf": "3.0.2", | ||
"ts-jest": "27.1.1", | ||
"tsup": "5.11.1" | ||
}, | ||
"files": [ | ||
"/dist" | ||
], | ||
"gitHead": "ca85e1dbe78e521ab829624f9b4c8ad0744d3a95" | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "63b9d66978bcf803b186efd292f08101a7c66d66" | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
33386
310.9%0
-100%0
-100%238
332.73%239
0.42%2
100%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed