@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
141 lines (140 loc) • 3.62 kB
JavaScript
import { createLongLivedTokenAuth as R, createConnection as l, getAuth as _, ERR_CANNOT_CONNECT as u, ERR_INVALID_AUTH as h, ERR_INVALID_HTTPS_TO_HTTP as y, ERR_HASS_HOST_REQUIRED as T, ERR_CONNECTION_LOST as E } from "home-assistant-js-websocket";
import { saveTokens as d, clearTokens as C, loadTokens as p } from "./token-storage.js";
function i(t, n) {
const r = (() => {
switch (t) {
case h:
return `ERR_INVALID_AUTH: Invalid authentication. ${n ? 'Check your "Long-Lived Access Token".' : ""}`;
case u:
return "ERR_CANNOT_CONNECT: Unable to connect";
case E:
return "ERR_CONNECTION_LOST: Lost connection to home assistant.";
case T:
return "ERR_HASS_HOST_REQUIRED: Please enter a Home Assistant URL.";
case y:
return 'ERR_INVALID_HTTPS_TO_HTTP: Cannot connect to Home Assistant instances over "http://".';
default:
return null;
}
})();
return r !== null ? r : t?.error || t?.message || `Unknown Error (${t})`;
}
function f() {
try {
return typeof window < "u" ? window.top?.hassConnection : void 0;
} catch (t) {
console.error("Error getting inherited connection", t);
return;
}
}
function g(t, n) {
const o = location && location.search.includes("auth_callback=1"), r = !!f(), c = !!n, a = !!p(t, !1);
switch (!0) {
case o:
return "auth-callback";
case r:
return "inherited-auth";
case c:
return "provided-token";
case a:
return "saved-tokens";
default:
return "user-request";
}
}
const N = async (t, n) => {
const o = g(t, n);
if (o === "inherited-auth")
try {
const { auth: e, conn: s } = await f();
return {
type: "success",
connection: s,
auth: e
};
} catch (e) {
return {
type: "error",
error: i(e, n)
};
}
if (o === "provided-token" && n)
try {
const e = await R(t, n);
return {
type: "success",
connection: await l({ auth: e }),
auth: e
};
} catch (e) {
return {
type: "error",
error: i(e, n)
};
}
const r = {
saveTokens: d,
loadTokens: () => Promise.resolve(p(t))
};
if (t && o === "user-request") {
if (r.hassUrl = t, r.hassUrl === "")
return {
type: "error",
error: "Please enter a Home Assistant URL."
};
if (r.hassUrl.indexOf("://") === -1)
return {
type: "error",
error: "Please enter your full URL, including the protocol part (https://)."
};
try {
new URL(r.hassUrl);
} catch (e) {
return console.error("Error:", e), {
type: "error",
error: "Invalid URL"
};
}
}
let c;
try {
c = await _(r);
} catch (e) {
return e?.error === "invalid_grant" ? (C(), N(t, n)) : o === "saved-tokens" && e === u ? {
type: "failed",
cannotConnect: !0
} : {
type: "error",
error: i(e, n)
};
} finally {
typeof window < "u" && location && location.search.includes("auth_callback=1") && history.replaceState(null, "", location.pathname);
}
let a;
try {
a = await l({ auth: c });
} catch (e) {
if (o === "saved-tokens") {
if (e === u)
return {
type: "failed",
cannotConnect: !0
};
e === h && d(null);
}
return {
type: "error",
error: i(e, n)
};
}
return {
type: "success",
connection: a,
auth: c
};
};
export {
i as handleError,
N as tryConnection
};
//# sourceMappingURL=tryConnection.js.map