import { Auth } from "home-assistant-js-websocket" ; export async function handleFetchPromise < T > ( fetchPromise ) { let response ; try { response = await fetchPromise ; } catch ( err ) { throw { error : "Request error" , status_code : undefined , body : undefined , } ; } let body = null ; const contentType = response . headers . get ( "content-type" ) ; if ( contentType && contentType . includes ( "application/json" ) ) { try { body = await response . json ( ) ; } catch ( err ) { throw { error : "Unable to parse JSON response" , status_code : err . status , body : null , } ; } } else { body = await response . text ( ) ; } if ( ! response . ok ) { throw { error : `Response error: ${ response . status }` , status_code : response . status , body , } ; } return body as unknown as T ; } const fetchWithAuth = async ( auth , input , init = { } ) : Promise < Response > => { if ( auth . expired ) { await auth . refreshAccessToken ( ) ; } init . credentials = "same-origin" ; if ( ! init . headers ) { init . headers = { } ; } if ( ! init . headers ) { init . headers = { } ; } init . headers . authorization = `Bearer ${ auth . accessToken }` ; return fetch ( input , init ) ; } ; export default async function hassCallApi < T > ( auth , method , path , parameters ? ) { const url = `${ auth . data . hassUrl }/api/${ path }` ; const init = { method , headers : { } , } ; if ( parameters ) { init . headers [ "Content-Type" ] = "application/json;charset=UTF-8" ; init . body = JSON . stringify ( parameters ) ; } return handleFetchPromise < T > ( fetchWithAuth ( auth , url , init ) ) ; } export async function fetchHistory ( auth , entity , start , end ) { let url = "history/period" ; if ( start ) url += `/${ start . toISOString ( ) }` ; url += `?filter_entity_id=${ entity }` ; if ( end ) url += `&end_time=${ end . toISOString ( ) }` ; if ( process . env . NODE_ENV === "development" ) console . log ( "fetchHistory:" , url ) ; return hassCallApi ( auth , "GET" , url ) ; }