-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use composable instead of 'util' for R API consumption
- Loading branch information
1 parent
0646351
commit da0cc1b
Showing
4 changed files
with
76 additions
and
38 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
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
import type { RApiResponse } from "./utils/useRApi"; | ||
import { useRApi } from "./utils/useRApi"; | ||
|
||
interface VersionDataResponse extends RApiResponse { | ||
data: { | ||
"daedalus": string | ||
"daedalus.api": string | ||
} | null | ||
} | ||
|
||
const versionEndpoint = "/"; | ||
|
||
export function useVersionData() { | ||
const response = useRApi<VersionDataResponse>(versionEndpoint); | ||
|
||
return { | ||
versionFetchStatus: response.fetchStatus, | ||
refreshVersionFetch: response.refresh, | ||
versionFetchError: response.error, | ||
versionData: response.responseData, | ||
}; | ||
} |
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,29 @@ | ||
const rApiAddress = "http://localhost:8001"; | ||
|
||
interface RApiError { | ||
error: string | ||
detail: string | ||
} | ||
|
||
export interface RApiResponse { | ||
status: string | ||
errors: Array<RApiError> | null | ||
data: object | null | ||
} | ||
|
||
// NB 'refresh' cannot yet be used since it would be called in the browser and | ||
// thus run up against CORS restrictions. CORS also prevents hot module reloading, currently. | ||
export function useRApi<T extends RApiResponse>(endpoint: string) { | ||
const url = rApiAddress + endpoint; | ||
|
||
const { data, error, status: fetchStatus, refresh } = useFetch<T>(url); | ||
|
||
const responseData = computed(() => data.value as T | null); | ||
|
||
return { | ||
responseData, // This is the response data, not the value of the 'data' key in the response data. | ||
fetchStatus, // This is the status of the fetch request (e.g. pending), not the value of the 'status' part of the response data. | ||
refresh, // refresh is a function that can be called to re-fetch the data | ||
error, // This is the error object from Vue, not the value of the 'errors' key in the response data. | ||
}; | ||
} |