Skip to content

Commit

Permalink
Proxy friendly API calls (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoboMagus authored Nov 9, 2022
1 parent afad4eb commit 7e1b00b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
19 changes: 10 additions & 9 deletions v2/esp-app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LitElement, html, css, PropertyValues } from "lit";
import { customElement, state, query } from "lit/decorators.js";
import { getBasePath } from "./esp-entity-table";

import "./esp-entity-table";
import "./esp-log";
Expand All @@ -8,13 +9,7 @@ import "./esp-logo";
import cssReset from "./css/reset";
import cssButton from "./css/button";

function getEventsUrl(){
url = window.location.pathname;
url += url.endsWith("/") ? "" : "/";
return url + "events";
};

window.source = new EventSource(getEventsUrl());
window.source = new EventSource(getBasePath() + "/events");

interface Config {
ota: boolean;
Expand Down Expand Up @@ -84,12 +79,18 @@ export default class EspApp extends LitElement {
}

ota() {
if (this.config.ota)
if (this.config.ota) {
let basePath = getBasePath();
return html`<h2>OTA Update</h2>
<form method="POST" action="/update" enctype="multipart/form-data">
<form
method="POST"
action="${basePath}/update"
enctype="multipart/form-data"
>
<input class="btn" type="file" name="update" />
<input class="btn" type="submit" value="Update" />
</form>`;
}
}

render() {
Expand Down
9 changes: 8 additions & 1 deletion v2/esp-entity-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ interface entityConfig {
speed: string;
}

export function getBasePath() {
let str = window.location.pathname;
return str.endsWith("/") ? str.slice(0, -1) : str;
}

let basePath = getBasePath();

@customElement("esp-entity-table")
export class EntityTable extends LitElement {
@state({ type: Array, reflect: true }) entities: entityConfig[] = [];
Expand Down Expand Up @@ -243,7 +250,7 @@ export class EntityTable extends LitElement {
}

restAction(entity: entityConfig, action: String) {
fetch(`/${entity.domain}/${entity.id}/${action}`, {
fetch(`${basePath}/${entity.domain}/${entity.id}/${action}`, {
method: "POST",
body: "true",
}).then((r) => {
Expand Down

0 comments on commit 7e1b00b

Please sign in to comment.