import axios , { CancelTokenSource } from "axios" ; import { Midi } from "@utils/Midi/Midi" ; let getFileDetailsToken ; export async function getFileDetails ( file ) { if ( getFileDetailsToken ) { getFileDetailsToken . cancel ( ) ; } getFileDetailsToken = axios . CancelToken . source ( ) ; const formData = new FormData ( ) ; formData . append ( "file" , file , file . name ) ; try { const { data } = await axios . post ( "/api/process-file" , formData , { cancelToken : getFileDetailsToken . token } ) ; getFileDetailsToken = null ; return new Midi ( data ) ; } catch ( e ) { throw new Error ( e . response ?. data ) ; } } let getDetailsForFileToken ; export async function getDetailsFromURL ( url , name ) { if ( getDetailsForFileToken ) { getDetailsForFileToken . cancel ( ) ; } getDetailsForFileToken = axios . CancelToken . source ( ) ; try { const { data } = await axios . get ( "/api/process-url" , { params : { url , name } , cancelToken : getDetailsForFileToken . token } ) ; getDetailsForFileToken = null ; return new Midi ( data ) ; } catch ( e ) { console . log ( e ) ; throw new Error ( e . response ?. data ) ; } }