@liff/sub-window
Advanced tools
Comparing version 2.22.2 to 2.22.3
@@ -32,2 +32,3 @@ import { SUB_WINDOW_STATUS, SUB_WINDOW_HEALTH_CHECK_MESSAGE } from '@liff/consts'; | ||
native?: ExternalNativeType; | ||
onBeforeTransition?: () => Promise<void>; | ||
} | ||
@@ -34,0 +35,0 @@ export interface MSITResponse extends Base { |
@@ -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(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; | ||
"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 v(e,t){e&&S[e]&&S[e].forEach((function(e){e(t)}))}function g(e,t){S[e]||(S[e]=[]),S[e].push(t)}function h(e,t){if(S[e]){var r=S[e].indexOf(t);r>=0&&S[e].splice(r,1)}}var I,p,w,m,N,E=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}(),T=new E(new s.InMemoryStorage);function O(){var e=T.getItem("subWindowStatusUpdated");return null!==e&&JSON.parse(e)}function W(e){T.setItem("subWindowStatusUpdated",String(e))}function U(e){I=e}function b(){return I}function L(){return w}function A(){return m}function D(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,(N=new f.MessageBus(t)).setup()];case 1:return e.sent(),[2,N]}}))}))}function y(){return N}var C=new s.InMemoryStorage,B=new E(s.isNonBrowserEnvironment()?C:window.sessionStorage);function M(){return B.getItem("mainWindowOrigin")}function P(t,r){return void 0===r&&(r={}),e.__awaiter(this,void 0,void 0,(function(){var n,a,u,f,l,d,S,v;return e.__generator(this,(function(e){switch(e.label){case 0:if(null==(n=y())?void 0:n.isReady())return[3,5];if(a=JSON.stringify(r),u=c.getConfig().liffId,f=M(),!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 v=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},v)}))];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 R(e){var t,r=A();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(L()),j();break;case i.SUB_WINDOW_STATUS.CANCEL:case i.SUB_WINDOW_STATUS.SUBMIT:W(!0),window.clearInterval(L()),window.removeEventListener("message",R),v(a,s),null===(t=b())||void 0===t||t.postMessage({type:a},A());break;default:o.logger.debug("unexpected message")}}}}var x=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var r,n,o,s;return e.__generator(this,(function(e){if(O())return[2];switch(r=t.context,n=r.eventName,o=r.data,s=y(),n){case i.SUB_WINDOW_STATUS.INIT:K(!o.hasOpener);break;case i.SUB_WINDOW_STATUS.CANCEL:case i.SUB_WINDOW_STATUS.SUBMIT:W(!0),v(n,o),null==s||s.reply(t,{eventName:n});break;case i.SUB_WINDOW_STATUS.CLOSE:!1===O()&&(W(!0),v(i.SUB_WINDOW_STATUS.CLOSE,{})),j()}return[2]}))}))};function H(){window.clearInterval(p),window.clearInterval(L()),window.removeEventListener("message",R)}function K(e){if(void 0===e&&(e=!1),H(),W(!1),e){var t=b();t&&(t.close(),U(null))}}function j(){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return(t=y())?[4,t.teardown()]:[3,2];case 1:e.sent(),e.label=2;case 2:return[2]}}))}))}function q(t){var r;return e.__awaiter(this,void 0,void 0,(function(){var n,o,u,c,l,d,S,g,h,I,N;return e.__generator(this,(function(E){switch(E.label){case 0:return(n=s.extractLiffId(t.url))?(K(!0),[4,j()]):[2,Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"params.url must be liff url"))];case 1:return E.sent(),U("ios"!==a.getOS()||s.isIpad()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),o=t.url,u=t.appData,(c=new URL(o)).searchParams.append(i.SUB_WINDOW_IDNTIFICATION_KEY,"true"),[4,D()];case 2:return l=E.sent(),c.searchParams.append(f.IDENTIFIER_KEY,l.identification.identifier),c.searchParams.append(f.CRYPTO_KEY,l.identification.cryptoKey),c.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(".")}(c.hostname),d=c.toString(),[4,_(n)];case 3:if(S=E.sent(),g=S.origin,h=S.subwindowCommonModule,!(I=b()))throw s.createLiffError(i.CREATE_SUBWINDOW_FAILED);if(!h)return I.close(),[2];!function(e){m=e}(g),l.listen(x),l.setData("appData",u),window.addEventListener("message",R),E.label=4;case 4:return E.trys.push([4,6,,7]),[4,null===(r=t.onBeforeTransition)||void 0===r?void 0:r.call(t)];case 5:return E.sent(),[3,7];case 6:throw N=E.sent(),I.close(),N;case 7:return I.location.href=d,T=function(e,t){var r=b(),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)}(g,u),w=T,function(e){p=e}(window.setInterval((function(){var e=b();e&&e.closed&&(H(),U(null),!1===O()&&(W(!0),v(i.SUB_WINDOW_STATUS.CLOSE,{})))}),i.SUB_WINDOW_MONITOR_CLOSE_INTERVAL)),[2]}var T}))}))}var G=null;function V(t){return e.__awaiter(this,void 0,void 0,(function(){var r,n,o,a,c,f,l,d,_,S,v,g,h,I,p=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(p,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,V({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];G=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];G=null,t.onError.apply(t,e.__spreadArray([],e.__read(r),!1))},d=Date.now(),null===G&&(G=d),_=d-G,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,J(S)]:[3,9];case 8:return g=w.sent(),v=g?s.createLiffError(i.INVALID_ARGUMENT,g.errorDetail):s.createLiffError(i.UNKNOWN,"Some error happened in the server"),l(v),[3,17];case 9:return 200!==S.status?[3,16]:[4,J(S)];case 10:if(!(g=w.sent()))return l(s.createLiffError(i.UNKNOWN,"Some error happened in the server")),[2];switch(h=g.status,I=g.result,h){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(h,I),[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 J(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 k(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 Z={height:"full",closeButtonPosition:"right",closeButtonColor:"black",closeButtonLabel:""};function F(t){var r=t.appData,n=t.native,o=c.getConfig().liffId,a=c.getMSTChallenge(),f=s.extractLiffId(t.url);if(!o)return Promise.reject(s.createLiffError(i.UNAUTHORIZED,"liffId is invalid"));if(!a)return Promise.reject(s.createLiffError(i.UNAUTHORIZED,"mst_challenge is invalid"));if(!f)return Promise.reject(s.createLiffError(i.INVALID_ARGUMENT,"params.url must be liff url"));var l=Object.assign({},Z,n);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:o,subLiffId:f,mstChallenge:a,appData:r,view:k(l)}).then((function(e){var t=e.msit;return V({msit:t,mstChallenge:a,onSuccess:function(e,t){v(e,t)},onError:function(e){v(i.SUB_WINDOW_STATUS.ERROR,e)}}),t})).then((function(r){return function(t,r){var n;return e.__awaiter(this,void 0,void 0,(function(){var o,s;return e.__generator(this,(function(e){switch(e.label){case 0:return o=t.url,(s=new URLSearchParams).set("msit",r),[4,null===(n=t.onBeforeTransition)||void 0===n?void 0:n.call(t)];case 1:return e.sent(),location.href="".concat(i.SUB_WINDOW_MODAL_SCHEME_URL,"?url=").concat(encodeURIComponent(o),"&").concat(s.toString()),[2]}}))}))}(t,r)}))}function Y(e){return n.validators.subwindowOpen(),r.isInClient()?F(e):q(e)}function X(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")}function Q(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,X({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(),W(!0),[2,n]}}))}))}(t):function(e){return void 0===e&&(e={}),P(i.SUB_WINDOW_STATUS.CANCEL,e)}(t)}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,X({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(),W(!0),[2,n]}}))}))}(t):function(e){return void 0===e&&(e={}),P(i.SUB_WINDOW_STATUS.SUBMIT,e)}(t)}function ee(){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!==O()?[3,2]:(t=c.getMST())?[4,X({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=y())?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()])}))}))}()}function te(){return z(),function(){var e,t=c.getAppData();try{e=t?JSON.parse(t):{}}catch(r){e={}}return Promise.resolve(e)}()}var re={on:g,off:h,open:Y,cancel:Q,submit:$,close:ee,getAppData:te},ne=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(){var t=this;return{on:g.bind(this),off:h.bind(this),open:function(r){return Y.call(t,e.__assign(e.__assign({},r),{onBeforeTransition:void 0}))},cancel:Q.bind(this),submit:$.bind(this),close:ee.bind(this),getAppData:te.bind(this)}},r}(t.LiffModule);exports.SubWindowModule=ne,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=M,exports.getMessageBus=y,exports.initMessageBus=D,exports.setMainWindowOrigin=function(e){B.setItem("mainWindowOrigin",e)},exports.subWindow=re; |
@@ -8,2 +8,3 @@ import { LiffModule } from '@liff/use'; | ||
import { on, off } from './shared/events'; | ||
import { AppData, EventType, MSITResponse, OpenModalParams, PostResponse, SubWindowMessage } from './def'; | ||
export { getMSTByMSIT } from './server/getMSTByMSIT'; | ||
@@ -21,14 +22,14 @@ export { getAppData } from './server/getAppData'; | ||
}; | ||
type Api = typeof subWindow; | ||
type Api = { | ||
on: (eventName: EventType, fn: Function) => void; | ||
off: (eventName: EventType, fn: Function) => void; | ||
open: (params: Omit<OpenModalParams, 'onBeforeTransition'>) => Promise<MSITResponse | void>; | ||
cancel: (message?: SubWindowMessage) => Promise<PostResponse>; | ||
submit: (message?: SubWindowMessage) => Promise<PostResponse>; | ||
close: () => Promise<void>; | ||
getAppData: () => Promise<AppData>; | ||
}; | ||
export declare class SubWindowModule extends LiffModule<Api, never, {}> { | ||
get name(): string; | ||
install(): { | ||
on: typeof on; | ||
off: typeof off; | ||
open: typeof open; | ||
cancel: typeof cancel; | ||
submit: typeof submit; | ||
close: typeof close; | ||
getAppData: typeof getAppData; | ||
}; | ||
install(): Api; | ||
} |
@@ -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,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}; | ||
import{__awaiter as t,__generator as e,__read as n,__spreadArray as r,__extends as i,__assign as o}from"tslib";import{LiffModule as s}from"@liff/use";import{isInClient as a}from"@liff/is-in-client";import{validators as u}from"@liff/is-api-available";import{STORE_KEY as c,INVALID_CONFIG as f,EXCEPTION_IN_SUBWINDOW as l,SUB_WINDOW_STATUS as d,CREATE_SUBWINDOW_FAILED as m,SUB_WINDOW_IDNTIFICATION_KEY as v,INVALID_ARGUMENT as p,SUB_WINDOW_HEALTH_CHECK_MESSAGE as h,SUB_WINDOW_HEALTH_CHECK_INTERVAL as w,SUB_WINDOW_MONITOR_CLOSE_INTERVAL as g,UNKNOWN as b,UNAUTHORIZED as y,SUB_WINDOW_MODAL_SCHEME_URL as S}from"@liff/consts";import{logger as I}from"@liff/logger";import{createLiffError as O,InMemoryStorage as P,isNonBrowserEnvironment as C,isIpad as L,extractLiffId as E}from"@liff/util";import{getOS as N}from"@liff/get-os";import{getEndPoint as T,fetch as j,requestWithoutErrorHandling as M}from"@liff/server-api";import{getConfig as B,getMSTChallenge as D,getMST as A,getAppData as J}from"@liff/store";import{WINDOW as R,MessageBus as U,IDENTIFIER_KEY as W,CRYPTO_KEY as x}from"@liff/message-bus";import{isSubWindow as k}from"@liff/is-sub-window";import{closeWindow as K}from"@liff/close-window";function G(t){var e=T("subWindowGetOrigin");return j(e(t))}var V={};function F(t,e){t&&V[t]&&V[t].forEach((function(t){t(e)}))}function _(t,e){V[t]||(V[t]=[]),V[t].push(e)}function q(t,e){if(V[t]){var n=V[t].indexOf(e);n>=0&&V[t].splice(n,1)}}var z,H,Q,X,Y,Z=function(){function t(t){this.storage=t}return t.prototype.getItem=function(t){return this.storage.getItem("".concat(this.getKeyPrefix(),":").concat(t))},t.prototype.setItem=function(t,e){this.storage.setItem("".concat(this.getKeyPrefix(),":").concat(t),e)},t.prototype.removeItem=function(t){this.storage.removeItem("".concat(this.getKeyPrefix(),":").concat(t))},t.prototype.clear=function(){this.storage.clear()},t.prototype.getKeyPrefix=function(){return"".concat(c,":").concat(this.getLiffId())},t.prototype.getLiffId=function(){var t=B().liffId;if(!t)throw O(f,"liffId is necessary for liff.init()");return t},t}(),$=new Z(new P);function tt(){var t=$.getItem("subWindowStatusUpdated");return null!==t&&JSON.parse(t)}function et(t){$.setItem("subWindowStatusUpdated",String(t))}function nt(t){z=t}function rt(){return z}function it(){return Q}function ot(){return X}function st(n){return void 0===n&&(n=R.MAIN),t(this,void 0,void 0,(function(){return e(this,(function(t){switch(t.label){case 0:return[4,(Y=new U(n)).setup()];case 1:return t.sent(),[2,Y]}}))}))}function at(){return Y}var ut=new P,ct=new Z(C()?ut:window.sessionStorage);function ft(t){ct.setItem("mainWindowOrigin",t)}function lt(){return ct.getItem("mainWindowOrigin")}function dt(n,r){return void 0===r&&(r={}),t(this,void 0,void 0,(function(){var t,i,o,s,a,u,c,f;return e(this,(function(e){switch(e.label){case 0:if(null==(t=at())?void 0:t.isReady())return[3,5];if(i=JSON.stringify(r),o=B().liffId,s=lt(),!window.opener||!s||!o)throw O(l);a=!1,e.label=1;case 1:return e.trys.push([1,3,,4]),[4,G(o)];case 2:return u=e.sent(),a=u.subwindowCommonModule,[3,4];case 3:throw c=e.sent(),I.debug(c),O(l);case 4:return f=a?s:location.origin,[2,new Promise((function(t){window.addEventListener("message",(function e(r){(function(t){if(t.data&&"string"==typeof t.data.type&&[d.SUBMIT,d.CANCEL].includes(t.data.type))return!0;return!1})(r)&&(window.removeEventListener("message",e),t({status:n,result:i}))})),window.opener.postMessage({status:n,result:i},f)}))];case 5:return t.send({eventName:n,data:r}),[4,new Promise((function(t){setTimeout(t,500)}))];case 6:return e.sent(),[2,{status:n,result:JSON.stringify(r)}]}}))}))}function mt(t){var e,n=ot();if(t.origin===n){var r=t.data;if(r){var i,o=r.status,s=r.result;try{i=JSON.parse(s||"{}")}catch(a){i={}}switch(o){case h:window.clearInterval(it()),wt();break;case d.CANCEL:case d.SUBMIT:et(!0),window.clearInterval(it()),window.removeEventListener("message",mt),F(o,i),null===(e=rt())||void 0===e||e.postMessage({type:o},ot());break;default:I.debug("unexpected message")}}}}var vt=function(n){return t(void 0,void 0,void 0,(function(){var t,r,i,o;return e(this,(function(e){if(tt())return[2];switch(t=n.context,r=t.eventName,i=t.data,o=at(),r){case d.INIT:ht(!i.hasOpener);break;case d.CANCEL:case d.SUBMIT:et(!0),F(r,i),null==o||o.reply(n,{eventName:r});break;case d.CLOSE:!1===tt()&&(et(!0),F(d.CLOSE,{})),wt()}return[2]}))}))};function pt(){window.clearInterval(H),window.clearInterval(it()),window.removeEventListener("message",mt)}function ht(t){if(void 0===t&&(t=!1),pt(),et(!1),t){var e=rt();e&&(e.close(),nt(null))}}function wt(){return t(this,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return(t=at())?[4,t.teardown()]:[3,2];case 1:e.sent(),e.label=2;case 2:return[2]}}))}))}function gt(i){var o;return t(this,void 0,void 0,(function(){var t,s,a,u,c,f,l,b,y,S,I;return e(this,(function(e){switch(e.label){case 0:return(t=E(i.url))?(ht(!0),[4,wt()]):[2,Promise.reject(O(p,"params.url must be liff url"))];case 1:return e.sent(),nt("ios"!==N()||L()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),s=i.url,a=i.appData,(u=new URL(s)).searchParams.append(v,"true"),[4,st()];case 2:return c=e.sent(),u.searchParams.append(W,c.identification.identifier),u.searchParams.append(x,c.identification.cryptoKey),u.hostname=function(t){var e=n(t.split(".")),i=e[0],o=e.slice(1);return r(["".concat(i,"-ext")],n(o),!1).join(".")}(u.hostname),f=u.toString(),[4,G(t)];case 3:if(l=e.sent(),b=l.origin,y=l.subwindowCommonModule,!(S=rt()))throw O(m);if(!y)return S.close(),[2];!function(t){X=t}(b),c.listen(vt),c.setData("appData",a),window.addEventListener("message",mt),e.label=4;case 4:return e.trys.push([4,6,,7]),[4,null===(o=i.onBeforeTransition)||void 0===o?void 0:o.call(i)];case 5:return e.sent(),[3,7];case 6:throw I=e.sent(),S.close(),I;case 7:return S.location.href=f,P=function(t,e){var n=rt(),r={type:h};return e&&(r.message=JSON.stringify(e)),window.setInterval((function(){null==n||n.postMessage(r,t)}),w)}(b,a),Q=P,function(t){H=t}(window.setInterval((function(){var t=rt();t&&t.closed&&(pt(),nt(null),!1===tt()&&(et(!0),F(d.CLOSE,{})))}),g)),[2]}var P}))}))}var bt=null;function yt(i){return t(this,void 0,void 0,(function(){var o,s,a,u,c,f,l,m,v,h,w,g,y,S,I=this;return e(this,(function(P){switch(P.label){case 0:if(o=i.msit,s=i.mstChallenge,a=i.reconnectCount,u=void 0===a?0:a,c=function(){return t(I,void 0,void 0,(function(){return e(this,(function(t){switch(t.label){case 0:return[4,(e=1e3,new Promise((function(t){return setTimeout(t,e)})))];case 1:return t.sent(),[4,yt({msit:o,mstChallenge:s,onSuccess:i.onSuccess,onError:i.onError,reconnectCount:u+1})];case 2:return t.sent(),[2]}var e}))}))},f=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];bt=null,i.onSuccess.apply(i,r([],n(t),!1))},l=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];bt=null,i.onError.apply(i,r([],n(t),!1))},m=Date.now(),null===bt&&(bt=m),v=m-bt,u>=10||v>6e5)return l(O(b,"Failed to connect")),[2];P.label=1;case 1:return P.trys.push([1,3,,5]),[4,M(T("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,St(h)]:[3,9];case 8:return g=P.sent(),w=g?O(p,g.errorDetail):O(b,"Some error happened in the server"),l(w),[3,17];case 9:return 200!==h.status?[3,16]:[4,St(h)];case 10:if(!(g=P.sent()))return l(O(b,"Some error happened in the server")),[2];switch(y=g.status,S=g.result,y){case d.ERROR:return[3,11];case d.CLOSE:case d.CANCEL:case d.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:l(O(b,"Some error happened in the server")),P.label=15;case 15:return[3,17];case 16:l(O(b,"Some error happened in the server")),P.label=17;case 17:return[2]}}))}))}function St(n){return t(this,void 0,void 0,(function(){return e(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,n.json()];case 1:return[2,t.sent()];case 2:return t.sent(),[2,null];case 3:return[2]}}))}))}function It(t){var e={};return Object.keys(t).forEach((function(n){"closeButtonColor"===n?"white"===t[n]?e[n]="#ffffff":e[n]="#000000":e[n]=t[n]})),e}var Ot={height:"full",closeButtonPosition:"right",closeButtonColor:"black",closeButtonLabel:""};function Pt(n){var r=n.appData,i=n.native,o=B().liffId,s=D(),a=E(n.url);if(!o)return Promise.reject(O(y,"liffId is invalid"));if(!s)return Promise.reject(O(y,"mst_challenge is invalid"));if(!a)return Promise.reject(O(p,"params.url must be liff url"));var u=Object.assign({},Ot,i);return function(t){var e=t.mainLiffId,n=t.subLiffId,r=t.mstChallenge,i=t.appData,o=t.view;return e&&r?j(T("subWindowGetMSIT"),{method:"POST",body:JSON.stringify({mainLiffId:e,subLiffId:n,mstChallenge:r,appData:i,view:o})}):Promise.reject(O(p,"no proper argument"))}({mainLiffId:o,subLiffId:a,mstChallenge:s,appData:r,view:It(u)}).then((function(t){var e=t.msit;return yt({msit:e,mstChallenge:s,onSuccess:function(t,e){F(t,e)},onError:function(t){F(d.ERROR,t)}}),e})).then((function(r){return function(n,r){var i;return t(this,void 0,void 0,(function(){var t,o;return e(this,(function(e){switch(e.label){case 0:return t=n.url,(o=new URLSearchParams).set("msit",r),[4,null===(i=n.onBeforeTransition)||void 0===i?void 0:i.call(n)];case 1:return e.sent(),location.href="".concat(S,"?url=").concat(encodeURIComponent(t),"&").concat(o.toString()),[2]}}))}))}(n,r)}))}function Ct(t){return u.subwindowOpen(),a()?Pt(t):gt(t)}function Lt(t){if(!t.mst||!t.status)return Promise.reject(O(p,"no proper argument"));var e=JSON.stringify(t);return j(T("subWindowPost"),{method:"POST",body:e})}function Et(){if(!k())throw O(y,"this api can be only called in child window")}function Nt(n){return void 0===n&&(n={}),Et(),a()?function(n){return void 0===n&&(n={}),t(this,void 0,void 0,(function(){var t,r;return e(this,(function(e){switch(e.label){case 0:return(t=A())?[4,Lt({mst:t,status:d.CANCEL,result:n})]:[2,Promise.reject(O(y,"mst is invalid"))];case 1:return r=e.sent(),et(!0),[2,r]}}))}))}(n):function(t){return void 0===t&&(t={}),dt(d.CANCEL,t)}(n)}function Tt(n){return void 0===n&&(n={}),Et(),a()?function(n){return void 0===n&&(n={}),t(this,void 0,void 0,(function(){var t,r;return e(this,(function(e){switch(e.label){case 0:return(t=A())?[4,Lt({mst:t,status:d.SUBMIT,result:n})]:[2,Promise.reject(O(y,"mst is invalid"))];case 1:return r=e.sent(),et(!0),[2,r]}}))}))}(n):function(t){return void 0===t&&(t={}),dt(d.SUBMIT,t)}(n)}function jt(){return Et(),a()?function(){return t(this,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return!1!==tt()?[3,2]:(t=A())?[4,Lt({mst:t,status:d.CLOSE,result:{}})]:[2,Promise.reject(O(y,"mst is invalid"))];case 1:e.sent(),e.label=2;case 2:return K(),[2]}}))}))}():function(){return t(this,void 0,void 0,(function(){var t;return e(this,(function(e){return(null==(t=at())?void 0:t.isReady())?(t.send({eventName:d.CLOSE}),[2,new Promise((function(t){setTimeout((function(){K(),t()}),w)}))]):(K(),[2,Promise.resolve()])}))}))}()}function Mt(){return Et(),function(){var t,e=J();try{t=e?JSON.parse(e):{}}catch(n){t={}}return Promise.resolve(t)}()}function Bt(t){var e=t.msit,n=t.mstVerifier;return e&&n?j(T("subWindowGetMSTByMSIT"),{method:"POST",body:JSON.stringify({msit:e,mstVerifier:n})}):Promise.reject(O(p,"no proper argument"))}function Dt(t){var e=t.mst;return e?j(T("subWindowGetAppData"),{method:"POST",body:JSON.stringify({mst:e})}):Promise.reject(O(p,"no proper argument"))}var At={on:_,off:q,open:Ct,cancel:Nt,submit:Tt,close:jt,getAppData:Mt},Jt=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return i(e,t),Object.defineProperty(e.prototype,"name",{get:function(){return"subWindow"},enumerable:!1,configurable:!0}),e.prototype.install=function(){var t=this;return{on:_.bind(this),off:q.bind(this),open:function(e){return Ct.call(t,o(o({},e),{onBeforeTransition:void 0}))},cancel:Nt.bind(this),submit:Tt.bind(this),close:jt.bind(this),getAppData:Mt.bind(this)}},e}(s);export{Jt as SubWindowModule,Dt as getAppData,Bt as getMSTByMSIT,lt as getMainWindowOrigin,at as getMessageBus,st as initMessageBus,ft as setMainWindowOrigin,At as subWindow}; |
{ | ||
"name": "@liff/sub-window", | ||
"version": "2.22.2", | ||
"version": "2.22.3", | ||
"description": "@liff/sub-window", | ||
@@ -34,15 +34,15 @@ "license": "SEE LICENSE IN README.md", | ||
"dependencies": { | ||
"@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" | ||
"@liff/close-window": "2.22.3", | ||
"@liff/consts": "2.22.3", | ||
"@liff/get-os": "2.22.3", | ||
"@liff/is-api-available": "2.22.3", | ||
"@liff/is-in-client": "2.22.3", | ||
"@liff/is-sub-window": "2.22.3", | ||
"@liff/logger": "2.22.3", | ||
"@liff/message-bus": "2.22.3", | ||
"@liff/server-api": "2.22.3", | ||
"@liff/store": "2.22.3", | ||
"@liff/use": "2.22.3", | ||
"@liff/util": "2.22.3" | ||
} | ||
} |
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
36270
299
+ Added@liff/close-window@2.22.3(transitive)
+ Added@liff/consts@2.22.3(transitive)
+ Added@liff/get-line-version@2.22.3(transitive)
+ Added@liff/get-os@2.22.3(transitive)
+ Added@liff/hooks@2.22.3(transitive)
+ Added@liff/is-api-available@2.22.3(transitive)
+ Added@liff/is-in-client@2.22.3(transitive)
+ Added@liff/is-logged-in@2.22.3(transitive)
+ Added@liff/is-sub-window@2.22.3(transitive)
+ Added@liff/logger@2.22.3(transitive)
+ Added@liff/message-bus@2.22.3(transitive)
+ Added@liff/native-bridge@2.22.3(transitive)
+ Added@liff/server-api@2.22.3(transitive)
+ Added@liff/store@2.22.3(transitive)
+ Added@liff/types@2.22.3(transitive)
+ Added@liff/use@2.22.3(transitive)
+ Added@liff/util@2.22.3(transitive)
- Removed@liff/close-window@2.22.2(transitive)
- Removed@liff/consts@2.22.2(transitive)
- Removed@liff/get-line-version@2.22.2(transitive)
- Removed@liff/get-os@2.22.2(transitive)
- Removed@liff/hooks@2.22.2(transitive)
- Removed@liff/is-api-available@2.22.2(transitive)
- Removed@liff/is-in-client@2.22.2(transitive)
- Removed@liff/is-logged-in@2.22.2(transitive)
- Removed@liff/is-sub-window@2.22.2(transitive)
- Removed@liff/logger@2.22.2(transitive)
- Removed@liff/message-bus@2.22.2(transitive)
- Removed@liff/native-bridge@2.22.2(transitive)
- Removed@liff/server-api@2.22.2(transitive)
- Removed@liff/store@2.22.2(transitive)
- Removed@liff/types@2.22.2(transitive)
- Removed@liff/use@2.22.2(transitive)
- Removed@liff/util@2.22.2(transitive)
Updated@liff/close-window@2.22.3
Updated@liff/consts@2.22.3
Updated@liff/get-os@2.22.3
Updated@liff/is-in-client@2.22.3
Updated@liff/is-sub-window@2.22.3
Updated@liff/logger@2.22.3
Updated@liff/message-bus@2.22.3
Updated@liff/server-api@2.22.3
Updated@liff/store@2.22.3
Updated@liff/use@2.22.3
Updated@liff/util@2.22.3