@liff/sub-window
Advanced tools
Comparing version 2.22.1 to 2.22.2
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("@liff/use"),r=require("@liff/is-in-client"),n=require("@liff/is-api-available"),i=require("@liff/consts"),o=require("@liff/logger"),s=require("@liff/util"),a=require("@liff/get-os"),u=require("@liff/server-api"),c=require("@liff/store"),f=require("@liff/message-bus"),l=require("@liff/is-sub-window"),d=require("@liff/close-window");function _(e){var t=u.getEndPoint("subWindowGetOrigin");return u.fetch(t(e))}var S={};function I(e,t){e&&S[e]&&S[e].forEach((function(e){e(t)}))}var v,g,p,m,w,h=function(){function e(e){this.storage=e}return e.prototype.getItem=function(e){return this.storage.getItem("".concat(this.getKeyPrefix(),":").concat(e))},e.prototype.setItem=function(e,t){this.storage.setItem("".concat(this.getKeyPrefix(),":").concat(e),t)},e.prototype.removeItem=function(e){this.storage.removeItem("".concat(this.getKeyPrefix(),":").concat(e))},e.prototype.clear=function(){this.storage.clear()},e.prototype.getKeyPrefix=function(){return"".concat(i.STORE_KEY,":").concat(this.getLiffId())},e.prototype.getLiffId=function(){var e=c.getConfig().liffId;if(!e)throw s.createLiffError(i.INVALID_CONFIG,"liffId is necessary for liff.init()");return e},e}(),N=new h(s.inMemoryStorage);function E(){var e=N.getItem("subWindowStatusUpdated");return null!==e&&JSON.parse(e)}function T(e){N.setItem("subWindowStatusUpdated",String(e))}function O(e){v=e}function W(){return v}function U(){return p}function L(){return m}function A(t){return void 0===t&&(t=f.WINDOW.MAIN),e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return[4,(w=new f.MessageBus(t)).setup()];case 1:return e.sent(),[2,w]}}))}))}function b(){return w}var D=new h(window.sessionStorage);function C(){return D.getItem("mainWindowOrigin")}function y(t,r){return void 0===r&&(r={}),e.__awaiter(this,void 0,void 0,(function(){var n,a,u,f,l,d,S,I;return e.__generator(this,(function(e){switch(e.label){case 0:if(null==(n=b())?void 0:n.isReady())return[3,5];if(a=JSON.stringify(r),u=c.getConfig().liffId,f=C(),!window.opener||!f||!u)throw s.createLiffError(i.EXCEPTION_IN_SUBWINDOW);l=!1,e.label=1;case 1:return e.trys.push([1,3,,4]),[4,_(u)];case 2:return d=e.sent(),l=d.subwindowCommonModule,[3,4];case 3:throw S=e.sent(),o.logger.debug(S),s.createLiffError(i.EXCEPTION_IN_SUBWINDOW);case 4:return I=l?f:location.origin,[2,new Promise((function(e){window.addEventListener("message",(function r(n){(function(e){if(e.data&&"string"==typeof e.data.type&&[i.SUB_WINDOW_STATUS.SUBMIT,i.SUB_WINDOW_STATUS.CANCEL].includes(e.data.type))return!0;return!1})(n)&&(window.removeEventListener("message",r),e({status:t,result:a}))})),window.opener.postMessage({status:t,result:a},I)}))];case 5:return n.send({eventName:t,data:r}),[4,new Promise((function(e){setTimeout(e,500)}))];case 6:return e.sent(),[2,{status:t,result:JSON.stringify(r)}]}}))}))}function B(e){var t,r=L();if(e.origin===r){var n=e.data;if(n){var s,a=n.status,u=n.result;try{s=JSON.parse(u||"{}")}catch(c){s={}}switch(a){case i.SUB_WINDOW_HEALTH_CHECK_MESSAGE:window.clearInterval(U()),x();break;case i.SUB_WINDOW_STATUS.CANCEL:case i.SUB_WINDOW_STATUS.SUBMIT:T(!0),window.clearInterval(U()),window.removeEventListener("message",B),I(a,s),null===(t=W())||void 0===t||t.postMessage({type:a},L());break;default:o.logger.debug("unexpected message")}}}}var P=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var r,n,o,s;return e.__generator(this,(function(e){if(E())return[2];switch(r=t.context,n=r.eventName,o=r.data,s=b(),n){case i.SUB_WINDOW_STATUS.INIT:R(!o.hasOpener);break;case i.SUB_WINDOW_STATUS.CANCEL:case i.SUB_WINDOW_STATUS.SUBMIT:T(!0),I(n,o),null==s||s.reply(t,{eventName:n});break;case i.SUB_WINDOW_STATUS.CLOSE:!1===E()&&(T(!0),I(i.SUB_WINDOW_STATUS.CLOSE,{})),x()}return[2]}))}))};function M(){window.clearInterval(g),window.clearInterval(U()),window.removeEventListener("message",B)}function R(e){if(void 0===e&&(e=!1),M(),T(!1),e){var t=W();t&&(t.close(),O(null))}}function x(){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return(t=b())?[4,t.teardown()]:[3,2];case 1:e.sent(),e.label=2;case 2:return[2]}}))}))}function H(t){return e.__awaiter(this,void 0,void 0,(function(){var r,n,o,u,c,l,d,S,v,w;return e.__generator(this,(function(h){switch(h.label){case 0:return(r=s.extractLiffId(t.url))?(R(!0),[4,x()]):[2,Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"params.url must be liff url"))];case 1:return h.sent(),n=t.url,o=t.appData,(u=new URL(n)).searchParams.append(i.SUB_WINDOW_IDNTIFICATION_KEY,"true"),[4,A()];case 2:return c=h.sent(),u.searchParams.append(f.IDENTIFIER_KEY,c.identification.identifier),u.searchParams.append(f.CRYPTO_KEY,c.identification.cryptoKey),u.hostname=function(t){var r=e.__read(t.split(".")),n=r[0],i=r.slice(1);return e.__spreadArray(["".concat(n,"-ext")],e.__read(i),!1).join(".")}(u.hostname),l=u.toString(),O("ios"!==a.getOS()||s.isIpad()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),[4,_(r)];case 3:if(d=h.sent(),S=d.origin,v=d.subwindowCommonModule,!(w=W()))throw s.createLiffError(i.CREATE_SUBWINDOW_FAILED);return v?(function(e){m=e}(S),c.listen(P),c.setData("appData",o),window.addEventListener("message",B),w.location.href=l,N=function(e,t){var r=W(),n={type:i.SUB_WINDOW_HEALTH_CHECK_MESSAGE};return t&&(n.message=JSON.stringify(t)),window.setInterval((function(){null==r||r.postMessage(n,e)}),i.SUB_WINDOW_HEALTH_CHECK_INTERVAL)}(S,o),p=N,function(e){g=e}(window.setInterval((function(){var e=W();e&&e.closed&&(M(),O(null),!1===E()&&(T(!0),I(i.SUB_WINDOW_STATUS.CLOSE,{})))}),i.SUB_WINDOW_MONITOR_CLOSE_INTERVAL)),[2]):(w.close(),[2])}var N}))}))}var K=null;function j(t){return e.__awaiter(this,void 0,void 0,(function(){var r,n,o,a,c,f,l,d,_,S,I,v,g,p,m=this;return e.__generator(this,(function(w){switch(w.label){case 0:if(r=t.msit,n=t.mstChallenge,o=t.reconnectCount,a=void 0===o?0:o,c=function(){return e.__awaiter(m,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return[4,(i=1e3,new Promise((function(e){return setTimeout(e,i)})))];case 1:return e.sent(),[4,j({msit:r,mstChallenge:n,onSuccess:t.onSuccess,onError:t.onError,reconnectCount:a+1})];case 2:return e.sent(),[2]}var i}))}))},f=function(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];K=null,t.onSuccess.apply(t,e.__spreadArray([],e.__read(r),!1))},l=function(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];K=null,t.onError.apply(t,e.__spreadArray([],e.__read(r),!1))},d=Date.now(),null===K&&(K=d),_=d-K,a>=10||_>6e5)return l(s.createLiffError(i.UNKNOWN,"Failed to connect")),[2];w.label=1;case 1:return w.trys.push([1,3,,5]),[4,u.requestWithoutErrorHandling(u.getEndPoint("subWindowSubscribe"),{method:"POST",body:JSON.stringify({msit:r,mstChallenge:n})})];case 2:return S=w.sent(),[3,5];case 3:return w.sent(),[4,c()];case 4:return w.sent(),[2];case 5:return S.status>=500?[4,c()]:[3,7];case 6:return w.sent(),[3,17];case 7:return S.status>=400&&500>S.status?[4,q(S)]:[3,9];case 8:return v=w.sent(),I=v?s.createLiffError(i.INVALID_ARGUMENT,v.errorDetail):s.createLiffError(i.UNKNOWN,"Some error happened in the server"),l(I),[3,17];case 9:return 200!==S.status?[3,16]:[4,q(S)];case 10:if(!(v=w.sent()))return l(s.createLiffError(i.UNKNOWN,"Some error happened in the server")),[2];switch(g=v.status,p=v.result,g){case i.SUB_WINDOW_STATUS.ERROR:return[3,11];case i.SUB_WINDOW_STATUS.CLOSE:case i.SUB_WINDOW_STATUS.CANCEL:case i.SUB_WINDOW_STATUS.SUBMIT:return[3,13]}return[3,14];case 11:return[4,c()];case 12:return w.sent(),[3,15];case 13:return f(g,p),[3,15];case 14:l(s.createLiffError(i.UNKNOWN,"Some error happened in the server")),w.label=15;case 15:return[3,17];case 16:l(s.createLiffError(i.UNKNOWN,"Some error happened in the server")),w.label=17;case 17:return[2]}}))}))}function q(t){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,t.json()];case 1:return[2,e.sent()];case 2:return e.sent(),[2,null];case 3:return[2]}}))}))}function G(e){var t={};return Object.keys(e).forEach((function(r){"closeButtonColor"===r?"white"===e[r]?t[r]="#ffffff":t[r]="#000000":t[r]=e[r]})),t}var V={height:"full",closeButtonPosition:"right",closeButtonColor:"black",closeButtonLabel:""};function J(e){var t=e.appData,r=e.native,n=c.getConfig().liffId,o=c.getMSTChallenge(),a=s.extractLiffId(e.url);if(!n)return Promise.reject(s.createLiffError(i.UNAUTHORIZED,"liffId is invalid"));if(!o)return Promise.reject(s.createLiffError(i.UNAUTHORIZED,"mst_challenge is invalid"));if(!a)return Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"params.url must be liff url"));var f=Object.assign({},V,r);return function(e){var t=e.mainLiffId,r=e.subLiffId,n=e.mstChallenge,o=e.appData,a=e.view;return t&&n?u.fetch(u.getEndPoint("subWindowGetMSIT"),{method:"POST",body:JSON.stringify({mainLiffId:t,subLiffId:r,mstChallenge:n,appData:o,view:a})}):Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"no proper argument"))}({mainLiffId:n,subLiffId:a,mstChallenge:o,appData:t,view:G(f)}).then((function(t){var r=t.msit;j({msit:r,mstChallenge:o,onSuccess:function(e,t){I(e,t)},onError:function(e){I(i.SUB_WINDOW_STATUS.ERROR,e)}}),function(e,t){var r=e.url,n=new URLSearchParams;n.set("msit",t),location.href="".concat(i.SUB_WINDOW_MODAL_SCHEME_URL,"?url=").concat(encodeURIComponent(r),"&").concat(n.toString())}(e,r)}))}function k(e){if(!e.mst||!e.status)return Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"no proper argument"));var t=JSON.stringify(e);return u.fetch(u.getEndPoint("subWindowPost"),{method:"POST",body:t})}function Z(){if(!l.isSubWindow())throw s.createLiffError(i.UNAUTHORIZED,"this api can be only called in child window")}var F={on:function(e,t){S[e]||(S[e]=[]),S[e].push(t)},off:function(e,t){if(S[e]){var r=S[e].indexOf(t);r>=0&&S[e].splice(r,1)}},open:function(e){return n.validators.subwindowOpen(),r.isInClient()?J(e):H(e)},cancel:function(t){return void 0===t&&(t={}),Z(),r.isInClient()?function(t){return void 0===t&&(t={}),e.__awaiter(this,void 0,void 0,(function(){var r,n;return e.__generator(this,(function(e){switch(e.label){case 0:return(r=c.getMST())?[4,k({mst:r,status:i.SUB_WINDOW_STATUS.CANCEL,result:t})]:[2,Promise.reject(s.createLiffError(i.UNAUTHORIZED,"mst is invalid"))];case 1:return n=e.sent(),T(!0),[2,n]}}))}))}(t):function(e){return void 0===e&&(e={}),y(i.SUB_WINDOW_STATUS.CANCEL,e)}(t)},submit:function(t){return void 0===t&&(t={}),Z(),r.isInClient()?function(t){return void 0===t&&(t={}),e.__awaiter(this,void 0,void 0,(function(){var r,n;return e.__generator(this,(function(e){switch(e.label){case 0:return(r=c.getMST())?[4,k({mst:r,status:i.SUB_WINDOW_STATUS.SUBMIT,result:t})]:[2,Promise.reject(s.createLiffError(i.UNAUTHORIZED,"mst is invalid"))];case 1:return n=e.sent(),T(!0),[2,n]}}))}))}(t):function(e){return void 0===e&&(e={}),y(i.SUB_WINDOW_STATUS.SUBMIT,e)}(t)},close:function(){return Z(),r.isInClient()?function(){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return!1!==E()?[3,2]:(t=c.getMST())?[4,k({mst:t,status:i.SUB_WINDOW_STATUS.CLOSE,result:{}})]:[2,Promise.reject(s.createLiffError(i.UNAUTHORIZED,"mst is invalid"))];case 1:e.sent(),e.label=2;case 2:return d.closeWindow(),[2]}}))}))}():function(){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){return(null==(t=b())?void 0:t.isReady())?(t.send({eventName:i.SUB_WINDOW_STATUS.CLOSE}),[2,new Promise((function(e){setTimeout((function(){d.closeWindow(),e()}),i.SUB_WINDOW_HEALTH_CHECK_INTERVAL)}))]):(d.closeWindow(),[2,Promise.resolve()])}))}))}()},getAppData:function(){return Z(),function(){var e,t=c.getAppData();try{e=t?JSON.parse(t):{}}catch(r){e={}}return Promise.resolve(e)}()}},Y=function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return e.__extends(r,t),Object.defineProperty(r.prototype,"name",{get:function(){return"subWindow"},enumerable:!1,configurable:!0}),r.prototype.install=function(){return F},r}(t.LiffModule);exports.SubWindowModule=Y,exports.getAppData=function(e){var t=e.mst;return t?u.fetch(u.getEndPoint("subWindowGetAppData"),{method:"POST",body:JSON.stringify({mst:t})}):Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"no proper argument"))},exports.getMSTByMSIT=function(e){var t=e.msit,r=e.mstVerifier;return t&&r?u.fetch(u.getEndPoint("subWindowGetMSTByMSIT"),{method:"POST",body:JSON.stringify({msit:t,mstVerifier:r})}):Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"no proper argument"))},exports.getMainWindowOrigin=C,exports.getMessageBus=b,exports.initMessageBus=A,exports.setMainWindowOrigin=function(e){D.setItem("mainWindowOrigin",e)},exports.subWindow=F; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("@liff/use"),r=require("@liff/is-in-client"),n=require("@liff/is-api-available"),i=require("@liff/consts"),o=require("@liff/logger"),s=require("@liff/util"),a=require("@liff/get-os"),u=require("@liff/server-api"),c=require("@liff/store"),f=require("@liff/message-bus"),l=require("@liff/is-sub-window"),d=require("@liff/close-window");function _(e){var t=u.getEndPoint("subWindowGetOrigin");return u.fetch(t(e))}var S={};function I(e,t){e&&S[e]&&S[e].forEach((function(e){e(t)}))}var v,g,p,m,w,h=function(){function e(e){this.storage=e}return e.prototype.getItem=function(e){return this.storage.getItem("".concat(this.getKeyPrefix(),":").concat(e))},e.prototype.setItem=function(e,t){this.storage.setItem("".concat(this.getKeyPrefix(),":").concat(e),t)},e.prototype.removeItem=function(e){this.storage.removeItem("".concat(this.getKeyPrefix(),":").concat(e))},e.prototype.clear=function(){this.storage.clear()},e.prototype.getKeyPrefix=function(){return"".concat(i.STORE_KEY,":").concat(this.getLiffId())},e.prototype.getLiffId=function(){var e=c.getConfig().liffId;if(!e)throw s.createLiffError(i.INVALID_CONFIG,"liffId is necessary for liff.init()");return e},e}(),N=new h(new s.InMemoryStorage);function E(){var e=N.getItem("subWindowStatusUpdated");return null!==e&&JSON.parse(e)}function T(e){N.setItem("subWindowStatusUpdated",String(e))}function O(e){v=e}function W(){return v}function U(){return p}function L(){return m}function A(t){return void 0===t&&(t=f.WINDOW.MAIN),e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return[4,(w=new f.MessageBus(t)).setup()];case 1:return e.sent(),[2,w]}}))}))}function b(){return w}var D=new s.InMemoryStorage,y=new h(s.isNonBrowserEnvironment()?D:window.sessionStorage);function C(){return y.getItem("mainWindowOrigin")}function B(t,r){return void 0===r&&(r={}),e.__awaiter(this,void 0,void 0,(function(){var n,a,u,f,l,d,S,I;return e.__generator(this,(function(e){switch(e.label){case 0:if(null==(n=b())?void 0:n.isReady())return[3,5];if(a=JSON.stringify(r),u=c.getConfig().liffId,f=C(),!window.opener||!f||!u)throw s.createLiffError(i.EXCEPTION_IN_SUBWINDOW);l=!1,e.label=1;case 1:return e.trys.push([1,3,,4]),[4,_(u)];case 2:return d=e.sent(),l=d.subwindowCommonModule,[3,4];case 3:throw S=e.sent(),o.logger.debug(S),s.createLiffError(i.EXCEPTION_IN_SUBWINDOW);case 4:return I=l?f:location.origin,[2,new Promise((function(e){window.addEventListener("message",(function r(n){(function(e){if(e.data&&"string"==typeof e.data.type&&[i.SUB_WINDOW_STATUS.SUBMIT,i.SUB_WINDOW_STATUS.CANCEL].includes(e.data.type))return!0;return!1})(n)&&(window.removeEventListener("message",r),e({status:t,result:a}))})),window.opener.postMessage({status:t,result:a},I)}))];case 5:return n.send({eventName:t,data:r}),[4,new Promise((function(e){setTimeout(e,500)}))];case 6:return e.sent(),[2,{status:t,result:JSON.stringify(r)}]}}))}))}function M(e){var t,r=L();if(e.origin===r){var n=e.data;if(n){var s,a=n.status,u=n.result;try{s=JSON.parse(u||"{}")}catch(c){s={}}switch(a){case i.SUB_WINDOW_HEALTH_CHECK_MESSAGE:window.clearInterval(U()),H();break;case i.SUB_WINDOW_STATUS.CANCEL:case i.SUB_WINDOW_STATUS.SUBMIT:T(!0),window.clearInterval(U()),window.removeEventListener("message",M),I(a,s),null===(t=W())||void 0===t||t.postMessage({type:a},L());break;default:o.logger.debug("unexpected message")}}}}var P=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var r,n,o,s;return e.__generator(this,(function(e){if(E())return[2];switch(r=t.context,n=r.eventName,o=r.data,s=b(),n){case i.SUB_WINDOW_STATUS.INIT:x(!o.hasOpener);break;case i.SUB_WINDOW_STATUS.CANCEL:case i.SUB_WINDOW_STATUS.SUBMIT:T(!0),I(n,o),null==s||s.reply(t,{eventName:n});break;case i.SUB_WINDOW_STATUS.CLOSE:!1===E()&&(T(!0),I(i.SUB_WINDOW_STATUS.CLOSE,{})),H()}return[2]}))}))};function R(){window.clearInterval(g),window.clearInterval(U()),window.removeEventListener("message",M)}function x(e){if(void 0===e&&(e=!1),R(),T(!1),e){var t=W();t&&(t.close(),O(null))}}function H(){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return(t=b())?[4,t.teardown()]:[3,2];case 1:e.sent(),e.label=2;case 2:return[2]}}))}))}function K(t){return e.__awaiter(this,void 0,void 0,(function(){var r,n,o,u,c,l,d,S,v,w;return e.__generator(this,(function(h){switch(h.label){case 0:return(r=s.extractLiffId(t.url))?(x(!0),[4,H()]):[2,Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"params.url must be liff url"))];case 1:return h.sent(),O("ios"!==a.getOS()||s.isIpad()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),n=t.url,o=t.appData,(u=new URL(n)).searchParams.append(i.SUB_WINDOW_IDNTIFICATION_KEY,"true"),[4,A()];case 2:return c=h.sent(),u.searchParams.append(f.IDENTIFIER_KEY,c.identification.identifier),u.searchParams.append(f.CRYPTO_KEY,c.identification.cryptoKey),u.hostname=function(t){var r=e.__read(t.split(".")),n=r[0],i=r.slice(1);return e.__spreadArray(["".concat(n,"-ext")],e.__read(i),!1).join(".")}(u.hostname),l=u.toString(),[4,_(r)];case 3:if(d=h.sent(),S=d.origin,v=d.subwindowCommonModule,!(w=W()))throw s.createLiffError(i.CREATE_SUBWINDOW_FAILED);return v?(function(e){m=e}(S),c.listen(P),c.setData("appData",o),window.addEventListener("message",M),w.location.href=l,N=function(e,t){var r=W(),n={type:i.SUB_WINDOW_HEALTH_CHECK_MESSAGE};return t&&(n.message=JSON.stringify(t)),window.setInterval((function(){null==r||r.postMessage(n,e)}),i.SUB_WINDOW_HEALTH_CHECK_INTERVAL)}(S,o),p=N,function(e){g=e}(window.setInterval((function(){var e=W();e&&e.closed&&(R(),O(null),!1===E()&&(T(!0),I(i.SUB_WINDOW_STATUS.CLOSE,{})))}),i.SUB_WINDOW_MONITOR_CLOSE_INTERVAL)),[2]):(w.close(),[2])}var N}))}))}var j=null;function q(t){return e.__awaiter(this,void 0,void 0,(function(){var r,n,o,a,c,f,l,d,_,S,I,v,g,p,m=this;return e.__generator(this,(function(w){switch(w.label){case 0:if(r=t.msit,n=t.mstChallenge,o=t.reconnectCount,a=void 0===o?0:o,c=function(){return e.__awaiter(m,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return[4,(i=1e3,new Promise((function(e){return setTimeout(e,i)})))];case 1:return e.sent(),[4,q({msit:r,mstChallenge:n,onSuccess:t.onSuccess,onError:t.onError,reconnectCount:a+1})];case 2:return e.sent(),[2]}var i}))}))},f=function(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];j=null,t.onSuccess.apply(t,e.__spreadArray([],e.__read(r),!1))},l=function(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];j=null,t.onError.apply(t,e.__spreadArray([],e.__read(r),!1))},d=Date.now(),null===j&&(j=d),_=d-j,a>=10||_>6e5)return l(s.createLiffError(i.UNKNOWN,"Failed to connect")),[2];w.label=1;case 1:return w.trys.push([1,3,,5]),[4,u.requestWithoutErrorHandling(u.getEndPoint("subWindowSubscribe"),{method:"POST",body:JSON.stringify({msit:r,mstChallenge:n})})];case 2:return S=w.sent(),[3,5];case 3:return w.sent(),[4,c()];case 4:return w.sent(),[2];case 5:return S.status>=500?[4,c()]:[3,7];case 6:return w.sent(),[3,17];case 7:return S.status>=400&&500>S.status?[4,G(S)]:[3,9];case 8:return v=w.sent(),I=v?s.createLiffError(i.INVALID_ARGUMENT,v.errorDetail):s.createLiffError(i.UNKNOWN,"Some error happened in the server"),l(I),[3,17];case 9:return 200!==S.status?[3,16]:[4,G(S)];case 10:if(!(v=w.sent()))return l(s.createLiffError(i.UNKNOWN,"Some error happened in the server")),[2];switch(g=v.status,p=v.result,g){case i.SUB_WINDOW_STATUS.ERROR:return[3,11];case i.SUB_WINDOW_STATUS.CLOSE:case i.SUB_WINDOW_STATUS.CANCEL:case i.SUB_WINDOW_STATUS.SUBMIT:return[3,13]}return[3,14];case 11:return[4,c()];case 12:return w.sent(),[3,15];case 13:return f(g,p),[3,15];case 14:l(s.createLiffError(i.UNKNOWN,"Some error happened in the server")),w.label=15;case 15:return[3,17];case 16:l(s.createLiffError(i.UNKNOWN,"Some error happened in the server")),w.label=17;case 17:return[2]}}))}))}function G(t){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,t.json()];case 1:return[2,e.sent()];case 2:return e.sent(),[2,null];case 3:return[2]}}))}))}function V(e){var t={};return Object.keys(e).forEach((function(r){"closeButtonColor"===r?"white"===e[r]?t[r]="#ffffff":t[r]="#000000":t[r]=e[r]})),t}var J={height:"full",closeButtonPosition:"right",closeButtonColor:"black",closeButtonLabel:""};function k(e){var t=e.appData,r=e.native,n=c.getConfig().liffId,o=c.getMSTChallenge(),a=s.extractLiffId(e.url);if(!n)return Promise.reject(s.createLiffError(i.UNAUTHORIZED,"liffId is invalid"));if(!o)return Promise.reject(s.createLiffError(i.UNAUTHORIZED,"mst_challenge is invalid"));if(!a)return Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"params.url must be liff url"));var f=Object.assign({},J,r);return function(e){var t=e.mainLiffId,r=e.subLiffId,n=e.mstChallenge,o=e.appData,a=e.view;return t&&n?u.fetch(u.getEndPoint("subWindowGetMSIT"),{method:"POST",body:JSON.stringify({mainLiffId:t,subLiffId:r,mstChallenge:n,appData:o,view:a})}):Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"no proper argument"))}({mainLiffId:n,subLiffId:a,mstChallenge:o,appData:t,view:V(f)}).then((function(t){var r=t.msit;q({msit:r,mstChallenge:o,onSuccess:function(e,t){I(e,t)},onError:function(e){I(i.SUB_WINDOW_STATUS.ERROR,e)}}),function(e,t){var r=e.url,n=new URLSearchParams;n.set("msit",t),location.href="".concat(i.SUB_WINDOW_MODAL_SCHEME_URL,"?url=").concat(encodeURIComponent(r),"&").concat(n.toString())}(e,r)}))}function Z(e){if(!e.mst||!e.status)return Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"no proper argument"));var t=JSON.stringify(e);return u.fetch(u.getEndPoint("subWindowPost"),{method:"POST",body:t})}function F(){if(!l.isSubWindow())throw s.createLiffError(i.UNAUTHORIZED,"this api can be only called in child window")}var Y={on:function(e,t){S[e]||(S[e]=[]),S[e].push(t)},off:function(e,t){if(S[e]){var r=S[e].indexOf(t);r>=0&&S[e].splice(r,1)}},open:function(e){return n.validators.subwindowOpen(),r.isInClient()?k(e):K(e)},cancel:function(t){return void 0===t&&(t={}),F(),r.isInClient()?function(t){return void 0===t&&(t={}),e.__awaiter(this,void 0,void 0,(function(){var r,n;return e.__generator(this,(function(e){switch(e.label){case 0:return(r=c.getMST())?[4,Z({mst:r,status:i.SUB_WINDOW_STATUS.CANCEL,result:t})]:[2,Promise.reject(s.createLiffError(i.UNAUTHORIZED,"mst is invalid"))];case 1:return n=e.sent(),T(!0),[2,n]}}))}))}(t):function(e){return void 0===e&&(e={}),B(i.SUB_WINDOW_STATUS.CANCEL,e)}(t)},submit:function(t){return void 0===t&&(t={}),F(),r.isInClient()?function(t){return void 0===t&&(t={}),e.__awaiter(this,void 0,void 0,(function(){var r,n;return e.__generator(this,(function(e){switch(e.label){case 0:return(r=c.getMST())?[4,Z({mst:r,status:i.SUB_WINDOW_STATUS.SUBMIT,result:t})]:[2,Promise.reject(s.createLiffError(i.UNAUTHORIZED,"mst is invalid"))];case 1:return n=e.sent(),T(!0),[2,n]}}))}))}(t):function(e){return void 0===e&&(e={}),B(i.SUB_WINDOW_STATUS.SUBMIT,e)}(t)},close:function(){return F(),r.isInClient()?function(){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return!1!==E()?[3,2]:(t=c.getMST())?[4,Z({mst:t,status:i.SUB_WINDOW_STATUS.CLOSE,result:{}})]:[2,Promise.reject(s.createLiffError(i.UNAUTHORIZED,"mst is invalid"))];case 1:e.sent(),e.label=2;case 2:return d.closeWindow(),[2]}}))}))}():function(){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){return(null==(t=b())?void 0:t.isReady())?(t.send({eventName:i.SUB_WINDOW_STATUS.CLOSE}),[2,new Promise((function(e){setTimeout((function(){d.closeWindow(),e()}),i.SUB_WINDOW_HEALTH_CHECK_INTERVAL)}))]):(d.closeWindow(),[2,Promise.resolve()])}))}))}()},getAppData:function(){return F(),function(){var e,t=c.getAppData();try{e=t?JSON.parse(t):{}}catch(r){e={}}return Promise.resolve(e)}()}},X=function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return e.__extends(r,t),Object.defineProperty(r.prototype,"name",{get:function(){return"subWindow"},enumerable:!1,configurable:!0}),r.prototype.install=function(){return Y},r}(t.LiffModule);exports.SubWindowModule=X,exports.getAppData=function(e){var t=e.mst;return t?u.fetch(u.getEndPoint("subWindowGetAppData"),{method:"POST",body:JSON.stringify({mst:t})}):Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"no proper argument"))},exports.getMSTByMSIT=function(e){var t=e.msit,r=e.mstVerifier;return t&&r?u.fetch(u.getEndPoint("subWindowGetMSTByMSIT"),{method:"POST",body:JSON.stringify({msit:t,mstVerifier:r})}):Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"no proper argument"))},exports.getMainWindowOrigin=C,exports.getMessageBus=b,exports.initMessageBus=A,exports.setMainWindowOrigin=function(e){y.setItem("mainWindowOrigin",e)},exports.subWindow=Y; |
@@ -1,1 +0,1 @@ | ||
import{__awaiter as e,__generator as t,__read as n,__spreadArray as r,__extends as i}from"tslib";import{LiffModule as o}from"@liff/use";import{isInClient as s}from"@liff/is-in-client";import{validators as u}from"@liff/is-api-available";import{STORE_KEY as a,INVALID_CONFIG as c,EXCEPTION_IN_SUBWINDOW as f,SUB_WINDOW_STATUS as l,CREATE_SUBWINDOW_FAILED as d,SUB_WINDOW_IDNTIFICATION_KEY as m,INVALID_ARGUMENT as v,SUB_WINDOW_HEALTH_CHECK_MESSAGE as p,SUB_WINDOW_HEALTH_CHECK_INTERVAL as h,SUB_WINDOW_MONITOR_CLOSE_INTERVAL as w,UNKNOWN as g,UNAUTHORIZED as b,SUB_WINDOW_MODAL_SCHEME_URL as y}from"@liff/consts";import{logger as S}from"@liff/logger";import{createLiffError as I,inMemoryStorage as O,isIpad as P,extractLiffId as C}from"@liff/util";import{getOS as L}from"@liff/get-os";import{getEndPoint as E,fetch as N,requestWithoutErrorHandling as T}from"@liff/server-api";import{getConfig as j,getMSTChallenge as M,getMST as D,getAppData as B}from"@liff/store";import{WINDOW as J,MessageBus as R,IDENTIFIER_KEY as U,CRYPTO_KEY as W}from"@liff/message-bus";import{isSubWindow as A}from"@liff/is-sub-window";import{closeWindow as x}from"@liff/close-window";function k(e){var t=E("subWindowGetOrigin");return N(t(e))}var K={};function G(e,t){e&&K[e]&&K[e].forEach((function(e){e(t)}))}var V,F,_,q,z,H=function(){function e(e){this.storage=e}return e.prototype.getItem=function(e){return this.storage.getItem("".concat(this.getKeyPrefix(),":").concat(e))},e.prototype.setItem=function(e,t){this.storage.setItem("".concat(this.getKeyPrefix(),":").concat(e),t)},e.prototype.removeItem=function(e){this.storage.removeItem("".concat(this.getKeyPrefix(),":").concat(e))},e.prototype.clear=function(){this.storage.clear()},e.prototype.getKeyPrefix=function(){return"".concat(a,":").concat(this.getLiffId())},e.prototype.getLiffId=function(){var e=j().liffId;if(!e)throw I(c,"liffId is necessary for liff.init()");return e},e}(),Q=new H(O);function X(){var e=Q.getItem("subWindowStatusUpdated");return null!==e&&JSON.parse(e)}function Y(e){Q.setItem("subWindowStatusUpdated",String(e))}function Z(e){V=e}function $(){return V}function ee(){return _}function te(){return q}function ne(n){return void 0===n&&(n=J.MAIN),e(this,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return[4,(z=new R(n)).setup()];case 1:return e.sent(),[2,z]}}))}))}function re(){return z}var ie=new H(window.sessionStorage);function oe(e){ie.setItem("mainWindowOrigin",e)}function se(){return ie.getItem("mainWindowOrigin")}function ue(n,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){var e,i,o,s,u,a,c,d;return t(this,(function(t){switch(t.label){case 0:if(null==(e=re())?void 0:e.isReady())return[3,5];if(i=JSON.stringify(r),o=j().liffId,s=se(),!window.opener||!s||!o)throw I(f);u=!1,t.label=1;case 1:return t.trys.push([1,3,,4]),[4,k(o)];case 2:return a=t.sent(),u=a.subwindowCommonModule,[3,4];case 3:throw c=t.sent(),S.debug(c),I(f);case 4:return d=u?s:location.origin,[2,new Promise((function(e){window.addEventListener("message",(function t(r){(function(e){if(e.data&&"string"==typeof e.data.type&&[l.SUBMIT,l.CANCEL].includes(e.data.type))return!0;return!1})(r)&&(window.removeEventListener("message",t),e({status:n,result:i}))})),window.opener.postMessage({status:n,result:i},d)}))];case 5:return e.send({eventName:n,data:r}),[4,new Promise((function(e){setTimeout(e,500)}))];case 6:return t.sent(),[2,{status:n,result:JSON.stringify(r)}]}}))}))}function ae(e){var t,n=te();if(e.origin===n){var r=e.data;if(r){var i,o=r.status,s=r.result;try{i=JSON.parse(s||"{}")}catch(u){i={}}switch(o){case p:window.clearInterval(ee()),de();break;case l.CANCEL:case l.SUBMIT:Y(!0),window.clearInterval(ee()),window.removeEventListener("message",ae),G(o,i),null===(t=$())||void 0===t||t.postMessage({type:o},te());break;default:S.debug("unexpected message")}}}}var ce=function(n){return e(void 0,void 0,void 0,(function(){var e,r,i,o;return t(this,(function(t){if(X())return[2];switch(e=n.context,r=e.eventName,i=e.data,o=re(),r){case l.INIT:le(!i.hasOpener);break;case l.CANCEL:case l.SUBMIT:Y(!0),G(r,i),null==o||o.reply(n,{eventName:r});break;case l.CLOSE:!1===X()&&(Y(!0),G(l.CLOSE,{})),de()}return[2]}))}))};function fe(){window.clearInterval(F),window.clearInterval(ee()),window.removeEventListener("message",ae)}function le(e){if(void 0===e&&(e=!1),fe(),Y(!1),e){var t=$();t&&(t.close(),Z(null))}}function de(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return(e=re())?[4,e.teardown()]:[3,2];case 1:t.sent(),t.label=2;case 2:return[2]}}))}))}function me(i){return e(this,void 0,void 0,(function(){var e,o,s,u,a,c,f,g,b,y;return t(this,(function(t){switch(t.label){case 0:return(e=C(i.url))?(le(!0),[4,de()]):[2,Promise.reject(I(v,"params.url must be liff url"))];case 1:return t.sent(),o=i.url,s=i.appData,(u=new URL(o)).searchParams.append(m,"true"),[4,ne()];case 2:return a=t.sent(),u.searchParams.append(U,a.identification.identifier),u.searchParams.append(W,a.identification.cryptoKey),u.hostname=function(e){var t=n(e.split(".")),i=t[0],o=t.slice(1);return r(["".concat(i,"-ext")],n(o),!1).join(".")}(u.hostname),c=u.toString(),Z("ios"!==L()||P()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),[4,k(e)];case 3:if(f=t.sent(),g=f.origin,b=f.subwindowCommonModule,!(y=$()))throw I(d);return b?(function(e){q=e}(g),a.listen(ce),a.setData("appData",s),window.addEventListener("message",ae),y.location.href=c,S=function(e,t){var n=$(),r={type:p};return t&&(r.message=JSON.stringify(t)),window.setInterval((function(){null==n||n.postMessage(r,e)}),h)}(g,s),_=S,function(e){F=e}(window.setInterval((function(){var e=$();e&&e.closed&&(fe(),Z(null),!1===X()&&(Y(!0),G(l.CLOSE,{})))}),w)),[2]):(y.close(),[2])}var S}))}))}var ve=null;function pe(i){return e(this,void 0,void 0,(function(){var o,s,u,a,c,f,d,m,p,h,w,b,y,S,O=this;return t(this,(function(P){switch(P.label){case 0:if(o=i.msit,s=i.mstChallenge,u=i.reconnectCount,a=void 0===u?0:u,c=function(){return e(O,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return[4,(t=1e3,new Promise((function(e){return setTimeout(e,t)})))];case 1:return e.sent(),[4,pe({msit:o,mstChallenge:s,onSuccess:i.onSuccess,onError:i.onError,reconnectCount:a+1})];case 2:return e.sent(),[2]}var t}))}))},f=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];ve=null,i.onSuccess.apply(i,r([],n(e),!1))},d=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];ve=null,i.onError.apply(i,r([],n(e),!1))},m=Date.now(),null===ve&&(ve=m),p=m-ve,a>=10||p>6e5)return d(I(g,"Failed to connect")),[2];P.label=1;case 1:return P.trys.push([1,3,,5]),[4,T(E("subWindowSubscribe"),{method:"POST",body:JSON.stringify({msit:o,mstChallenge:s})})];case 2:return h=P.sent(),[3,5];case 3:return P.sent(),[4,c()];case 4:return P.sent(),[2];case 5:return h.status>=500?[4,c()]:[3,7];case 6:return P.sent(),[3,17];case 7:return h.status>=400&&500>h.status?[4,he(h)]:[3,9];case 8:return b=P.sent(),w=b?I(v,b.errorDetail):I(g,"Some error happened in the server"),d(w),[3,17];case 9:return 200!==h.status?[3,16]:[4,he(h)];case 10:if(!(b=P.sent()))return d(I(g,"Some error happened in the server")),[2];switch(y=b.status,S=b.result,y){case l.ERROR:return[3,11];case l.CLOSE:case l.CANCEL:case l.SUBMIT:return[3,13]}return[3,14];case 11:return[4,c()];case 12:return P.sent(),[3,15];case 13:return f(y,S),[3,15];case 14:d(I(g,"Some error happened in the server")),P.label=15;case 15:return[3,17];case 16:d(I(g,"Some error happened in the server")),P.label=17;case 17:return[2]}}))}))}function he(n){return e(this,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,n.json()];case 1:return[2,e.sent()];case 2:return e.sent(),[2,null];case 3:return[2]}}))}))}function we(e){var t={};return Object.keys(e).forEach((function(n){"closeButtonColor"===n?"white"===e[n]?t[n]="#ffffff":t[n]="#000000":t[n]=e[n]})),t}var ge={height:"full",closeButtonPosition:"right",closeButtonColor:"black",closeButtonLabel:""};function be(e){var t=e.appData,n=e.native,r=j().liffId,i=M(),o=C(e.url);if(!r)return Promise.reject(I(b,"liffId is invalid"));if(!i)return Promise.reject(I(b,"mst_challenge is invalid"));if(!o)return Promise.reject(I(v,"params.url must be liff url"));var s=Object.assign({},ge,n);return function(e){var t=e.mainLiffId,n=e.subLiffId,r=e.mstChallenge,i=e.appData,o=e.view;return t&&r?N(E("subWindowGetMSIT"),{method:"POST",body:JSON.stringify({mainLiffId:t,subLiffId:n,mstChallenge:r,appData:i,view:o})}):Promise.reject(I(v,"no proper argument"))}({mainLiffId:r,subLiffId:o,mstChallenge:i,appData:t,view:we(s)}).then((function(t){var n=t.msit;pe({msit:n,mstChallenge:i,onSuccess:function(e,t){G(e,t)},onError:function(e){G(l.ERROR,e)}}),function(e,t){var n=e.url,r=new URLSearchParams;r.set("msit",t),location.href="".concat(y,"?url=").concat(encodeURIComponent(n),"&").concat(r.toString())}(e,n)}))}function ye(e){if(!e.mst||!e.status)return Promise.reject(I(v,"no proper argument"));var t=JSON.stringify(e);return N(E("subWindowPost"),{method:"POST",body:t})}function Se(){if(!A())throw I(b,"this api can be only called in child window")}function Ie(e){var t=e.msit,n=e.mstVerifier;return t&&n?N(E("subWindowGetMSTByMSIT"),{method:"POST",body:JSON.stringify({msit:t,mstVerifier:n})}):Promise.reject(I(v,"no proper argument"))}function Oe(e){var t=e.mst;return t?N(E("subWindowGetAppData"),{method:"POST",body:JSON.stringify({mst:t})}):Promise.reject(I(v,"no proper argument"))}var Pe={on:function(e,t){K[e]||(K[e]=[]),K[e].push(t)},off:function(e,t){if(K[e]){var n=K[e].indexOf(t);n>=0&&K[e].splice(n,1)}},open:function(e){return u.subwindowOpen(),s()?be(e):me(e)},cancel:function(n){return void 0===n&&(n={}),Se(),s()?function(n){return void 0===n&&(n={}),e(this,void 0,void 0,(function(){var e,r;return t(this,(function(t){switch(t.label){case 0:return(e=D())?[4,ye({mst:e,status:l.CANCEL,result:n})]:[2,Promise.reject(I(b,"mst is invalid"))];case 1:return r=t.sent(),Y(!0),[2,r]}}))}))}(n):function(e){return void 0===e&&(e={}),ue(l.CANCEL,e)}(n)},submit:function(n){return void 0===n&&(n={}),Se(),s()?function(n){return void 0===n&&(n={}),e(this,void 0,void 0,(function(){var e,r;return t(this,(function(t){switch(t.label){case 0:return(e=D())?[4,ye({mst:e,status:l.SUBMIT,result:n})]:[2,Promise.reject(I(b,"mst is invalid"))];case 1:return r=t.sent(),Y(!0),[2,r]}}))}))}(n):function(e){return void 0===e&&(e={}),ue(l.SUBMIT,e)}(n)},close:function(){return Se(),s()?function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return!1!==X()?[3,2]:(e=D())?[4,ye({mst:e,status:l.CLOSE,result:{}})]:[2,Promise.reject(I(b,"mst is invalid"))];case 1:t.sent(),t.label=2;case 2:return x(),[2]}}))}))}():function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){return(null==(e=re())?void 0:e.isReady())?(e.send({eventName:l.CLOSE}),[2,new Promise((function(e){setTimeout((function(){x(),e()}),h)}))]):(x(),[2,Promise.resolve()])}))}))}()},getAppData:function(){return Se(),function(){var e,t=B();try{e=t?JSON.parse(t):{}}catch(n){e={}}return Promise.resolve(e)}()}},Ce=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),Object.defineProperty(t.prototype,"name",{get:function(){return"subWindow"},enumerable:!1,configurable:!0}),t.prototype.install=function(){return Pe},t}(o);export{Ce as SubWindowModule,Oe as getAppData,Ie as getMSTByMSIT,se as getMainWindowOrigin,re as getMessageBus,ne as initMessageBus,oe as setMainWindowOrigin,Pe as subWindow}; | ||
import{__awaiter as e,__generator as t,__read as n,__spreadArray as r,__extends as i}from"tslib";import{LiffModule as o}from"@liff/use";import{isInClient as s}from"@liff/is-in-client";import{validators as u}from"@liff/is-api-available";import{STORE_KEY as a,INVALID_CONFIG as c,EXCEPTION_IN_SUBWINDOW as f,SUB_WINDOW_STATUS as l,CREATE_SUBWINDOW_FAILED as d,SUB_WINDOW_IDNTIFICATION_KEY as m,INVALID_ARGUMENT as v,SUB_WINDOW_HEALTH_CHECK_MESSAGE as p,SUB_WINDOW_HEALTH_CHECK_INTERVAL as h,SUB_WINDOW_MONITOR_CLOSE_INTERVAL as w,UNKNOWN as g,UNAUTHORIZED as b,SUB_WINDOW_MODAL_SCHEME_URL as y}from"@liff/consts";import{logger as S}from"@liff/logger";import{createLiffError as I,InMemoryStorage as O,isNonBrowserEnvironment as P,isIpad as C,extractLiffId as L}from"@liff/util";import{getOS as E}from"@liff/get-os";import{getEndPoint as N,fetch as T,requestWithoutErrorHandling as j}from"@liff/server-api";import{getConfig as M,getMSTChallenge as D,getMST as B,getAppData as J}from"@liff/store";import{WINDOW as R,MessageBus as U,IDENTIFIER_KEY as W,CRYPTO_KEY as A}from"@liff/message-bus";import{isSubWindow as x}from"@liff/is-sub-window";import{closeWindow as k}from"@liff/close-window";function K(e){var t=N("subWindowGetOrigin");return T(t(e))}var G={};function V(e,t){e&&G[e]&&G[e].forEach((function(e){e(t)}))}var F,_,q,z,H,Q=function(){function e(e){this.storage=e}return e.prototype.getItem=function(e){return this.storage.getItem("".concat(this.getKeyPrefix(),":").concat(e))},e.prototype.setItem=function(e,t){this.storage.setItem("".concat(this.getKeyPrefix(),":").concat(e),t)},e.prototype.removeItem=function(e){this.storage.removeItem("".concat(this.getKeyPrefix(),":").concat(e))},e.prototype.clear=function(){this.storage.clear()},e.prototype.getKeyPrefix=function(){return"".concat(a,":").concat(this.getLiffId())},e.prototype.getLiffId=function(){var e=M().liffId;if(!e)throw I(c,"liffId is necessary for liff.init()");return e},e}(),X=new Q(new O);function Y(){var e=X.getItem("subWindowStatusUpdated");return null!==e&&JSON.parse(e)}function Z(e){X.setItem("subWindowStatusUpdated",String(e))}function $(e){F=e}function ee(){return F}function te(){return q}function ne(){return z}function re(n){return void 0===n&&(n=R.MAIN),e(this,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return[4,(H=new U(n)).setup()];case 1:return e.sent(),[2,H]}}))}))}function ie(){return H}var oe=new O,se=new Q(P()?oe:window.sessionStorage);function ue(e){se.setItem("mainWindowOrigin",e)}function ae(){return se.getItem("mainWindowOrigin")}function ce(n,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){var e,i,o,s,u,a,c,d;return t(this,(function(t){switch(t.label){case 0:if(null==(e=ie())?void 0:e.isReady())return[3,5];if(i=JSON.stringify(r),o=M().liffId,s=ae(),!window.opener||!s||!o)throw I(f);u=!1,t.label=1;case 1:return t.trys.push([1,3,,4]),[4,K(o)];case 2:return a=t.sent(),u=a.subwindowCommonModule,[3,4];case 3:throw c=t.sent(),S.debug(c),I(f);case 4:return d=u?s:location.origin,[2,new Promise((function(e){window.addEventListener("message",(function t(r){(function(e){if(e.data&&"string"==typeof e.data.type&&[l.SUBMIT,l.CANCEL].includes(e.data.type))return!0;return!1})(r)&&(window.removeEventListener("message",t),e({status:n,result:i}))})),window.opener.postMessage({status:n,result:i},d)}))];case 5:return e.send({eventName:n,data:r}),[4,new Promise((function(e){setTimeout(e,500)}))];case 6:return t.sent(),[2,{status:n,result:JSON.stringify(r)}]}}))}))}function fe(e){var t,n=ne();if(e.origin===n){var r=e.data;if(r){var i,o=r.status,s=r.result;try{i=JSON.parse(s||"{}")}catch(u){i={}}switch(o){case p:window.clearInterval(te()),ve();break;case l.CANCEL:case l.SUBMIT:Z(!0),window.clearInterval(te()),window.removeEventListener("message",fe),V(o,i),null===(t=ee())||void 0===t||t.postMessage({type:o},ne());break;default:S.debug("unexpected message")}}}}var le=function(n){return e(void 0,void 0,void 0,(function(){var e,r,i,o;return t(this,(function(t){if(Y())return[2];switch(e=n.context,r=e.eventName,i=e.data,o=ie(),r){case l.INIT:me(!i.hasOpener);break;case l.CANCEL:case l.SUBMIT:Z(!0),V(r,i),null==o||o.reply(n,{eventName:r});break;case l.CLOSE:!1===Y()&&(Z(!0),V(l.CLOSE,{})),ve()}return[2]}))}))};function de(){window.clearInterval(_),window.clearInterval(te()),window.removeEventListener("message",fe)}function me(e){if(void 0===e&&(e=!1),de(),Z(!1),e){var t=ee();t&&(t.close(),$(null))}}function ve(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return(e=ie())?[4,e.teardown()]:[3,2];case 1:t.sent(),t.label=2;case 2:return[2]}}))}))}function pe(i){return e(this,void 0,void 0,(function(){var e,o,s,u,a,c,f,g,b,y;return t(this,(function(t){switch(t.label){case 0:return(e=L(i.url))?(me(!0),[4,ve()]):[2,Promise.reject(I(v,"params.url must be liff url"))];case 1:return t.sent(),$("ios"!==E()||C()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),o=i.url,s=i.appData,(u=new URL(o)).searchParams.append(m,"true"),[4,re()];case 2:return a=t.sent(),u.searchParams.append(W,a.identification.identifier),u.searchParams.append(A,a.identification.cryptoKey),u.hostname=function(e){var t=n(e.split(".")),i=t[0],o=t.slice(1);return r(["".concat(i,"-ext")],n(o),!1).join(".")}(u.hostname),c=u.toString(),[4,K(e)];case 3:if(f=t.sent(),g=f.origin,b=f.subwindowCommonModule,!(y=ee()))throw I(d);return b?(function(e){z=e}(g),a.listen(le),a.setData("appData",s),window.addEventListener("message",fe),y.location.href=c,S=function(e,t){var n=ee(),r={type:p};return t&&(r.message=JSON.stringify(t)),window.setInterval((function(){null==n||n.postMessage(r,e)}),h)}(g,s),q=S,function(e){_=e}(window.setInterval((function(){var e=ee();e&&e.closed&&(de(),$(null),!1===Y()&&(Z(!0),V(l.CLOSE,{})))}),w)),[2]):(y.close(),[2])}var S}))}))}var he=null;function we(i){return e(this,void 0,void 0,(function(){var o,s,u,a,c,f,d,m,p,h,w,b,y,S,O=this;return t(this,(function(P){switch(P.label){case 0:if(o=i.msit,s=i.mstChallenge,u=i.reconnectCount,a=void 0===u?0:u,c=function(){return e(O,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return[4,(t=1e3,new Promise((function(e){return setTimeout(e,t)})))];case 1:return e.sent(),[4,we({msit:o,mstChallenge:s,onSuccess:i.onSuccess,onError:i.onError,reconnectCount:a+1})];case 2:return e.sent(),[2]}var t}))}))},f=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];he=null,i.onSuccess.apply(i,r([],n(e),!1))},d=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];he=null,i.onError.apply(i,r([],n(e),!1))},m=Date.now(),null===he&&(he=m),p=m-he,a>=10||p>6e5)return d(I(g,"Failed to connect")),[2];P.label=1;case 1:return P.trys.push([1,3,,5]),[4,j(N("subWindowSubscribe"),{method:"POST",body:JSON.stringify({msit:o,mstChallenge:s})})];case 2:return h=P.sent(),[3,5];case 3:return P.sent(),[4,c()];case 4:return P.sent(),[2];case 5:return h.status>=500?[4,c()]:[3,7];case 6:return P.sent(),[3,17];case 7:return h.status>=400&&500>h.status?[4,ge(h)]:[3,9];case 8:return b=P.sent(),w=b?I(v,b.errorDetail):I(g,"Some error happened in the server"),d(w),[3,17];case 9:return 200!==h.status?[3,16]:[4,ge(h)];case 10:if(!(b=P.sent()))return d(I(g,"Some error happened in the server")),[2];switch(y=b.status,S=b.result,y){case l.ERROR:return[3,11];case l.CLOSE:case l.CANCEL:case l.SUBMIT:return[3,13]}return[3,14];case 11:return[4,c()];case 12:return P.sent(),[3,15];case 13:return f(y,S),[3,15];case 14:d(I(g,"Some error happened in the server")),P.label=15;case 15:return[3,17];case 16:d(I(g,"Some error happened in the server")),P.label=17;case 17:return[2]}}))}))}function ge(n){return e(this,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,n.json()];case 1:return[2,e.sent()];case 2:return e.sent(),[2,null];case 3:return[2]}}))}))}function be(e){var t={};return Object.keys(e).forEach((function(n){"closeButtonColor"===n?"white"===e[n]?t[n]="#ffffff":t[n]="#000000":t[n]=e[n]})),t}var ye={height:"full",closeButtonPosition:"right",closeButtonColor:"black",closeButtonLabel:""};function Se(e){var t=e.appData,n=e.native,r=M().liffId,i=D(),o=L(e.url);if(!r)return Promise.reject(I(b,"liffId is invalid"));if(!i)return Promise.reject(I(b,"mst_challenge is invalid"));if(!o)return Promise.reject(I(v,"params.url must be liff url"));var s=Object.assign({},ye,n);return function(e){var t=e.mainLiffId,n=e.subLiffId,r=e.mstChallenge,i=e.appData,o=e.view;return t&&r?T(N("subWindowGetMSIT"),{method:"POST",body:JSON.stringify({mainLiffId:t,subLiffId:n,mstChallenge:r,appData:i,view:o})}):Promise.reject(I(v,"no proper argument"))}({mainLiffId:r,subLiffId:o,mstChallenge:i,appData:t,view:be(s)}).then((function(t){var n=t.msit;we({msit:n,mstChallenge:i,onSuccess:function(e,t){V(e,t)},onError:function(e){V(l.ERROR,e)}}),function(e,t){var n=e.url,r=new URLSearchParams;r.set("msit",t),location.href="".concat(y,"?url=").concat(encodeURIComponent(n),"&").concat(r.toString())}(e,n)}))}function Ie(e){if(!e.mst||!e.status)return Promise.reject(I(v,"no proper argument"));var t=JSON.stringify(e);return T(N("subWindowPost"),{method:"POST",body:t})}function Oe(){if(!x())throw I(b,"this api can be only called in child window")}function Pe(e){var t=e.msit,n=e.mstVerifier;return t&&n?T(N("subWindowGetMSTByMSIT"),{method:"POST",body:JSON.stringify({msit:t,mstVerifier:n})}):Promise.reject(I(v,"no proper argument"))}function Ce(e){var t=e.mst;return t?T(N("subWindowGetAppData"),{method:"POST",body:JSON.stringify({mst:t})}):Promise.reject(I(v,"no proper argument"))}var Le={on:function(e,t){G[e]||(G[e]=[]),G[e].push(t)},off:function(e,t){if(G[e]){var n=G[e].indexOf(t);n>=0&&G[e].splice(n,1)}},open:function(e){return u.subwindowOpen(),s()?Se(e):pe(e)},cancel:function(n){return void 0===n&&(n={}),Oe(),s()?function(n){return void 0===n&&(n={}),e(this,void 0,void 0,(function(){var e,r;return t(this,(function(t){switch(t.label){case 0:return(e=B())?[4,Ie({mst:e,status:l.CANCEL,result:n})]:[2,Promise.reject(I(b,"mst is invalid"))];case 1:return r=t.sent(),Z(!0),[2,r]}}))}))}(n):function(e){return void 0===e&&(e={}),ce(l.CANCEL,e)}(n)},submit:function(n){return void 0===n&&(n={}),Oe(),s()?function(n){return void 0===n&&(n={}),e(this,void 0,void 0,(function(){var e,r;return t(this,(function(t){switch(t.label){case 0:return(e=B())?[4,Ie({mst:e,status:l.SUBMIT,result:n})]:[2,Promise.reject(I(b,"mst is invalid"))];case 1:return r=t.sent(),Z(!0),[2,r]}}))}))}(n):function(e){return void 0===e&&(e={}),ce(l.SUBMIT,e)}(n)},close:function(){return Oe(),s()?function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return!1!==Y()?[3,2]:(e=B())?[4,Ie({mst:e,status:l.CLOSE,result:{}})]:[2,Promise.reject(I(b,"mst is invalid"))];case 1:t.sent(),t.label=2;case 2:return k(),[2]}}))}))}():function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){return(null==(e=ie())?void 0:e.isReady())?(e.send({eventName:l.CLOSE}),[2,new Promise((function(e){setTimeout((function(){k(),e()}),h)}))]):(k(),[2,Promise.resolve()])}))}))}()},getAppData:function(){return Oe(),function(){var e,t=J();try{e=t?JSON.parse(t):{}}catch(n){e={}}return Promise.resolve(e)}()}},Ee=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),Object.defineProperty(t.prototype,"name",{get:function(){return"subWindow"},enumerable:!1,configurable:!0}),t.prototype.install=function(){return Le},t}(o);export{Ee as SubWindowModule,Ce as getAppData,Pe as getMSTByMSIT,ae as getMainWindowOrigin,ie as getMessageBus,re as initMessageBus,ue as setMainWindowOrigin,Le as subWindow}; |
{ | ||
"name": "@liff/sub-window", | ||
"version": "2.22.1", | ||
"version": "2.22.2", | ||
"description": "@liff/sub-window", | ||
@@ -34,15 +34,15 @@ "license": "SEE LICENSE IN README.md", | ||
"dependencies": { | ||
"@liff/close-window": "2.22.1", | ||
"@liff/consts": "2.22.1", | ||
"@liff/get-os": "2.22.1", | ||
"@liff/is-api-available": "2.22.1", | ||
"@liff/is-in-client": "2.22.1", | ||
"@liff/is-sub-window": "2.22.1", | ||
"@liff/logger": "2.22.1", | ||
"@liff/message-bus": "2.22.1", | ||
"@liff/server-api": "2.22.1", | ||
"@liff/store": "2.22.1", | ||
"@liff/use": "2.22.1", | ||
"@liff/util": "2.22.1" | ||
"@liff/close-window": "2.22.2", | ||
"@liff/consts": "2.22.2", | ||
"@liff/get-os": "2.22.2", | ||
"@liff/is-api-available": "2.22.2", | ||
"@liff/is-in-client": "2.22.2", | ||
"@liff/is-sub-window": "2.22.2", | ||
"@liff/logger": "2.22.2", | ||
"@liff/message-bus": "2.22.2", | ||
"@liff/server-api": "2.22.2", | ||
"@liff/store": "2.22.2", | ||
"@liff/use": "2.22.2", | ||
"@liff/util": "2.22.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34504
+ Added@liff/close-window@2.22.2(transitive)
+ Added@liff/consts@2.22.2(transitive)
+ Added@liff/get-line-version@2.22.2(transitive)
+ Added@liff/get-os@2.22.2(transitive)
+ Added@liff/hooks@2.22.2(transitive)
+ Added@liff/is-api-available@2.22.2(transitive)
+ Added@liff/is-in-client@2.22.2(transitive)
+ Added@liff/is-logged-in@2.22.2(transitive)
+ Added@liff/is-sub-window@2.22.2(transitive)
+ Added@liff/logger@2.22.2(transitive)
+ Added@liff/message-bus@2.22.2(transitive)
+ Added@liff/native-bridge@2.22.2(transitive)
+ Added@liff/server-api@2.22.2(transitive)
+ Added@liff/store@2.22.2(transitive)
+ Added@liff/types@2.22.2(transitive)
+ Added@liff/use@2.22.2(transitive)
+ Added@liff/util@2.22.2(transitive)
- Removed@liff/close-window@2.22.1(transitive)
- Removed@liff/consts@2.22.1(transitive)
- Removed@liff/get-line-version@2.22.1(transitive)
- Removed@liff/get-os@2.22.1(transitive)
- Removed@liff/hooks@2.22.1(transitive)
- Removed@liff/is-api-available@2.22.1(transitive)
- Removed@liff/is-in-client@2.22.1(transitive)
- Removed@liff/is-logged-in@2.22.1(transitive)
- Removed@liff/is-sub-window@2.22.1(transitive)
- Removed@liff/logger@2.22.1(transitive)
- Removed@liff/message-bus@2.22.1(transitive)
- Removed@liff/native-bridge@2.22.1(transitive)
- Removed@liff/server-api@2.22.1(transitive)
- Removed@liff/store@2.22.1(transitive)
- Removed@liff/types@2.22.1(transitive)
- Removed@liff/use@2.22.1(transitive)
- Removed@liff/util@2.22.1(transitive)
Updated@liff/close-window@2.22.2
Updated@liff/consts@2.22.2
Updated@liff/get-os@2.22.2
Updated@liff/is-in-client@2.22.2
Updated@liff/is-sub-window@2.22.2
Updated@liff/logger@2.22.2
Updated@liff/message-bus@2.22.2
Updated@liff/server-api@2.22.2
Updated@liff/store@2.22.2
Updated@liff/use@2.22.2
Updated@liff/util@2.22.2