@interlinklabs/test-mdk
Version:
Interlink Labs MDK – universal React/TS SDK
2 lines (1 loc) • 5.31 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("cookie"),t=require("react/jsx-runtime"),n=require("react"),i=require("jwt-decode");const a="mini-app-2025",r=globalThis.fetch,o=async(e,t,n)=>{const i=await r("https://mini-app.interlinklabs.ai/api/ads/intent",{method:"POST",headers:{"Content-Type":"application/json","api-public":a,Authorization:`Bearer ${e}`},body:JSON.stringify({rewardType:t,value:n})});if(!i.ok)throw new Error("createIntent failed");const o=await i.json(),s=o?.data?.intentId??o?.intentId,c=o?.data?.challenge??o?.challenge;if(!s||!c)throw new Error("INVALID_INTENT_RESPONSE");return{intentId:s,challenge:c}},s=async(e,t)=>{const n=await r("https://mini-app.interlinklabs.ai/api/ads/claim",{method:"POST",headers:{"Content-Type":"application/json","api-public":a,Authorization:`Bearer ${e}`},body:JSON.stringify({intentId:t})});if(!n.ok)throw new Error("claim failed");return n.json()};let c=null;const l=()=>c;let u,d;function p(e,t){u=e,d=t,c=w}const w={get:t=>u?.headers.cookie?e.parse(u.headers.cookie)[t]??null:null,set(t,n,i=1){if(!d)return;const a=new Date(Date.now()+864e5*i);d.setHeader("Set-Cookie",e.serialize(t,n,{path:"/",expires:a,httpOnly:!1}))},del(t){d&&d.setHeader("Set-Cookie",e.serialize(t,"",{path:"/",expires:new Date(0)}))}};var f=Object.freeze({__proto__:null,cookieDriver:w,init:p});const h=globalThis.fetch;function E(e){try{return i.jwtDecode(e).loginId??null}catch{return null}}var _;!function(e){e.G_PINK="G_PINK",e.G_VIOLET="G_VIOLET",e.G_STEEL="G_STEEL",e.G_BLUE="G_BLUE",e.G_PURPLE="G_PURPLE",e.G_PEACH="G_PEACH",e.PINK="PINK",e.BLUE="BLUE",e.PURPLE="PURPLE",e.GREEN="GREEN",e.YELLOW="YELLOW",e.ORANGE="ORANGE",e.WHITE="WHITE"}(_||(_={}));const y="undefined"!=typeof window;let g=!1,k=null,N=null,m=null;const O=function({appid:e,onSuccess:i,onFailure:a,children:r}){const[o,l]=n.useState(null),[u,d]=n.useState(null),[p,w]=n.useState(!1),[E,_]=n.useState(null),[g,k]=n.useState(null),N=`jwt_${e}`;n.useEffect((()=>{(async function(){return c||(c="undefined"!=typeof window?(await Promise.resolve().then((function(){return I}))).cookieDriver:(await Promise.resolve().then((function(){return f}))).cookieDriver,c)})().then(l)}),[]);const m=()=>o?.get(N)??null,O=async t=>{const n=m()||t?.token;if(!n)return o?.del(N),w(!1),void a();await async function(e,t){try{const n=await h("https://mini-app.interlinklabs.ai/api/tracking/validate-token",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:e,appId:t})});return!!n.ok&&!!await n.json()}catch{return!1}}(n,e)?(o?.set(N,n,1),w(!0),i()):(o?.del(N),w(!1),a())};n.useEffect((()=>{if(!o)return;O();const e=e=>{try{const t=JSON.parse(e.data);switch(t.action){case"pass":O(t);break;case"fail":a();break;case"ad_verified":v();break;case"ad_closed":g?.("USER_CLOSED")}}catch{}};return y&&window.addEventListener("message",e),()=>{y&&window.removeEventListener("message",e)}}),[o]);const v=async()=>{const e=m();if(!e||!u)return g?.("NO_TOKEN_OR_INTENT");try{E?.(await s(e,u.intentId))}catch(e){g?.(e)}finally{d(null),_(null),k(null)}};return!o||p?null:t.jsx(t.Fragment,{children:r({open:()=>{y&&window.ReactNativeWebView?.postMessage(JSON.stringify({action:"pass",appid:e}))}})})};O.getLoginId=e=>{const t=l();return E(t?.get(`jwt_${e}`)??"")},O.getUserName=async function(e){try{const t=await h(`https://mini-app.interlinklabs.ai/api/tracking/profile/${e}`);if(!t.ok)return null;const n=await t.json();return n?.data?.username??null}catch{return null}},O.logOut=e=>{l()?.del(`jwt_${e}`)},O.getProfileUser=async e=>{try{const t=l(),n=t?.get(`jwt_${e}`);if(!n)return null;const i=E(n);if(!i)return null;const a=await fetch(`https://mini-app.interlinklabs.ai/api/tracking/profile/${i}`);if(!a.ok)return null;const r=await a.json();return r?.data}catch{return null}},O.showRewardedAd=async(e,t,n,i,a)=>{const r=l(),s=r?.get(`jwt_${e}`);if(!s)return a("NO_TOKEN");try{const r=await o(s,t,n);N=i,m=a,y&&window.ReactNativeWebView&&window.ReactNativeWebView.postMessage(JSON.stringify({action:"show_ad",challenge:r.challenge,rewardType:t,value:n,intentId:r.intentId,appid:e}))}catch(e){a(e)}},O.claimIntent=s,O.enableAdEvents=function(e={}){if(g||"undefined"==typeof window)return;const{autoClaim:t=!0,onVerified:n,onClosed:i}=e;k=async e=>{if("string"!=typeof e.data)return;let a;try{a=JSON.parse(e.data)}catch{return}switch(a.action){case"ad_verified":{const{intentId:e}=a;if(!e)return;if(n)n(a);else if(t)try{const t=document.cookie.split("; ").find((e=>e.startsWith(`jwt_${a.appid}=`)))?.split("=")[1]??"";if(!t)throw new Error("NO_TOKEN");const n=await O.claimIntent(t,e);N?.(n)}catch(e){m?.(e)}break}case"ad_closed":i?i():m?.("USER_CLOSED")}},window.addEventListener("message",k),g=!0},O.disableAdEvents=function(){g&&k&&(window.removeEventListener("message",k),k=null,g=!1)};const v={get(e){const t=`; ${document.cookie}`.split(`; ${e}=`);return 2===t.length?t.pop().split(";")[0]??null:null},set(e,t,n=1){const i=new Date(Date.now()+864e5*n).toUTCString();document.cookie=`${e}=${t}; expires=${i}; path=/`},del(e){document.cookie=`${e}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`}};var I=Object.freeze({__proto__:null,cookieDriver:v});exports.Mdk=O,exports.claimIntent=s,exports.createIntent=o,exports.default=O,exports.initNodeCookies=p;