UNPKG

@ng-org/web

Version:

JS/TS SDK of NextGraph for third-party web apps

159 lines (158 loc) 4.6 kB
function a(s, o) { let i = s; for (let t = 0; t < o.length; ++t) i = i[o[t]]; return i; } function g(s, o, i) { if (o.length === 0) throw new Error("Cannot assign target object itself"); let t = s; for (let l = 0; l < o.length; ++l) l === o.length - 1 ? t[o[l]] = i : t = t[o[l]]; return !0; } function P(s, o) { if (o.length === 0) throw new Error("Cannot remove target object itself"); let i = s; for (let t = 0; t < o.length; ++t) t === o.length - 1 ? delete i[o[t]] : i = i[o[t]]; return !0; } function h(s = /* @__PURE__ */ Object.create(null), o = /* @__PURE__ */ Object.create(null)) { class i extends Function { } const t = { async get(e, n) { return a(e, n); }, async apply(e, n, r, c) { return Reflect.apply(a(e, n), r, c); }, async set(e, n, r) { g(e, n, r); }, async deleteProperty(e, n) { P(e, n); }, async construct(e, n, r) { return Reflect.construct(a(e, n), r); }, async setPrototypeOf(e, n, r) { Reflect.setPrototypeOf(a(e, n), r); }, async getPrototypeOf(e, n) { return Reflect.getPrototypeOf(a(e, n)); }, async defineProperty(e, n, r, c) { Reflect.defineProperty(a(e, n), r, c); }, ...o }; function l(e = []) { return new Proxy(new i(), { get(n, r) { switch (r) { case "then": case "catch": case "finally": const c = (async () => await t.get(s, e))(); return c[r].bind(c); default: return l([...e, r]); } }, apply(n, r, c) { return t.apply(s, e, r, c); }, set(n, r, c) { return t.set(s, [...e, r], c), !0; }, deleteProperty(n, r) { return t.deleteProperty(s, [...e, r]), !0; }, construct(n, r) { return t.construct(s, e, r); }, setPrototypeOf(n, r) { return t.setPrototypeOf(s, e, r), !0; }, getPrototypeOf(n) { return t.getPrototypeOf(s, e); }, defineProperty(n, r, c) { return t.defineProperty(s, e, r, c), !0; }, has(n, r) { throw Error('Async proxy does not support "has" handler.'); }, getOwnPropertyDescriptor(n, r) { throw Error('Async proxy does not support "getOwnPropertyDescriptor" handler.'); }, ownKeys(n) { throw Error('Async proxy does not support "ownKeys" handler.'); }, preventExtensions(n) { throw Error('Async proxy does not support "preventExtensions" handler.'); }, isExtensible(n) { throw Error('Async proxy does not support "isExtensible" handler.'); } }); } return l(); } let p = !1; const y = "nextgraph.net", d = { redirect: `https://${y}/redir/#/?o=`, origin: `https://${y}` }, x = async function(s, o, i) { if (!window) throw new Error("init(callback,..) can only be called from a browser's window"); if (window.self === window.top) window.location.href = d.redirect + encodeURIComponent(window.location.href); else if (p === !1) { const { port1: t, port2: l } = new MessageChannel(); t.onmessage = async (e) => { e.data.ok && (p = e.data.ret, s && await s({ status: "loggedin", session: p })), t.close(); }, parent.postMessage({ method: "init", origin: window.location.href, singleton: o, access_requests: i, port: l }, d.origin, [l]); } else throw new Error("you must call init() only once"); }, _ = { doc_subscribe: 2, orm_start: 3 }; function b(s, o) { const { port1: i, port2: t } = new MessageChannel(); let l = _[s]; if (l) { let e = o[l], n = o.slice(0, -1); return parent.postMessage({ method: s, args: n, port: t }, d.origin, [t]), new Promise((c, w) => { let u = !1; i.onmessage = (f) => { if (f.data.stream) u || (c(() => { i.close(); }), u = !0), e(f.data.ret); else if (!f.data.ok) if (!u) w(f.data.ret), u = !0; else throw new Error(f.data.ret); }; }); } else return parent.postMessage({ method: s, args: o, port: t }, d.origin, [t]), new Promise((e, n) => { i.onmessage = (r) => { r.data.ok ? e(r.data.ret) : (console.error("method " + s + " with args ", o, " failed with ", r.data.ret), n(new Error(r.data.ret))); }; }); } const E = { apply(s, o, i, t) { if (p === !1) throw new Error("you must call init() first (and once)"); return b(o[0], t); } }, O = h({}, E); export { x as init, O as ng };