-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use composables for session settings * testing fetch methode * encountered CORS Error * added whitespace * rewrite upload data composable a bit * added API composable for ease of use * implemented api post/get request * only data should be shared for now * using .env for url and token * removing useless getters and setters * removed testing code * removed interface * should fix issues * updated post data format * removed sanatization --------- Co-authored-by: Kevin Beier <[email protected]>
- Loading branch information
Showing
8 changed files
with
206 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VITE_BASEURL=https://api.example.com | ||
VITE_BEARERTOKEN=YOUR_BEARER_TOKEN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import useNotificationSystem from '@/composables/useNotificationSystem/useNotificationSystem'; | ||
|
||
/** | ||
* API usage composable | ||
*/ | ||
export default function useAPI() { | ||
/** | ||
* Perform a GET request to the specified API endpoint. | ||
* @param {string} endpoint - The API endpoint to fetch data from. | ||
* @returns {Promise<object>} A Promise that resolves to the API response. | ||
* @throws {Error} If the request fails or the response is not OK. | ||
*/ | ||
const get = async (endpoint: string): Promise<object> => { | ||
try { | ||
const url = `${import.meta.env.VITE_BASEURL}/${endpoint}`; | ||
const response = await fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${import.meta.env.VITE_BEARERTOKEN}`, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
useNotificationSystem().createErrorNotification({ | ||
title: 'Failed to fetch data', | ||
}); | ||
throw new Error('Failed to fetch data'); | ||
} | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
useNotificationSystem().createErrorNotification({ | ||
title: 'Error occurred while fetching data', | ||
message: `${error}`, | ||
}); | ||
throw error; | ||
} | ||
}; | ||
|
||
/** | ||
* Perform a POST request to the specified API endpoint. | ||
* @param {string} endpoint - The API endpoint to post data to. | ||
* @param {object} data - The data to be posted to the API. | ||
* @returns {Promise<object>} A Promise that resolves to the API response. | ||
* @throws {Error} If the request fails or the response is not OK. | ||
*/ | ||
const post = async (endpoint: string, data: object): Promise<object> => { | ||
try { | ||
const url = `${import.meta.env.VITE_BASEURL}/${endpoint}`; | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${import.meta.env.VITE_BEARERTOKEN}`, | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
|
||
if (!response.ok) { | ||
useNotificationSystem().createErrorNotification({ | ||
title: 'Failed to post data', | ||
}); | ||
throw new Error('Failed to post data'); | ||
} | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
useNotificationSystem().createErrorNotification({ | ||
title: 'Error occurred while posting data', | ||
message: `${error}`, | ||
}); | ||
throw error; | ||
} | ||
}; | ||
|
||
return { | ||
get, | ||
post, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ref, Ref } from 'vue'; | ||
import useNotificationSystem from '@/composables/useNotificationSystem/useNotificationSystem'; | ||
|
||
const sessionToken = ref(''); | ||
// Reactive variable to track session status | ||
const isSessionActive: Ref<boolean> = ref(false); | ||
|
||
export default function useSession() { | ||
/** | ||
* Starts the session | ||
*/ | ||
const startSession = (): void => { | ||
isSessionActive.value = true; | ||
useNotificationSystem().createNotification({ | ||
title: 'Session started', | ||
}); | ||
}; | ||
|
||
/** | ||
* Stops the session | ||
*/ | ||
const stopSession = (): void => { | ||
isSessionActive.value = false; | ||
useNotificationSystem().createNotification({ | ||
title: 'Session stopped', | ||
type: 'info', | ||
}); | ||
}; | ||
|
||
return { | ||
sessionToken, | ||
isSessionActive, | ||
startSession, | ||
stopSession, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ref } from 'vue'; | ||
|
||
const streamData = ref(false); | ||
const streamRegexAndCaptureAreaSettings = ref(false); | ||
|
||
/** | ||
* Uploadable data composable | ||
*/ | ||
export default function useUploadData() { | ||
return { | ||
streamData, | ||
streamRegexAndCaptureAreaSettings, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Interface for matched elements | ||
*/ | ||
export interface MatchedElement { | ||
rating: number; | ||
match: { | ||
index: number; | ||
element: string; | ||
}; | ||
timestamp?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters