2021-04-04 16:34:10 +02:00
|
|
|
import {
|
|
|
|
AccessPoint,
|
|
|
|
AccessPointCreateResponse,
|
|
|
|
cSite,
|
|
|
|
LoginResponse,
|
|
|
|
Site,
|
|
|
|
token,
|
|
|
|
} from "./types";
|
2021-04-03 23:21:58 +02:00
|
|
|
import { get as idbGet, set as idbSet, del as idbDel } from "idb-keyval";
|
|
|
|
|
|
|
|
export * from "./types";
|
|
|
|
|
|
|
|
const enum HTTPMethod {
|
|
|
|
GET = "GET",
|
|
|
|
POST = "POST",
|
|
|
|
PATCH = "PATCH",
|
|
|
|
}
|
2021-04-03 17:34:28 +02:00
|
|
|
|
|
|
|
const url = "http://192.168.178.5:8080/api";
|
|
|
|
let token: token;
|
|
|
|
let ready = false;
|
|
|
|
|
|
|
|
export async function init() {
|
|
|
|
if (ready) return;
|
|
|
|
|
2021-04-03 23:21:58 +02:00
|
|
|
await idbGet("token").then((val: token) => {
|
2021-04-03 17:34:28 +02:00
|
|
|
if (val !== undefined) {
|
|
|
|
token = val;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
|
2021-04-03 23:21:58 +02:00
|
|
|
export async function logout(): Promise<void> {
|
|
|
|
token = { jwt: "", expiration: 0 };
|
|
|
|
await idbDel("token");
|
|
|
|
}
|
|
|
|
|
2021-04-03 17:34:28 +02:00
|
|
|
export async function isLoggedIn(): Promise<boolean> {
|
|
|
|
await init();
|
2021-04-03 23:21:58 +02:00
|
|
|
return token?.expiration > Date.now();
|
2021-04-03 17:34:28 +02:00
|
|
|
}
|
|
|
|
|
2021-04-03 23:21:58 +02:00
|
|
|
interface ReqOptions extends RequestInit {
|
|
|
|
method: HTTPMethod;
|
|
|
|
auth?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function request<T = unknown>(
|
|
|
|
endpoint: string,
|
|
|
|
options: ReqOptions,
|
|
|
|
data?: any
|
|
|
|
): Promise<T> {
|
|
|
|
if (options.auth) {
|
|
|
|
await init();
|
|
|
|
options.headers = {
|
|
|
|
...options.headers,
|
|
|
|
Authorization: `Bearer ${token.jwt}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
options.method === HTTPMethod.POST ||
|
|
|
|
options.method === HTTPMethod.PATCH
|
|
|
|
) {
|
|
|
|
options.headers = {
|
|
|
|
...options.headers,
|
2021-04-03 17:34:28 +02:00
|
|
|
"Content-Type": "application/json",
|
2021-04-03 23:21:58 +02:00
|
|
|
};
|
|
|
|
options.body = JSON.stringify(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await fetch(`${url}${endpoint}`, {
|
|
|
|
...options,
|
|
|
|
headers: { ...options.headers, Accept: "application/json" },
|
2021-04-03 17:34:28 +02:00
|
|
|
});
|
2021-04-03 23:21:58 +02:00
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function login(
|
|
|
|
username: string,
|
|
|
|
password: string
|
|
|
|
): Promise<boolean> {
|
|
|
|
const login = await request<LoginResponse>(
|
|
|
|
"/auth",
|
|
|
|
{ method: HTTPMethod.POST },
|
|
|
|
{ username, password }
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!login.success) {
|
|
|
|
throw new Error(login.message);
|
2021-04-03 17:34:28 +02:00
|
|
|
}
|
2021-04-03 23:21:58 +02:00
|
|
|
token = {
|
|
|
|
jwt: login.token,
|
|
|
|
expiration: Date.parse(login.expiration),
|
|
|
|
};
|
|
|
|
idbSet("token", token).catch((error) => console.error(error));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSites(): Promise<Site[]> {
|
|
|
|
return request<Site[]>("/sites", { auth: true, method: HTTPMethod.GET });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createSite(site: Site) {
|
2021-04-04 16:34:10 +02:00
|
|
|
return request<cSite>(
|
|
|
|
"/sites",
|
|
|
|
{ auth: true, method: HTTPMethod.POST },
|
|
|
|
site
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAccessPointDetails(
|
|
|
|
sideID?: number
|
|
|
|
): Promise<AccessPoint[]> {
|
|
|
|
const endpoint = `${sideID ? "/sites/" + sideID : ""}/aps`;
|
|
|
|
return request<AccessPoint[]>(endpoint, {
|
|
|
|
auth: true,
|
|
|
|
method: HTTPMethod.GET,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAccessPointByID(id: number): Promise<AccessPoint> {
|
|
|
|
return request<AccessPoint>(`/aps/${id}`, {
|
|
|
|
auth: true,
|
|
|
|
method: HTTPMethod.GET,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createAccessPoint(
|
|
|
|
ap: AccessPoint
|
|
|
|
): Promise<AccessPointCreateResponse> {
|
|
|
|
return request<AccessPointCreateResponse>(
|
|
|
|
"/aps",
|
|
|
|
{ auth: true, method: HTTPMethod.POST },
|
|
|
|
ap
|
|
|
|
);
|
2021-04-03 17:34:28 +02:00
|
|
|
}
|