manage.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import CookieManager from "./CookieManager.js";
  2. const cookie = new CookieManager();
  3. document.addEventListener("DOMContentLoaded", () => {
  4. const tokenInput = document.getElementById("token")
  5. const userInput = document.getElementById("user")
  6. const keyInput = document.getElementById("key")
  7. const dataInput = document.getElementById("data")
  8. const getButton = document.getElementById("get")
  9. const saveButton = document.getElementById("save")
  10. const mergeButton = document.getElementById("merge")
  11. const deleteButton = document.getElementById("delete")
  12. const errorDisplay = document.getElementById("error")
  13. tokenInput.value = cookie.get("auth")
  14. userInput.value = cookie.parseJwt(tokenInput.value)?.path ?? "default"
  15. keyInput.value = "init"
  16. const dataEndpoint = `/v1/data`;
  17. getButton.addEventListener("click", async () => {
  18. errorDisplay.innerHTML = ""
  19. const key = keyInput.value
  20. const user = userInput.value ?? "default"
  21. const result = await fetch(`${dataEndpoint}/${user}/${key}`, {
  22. method: "GET", // *GET, POST, PUT, DELETE, etc.
  23. mode: "cors", // no-cors, *cors, same-origin
  24. cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
  25. credentials: "same-origin", // include, *same-origin, omit
  26. headers: {
  27. "Content-Type": "application/json",
  28. "Authorization": `Bearer ${tokenInput.value}`,
  29. // 'Content-Type': 'application/x-www-form-urlencoded',
  30. },
  31. redirect: "follow", // manual, *follow, error
  32. 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
  33. });
  34. if (result.status == 200) {
  35. dataInput.value = JSON.stringify(await result.json(), null, 2);
  36. } else {
  37. dataInput.value = "{}";
  38. errorDisplay.innerHTML = `${result.status} ${result.statusText}: Retrieving ${dataEndpoint}/${user}/${key} was not successful.`
  39. }
  40. });
  41. saveButton.addEventListener("click", async () => {
  42. errorDisplay.innerHTML = ""
  43. const key = keyInput.value;
  44. // if(!confirm(`Are you sure you want to save '${key}'?`)) {
  45. // return
  46. // }
  47. const user = userInput.value ?? "default"
  48. const data = JSON.parse(dataInput.value);
  49. const result = await fetch(`${dataEndpoint}/${user}/${key}`, {
  50. method: "PUT", // *GET, POST, PUT, DELETE, etc.
  51. mode: "cors", // no-cors, *cors, same-origin
  52. cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
  53. credentials: "same-origin", // include, *same-origin, omit
  54. headers: {
  55. "Content-Type": "application/json",
  56. "Authorization": `Bearer ${tokenInput.value}`,
  57. // 'Content-Type': 'application/x-www-form-urlencoded',
  58. },
  59. redirect: "follow", // manual, *follow, error
  60. 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
  61. body: JSON.stringify(data), // body data type must match "Content-Type" header
  62. });
  63. if (result.status == 200) {
  64. // cookie.add("auth", tokenInput.value);
  65. } else {
  66. errorDisplay.innerHTML = `${result.status} ${result.statusText}: Saving ${dataEndpoint}/${user}/${key} was not successful.`
  67. }
  68. });
  69. mergeButton.addEventListener("click", async () => {
  70. errorDisplay.innerHTML = ""
  71. const key = keyInput.value;
  72. // if(!confirm(`Are you sure you want to save '${key}'?`)) {
  73. // return
  74. // }
  75. const user = userInput.value ?? "default"
  76. const data = JSON.parse(dataInput.value);
  77. const result = await fetch(`${dataEndpoint}/${user}/${key}`, {
  78. method: "POST", // *GET, POST, PUT, DELETE, etc.
  79. mode: "cors", // no-cors, *cors, same-origin
  80. cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
  81. credentials: "same-origin", // include, *same-origin, omit
  82. headers: {
  83. "Content-Type": "application/json",
  84. "Authorization": `Bearer ${tokenInput.value}`,
  85. // 'Content-Type': 'application/x-www-form-urlencoded',
  86. },
  87. redirect: "follow", // manual, *follow, error
  88. 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
  89. body: JSON.stringify(data), // body data type must match "Content-Type" header
  90. });
  91. if (result.status == 200) {
  92. // cookie.add("auth", tokenInput.value);
  93. } else {
  94. errorDisplay.innerHTML = `${result.status} ${result.statusText}: Saving ${dataEndpoint}/${user}/${key} was not successful.`
  95. }
  96. });
  97. deleteButton.addEventListener("click", async () => {
  98. errorDisplay.innerHTML = ""
  99. const key = keyInput.value;
  100. if(!confirm(`Are you sure you want to delete '${key}'? This cannot be undone.`)) {
  101. return
  102. }
  103. const user = userInput.value ?? "default"
  104. const result = await fetch(`${dataEndpoint}/${user}/${key}`, {
  105. method: "DELETE", // *GET, POST, PUT, DELETE, etc.
  106. mode: "cors", // no-cors, *cors, same-origin
  107. cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
  108. credentials: "same-origin", // include, *same-origin, omit
  109. headers: {
  110. "Content-Type": "application/json",
  111. "Authorization": `Bearer ${tokenInput.value}`,
  112. // 'Content-Type': 'application/x-www-form-urlencoded',
  113. },
  114. redirect: "follow", // manual, *follow, error
  115. 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
  116. });
  117. if (result.status == 200) {
  118. // cookie.add("auth", tokenInput.value);
  119. } else {
  120. errorDisplay.innerHTML = `${result.status} ${result.statusText}: Deleting ${dataEndpoint}/${user}/${key} was not successful.`
  121. }
  122. });
  123. getButton.click()
  124. });