utils.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @file utils.js
  3. * @description Utility functions for making API requests and handling responses.
  4. */
  5. "use strict";
  6. /**
  7. * Fetches a resource from the server with authentication token.
  8. *
  9. * @async
  10. * @param {string} url - The URL of the resource to fetch.
  11. * @param {Object} options - Options for the fetch request (e.g. method, body).
  12. * @returns {Promise<Object>} A promise resolving to the response data.
  13. */
  14. async function fetchWithToken(url, options) {
  15. const authToken = localStorage.getItem('token');
  16. return await fetch(url, {...options, headers: {
  17. 'Authorization': `Bearer ${authToken}`,
  18. 'Content-Type': 'application/json',
  19. 'Cache-Control': 'no-cache, no-store',
  20. }});
  21. }
  22. /**
  23. * Fetches a resource from the server without authentication token.
  24. *
  25. * @async
  26. * @param {string} url - The URL of the resource to fetch.
  27. * @param {Object} options - Options for the fetch request (e.g. method, body).
  28. * @returns {Promise<Object>} A promise resolving to the response data.
  29. */
  30. async function fetchRegular(url, options) {
  31. return await fetch(url, {...options, headers: {
  32. 'Content-Type': 'application/json',
  33. 'Cache-Control': 'no-cache, no-store',
  34. }});
  35. }
  36. /**
  37. * Formats a time as HH:MM string.
  38. *
  39. * @param {Date} date - The date to format.
  40. * @returns {string} The formatted time string (e.g. "09:00").
  41. */
  42. function formatTime(date) {
  43. const hours = date.getHours().toString().padStart(2, '0');
  44. const minutes = date.getMinutes().toString().padStart(2, '0');
  45. return `${hours}:${minutes}`;
  46. }
  47. /**
  48. * Gets the time span between two dates.
  49. *
  50. * @param {Date} start - The start date.
  51. * @param {Date} end - The end date.
  52. * @returns {string} The time span string (e.g. "09:00 to 10:00").
  53. */
  54. function getTimeSpan(start, end) {
  55. return `${formatTime(new Date(start))} to ${formatTime(new Date(end))}`;
  56. }
  57. /**
  58. * Parses the response data and redirects if necessary.
  59. *
  60. * @async
  61. * @param {Response} response - The server response.
  62. * @returns {Promise<Object>} A promise resolving to the parsed response data, or null if a redirect was taken.
  63. */
  64. async function parseOrRedirect(response) {
  65. if (response.status === 401) {
  66. // Handle unauthorized request (redirect to login page)
  67. window.location.replace("/html/auth.html");
  68. return;
  69. }
  70. if (response.status === 410) {
  71. // Handle deprecated resource (redirect to deprecated page)
  72. window.location.replace("/html/410.html");
  73. return;
  74. }
  75. const js = await response.json();
  76. if (!response.ok) {
  77. // Handle non-200 status code (redirect to error page)
  78. window.location.replace("/html/404.html");
  79. }
  80. return js;
  81. }
  82. export { fetchWithToken, getTimeSpan, parseOrRedirect, fetchRegular };