underbbs/frontend/ts/util.ts

42 lines
No EOL
976 B
TypeScript

import { DatagramSocket } from './websocket'
import { BatchTimer } from './batch-timer'
function _(key: string, value: any | null | undefined = undefined): any | null {
const x = <any>window;
if (value !== undefined) {
x[key] = value;
}
return x[key];
}
function $(id: string): HTMLElement | null {
return document.getElementById(id);
}
function errMsg(msg: string): void {
const div = $("err_div");
const w = $("err_wrapper");
if (div && w) {
div.innerText = msg;
w.style.display = "block";
}
}
function closeErr(): void {
const w = $("err_wrapper");
if (w) {
w.style.display = "none";
}
}
async function authorizedFetch(method: string, uri: string, body: any): Promise<Response> {
const headers = new Headers()
headers.set('Authorization', 'Bearer ' + DatagramSocket.skey)
return await fetch(uri, {
method: method,
headers: headers,
body: body,
})
}
export default { _, $, authorizedFetch, errMsg, closeErr }