Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Oct 19, 2023
1 parent a9444e6 commit 0c4f78f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
39 changes: 20 additions & 19 deletions shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import {
convertDashToCamel,
createTemplate,
getJwt,
goHome,
isHtmlElement,
isNotNull,
isNull,
isObject,
isString,
isTrue,
removeJwt,
stringify,
} from "./util.js";
import { makeRpcCall } from "./deps.js";
Expand Down Expand Up @@ -231,9 +229,8 @@ export class Shadow extends HTMLElement {
const jwtOrNull = hasJwtEnabled ? getJwt("jwt") : null;
const realUrlOrPath = hasJwtEnabled ? urlOrPath.slice(5) : urlOrPath;
const url = new URL(realUrlOrPath, location.href).href;
if (hasJwtEnabled && isNull(jwtOrNull)) {
console.error(`No jwt has been stored.`);
goHome();
if (hasJwtEnabled && !isString(jwtOrNull)) {
throw new Error(`The required jwt could not be found.`);
}
return [url, jwtOrNull];
}
Expand Down Expand Up @@ -268,14 +265,15 @@ export class Shadow extends HTMLElement {
} else {
return this.htmlData = createTemplate(await response.text());
}
} else if (response.status === 401 && isNotNull(jwt)) {
console.error(`Received status code ${response.status}.`);
removeJwt(this._jwtKeyName);
goHome();
} else {
throw new Error(
`Received status code ${response.status} instead of 200-299 range.`,
this.dispatchEvent(
new CustomEvent("fetchError", {
bubbles: true,
composed: true,
detail: { response },
}),
);
throw new Error(`Received http status code ${response.status}.`);
}
} catch (error) {
throw new ShadowError(error.message);
Expand All @@ -296,8 +294,8 @@ export class Shadow extends HTMLElement {
? devUrlOrNull
: this.getAttribute("rpc-url");
if (isString(urlOrPath)) {
const [url, jwt] = this._getUrlAndJwt(urlOrPath);
try {
const [url, jwt] = this._getUrlAndJwt(urlOrPath);
const result = await makeRpcCall(url)(
{ method, params: /**@type {any}*/ (this)[property] },
isNull(jwt) ? undefined : { jwt },
Expand All @@ -307,14 +305,17 @@ export class Shadow extends HTMLElement {
} else {
throw new Error("The rpc result is not an object.");
}
} catch (error) {
if (isString(jwt)) {
removeJwt(this._jwtKeyName);
goHome();
}
} catch (rpcError) {
this.dispatchEvent(
new CustomEvent("rpcError", {
bubbles: true,
composed: true,
detail: { rpcError },
}),
);
const errorMessage =
`Received rpc error code ${error?.code} with the message: "${error?.message}"${
error?.data ? ":\n" + error.data : "."
`Received rpc error code ${rpcError?.code} with the message: "${rpcError?.message}"${
rpcError?.data ? ":\n" + rpcError.data : "."
}`;
throw new ShadowError(errorMessage);
}
Expand Down
20 changes: 1 addition & 19 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export function isHtmlElement(input) {
}

/**
* isTemplate.
* @param {unknown} input
* @returns {input is HTMLTemplateElement}
*/
Expand All @@ -103,26 +102,9 @@ export function isTemplate(input) {
}

/**
* removeJwt.
*
* @param {string} keyName
*/
export function removeJwt(keyName) {
window.localStorage.removeItem(keyName);
}

/**
* getJwt.
*
* @param {string} keyName
* @returns {string | null }
*/
export function getJwt(keyName) {
return window.localStorage.getItem(keyName);
}

/**
* goHome.
*/
export function goHome() {
window.location.href = window.location.origin;
}

0 comments on commit 0c4f78f

Please sign in to comment.