123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import CookieManager from "./CookieManager.js";
- const cookie = new CookieManager();
- document.addEventListener("DOMContentLoaded", () => {
- const tokenInput = document.getElementById("token")
- const userInput = document.getElementById("user")
- const keyInput = document.getElementById("key")
- const dataInput = document.getElementById("data")
- const getButton = document.getElementById("get")
- const saveButton = document.getElementById("save")
- const mergeButton = document.getElementById("merge")
- const deleteButton = document.getElementById("delete")
- const errorDisplay = document.getElementById("error")
- tokenInput.value = cookie.get("auth")
- userInput.value = cookie.parseJwt(tokenInput.value)?.path ?? "default"
- keyInput.value = "init"
- const dataEndpoint = `/v1/data`;
- getButton.addEventListener("click", async () => {
- errorDisplay.innerHTML = ""
- const key = keyInput.value
- const user = userInput.value ?? "default"
- const result = await fetch(`${dataEndpoint}/${user}/${key}`, {
- method: "GET", // *GET, POST, PUT, DELETE, etc.
- mode: "cors", // no-cors, *cors, same-origin
- cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
- credentials: "same-origin", // include, *same-origin, omit
- headers: {
- "Content-Type": "application/json",
- "Authorization": `Bearer ${tokenInput.value}`,
- // 'Content-Type': 'application/x-www-form-urlencoded',
- },
- redirect: "follow", // manual, *follow, error
- referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
- });
- if (result.status == 200) {
- dataInput.value = JSON.stringify(await result.json(), null, 2);
- } else {
- dataInput.value = "{}";
- errorDisplay.innerHTML = `${result.status} ${result.statusText}: Retrieving ${dataEndpoint}/${user}/${key} was not successful.`
- }
- });
- saveButton.addEventListener("click", async () => {
- errorDisplay.innerHTML = ""
- const key = keyInput.value;
- // if(!confirm(`Are you sure you want to save '${key}'?`)) {
- // return
- // }
- const user = userInput.value ?? "default"
- const data = JSON.parse(dataInput.value);
- const result = await fetch(`${dataEndpoint}/${user}/${key}`, {
- method: "PUT", // *GET, POST, PUT, DELETE, etc.
- mode: "cors", // no-cors, *cors, same-origin
- cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
- credentials: "same-origin", // include, *same-origin, omit
- headers: {
- "Content-Type": "application/json",
- "Authorization": `Bearer ${tokenInput.value}`,
- // 'Content-Type': 'application/x-www-form-urlencoded',
- },
- redirect: "follow", // manual, *follow, error
- referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
- body: JSON.stringify(data), // body data type must match "Content-Type" header
- });
- if (result.status == 200) {
- // cookie.add("auth", tokenInput.value);
- } else {
- errorDisplay.innerHTML = `${result.status} ${result.statusText}: Saving ${dataEndpoint}/${user}/${key} was not successful.`
- }
- });
- mergeButton.addEventListener("click", async () => {
- errorDisplay.innerHTML = ""
- const key = keyInput.value;
- // if(!confirm(`Are you sure you want to save '${key}'?`)) {
- // return
- // }
- const user = userInput.value ?? "default"
- const data = JSON.parse(dataInput.value);
- const result = await fetch(`${dataEndpoint}/${user}/${key}`, {
- method: "POST", // *GET, POST, PUT, DELETE, etc.
- mode: "cors", // no-cors, *cors, same-origin
- cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
- credentials: "same-origin", // include, *same-origin, omit
- headers: {
- "Content-Type": "application/json",
- "Authorization": `Bearer ${tokenInput.value}`,
- // 'Content-Type': 'application/x-www-form-urlencoded',
- },
- redirect: "follow", // manual, *follow, error
- referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
- body: JSON.stringify(data), // body data type must match "Content-Type" header
- });
- if (result.status == 200) {
- // cookie.add("auth", tokenInput.value);
- } else {
- errorDisplay.innerHTML = `${result.status} ${result.statusText}: Saving ${dataEndpoint}/${user}/${key} was not successful.`
- }
- });
- deleteButton.addEventListener("click", async () => {
- errorDisplay.innerHTML = ""
- const key = keyInput.value;
- if(!confirm(`Are you sure you want to delete '${key}'? This cannot be undone.`)) {
- return
- }
- const user = userInput.value ?? "default"
- const result = await fetch(`${dataEndpoint}/${user}/${key}`, {
- method: "DELETE", // *GET, POST, PUT, DELETE, etc.
- mode: "cors", // no-cors, *cors, same-origin
- cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
- credentials: "same-origin", // include, *same-origin, omit
- headers: {
- "Content-Type": "application/json",
- "Authorization": `Bearer ${tokenInput.value}`,
- // 'Content-Type': 'application/x-www-form-urlencoded',
- },
- redirect: "follow", // manual, *follow, error
- referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
- });
- if (result.status == 200) {
- // cookie.add("auth", tokenInput.value);
- } else {
- errorDisplay.innerHTML = `${result.status} ${result.statusText}: Deleting ${dataEndpoint}/${user}/${key} was not successful.`
- }
- });
- getButton.click()
- });
|