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