@liff/sub-window
Advanced tools
Comparing version 2.21.0 to 2.21.1
import { EventType, SubWindowMessage, PostResponse } from '../def'; | ||
import MessageBus, { WindowType } from '@liff/message-bus'; | ||
import { MessageBus, WindowType } from '@liff/message-bus'; | ||
export declare const TIMER_FOR_UNEXPECTED_CLOSE = 500; | ||
@@ -4,0 +4,0 @@ export declare function setOpenedWindow(w: Window | null): void; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@liff/consts"),t=require("@liff/util"),r=require("@liff/is-in-client"),n=require("@liff/is-api-available"),i=require("tslib"),o=require("@liff/logger"),s=require("@liff/get-os"),a=require("@liff/server-api"),u=require("@liff/store"),c=require("@liff/message-bus"),f=require("@liff/is-sub-window"),l=require("@liff/close-window");function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var _=d(c);function S(e){var t=a.getEndPoint("subWindowGetOrigin");return a.fetch(t(e))}var I={};function w(e,t){e&&I[e]&&I[e].forEach((function(e){e(t)}))}var v,m,g,h,p,E=function(){function r(e){this.storage=e}return r.prototype.getItem=function(e){return this.storage.getItem(this.getKeyPrefix()+":"+e)},r.prototype.setItem=function(e,t){this.storage.setItem(this.getKeyPrefix()+":"+e,t)},r.prototype.removeItem=function(e){this.storage.removeItem(this.getKeyPrefix()+":"+e)},r.prototype.clear=function(){this.storage.clear()},r.prototype.getKeyPrefix=function(){return e.STORE_KEY+":"+this.getLiffId()},r.prototype.getLiffId=function(){var r=u.getConfig().liffId;if(!r)throw t.createLiffError(e.INVALID_CONFIG,"liffId is necessary for liff.init()");return r},r}(),N=new E(t.inMemoryStorage);function T(){var e=N.getItem("subWindowStatusUpdated");return null!==e&&JSON.parse(e)}function O(e){N.setItem("subWindowStatusUpdated",String(e))}function U(e){v=e}function W(){return v}function L(){return g}function b(){return h}function A(e){return void 0===e&&(e=c.WINDOW.MAIN),i.__awaiter(this,void 0,void 0,(function(){return i.__generator(this,(function(t){switch(t.label){case 0:return[4,(p=new _.default(e)).setup()];case 1:return t.sent(),[2,p]}}))}))}function D(){return p}var C=new E(window.sessionStorage);function B(){return C.getItem("mainWindowOrigin")}function y(r,n){return void 0===n&&(n={}),i.__awaiter(this,void 0,void 0,(function(){var s,a,c,f,l,d,_,I;return i.__generator(this,(function(i){switch(i.label){case 0:if(null==(s=D())?void 0:s.isReady())return[3,5];if(a=JSON.stringify(n),c=u.getConfig().liffId,f=B(),!window.opener||!f||!c)throw t.createLiffError(e.EXCEPTION_IN_SUBWINDOW);l=!1,i.label=1;case 1:return i.trys.push([1,3,,4]),[4,S(c)];case 2:return d=i.sent(),l=d.subwindowCommonModule,[3,4];case 3:throw _=i.sent(),o.logger.debug(_),t.createLiffError(e.EXCEPTION_IN_SUBWINDOW);case 4:return I=l?f:location.origin,[2,new Promise((function(t){window.addEventListener("message",(function n(i){(function(t){if(t.data&&"string"==typeof t.data.type&&[e.SUB_WINDOW_STATUS.SUBMIT,e.SUB_WINDOW_STATUS.CANCEL].includes(t.data.type))return!0;return!1})(i)&&(window.removeEventListener("message",n),t({status:r,result:a}))})),window.opener.postMessage({status:r,result:a},I)}))];case 5:return s.send({eventName:r,data:n}),[4,new Promise((function(e){setTimeout(e,500)}))];case 6:return i.sent(),[2,{status:r,result:JSON.stringify(n)}]}}))}))}function P(t){var r,n=b();if(t.origin===n){var i=t.data;if(i){var s,a=i.status,u=i.result;try{s=JSON.parse(u||"{}")}catch(c){s={}}switch(a){case e.SUB_WINDOW_HEALTH_CHECK_MESSAGE:window.clearInterval(L()),x();break;case e.SUB_WINDOW_STATUS.CANCEL:case e.SUB_WINDOW_STATUS.SUBMIT:O(!0),window.clearInterval(L()),window.removeEventListener("message",P),w(a,s),null===(r=W())||void 0===r||r.postMessage({type:a},b());break;default:o.logger.debug("unexpected message")}}}}var M=function(t){return i.__awaiter(void 0,void 0,void 0,(function(){var r,n,o,s;return i.__generator(this,(function(i){if(T())return[2];switch(r=t.context,n=r.eventName,o=r.data,s=D(),n){case e.SUB_WINDOW_STATUS.INIT:H(!o.hasOpener);break;case e.SUB_WINDOW_STATUS.CANCEL:case e.SUB_WINDOW_STATUS.SUBMIT:O(!0),w(n,o),null==s||s.reply(t,{eventName:n});break;case e.SUB_WINDOW_STATUS.CLOSE:!1===T()&&(O(!0),w(e.SUB_WINDOW_STATUS.CLOSE,{})),x()}return[2]}))}))};function R(){window.clearInterval(m),window.clearInterval(L()),window.removeEventListener("message",P)}function H(e){if(void 0===e&&(e=!1),R(),O(!1),e){var t=W();t&&(t.close(),U(null))}}function x(){return i.__awaiter(this,void 0,void 0,(function(){var e;return i.__generator(this,(function(t){switch(t.label){case 0:return(e=D())?[4,e.teardown()]:[3,2];case 1:t.sent(),t.label=2;case 2:return[2]}}))}))}function K(r){return i.__awaiter(this,void 0,void 0,(function(){var n,o,a,u,f,l,d,_,I,v;return i.__generator(this,(function(p){switch(p.label){case 0:return(n=t.extractLiffId(r.url))?(H(!0),[4,x()]):[2,Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"params.url must be liff url"))];case 1:return p.sent(),o=r.url,a=r.appData,(u=new URL(o)).searchParams.append(e.SUB_WINDOW_IDNTIFICATION_KEY,"true"),[4,A()];case 2:return f=p.sent(),u.searchParams.append(c.IDENTIFIER_KEY,f.identification.identifier),u.searchParams.append(c.CRYPTO_KEY,f.identification.cryptoKey),u.hostname=function(e){var t=i.__read(e.split(".")),r=t[0],n=t.slice(1);return i.__spread([r+"-ext"],n).join(".")}(u.hostname),l=u.toString(),U("ios"!==s.getOS()||t.isIpad()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),[4,S(n)];case 3:if(d=p.sent(),_=d.origin,I=d.subwindowCommonModule,!(v=W()))throw t.createLiffError(e.CREATE_SUBWINDOW_FAILED);return I?(function(e){h=e}(_),f.listen(M),f.setData("appData",a),window.addEventListener("message",P),v.location.href=l,E=function(t,r){var n=W(),i={type:e.SUB_WINDOW_HEALTH_CHECK_MESSAGE};return r&&(i.message=JSON.stringify(r)),window.setInterval((function(){null==n||n.postMessage(i,t)}),e.SUB_WINDOW_HEALTH_CHECK_INTERVAL)}(_,a),g=E,function(e){m=e}(window.setInterval((function(){var t=W();t&&t.closed&&(R(),U(null),!1===T()&&(O(!0),w(e.SUB_WINDOW_STATUS.CLOSE,{})))}),e.SUB_WINDOW_MONITOR_CLOSE_INTERVAL)),[2]):(v.close(),[2])}var E}))}))}function j(r){return i.__awaiter(this,void 0,void 0,(function(){var n,o,s,u,c,f,l,d,_,S,I;return i.__generator(this,(function(i){switch(i.label){case 0:n=r.msit,o=r.mstChallenge,s=r.onSuccess,u=r.onError,c=r.reconnectCount,f=void 0===c?0:c,i.label=1;case 1:return i.trys.push([1,3,,6]),[4,a.requestWithoutErrorHandling(a.getEndPoint("subWindowSubscribe"),{method:"POST",body:JSON.stringify({msit:n,mstChallenge:o})})];case 2:return l=i.sent(),[3,6];case 3:return i.sent(),[4,G()];case 4:return i.sent(),[4,V(j,{msit:n,mstChallenge:o,onSuccess:s,onError:u,reconnectCount:f+=1})];case 5:return i.sent(),[2];case 6:return l.status>=500?[4,G()]:[3,9];case 7:return i.sent(),[4,V(j,{msit:n,mstChallenge:o,onSuccess:s,onError:u,reconnectCount:f+=1})];case 8:return i.sent(),[3,20];case 9:return l.status>=400&&500>l.status?[4,q(l)]:[3,11];case 10:return(_=i.sent())?(d=_.errorDetail,u(t.createLiffError(e.INVALID_ARGUMENT,d))):u(t.createLiffError(e.UNKNOWN,"Some error happened in the server")),[3,20];case 11:return 200!==l.status?[3,19]:[4,q(l)];case 12:return(_=i.sent())?[3,13]:(u(t.createLiffError(e.UNKNOWN,"Some error happened in the server")),[3,18]);case 13:switch(S=_.status,I=_.result,S){case e.SUB_WINDOW_STATUS.ERROR:return[3,14];case e.SUB_WINDOW_STATUS.CLOSE:case e.SUB_WINDOW_STATUS.CANCEL:case e.SUB_WINDOW_STATUS.SUBMIT:return[3,16]}return[3,17];case 14:return[4,V(j,{msit:n,mstChallenge:o,onSuccess:s,onError:u,reconnectCount:f})];case 15:return i.sent(),[3,18];case 16:return s(S,I),[3,18];case 17:u(t.createLiffError(e.UNKNOWN,"Some error happened in the server")),i.label=18;case 18:return[3,20];case 19:u(t.createLiffError(e.UNKNOWN,"Some error happened in the server")),i.label=20;case 20:return[2]}}))}))}function G(){return new Promise((function(e){return setTimeout(e,1e3)}))}function q(e){return i.__awaiter(this,void 0,void 0,(function(){return i.__generator(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,e.json()];case 1:return[2,t.sent()];case 2:return t.sent(),[2,null];case 3:return[2]}}))}))}function V(r,n){return i.__awaiter(this,void 0,void 0,(function(){return i.__generator(this,(function(i){switch(i.label){case 0:return n.reconnectCount>=10?(n.onError(t.createLiffError(e.UNKNOWN,"Failed to connect")),[3,3]):[3,1];case 1:return[4,r(n)];case 2:i.sent(),i.label=3;case 3:return[2]}}))}))}function J(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 F={height:"full",closeButtonPosition:"right",closeButtonColor:"black",closeButtonLabel:""};function Z(r){var n=r.appData,i=r.native,o=u.getConfig().liffId,s=u.getMSTChallenge(),c=t.extractLiffId(r.url);if(!o)return Promise.reject(t.createLiffError(e.UNAUTHORIZED,"liffId is invalid"));if(!s)return Promise.reject(t.createLiffError(e.UNAUTHORIZED,"mst_challenge is invalid"));if(!c)return Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"params.url must be liff url"));var f=Object.assign({},F,i);return function(r){var n=r.mainLiffId,i=r.subLiffId,o=r.mstChallenge,s=r.appData,u=r.view;return n&&o?a.fetch(a.getEndPoint("subWindowGetMSIT"),{method:"POST",body:JSON.stringify({mainLiffId:n,subLiffId:i,mstChallenge:o,appData:s,view:u})}):Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"no proper argument"))}({mainLiffId:o,subLiffId:c,mstChallenge:s,appData:n,view:J(f)}).then((function(t){var n=t.msit;j({msit:n,mstChallenge:s,onSuccess:function(e,t){w(e,t)},onError:function(t){w(e.SUB_WINDOW_STATUS.ERROR,t)}}),function(t,r){var n=t.url,i=new URLSearchParams;i.set("msit",r),location.href=e.SUB_WINDOW_MODAL_SCHEME_URL+"?url="+encodeURIComponent(n)+"&"+i.toString()}(r,n)}))}function k(){if(!f.isSubWindow())throw t.createLiffError(e.UNAUTHORIZED,"this api can be only called in child window")}function Y(r){if(!r.mst||!r.status)return Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"no proper argument"));var n=JSON.stringify(r);return a.fetch(a.getEndPoint("subWindowPost"),{method:"POST",body:n})}var X={on:function(e,t){I[e]||(I[e]=[]),I[e].push(t)},off:function(e,t){if(I[e]){var r=I[e].indexOf(t);r>=0&&I[e].splice(r,1)}},open:function(i){if(!n.isApiAvailable("subwindowOpen"))throw t.createLiffError(e.FORBIDDEN,"No permission for liff.subWindow.open()");if(!t.isLIFFBrowser()&&t.isLINEBrowser())throw t.createLiffError(e.FORBIDDEN,"Subwindow is not supported in this browser");return function(){if(f.isSubWindow())throw t.createLiffError(e.UNAUTHORIZED,"this api can be only called in parent window")}(),r.isInClient()?Z(i):K(i)},cancel:function(n){return void 0===n&&(n={}),k(),r.isInClient()?function(r){return void 0===r&&(r={}),i.__awaiter(this,void 0,void 0,(function(){var n,o;return i.__generator(this,(function(i){switch(i.label){case 0:return(n=u.getMST())?[4,Y({mst:n,status:e.SUB_WINDOW_STATUS.CANCEL,result:r})]:[2,Promise.reject(t.createLiffError(e.UNAUTHORIZED,"mst is invalid"))];case 1:return o=i.sent(),O(!0),[2,o]}}))}))}(n):function(t){return void 0===t&&(t={}),y(e.SUB_WINDOW_STATUS.CANCEL,t)}(n)},submit:function(n){return void 0===n&&(n={}),k(),r.isInClient()?function(r){return void 0===r&&(r={}),i.__awaiter(this,void 0,void 0,(function(){var n,o;return i.__generator(this,(function(i){switch(i.label){case 0:return(n=u.getMST())?[4,Y({mst:n,status:e.SUB_WINDOW_STATUS.SUBMIT,result:r})]:[2,Promise.reject(t.createLiffError(e.UNAUTHORIZED,"mst is invalid"))];case 1:return o=i.sent(),O(!0),[2,o]}}))}))}(n):function(t){return void 0===t&&(t={}),y(e.SUB_WINDOW_STATUS.SUBMIT,t)}(n)},close:function(){return k(),r.isInClient()?function(){return i.__awaiter(this,void 0,void 0,(function(){var r;return i.__generator(this,(function(n){switch(n.label){case 0:return!1!==T()?[3,2]:(r=u.getMST())?[4,Y({mst:r,status:e.SUB_WINDOW_STATUS.CLOSE,result:{}})]:[2,Promise.reject(t.createLiffError(e.UNAUTHORIZED,"mst is invalid"))];case 1:n.sent(),n.label=2;case 2:return l.closeWindow(),[2]}}))}))}():function(){return i.__awaiter(this,void 0,void 0,(function(){var t;return i.__generator(this,(function(r){return(null==(t=D())?void 0:t.isReady())?(t.send({eventName:e.SUB_WINDOW_STATUS.CLOSE}),[2,new Promise((function(t){setTimeout((function(){l.closeWindow(),t()}),e.SUB_WINDOW_HEALTH_CHECK_INTERVAL)}))]):(l.closeWindow(),[2,Promise.resolve()])}))}))}()},getAppData:function(){return k(),function(){var e,t=u.getAppData();try{e=t?JSON.parse(t):{}}catch(r){e={}}return Promise.resolve(e)}()}};exports.getAppData=function(r){var n=r.mst;return n?a.fetch(a.getEndPoint("subWindowGetAppData"),{method:"POST",body:JSON.stringify({mst:n})}):Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"no proper argument"))},exports.getMSTByMSIT=function(r){var n=r.msit,i=r.mstVerifier;return n&&i?a.fetch(a.getEndPoint("subWindowGetMSTByMSIT"),{method:"POST",body:JSON.stringify({msit:n,mstVerifier:i})}):Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"no proper argument"))},exports.getMainWindowOrigin=B,exports.getMessageBus=D,exports.initMessageBus=A,exports.setMainWindowOrigin=function(e){C.setItem("mainWindowOrigin",e)},exports.subWindow=X; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@liff/consts"),t=require("@liff/util"),r=require("@liff/is-in-client"),n=require("@liff/is-api-available"),i=require("tslib"),o=require("@liff/logger"),s=require("@liff/get-os"),a=require("@liff/server-api"),u=require("@liff/store"),c=require("@liff/message-bus"),f=require("@liff/is-sub-window"),l=require("@liff/close-window");function d(e){var t=a.getEndPoint("subWindowGetOrigin");return a.fetch(t(e))}var _={};function S(e,t){e&&_[e]&&_[e].forEach((function(e){e(t)}))}var I,w,g,m,v,h=function(){function r(e){this.storage=e}return r.prototype.getItem=function(e){return this.storage.getItem(this.getKeyPrefix()+":"+e)},r.prototype.setItem=function(e,t){this.storage.setItem(this.getKeyPrefix()+":"+e,t)},r.prototype.removeItem=function(e){this.storage.removeItem(this.getKeyPrefix()+":"+e)},r.prototype.clear=function(){this.storage.clear()},r.prototype.getKeyPrefix=function(){return e.STORE_KEY+":"+this.getLiffId()},r.prototype.getLiffId=function(){var r=u.getConfig().liffId;if(!r)throw t.createLiffError(e.INVALID_CONFIG,"liffId is necessary for liff.init()");return r},r}(),p=new h(t.inMemoryStorage);function E(){var e=p.getItem("subWindowStatusUpdated");return null!==e&&JSON.parse(e)}function N(e){p.setItem("subWindowStatusUpdated",String(e))}function T(e){I=e}function O(){return I}function U(){return g}function W(){return m}function L(e){return void 0===e&&(e=c.WINDOW.MAIN),i.__awaiter(this,void 0,void 0,(function(){return i.__generator(this,(function(t){switch(t.label){case 0:return[4,(v=new c.MessageBus(e)).setup()];case 1:return t.sent(),[2,v]}}))}))}function b(){return v}var A=new h(window.sessionStorage);function D(){return A.getItem("mainWindowOrigin")}function C(r,n){return void 0===n&&(n={}),i.__awaiter(this,void 0,void 0,(function(){var s,a,c,f,l,_,S,I;return i.__generator(this,(function(i){switch(i.label){case 0:if(null==(s=b())?void 0:s.isReady())return[3,5];if(a=JSON.stringify(n),c=u.getConfig().liffId,f=D(),!window.opener||!f||!c)throw t.createLiffError(e.EXCEPTION_IN_SUBWINDOW);l=!1,i.label=1;case 1:return i.trys.push([1,3,,4]),[4,d(c)];case 2:return _=i.sent(),l=_.subwindowCommonModule,[3,4];case 3:throw S=i.sent(),o.logger.debug(S),t.createLiffError(e.EXCEPTION_IN_SUBWINDOW);case 4:return I=l?f:location.origin,[2,new Promise((function(t){window.addEventListener("message",(function n(i){(function(t){if(t.data&&"string"==typeof t.data.type&&[e.SUB_WINDOW_STATUS.SUBMIT,e.SUB_WINDOW_STATUS.CANCEL].includes(t.data.type))return!0;return!1})(i)&&(window.removeEventListener("message",n),t({status:r,result:a}))})),window.opener.postMessage({status:r,result:a},I)}))];case 5:return s.send({eventName:r,data:n}),[4,new Promise((function(e){setTimeout(e,500)}))];case 6:return i.sent(),[2,{status:r,result:JSON.stringify(n)}]}}))}))}function B(t){var r,n=W();if(t.origin===n){var i=t.data;if(i){var s,a=i.status,u=i.result;try{s=JSON.parse(u||"{}")}catch(c){s={}}switch(a){case e.SUB_WINDOW_HEALTH_CHECK_MESSAGE:window.clearInterval(U()),R();break;case e.SUB_WINDOW_STATUS.CANCEL:case e.SUB_WINDOW_STATUS.SUBMIT:N(!0),window.clearInterval(U()),window.removeEventListener("message",B),S(a,s),null===(r=O())||void 0===r||r.postMessage({type:a},W());break;default:o.logger.debug("unexpected message")}}}}var y=function(t){return i.__awaiter(void 0,void 0,void 0,(function(){var r,n,o,s;return i.__generator(this,(function(i){if(E())return[2];switch(r=t.context,n=r.eventName,o=r.data,s=b(),n){case e.SUB_WINDOW_STATUS.INIT:M(!o.hasOpener);break;case e.SUB_WINDOW_STATUS.CANCEL:case e.SUB_WINDOW_STATUS.SUBMIT:N(!0),S(n,o),null==s||s.reply(t,{eventName:n});break;case e.SUB_WINDOW_STATUS.CLOSE:!1===E()&&(N(!0),S(e.SUB_WINDOW_STATUS.CLOSE,{})),R()}return[2]}))}))};function P(){window.clearInterval(w),window.clearInterval(U()),window.removeEventListener("message",B)}function M(e){if(void 0===e&&(e=!1),P(),N(!1),e){var t=O();t&&(t.close(),T(null))}}function R(){return i.__awaiter(this,void 0,void 0,(function(){var e;return i.__generator(this,(function(t){switch(t.label){case 0:return(e=b())?[4,e.teardown()]:[3,2];case 1:t.sent(),t.label=2;case 2:return[2]}}))}))}function H(r){return i.__awaiter(this,void 0,void 0,(function(){var n,o,a,u,f,l,_,I,v,h;return i.__generator(this,(function(p){switch(p.label){case 0:return(n=t.extractLiffId(r.url))?(M(!0),[4,R()]):[2,Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"params.url must be liff url"))];case 1:return p.sent(),o=r.url,a=r.appData,(u=new URL(o)).searchParams.append(e.SUB_WINDOW_IDNTIFICATION_KEY,"true"),[4,L()];case 2:return f=p.sent(),u.searchParams.append(c.IDENTIFIER_KEY,f.identification.identifier),u.searchParams.append(c.CRYPTO_KEY,f.identification.cryptoKey),u.hostname=function(e){var t=i.__read(e.split(".")),r=t[0],n=t.slice(1);return i.__spread([r+"-ext"],n).join(".")}(u.hostname),l=u.toString(),T("ios"!==s.getOS()||t.isIpad()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),[4,d(n)];case 3:if(_=p.sent(),I=_.origin,v=_.subwindowCommonModule,!(h=O()))throw t.createLiffError(e.CREATE_SUBWINDOW_FAILED);return v?(function(e){m=e}(I),f.listen(y),f.setData("appData",a),window.addEventListener("message",B),h.location.href=l,U=function(t,r){var n=O(),i={type:e.SUB_WINDOW_HEALTH_CHECK_MESSAGE};return r&&(i.message=JSON.stringify(r)),window.setInterval((function(){null==n||n.postMessage(i,t)}),e.SUB_WINDOW_HEALTH_CHECK_INTERVAL)}(I,a),g=U,function(e){w=e}(window.setInterval((function(){var t=O();t&&t.closed&&(P(),T(null),!1===E()&&(N(!0),S(e.SUB_WINDOW_STATUS.CLOSE,{})))}),e.SUB_WINDOW_MONITOR_CLOSE_INTERVAL)),[2]):(h.close(),[2])}var U}))}))}function x(r){return i.__awaiter(this,void 0,void 0,(function(){var n,o,s,u,c,f,l,d,_,S,I;return i.__generator(this,(function(i){switch(i.label){case 0:n=r.msit,o=r.mstChallenge,s=r.onSuccess,u=r.onError,c=r.reconnectCount,f=void 0===c?0:c,i.label=1;case 1:return i.trys.push([1,3,,6]),[4,a.requestWithoutErrorHandling(a.getEndPoint("subWindowSubscribe"),{method:"POST",body:JSON.stringify({msit:n,mstChallenge:o})})];case 2:return l=i.sent(),[3,6];case 3:return i.sent(),[4,K()];case 4:return i.sent(),[4,G(x,{msit:n,mstChallenge:o,onSuccess:s,onError:u,reconnectCount:f+=1})];case 5:return i.sent(),[2];case 6:return l.status>=500?[4,K()]:[3,9];case 7:return i.sent(),[4,G(x,{msit:n,mstChallenge:o,onSuccess:s,onError:u,reconnectCount:f+=1})];case 8:return i.sent(),[3,20];case 9:return l.status>=400&&500>l.status?[4,j(l)]:[3,11];case 10:return(_=i.sent())?(d=_.errorDetail,u(t.createLiffError(e.INVALID_ARGUMENT,d))):u(t.createLiffError(e.UNKNOWN,"Some error happened in the server")),[3,20];case 11:return 200!==l.status?[3,19]:[4,j(l)];case 12:return(_=i.sent())?[3,13]:(u(t.createLiffError(e.UNKNOWN,"Some error happened in the server")),[3,18]);case 13:switch(S=_.status,I=_.result,S){case e.SUB_WINDOW_STATUS.ERROR:return[3,14];case e.SUB_WINDOW_STATUS.CLOSE:case e.SUB_WINDOW_STATUS.CANCEL:case e.SUB_WINDOW_STATUS.SUBMIT:return[3,16]}return[3,17];case 14:return[4,G(x,{msit:n,mstChallenge:o,onSuccess:s,onError:u,reconnectCount:f})];case 15:return i.sent(),[3,18];case 16:return s(S,I),[3,18];case 17:u(t.createLiffError(e.UNKNOWN,"Some error happened in the server")),i.label=18;case 18:return[3,20];case 19:u(t.createLiffError(e.UNKNOWN,"Some error happened in the server")),i.label=20;case 20:return[2]}}))}))}function K(){return new Promise((function(e){return setTimeout(e,1e3)}))}function j(e){return i.__awaiter(this,void 0,void 0,(function(){return i.__generator(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,e.json()];case 1:return[2,t.sent()];case 2:return t.sent(),[2,null];case 3:return[2]}}))}))}function G(r,n){return i.__awaiter(this,void 0,void 0,(function(){return i.__generator(this,(function(i){switch(i.label){case 0:return n.reconnectCount>=10?(n.onError(t.createLiffError(e.UNKNOWN,"Failed to connect")),[3,3]):[3,1];case 1:return[4,r(n)];case 2:i.sent(),i.label=3;case 3:return[2]}}))}))}function q(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(r){var n=r.appData,i=r.native,o=u.getConfig().liffId,s=u.getMSTChallenge(),c=t.extractLiffId(r.url);if(!o)return Promise.reject(t.createLiffError(e.UNAUTHORIZED,"liffId is invalid"));if(!s)return Promise.reject(t.createLiffError(e.UNAUTHORIZED,"mst_challenge is invalid"));if(!c)return Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"params.url must be liff url"));var f=Object.assign({},V,i);return function(r){var n=r.mainLiffId,i=r.subLiffId,o=r.mstChallenge,s=r.appData,u=r.view;return n&&o?a.fetch(a.getEndPoint("subWindowGetMSIT"),{method:"POST",body:JSON.stringify({mainLiffId:n,subLiffId:i,mstChallenge:o,appData:s,view:u})}):Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"no proper argument"))}({mainLiffId:o,subLiffId:c,mstChallenge:s,appData:n,view:q(f)}).then((function(t){var n=t.msit;x({msit:n,mstChallenge:s,onSuccess:function(e,t){S(e,t)},onError:function(t){S(e.SUB_WINDOW_STATUS.ERROR,t)}}),function(t,r){var n=t.url,i=new URLSearchParams;i.set("msit",r),location.href=e.SUB_WINDOW_MODAL_SCHEME_URL+"?url="+encodeURIComponent(n)+"&"+i.toString()}(r,n)}))}function F(){if(!f.isSubWindow())throw t.createLiffError(e.UNAUTHORIZED,"this api can be only called in child window")}function Z(r){if(!r.mst||!r.status)return Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"no proper argument"));var n=JSON.stringify(r);return a.fetch(a.getEndPoint("subWindowPost"),{method:"POST",body:n})}var k={on:function(e,t){_[e]||(_[e]=[]),_[e].push(t)},off:function(e,t){if(_[e]){var r=_[e].indexOf(t);r>=0&&_[e].splice(r,1)}},open:function(i){if(!n.isApiAvailable("subwindowOpen"))throw t.createLiffError(e.FORBIDDEN,"No permission for liff.subWindow.open()");if(!t.isLIFFBrowser()&&t.isLINEBrowser())throw t.createLiffError(e.FORBIDDEN,"Subwindow is not supported in this browser");return function(){if(f.isSubWindow())throw t.createLiffError(e.UNAUTHORIZED,"this api can be only called in parent window")}(),r.isInClient()?J(i):H(i)},cancel:function(n){return void 0===n&&(n={}),F(),r.isInClient()?function(r){return void 0===r&&(r={}),i.__awaiter(this,void 0,void 0,(function(){var n,o;return i.__generator(this,(function(i){switch(i.label){case 0:return(n=u.getMST())?[4,Z({mst:n,status:e.SUB_WINDOW_STATUS.CANCEL,result:r})]:[2,Promise.reject(t.createLiffError(e.UNAUTHORIZED,"mst is invalid"))];case 1:return o=i.sent(),N(!0),[2,o]}}))}))}(n):function(t){return void 0===t&&(t={}),C(e.SUB_WINDOW_STATUS.CANCEL,t)}(n)},submit:function(n){return void 0===n&&(n={}),F(),r.isInClient()?function(r){return void 0===r&&(r={}),i.__awaiter(this,void 0,void 0,(function(){var n,o;return i.__generator(this,(function(i){switch(i.label){case 0:return(n=u.getMST())?[4,Z({mst:n,status:e.SUB_WINDOW_STATUS.SUBMIT,result:r})]:[2,Promise.reject(t.createLiffError(e.UNAUTHORIZED,"mst is invalid"))];case 1:return o=i.sent(),N(!0),[2,o]}}))}))}(n):function(t){return void 0===t&&(t={}),C(e.SUB_WINDOW_STATUS.SUBMIT,t)}(n)},close:function(){return F(),r.isInClient()?function(){return i.__awaiter(this,void 0,void 0,(function(){var r;return i.__generator(this,(function(n){switch(n.label){case 0:return!1!==E()?[3,2]:(r=u.getMST())?[4,Z({mst:r,status:e.SUB_WINDOW_STATUS.CLOSE,result:{}})]:[2,Promise.reject(t.createLiffError(e.UNAUTHORIZED,"mst is invalid"))];case 1:n.sent(),n.label=2;case 2:return l.closeWindow(),[2]}}))}))}():function(){return i.__awaiter(this,void 0,void 0,(function(){var t;return i.__generator(this,(function(r){return(null==(t=b())?void 0:t.isReady())?(t.send({eventName:e.SUB_WINDOW_STATUS.CLOSE}),[2,new Promise((function(t){setTimeout((function(){l.closeWindow(),t()}),e.SUB_WINDOW_HEALTH_CHECK_INTERVAL)}))]):(l.closeWindow(),[2,Promise.resolve()])}))}))}()},getAppData:function(){return F(),function(){var e,t=u.getAppData();try{e=t?JSON.parse(t):{}}catch(r){e={}}return Promise.resolve(e)}()}};exports.getAppData=function(r){var n=r.mst;return n?a.fetch(a.getEndPoint("subWindowGetAppData"),{method:"POST",body:JSON.stringify({mst:n})}):Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"no proper argument"))},exports.getMSTByMSIT=function(r){var n=r.msit,i=r.mstVerifier;return n&&i?a.fetch(a.getEndPoint("subWindowGetMSTByMSIT"),{method:"POST",body:JSON.stringify({msit:n,mstVerifier:i})}):Promise.reject(t.createLiffError(e.INVALID_ARGUMENT,"no proper argument"))},exports.getMainWindowOrigin=D,exports.getMessageBus=b,exports.initMessageBus=L,exports.setMainWindowOrigin=function(e){A.setItem("mainWindowOrigin",e)},exports.subWindow=k; |
@@ -1,1 +0,1 @@ | ||
import{STORE_KEY as e,INVALID_CONFIG as t,EXCEPTION_IN_SUBWINDOW as n,SUB_WINDOW_STATUS as r,CREATE_SUBWINDOW_FAILED as i,SUB_WINDOW_HEALTH_CHECK_INTERVAL as s,SUB_WINDOW_MONITOR_CLOSE_INTERVAL as o,SUB_WINDOW_IDNTIFICATION_KEY as u,INVALID_ARGUMENT as a,SUB_WINDOW_HEALTH_CHECK_MESSAGE as c,UNKNOWN as f,UNAUTHORIZED as l,SUB_WINDOW_MODAL_SCHEME_URL as d,FORBIDDEN as m}from"@liff/consts";import{createLiffError as v,inMemoryStorage as p,isIpad as h,extractLiffId as w,isLIFFBrowser as g,isLINEBrowser as b}from"@liff/util";import{isInClient as S}from"@liff/is-in-client";import{isApiAvailable as y}from"@liff/is-api-available";import{__awaiter as I,__generator as C,__spread as O,__read as P}from"tslib";import{logger as L}from"@liff/logger";import{getOS as E}from"@liff/get-os";import{fetch as N,getEndPoint as T,requestWithoutErrorHandling as j}from"@liff/server-api";import{getConfig as M,getMSTChallenge as B,getMST as D,getAppData as J}from"@liff/store";import R,{WINDOW 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=T("subWindowGetOrigin");return N(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 n(e){this.storage=e}return n.prototype.getItem=function(e){return this.storage.getItem(this.getKeyPrefix()+":"+e)},n.prototype.setItem=function(e,t){this.storage.setItem(this.getKeyPrefix()+":"+e,t)},n.prototype.removeItem=function(e){this.storage.removeItem(this.getKeyPrefix()+":"+e)},n.prototype.clear=function(){this.storage.clear()},n.prototype.getKeyPrefix=function(){return e+":"+this.getLiffId()},n.prototype.getLiffId=function(){var e=M().liffId;if(!e)throw v(t,"liffId is necessary for liff.init()");return e},n}(),X=new Q(p);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(e){return void 0===e&&(e=U.MAIN),I(this,void 0,void 0,(function(){return C(this,(function(t){switch(t.label){case 0:return[4,(H=new R(e)).setup()];case 1:return t.sent(),[2,H]}}))}))}function ie(){return H}var se=new Q(window.sessionStorage);function oe(e){se.setItem("mainWindowOrigin",e)}function ue(){return se.getItem("mainWindowOrigin")}function ae(e,t){return void 0===t&&(t={}),I(this,void 0,void 0,(function(){var i,s,o,u,a,c,f,l;return C(this,(function(d){switch(d.label){case 0:if(null==(i=ie())?void 0:i.isReady())return[3,5];if(s=JSON.stringify(t),o=M().liffId,u=ue(),!window.opener||!u||!o)throw v(n);a=!1,d.label=1;case 1:return d.trys.push([1,3,,4]),[4,K(o)];case 2:return c=d.sent(),a=c.subwindowCommonModule,[3,4];case 3:throw f=d.sent(),L.debug(f),v(n);case 4:return l=a?u:location.origin,[2,new Promise((function(t){window.addEventListener("message",(function n(i){(function(e){if(e.data&&"string"==typeof e.data.type&&[r.SUBMIT,r.CANCEL].includes(e.data.type))return!0;return!1})(i)&&(window.removeEventListener("message",n),t({status:e,result:s}))})),window.opener.postMessage({status:e,result:s},l)}))];case 5:return i.send({eventName:e,data:t}),[4,new Promise((function(e){setTimeout(e,500)}))];case 6:return d.sent(),[2,{status:e,result:JSON.stringify(t)}]}}))}))}function ce(e){var t,n=ne();if(e.origin===n){var i=e.data;if(i){var s,o=i.status,u=i.result;try{s=JSON.parse(u||"{}")}catch(a){s={}}switch(o){case c:window.clearInterval(te()),me();break;case r.CANCEL:case r.SUBMIT:Z(!0),window.clearInterval(te()),window.removeEventListener("message",ce),V(o,s),null===(t=ee())||void 0===t||t.postMessage({type:o},ne());break;default:L.debug("unexpected message")}}}}var fe=function(e){return I(void 0,void 0,void 0,(function(){var t,n,i,s;return C(this,(function(o){if(Y())return[2];switch(t=e.context,n=t.eventName,i=t.data,s=ie(),n){case r.INIT:de(!i.hasOpener);break;case r.CANCEL:case r.SUBMIT:Z(!0),V(n,i),null==s||s.reply(e,{eventName:n});break;case r.CLOSE:!1===Y()&&(Z(!0),V(r.CLOSE,{})),me()}return[2]}))}))};function le(){window.clearInterval(_),window.clearInterval(te()),window.removeEventListener("message",ce)}function de(e){if(void 0===e&&(e=!1),le(),Z(!1),e){var t=ee();t&&(t.close(),$(null))}}function me(){return I(this,void 0,void 0,(function(){var e;return C(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 ve(e){return I(this,void 0,void 0,(function(){var t,n,f,l,d,m,p,g,b,S;return C(this,(function(y){switch(y.label){case 0:return(t=w(e.url))?(de(!0),[4,me()]):[2,Promise.reject(v(a,"params.url must be liff url"))];case 1:return y.sent(),n=e.url,f=e.appData,(l=new URL(n)).searchParams.append(u,"true"),[4,re()];case 2:return d=y.sent(),l.searchParams.append(W,d.identification.identifier),l.searchParams.append(A,d.identification.cryptoKey),l.hostname=function(e){var t=P(e.split(".")),n=t[0],r=t.slice(1);return O([n+"-ext"],r).join(".")}(l.hostname),m=l.toString(),$("ios"!==E()||h()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),[4,K(t)];case 3:if(p=y.sent(),g=p.origin,b=p.subwindowCommonModule,!(S=ee()))throw v(i);return b?(function(e){z=e}(g),d.listen(fe),d.setData("appData",f),window.addEventListener("message",ce),S.location.href=m,I=function(e,t){var n=ee(),r={type:c};return t&&(r.message=JSON.stringify(t)),window.setInterval((function(){null==n||n.postMessage(r,e)}),s)}(g,f),q=I,function(e){_=e}(window.setInterval((function(){var e=ee();e&&e.closed&&(le(),$(null),!1===Y()&&(Z(!0),V(r.CLOSE,{})))}),o)),[2]):(S.close(),[2])}var I}))}))}function pe(e){return I(this,void 0,void 0,(function(){var t,n,i,s,o,u,c,l,d,m,p;return C(this,(function(h){switch(h.label){case 0:t=e.msit,n=e.mstChallenge,i=e.onSuccess,s=e.onError,o=e.reconnectCount,u=void 0===o?0:o,h.label=1;case 1:return h.trys.push([1,3,,6]),[4,j(T("subWindowSubscribe"),{method:"POST",body:JSON.stringify({msit:t,mstChallenge:n})})];case 2:return c=h.sent(),[3,6];case 3:return h.sent(),[4,he()];case 4:return h.sent(),[4,ge(pe,{msit:t,mstChallenge:n,onSuccess:i,onError:s,reconnectCount:u+=1})];case 5:return h.sent(),[2];case 6:return c.status>=500?[4,he()]:[3,9];case 7:return h.sent(),[4,ge(pe,{msit:t,mstChallenge:n,onSuccess:i,onError:s,reconnectCount:u+=1})];case 8:return h.sent(),[3,20];case 9:return c.status>=400&&500>c.status?[4,we(c)]:[3,11];case 10:return(d=h.sent())?(l=d.errorDetail,s(v(a,l))):s(v(f,"Some error happened in the server")),[3,20];case 11:return 200!==c.status?[3,19]:[4,we(c)];case 12:return(d=h.sent())?[3,13]:(s(v(f,"Some error happened in the server")),[3,18]);case 13:switch(m=d.status,p=d.result,m){case r.ERROR:return[3,14];case r.CLOSE:case r.CANCEL:case r.SUBMIT:return[3,16]}return[3,17];case 14:return[4,ge(pe,{msit:t,mstChallenge:n,onSuccess:i,onError:s,reconnectCount:u})];case 15:return h.sent(),[3,18];case 16:return i(m,p),[3,18];case 17:s(v(f,"Some error happened in the server")),h.label=18;case 18:return[3,20];case 19:s(v(f,"Some error happened in the server")),h.label=20;case 20:return[2]}}))}))}function he(){return new Promise((function(e){return setTimeout(e,1e3)}))}function we(e){return I(this,void 0,void 0,(function(){return C(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,e.json()];case 1:return[2,t.sent()];case 2:return t.sent(),[2,null];case 3:return[2]}}))}))}function ge(e,t){return I(this,void 0,void 0,(function(){return C(this,(function(n){switch(n.label){case 0:return t.reconnectCount>=10?(t.onError(v(f,"Failed to connect")),[3,3]):[3,1];case 1:return[4,e(t)];case 2:n.sent(),n.label=3;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 Se={height:"full",closeButtonPosition:"right",closeButtonColor:"black",closeButtonLabel:""};function ye(e){var t=e.appData,n=e.native,i=M().liffId,s=B(),o=w(e.url);if(!i)return Promise.reject(v(l,"liffId is invalid"));if(!s)return Promise.reject(v(l,"mst_challenge is invalid"));if(!o)return Promise.reject(v(a,"params.url must be liff url"));var u=Object.assign({},Se,n);return function(e){var t=e.mainLiffId,n=e.subLiffId,r=e.mstChallenge,i=e.appData,s=e.view;return t&&r?N(T("subWindowGetMSIT"),{method:"POST",body:JSON.stringify({mainLiffId:t,subLiffId:n,mstChallenge:r,appData:i,view:s})}):Promise.reject(v(a,"no proper argument"))}({mainLiffId:i,subLiffId:o,mstChallenge:s,appData:t,view:be(u)}).then((function(t){var n=t.msit;pe({msit:n,mstChallenge:s,onSuccess:function(e,t){V(e,t)},onError:function(e){V(r.ERROR,e)}}),function(e,t){var n=e.url,r=new URLSearchParams;r.set("msit",t),location.href=d+"?url="+encodeURIComponent(n)+"&"+r.toString()}(e,n)}))}function Ie(){if(!x())throw v(l,"this api can be only called in child window")}function Ce(e){if(!e.mst||!e.status)return Promise.reject(v(a,"no proper argument"));var t=JSON.stringify(e);return N(T("subWindowPost"),{method:"POST",body:t})}function Oe(e){var t=e.msit,n=e.mstVerifier;return t&&n?N(T("subWindowGetMSTByMSIT"),{method:"POST",body:JSON.stringify({msit:t,mstVerifier:n})}):Promise.reject(v(a,"no proper argument"))}function Pe(e){var t=e.mst;return t?N(T("subWindowGetAppData"),{method:"POST",body:JSON.stringify({mst:t})}):Promise.reject(v(a,"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){if(!y("subwindowOpen"))throw v(m,"No permission for liff.subWindow.open()");if(!g()&&b())throw v(m,"Subwindow is not supported in this browser");return function(){if(x())throw v(l,"this api can be only called in parent window")}(),S()?ye(e):ve(e)},cancel:function(e){return void 0===e&&(e={}),Ie(),S()?function(e){return void 0===e&&(e={}),I(this,void 0,void 0,(function(){var t,n;return C(this,(function(i){switch(i.label){case 0:return(t=D())?[4,Ce({mst:t,status:r.CANCEL,result:e})]:[2,Promise.reject(v(l,"mst is invalid"))];case 1:return n=i.sent(),Z(!0),[2,n]}}))}))}(e):function(e){return void 0===e&&(e={}),ae(r.CANCEL,e)}(e)},submit:function(e){return void 0===e&&(e={}),Ie(),S()?function(e){return void 0===e&&(e={}),I(this,void 0,void 0,(function(){var t,n;return C(this,(function(i){switch(i.label){case 0:return(t=D())?[4,Ce({mst:t,status:r.SUBMIT,result:e})]:[2,Promise.reject(v(l,"mst is invalid"))];case 1:return n=i.sent(),Z(!0),[2,n]}}))}))}(e):function(e){return void 0===e&&(e={}),ae(r.SUBMIT,e)}(e)},close:function(){return Ie(),S()?function(){return I(this,void 0,void 0,(function(){var e;return C(this,(function(t){switch(t.label){case 0:return!1!==Y()?[3,2]:(e=D())?[4,Ce({mst:e,status:r.CLOSE,result:{}})]:[2,Promise.reject(v(l,"mst is invalid"))];case 1:t.sent(),t.label=2;case 2:return k(),[2]}}))}))}():function(){return I(this,void 0,void 0,(function(){var e;return C(this,(function(t){return(null==(e=ie())?void 0:e.isReady())?(e.send({eventName:r.CLOSE}),[2,new Promise((function(e){setTimeout((function(){k(),e()}),s)}))]):(k(),[2,Promise.resolve()])}))}))}()},getAppData:function(){return Ie(),function(){var e,t=J();try{e=t?JSON.parse(t):{}}catch(n){e={}}return Promise.resolve(e)}()}};export{Pe as getAppData,Oe as getMSTByMSIT,ue as getMainWindowOrigin,ie as getMessageBus,re as initMessageBus,oe as setMainWindowOrigin,Le as subWindow}; | ||
import{STORE_KEY as e,INVALID_CONFIG as t,EXCEPTION_IN_SUBWINDOW as n,SUB_WINDOW_STATUS as r,CREATE_SUBWINDOW_FAILED as i,SUB_WINDOW_HEALTH_CHECK_INTERVAL as s,SUB_WINDOW_MONITOR_CLOSE_INTERVAL as o,SUB_WINDOW_IDNTIFICATION_KEY as u,INVALID_ARGUMENT as a,SUB_WINDOW_HEALTH_CHECK_MESSAGE as c,UNKNOWN as f,UNAUTHORIZED as l,SUB_WINDOW_MODAL_SCHEME_URL as d,FORBIDDEN as m}from"@liff/consts";import{createLiffError as v,inMemoryStorage as p,isIpad as h,extractLiffId as w,isLIFFBrowser as g,isLINEBrowser as b}from"@liff/util";import{isInClient as S}from"@liff/is-in-client";import{isApiAvailable as y}from"@liff/is-api-available";import{__awaiter as I,__generator as C,__spread as O,__read as P}from"tslib";import{logger as L}from"@liff/logger";import{getOS as E}from"@liff/get-os";import{fetch as N,getEndPoint as T,requestWithoutErrorHandling as j}from"@liff/server-api";import{getConfig as M,getMSTChallenge as B,getMST as D,getAppData as J}from"@liff/store";import{MessageBus as R,WINDOW 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=T("subWindowGetOrigin");return N(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 n(e){this.storage=e}return n.prototype.getItem=function(e){return this.storage.getItem(this.getKeyPrefix()+":"+e)},n.prototype.setItem=function(e,t){this.storage.setItem(this.getKeyPrefix()+":"+e,t)},n.prototype.removeItem=function(e){this.storage.removeItem(this.getKeyPrefix()+":"+e)},n.prototype.clear=function(){this.storage.clear()},n.prototype.getKeyPrefix=function(){return e+":"+this.getLiffId()},n.prototype.getLiffId=function(){var e=M().liffId;if(!e)throw v(t,"liffId is necessary for liff.init()");return e},n}(),X=new Q(p);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(e){return void 0===e&&(e=U.MAIN),I(this,void 0,void 0,(function(){return C(this,(function(t){switch(t.label){case 0:return[4,(H=new R(e)).setup()];case 1:return t.sent(),[2,H]}}))}))}function ie(){return H}var se=new Q(window.sessionStorage);function oe(e){se.setItem("mainWindowOrigin",e)}function ue(){return se.getItem("mainWindowOrigin")}function ae(e,t){return void 0===t&&(t={}),I(this,void 0,void 0,(function(){var i,s,o,u,a,c,f,l;return C(this,(function(d){switch(d.label){case 0:if(null==(i=ie())?void 0:i.isReady())return[3,5];if(s=JSON.stringify(t),o=M().liffId,u=ue(),!window.opener||!u||!o)throw v(n);a=!1,d.label=1;case 1:return d.trys.push([1,3,,4]),[4,K(o)];case 2:return c=d.sent(),a=c.subwindowCommonModule,[3,4];case 3:throw f=d.sent(),L.debug(f),v(n);case 4:return l=a?u:location.origin,[2,new Promise((function(t){window.addEventListener("message",(function n(i){(function(e){if(e.data&&"string"==typeof e.data.type&&[r.SUBMIT,r.CANCEL].includes(e.data.type))return!0;return!1})(i)&&(window.removeEventListener("message",n),t({status:e,result:s}))})),window.opener.postMessage({status:e,result:s},l)}))];case 5:return i.send({eventName:e,data:t}),[4,new Promise((function(e){setTimeout(e,500)}))];case 6:return d.sent(),[2,{status:e,result:JSON.stringify(t)}]}}))}))}function ce(e){var t,n=ne();if(e.origin===n){var i=e.data;if(i){var s,o=i.status,u=i.result;try{s=JSON.parse(u||"{}")}catch(a){s={}}switch(o){case c:window.clearInterval(te()),me();break;case r.CANCEL:case r.SUBMIT:Z(!0),window.clearInterval(te()),window.removeEventListener("message",ce),V(o,s),null===(t=ee())||void 0===t||t.postMessage({type:o},ne());break;default:L.debug("unexpected message")}}}}var fe=function(e){return I(void 0,void 0,void 0,(function(){var t,n,i,s;return C(this,(function(o){if(Y())return[2];switch(t=e.context,n=t.eventName,i=t.data,s=ie(),n){case r.INIT:de(!i.hasOpener);break;case r.CANCEL:case r.SUBMIT:Z(!0),V(n,i),null==s||s.reply(e,{eventName:n});break;case r.CLOSE:!1===Y()&&(Z(!0),V(r.CLOSE,{})),me()}return[2]}))}))};function le(){window.clearInterval(_),window.clearInterval(te()),window.removeEventListener("message",ce)}function de(e){if(void 0===e&&(e=!1),le(),Z(!1),e){var t=ee();t&&(t.close(),$(null))}}function me(){return I(this,void 0,void 0,(function(){var e;return C(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 ve(e){return I(this,void 0,void 0,(function(){var t,n,f,l,d,m,p,g,b,S;return C(this,(function(y){switch(y.label){case 0:return(t=w(e.url))?(de(!0),[4,me()]):[2,Promise.reject(v(a,"params.url must be liff url"))];case 1:return y.sent(),n=e.url,f=e.appData,(l=new URL(n)).searchParams.append(u,"true"),[4,re()];case 2:return d=y.sent(),l.searchParams.append(W,d.identification.identifier),l.searchParams.append(A,d.identification.cryptoKey),l.hostname=function(e){var t=P(e.split(".")),n=t[0],r=t.slice(1);return O([n+"-ext"],r).join(".")}(l.hostname),m=l.toString(),$("ios"!==E()||h()?window.open("","liffsubwindow","width=480, height=640, menubar=no, toolbar=no, scrollbars=yes"):window.open()),[4,K(t)];case 3:if(p=y.sent(),g=p.origin,b=p.subwindowCommonModule,!(S=ee()))throw v(i);return b?(function(e){z=e}(g),d.listen(fe),d.setData("appData",f),window.addEventListener("message",ce),S.location.href=m,I=function(e,t){var n=ee(),r={type:c};return t&&(r.message=JSON.stringify(t)),window.setInterval((function(){null==n||n.postMessage(r,e)}),s)}(g,f),q=I,function(e){_=e}(window.setInterval((function(){var e=ee();e&&e.closed&&(le(),$(null),!1===Y()&&(Z(!0),V(r.CLOSE,{})))}),o)),[2]):(S.close(),[2])}var I}))}))}function pe(e){return I(this,void 0,void 0,(function(){var t,n,i,s,o,u,c,l,d,m,p;return C(this,(function(h){switch(h.label){case 0:t=e.msit,n=e.mstChallenge,i=e.onSuccess,s=e.onError,o=e.reconnectCount,u=void 0===o?0:o,h.label=1;case 1:return h.trys.push([1,3,,6]),[4,j(T("subWindowSubscribe"),{method:"POST",body:JSON.stringify({msit:t,mstChallenge:n})})];case 2:return c=h.sent(),[3,6];case 3:return h.sent(),[4,he()];case 4:return h.sent(),[4,ge(pe,{msit:t,mstChallenge:n,onSuccess:i,onError:s,reconnectCount:u+=1})];case 5:return h.sent(),[2];case 6:return c.status>=500?[4,he()]:[3,9];case 7:return h.sent(),[4,ge(pe,{msit:t,mstChallenge:n,onSuccess:i,onError:s,reconnectCount:u+=1})];case 8:return h.sent(),[3,20];case 9:return c.status>=400&&500>c.status?[4,we(c)]:[3,11];case 10:return(d=h.sent())?(l=d.errorDetail,s(v(a,l))):s(v(f,"Some error happened in the server")),[3,20];case 11:return 200!==c.status?[3,19]:[4,we(c)];case 12:return(d=h.sent())?[3,13]:(s(v(f,"Some error happened in the server")),[3,18]);case 13:switch(m=d.status,p=d.result,m){case r.ERROR:return[3,14];case r.CLOSE:case r.CANCEL:case r.SUBMIT:return[3,16]}return[3,17];case 14:return[4,ge(pe,{msit:t,mstChallenge:n,onSuccess:i,onError:s,reconnectCount:u})];case 15:return h.sent(),[3,18];case 16:return i(m,p),[3,18];case 17:s(v(f,"Some error happened in the server")),h.label=18;case 18:return[3,20];case 19:s(v(f,"Some error happened in the server")),h.label=20;case 20:return[2]}}))}))}function he(){return new Promise((function(e){return setTimeout(e,1e3)}))}function we(e){return I(this,void 0,void 0,(function(){return C(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,e.json()];case 1:return[2,t.sent()];case 2:return t.sent(),[2,null];case 3:return[2]}}))}))}function ge(e,t){return I(this,void 0,void 0,(function(){return C(this,(function(n){switch(n.label){case 0:return t.reconnectCount>=10?(t.onError(v(f,"Failed to connect")),[3,3]):[3,1];case 1:return[4,e(t)];case 2:n.sent(),n.label=3;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 Se={height:"full",closeButtonPosition:"right",closeButtonColor:"black",closeButtonLabel:""};function ye(e){var t=e.appData,n=e.native,i=M().liffId,s=B(),o=w(e.url);if(!i)return Promise.reject(v(l,"liffId is invalid"));if(!s)return Promise.reject(v(l,"mst_challenge is invalid"));if(!o)return Promise.reject(v(a,"params.url must be liff url"));var u=Object.assign({},Se,n);return function(e){var t=e.mainLiffId,n=e.subLiffId,r=e.mstChallenge,i=e.appData,s=e.view;return t&&r?N(T("subWindowGetMSIT"),{method:"POST",body:JSON.stringify({mainLiffId:t,subLiffId:n,mstChallenge:r,appData:i,view:s})}):Promise.reject(v(a,"no proper argument"))}({mainLiffId:i,subLiffId:o,mstChallenge:s,appData:t,view:be(u)}).then((function(t){var n=t.msit;pe({msit:n,mstChallenge:s,onSuccess:function(e,t){V(e,t)},onError:function(e){V(r.ERROR,e)}}),function(e,t){var n=e.url,r=new URLSearchParams;r.set("msit",t),location.href=d+"?url="+encodeURIComponent(n)+"&"+r.toString()}(e,n)}))}function Ie(){if(!x())throw v(l,"this api can be only called in child window")}function Ce(e){if(!e.mst||!e.status)return Promise.reject(v(a,"no proper argument"));var t=JSON.stringify(e);return N(T("subWindowPost"),{method:"POST",body:t})}function Oe(e){var t=e.msit,n=e.mstVerifier;return t&&n?N(T("subWindowGetMSTByMSIT"),{method:"POST",body:JSON.stringify({msit:t,mstVerifier:n})}):Promise.reject(v(a,"no proper argument"))}function Pe(e){var t=e.mst;return t?N(T("subWindowGetAppData"),{method:"POST",body:JSON.stringify({mst:t})}):Promise.reject(v(a,"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){if(!y("subwindowOpen"))throw v(m,"No permission for liff.subWindow.open()");if(!g()&&b())throw v(m,"Subwindow is not supported in this browser");return function(){if(x())throw v(l,"this api can be only called in parent window")}(),S()?ye(e):ve(e)},cancel:function(e){return void 0===e&&(e={}),Ie(),S()?function(e){return void 0===e&&(e={}),I(this,void 0,void 0,(function(){var t,n;return C(this,(function(i){switch(i.label){case 0:return(t=D())?[4,Ce({mst:t,status:r.CANCEL,result:e})]:[2,Promise.reject(v(l,"mst is invalid"))];case 1:return n=i.sent(),Z(!0),[2,n]}}))}))}(e):function(e){return void 0===e&&(e={}),ae(r.CANCEL,e)}(e)},submit:function(e){return void 0===e&&(e={}),Ie(),S()?function(e){return void 0===e&&(e={}),I(this,void 0,void 0,(function(){var t,n;return C(this,(function(i){switch(i.label){case 0:return(t=D())?[4,Ce({mst:t,status:r.SUBMIT,result:e})]:[2,Promise.reject(v(l,"mst is invalid"))];case 1:return n=i.sent(),Z(!0),[2,n]}}))}))}(e):function(e){return void 0===e&&(e={}),ae(r.SUBMIT,e)}(e)},close:function(){return Ie(),S()?function(){return I(this,void 0,void 0,(function(){var e;return C(this,(function(t){switch(t.label){case 0:return!1!==Y()?[3,2]:(e=D())?[4,Ce({mst:e,status:r.CLOSE,result:{}})]:[2,Promise.reject(v(l,"mst is invalid"))];case 1:t.sent(),t.label=2;case 2:return k(),[2]}}))}))}():function(){return I(this,void 0,void 0,(function(){var e;return C(this,(function(t){return(null==(e=ie())?void 0:e.isReady())?(e.send({eventName:r.CLOSE}),[2,new Promise((function(e){setTimeout((function(){k(),e()}),s)}))]):(k(),[2,Promise.resolve()])}))}))}()},getAppData:function(){return Ie(),function(){var e,t=J();try{e=t?JSON.parse(t):{}}catch(n){e={}}return Promise.resolve(e)}()}};export{Pe as getAppData,Oe as getMSTByMSIT,ue as getMainWindowOrigin,ie as getMessageBus,re as initMessageBus,oe as setMainWindowOrigin,Le as subWindow}; |
{ | ||
"name": "@liff/sub-window", | ||
"version": "2.21.0", | ||
"version": "2.21.1", | ||
"description": "@liff/sub-window", | ||
@@ -22,8 +22,8 @@ "license": "SEE LICENSE IN README.md", | ||
"typecheck": "tsc --noEmit", | ||
"build": "rollup -c", | ||
"build:npm": "yarn build", | ||
"build:cdn": "rollup -c rollup.config.js", | ||
"build:npm": "rollup -c rollup.config.js", | ||
"build:watch": "yarn build:npm -w --watch.include './src/**'", | ||
"dev-server": "yarn build:watch", | ||
"dev-server:https": "yarn build:watch", | ||
"build:prerelease": "yarn build rollup.config.prerelease.js", | ||
"build:prerelease": "rollup -c rollup.config.prerelease.js", | ||
"clean": "rm -rf ./lib ./tsconfig.tsbuildinfo" | ||
@@ -35,15 +35,15 @@ }, | ||
"dependencies": { | ||
"@liff/close-window": "2.21.0", | ||
"@liff/consts": "2.21.0", | ||
"@liff/get-os": "2.21.0", | ||
"@liff/is-api-available": "2.21.0", | ||
"@liff/is-in-client": "2.21.0", | ||
"@liff/is-sub-window": "2.21.0", | ||
"@liff/logger": "2.21.0", | ||
"@liff/message-bus": "2.21.0", | ||
"@liff/server-api": "2.21.0", | ||
"@liff/store": "2.21.0", | ||
"@liff/util": "2.21.0" | ||
"@liff/close-window": "2.21.1", | ||
"@liff/consts": "2.21.1", | ||
"@liff/get-os": "2.21.1", | ||
"@liff/is-api-available": "2.21.1", | ||
"@liff/is-in-client": "2.21.1", | ||
"@liff/is-sub-window": "2.21.1", | ||
"@liff/logger": "2.21.1", | ||
"@liff/message-bus": "2.21.1", | ||
"@liff/server-api": "2.21.1", | ||
"@liff/store": "2.21.1", | ||
"@liff/util": "2.21.1" | ||
}, | ||
"gitHead": "48d3b37f4128b609b861415093b81e06482ba10f" | ||
"gitHead": "9af2e0816a5cd5547b99e8cadba3ee274b270a9a" | ||
} |
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
33724
+ Added@liff/close-window@2.21.1(transitive)
+ Added@liff/consts@2.21.1(transitive)
+ Added@liff/get-line-version@2.21.1(transitive)
+ Added@liff/get-os@2.21.1(transitive)
+ Added@liff/hooks@2.21.1(transitive)
+ Added@liff/is-api-available@2.21.1(transitive)
+ Added@liff/is-in-client@2.21.1(transitive)
+ Added@liff/is-logged-in@2.21.1(transitive)
+ Added@liff/is-sub-window@2.21.1(transitive)
+ Added@liff/logger@2.21.1(transitive)
+ Added@liff/message-bus@2.21.1(transitive)
+ Added@liff/native-bridge@2.21.1(transitive)
+ Added@liff/server-api@2.21.1(transitive)
+ Added@liff/store@2.21.1(transitive)
+ Added@liff/types@2.21.1(transitive)
+ Added@liff/use@2.21.1(transitive)
+ Added@liff/util@2.21.1(transitive)
- Removed@liff/close-window@2.21.0(transitive)
- Removed@liff/consts@2.21.0(transitive)
- Removed@liff/get-line-version@2.21.0(transitive)
- Removed@liff/get-os@2.21.0(transitive)
- Removed@liff/hooks@2.21.0(transitive)
- Removed@liff/is-api-available@2.21.0(transitive)
- Removed@liff/is-in-client@2.21.0(transitive)
- Removed@liff/is-logged-in@2.21.0(transitive)
- Removed@liff/is-sub-window@2.21.0(transitive)
- Removed@liff/logger@2.21.0(transitive)
- Removed@liff/message-bus@2.21.0(transitive)
- Removed@liff/native-bridge@2.21.0(transitive)
- Removed@liff/server-api@2.21.0(transitive)
- Removed@liff/store@2.21.0(transitive)
- Removed@liff/types@2.21.0(transitive)
- Removed@liff/use@2.21.0(transitive)
- Removed@liff/util@2.21.0(transitive)
Updated@liff/close-window@2.21.1
Updated@liff/consts@2.21.1
Updated@liff/get-os@2.21.1
Updated@liff/is-in-client@2.21.1
Updated@liff/is-sub-window@2.21.1
Updated@liff/logger@2.21.1
Updated@liff/message-bus@2.21.1
Updated@liff/server-api@2.21.1
Updated@liff/store@2.21.1
Updated@liff/util@2.21.1