api.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { API_ENDPOINT, IS_PRODUCTION } from "./settings";
  2. //import { nanoid } from "nanoid";
  3. import testGame from "../games/testGame";
  4. import perfGame from "../games/perfGame";
  5. import { nanoid } from "nanoid";
  6. const uploadURI = `${API_ENDPOINT}/file`;
  7. const gameURI = `${API_ENDPOINT}/store/game`;
  8. const execURI = `${API_ENDPOINT}/execute`;
  9. const authURI = `${API_ENDPOINT}/auth`;
  10. // Register to backend
  11. export const register = async () => {
  12. await fetch(`${execURI}/_register`, {
  13. credentials: "include",
  14. headers: {
  15. "X-SPC-HOST": `${window.location.origin}/exec`,
  16. },
  17. });
  18. };
  19. register();
  20. export const uploadImage = async (namespace, file) => {
  21. const payload = new FormData();
  22. payload.append("file", file);
  23. const result = await fetch(`${uploadURI}/${namespace}/`, {
  24. method: "POST",
  25. body: payload, // this sets the `Content-Type` header to `multipart/form-data`
  26. });
  27. return await result.text();
  28. };
  29. // TODO Add delete Image
  30. export const getGames = async () => {
  31. const fetchParams = new URLSearchParams({
  32. fields: "_id,board,owner",
  33. });
  34. let gameList = [];
  35. const result = await fetch(`${gameURI}?${"" + fetchParams}`, {
  36. credentials: "include",
  37. });
  38. if (result.status === 200) {
  39. const serverGames = await result.json();
  40. gameList = serverGames.map((game) => ({
  41. name: game.board.name,
  42. id: game._id,
  43. owner: game.owner,
  44. url: `${gameURI}/${game._id}`,
  45. published: game.board.published,
  46. }));
  47. }
  48. if (!IS_PRODUCTION) {
  49. gameList = [
  50. { name: "Test Game", data: testGame, id: "test", published: true },
  51. { name: "Perf Test", data: perfGame, id: "perf", published: true },
  52. ...gameList,
  53. ];
  54. }
  55. return gameList;
  56. };
  57. const fetchGame = async (url) => {
  58. const result = await fetch(url, {
  59. credentials: "include",
  60. });
  61. return await result.json();
  62. };
  63. export const getGame = async (gameId) => {
  64. const games = await getGames();
  65. const game = games.find(({ id }) => id === gameId);
  66. if (!game) {
  67. throw new Error("Resource not found");
  68. }
  69. const { url, data } = game;
  70. if (url) {
  71. return await fetchGame(url);
  72. } else {
  73. return data;
  74. }
  75. };
  76. export const createGame = async (data) => {
  77. const newGameId = nanoid();
  78. const result = await updateGame(newGameId, data);
  79. return result;
  80. };
  81. export const updateGame = async (id, data) => {
  82. const result = await fetch(`${execURI}/saveGame/${id}`, {
  83. method: "POST",
  84. headers: {
  85. Accept: "application/json",
  86. "Content-Type": "application/json",
  87. "X-SPC-HOST": `${window.location.origin}/exec`,
  88. },
  89. body: JSON.stringify(data),
  90. credentials: "include",
  91. });
  92. if (result.status === 404) {
  93. throw new Error("Resource not found");
  94. }
  95. return await result.json();
  96. };
  97. export const deleteGame = async (id) => {
  98. const result = await fetch(`${gameURI}/${id}`, {
  99. method: "DELETE",
  100. credentials: "include",
  101. });
  102. return await result.json();
  103. };
  104. export const sendAuthToken = async (email) => {
  105. const result = await fetch(`${authURI}/`, {
  106. method: "POST",
  107. headers: {
  108. Accept: "application/json",
  109. "Content-Type": "application/json",
  110. "X-Auth-HOST": `${window.location.origin}`,
  111. },
  112. body: JSON.stringify({ userEmail: email }),
  113. credentials: "include",
  114. });
  115. if (result.status !== 200) {
  116. throw new Error("Can't send token");
  117. }
  118. };
  119. export const login = async (userHash, token) => {
  120. const result = await fetch(`${authURI}/verify/${userHash}/${token}`, {
  121. credentials: "include",
  122. });
  123. if (result.status !== 200) {
  124. throw new Error("Auth failed");
  125. }
  126. };
  127. export const logout = async () => {
  128. const result = await fetch(`${authURI}/logout/`, {
  129. credentials: "include",
  130. });
  131. if (result.status !== 200) {
  132. throw new Error("Logout failed");
  133. }
  134. };