Skip to content
Snippets Groups Projects
Select Git revision
  • cf2025
  • main default protected
  • cf2024
  • cf2023-euro
  • cf2023-offline
5 results

api.js

Blame
  • api.js 708 B
    import baseFetch from "unfetch";
    
    import { AuthStore } from "./stores";
    
    export const fetch = async (
      url,
      { headers = {}, expectedStatus = 200, method = "GET", body = null } = {}
    ) => {
      const { isAuthenticated, user } = AuthStore.getRawState();
    
      if (isAuthenticated) {
        headers.Authorization = "Bearer " + user.accessToken;
      }
    
      if (!headers["Content-Type"]) {
        headers["Content-Type"] = "application/json";
      }
    
      const response = await baseFetch(process.env.REACT_APP_API_BASE_URL + url, {
        body,
        method,
        headers,
      });
    
      if (!!expectedStatus && response.status !== expectedStatus) {
        throw new Error(`Unexpected status code ${response.status}`);
      }
    
      return response;
    };