import * as queryString from "query-string" ; import { authStore } from "../../stores/AuthStore" ; import { APIUtils } from "../v1/APIUtils" ; import { IAuthData } from "./AuthAPI" ; const API_BASE_URL = "api" ; const API_VERSION = "v1" ; const DEFAULT_HEADERS = { Accept : "application/json" } ; async function request ( options ) { const requestHeaders = { ... DEFAULT_HEADERS , ... options . headers } ; if ( ! options . isFormData ) { requestHeaders [ "Content-Type" ] = "application/json" ; } if ( options . authenticated ) { if ( typeof options . authenticated === "boolean" ) { requestHeaders . client = authStore . client ; requestHeaders [ "access-token" ] = authStore . accessToken ; requestHeaders . uid = authStore . currentUser && authStore . currentUser . email ; } else { requestHeaders . client = options . authenticated . client ; requestHeaders [ "access-token" ] = options . authenticated . accessToken ; requestHeaders . uid = options . authenticated . uid ; } } let fullURL = `/${ API_BASE_URL }/${ API_VERSION }/${ options . url }` ; if ( options . method === "GET" && options . params ) { fullURL += `?${ queryString . stringify ( options . params , { arrayFormat : "bracket" } ) }` ; } let response ; try { response = await fetch ( fullURL , { method : options . method , headers : requestHeaders , body : options . method === "GET" ? null : options . isFormData ? options . params : JSON . stringify ( options . params ) } ) ; if ( response . status === 403 ) { const json = await response . json ( ) ; APIUtils . handleErrors ( json ) ; return { error : true , message : "FORBIDDEN" } ; } else { APIUtils . saveTokenFromResponseIfAvailable ( response ) ; } } catch ( error ) { console . error ( "Error while fetching:" , error ) ; } return response && ! options . isFileDownload ? response . json ( ) : response ; } const API = { getRequest : ( url , authenticated , queryParams ? , headers ? , isFileDownload ? ) : any => { return request ( { url : url , authenticated : authenticated , method : "GET" , headers : headers , params : queryParams , isFileDownload : isFileDownload } ) ; } , postRequest : ( url , authenticated , body ? , headers ? , isFormData ? ) : any => { return request ( { url : url , authenticated : authenticated , method : "POST" , headers : headers , isFormData : isFormData , params : body } ) ; } , putRequest : ( url , authenticated , body ? , headers ? ) : any => { return request ( { url : url , authenticated : authenticated , method : "PUT" , headers : headers , params : body } ) ; } , deleteRequest : ( url , authenticated , body ? , headers ? ) : any => { return request ( { url : url , authenticated : authenticated , method : "DELETE" , headers : headers , params : body } ) ; } } ; export { API } ;