-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16847 from davelopez/refactor_client_api_modules
Refactor client API packages
- Loading branch information
Showing
139 changed files
with
672 additions
and
578 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 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,86 @@ | ||
# Best practices when querying the API from UI components | ||
|
||
If you need to query the API from a component, there are several ways to do so. This document will help you decide which one to use and provide some best practices when doing so to keep the code clean and maintainable. | ||
|
||
## Choose the Right Approach | ||
|
||
When querying APIs in Vue components, consider the following approaches and their best practices: | ||
|
||
## 1. Prefer Composables over Stores over Direct API Calls | ||
|
||
### Composables | ||
|
||
- **What Are Composables?**: please read the official Vue documentation on composables [here](https://vuejs.org/guide/reusability/composables.html). | ||
|
||
If there is already a composable that takes care of the logic you need for accessing a particular resource in the API please use it or consider writing a new one. They provide a type-safe interface and a higher level of abstraction than the related Store or the API itself. They might rely on one or more Stores for caching and reactivity. | ||
|
||
### Stores | ||
|
||
- **Stores Explained**: Please read the official Vue documentation on State Management [here](https://vuejs.org/guide/scaling-up/state-management.html). | ||
|
||
If there is no Composable for the API endpoint you are using, try using a (Pinia) Store instead. Stores are type-safe and provide a reactive interface to the API. They can also be used to cache data, ensuring a single source of truth. | ||
|
||
- **Use Pinia Stores**: If you need to create a new Store, make sure to create a Pinia Store, and not a Vuex Store as Pinia will be the replacement for Vuex. Also, use [Composition API syntax over the Options API](https://vuejs.org/guide/extras/composition-api-faq.html) for increased readability, code organization, type inference, etc. | ||
|
||
### Direct API Calls | ||
|
||
- If the type of data you are querying should not be cached, or you just need to update or create new data, you can use the API directly. Make sure to use the **Fetcher** (see below) instead of Axios, as it provides a type-safe interface to the API along with some extra benefits. | ||
|
||
## 2. Prefer Fetcher over Axios (when possible) | ||
|
||
- **Use Fetcher with OpenAPI Specs**: If there is an OpenAPI spec for the API endpoint you are using (in other words, there is a FastAPI route defined in Galaxy), always use the Fetcher. It will provide you with a type-safe interface to the API. | ||
|
||
**Do** | ||
|
||
```typescript | ||
import { fetcher } from "@/api/schema"; | ||
const datasetsFetcher = fetcher.path("/api/dataset/{id}").method("get").create(); | ||
|
||
const { data: dataset } = await datasetsFetcher({ id: "testID" }); | ||
``` | ||
|
||
**Don't** | ||
|
||
```js | ||
import axios from "axios"; | ||
import { getAppRoot } from "onload/loadConfig"; | ||
import { rethrowSimple } from "utils/simple-error"; | ||
|
||
async getDataset(datasetId) { | ||
const url = `${getAppRoot()}api/datasets/${datasetId}`; | ||
try { | ||
const response = await axios.get(url); | ||
return response.data; | ||
} catch (e) { | ||
rethrowSimple(e); | ||
} | ||
} | ||
|
||
const dataset = await getDataset("testID"); | ||
``` | ||
|
||
> **Reason** | ||
> | ||
> The `fetcher` class provides a type-safe interface to the API, and is already configured to use the correct base URL and error handling. | ||
## 3. Where to put your API queries? | ||
|
||
The short answer is: **it depends**. There are several factors to consider when deciding where to put your API queries: | ||
|
||
### Is the data you are querying related exclusively to a particular component? | ||
|
||
If so, you should put the query in the component itself. If not, you should consider putting it in a Composable or a Store. | ||
|
||
### Can the data be cached? | ||
|
||
If so, you should consider putting the query in a Store. If not, you should consider putting it in a Composable or the component itself. | ||
|
||
### Is the query going to be used in more than one place? | ||
|
||
If so, you should consider putting it under src/api/<resource>.ts and exporting it from there. This will allow you to reuse the query in multiple places specially if you need to do some extra processing of the data. Also it will help to keep track of what parts of the API are being used and where. | ||
|
||
### Should I use the `fetcher` directly or should I write a wrapper function? | ||
|
||
- If you **don't need to do any extra processing** of the data, you can use the `fetcher` directly. | ||
- If you **need to do some extra processing**, you should consider writing a wrapper function. Extra processing can be anything from transforming the data to adding extra parameters to the query or omitting some of them, handling conditional types in response data, etc. | ||
- Using a **wrapper function** will help in case we decide to replace the `fetcher` with something else in the future (as we are doing now with _Axios_). |
13 changes: 8 additions & 5 deletions
13
...res/services/datasetCollection.service.ts → client/src/api/datasetCollections.ts
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,89 @@ | ||
import type { FetchArgType } from "openapi-typescript-fetch"; | ||
|
||
import { DatasetDetails } from "@/api"; | ||
import { fetcher } from "@/api/schema"; | ||
import { withPrefix } from "@/utils/redirect"; | ||
|
||
export const datasetsFetcher = fetcher.path("/api/datasets").method("get").create(); | ||
|
||
type GetDatasetsApiOptions = FetchArgType<typeof datasetsFetcher>; | ||
type GetDatasetsQuery = Pick<GetDatasetsApiOptions, "limit" | "offset">; | ||
// custom interface for how we use getDatasets | ||
interface GetDatasetsOptions extends GetDatasetsQuery { | ||
sortBy?: string; | ||
sortDesc?: string; | ||
query?: string; | ||
} | ||
|
||
/** Datasets request helper **/ | ||
export async function getDatasets(options: GetDatasetsOptions = {}) { | ||
const params: GetDatasetsApiOptions = {}; | ||
if (options.sortBy) { | ||
const sortPrefix = options.sortDesc ? "-dsc" : "-asc"; | ||
params.order = `${options.sortBy}${sortPrefix}`; | ||
} | ||
if (options.limit) { | ||
params.limit = options.limit; | ||
} | ||
if (options.offset) { | ||
params.offset = options.offset; | ||
} | ||
if (options.query) { | ||
params.q = ["name-contains"]; | ||
params.qv = [options.query]; | ||
} | ||
const { data } = await datasetsFetcher(params); | ||
return data; | ||
} | ||
|
||
const getDataset = fetcher.path("/api/datasets/{dataset_id}").method("get").create(); | ||
|
||
export async function fetchDatasetDetails(params: { id: string }): Promise<DatasetDetails> { | ||
const { data } = await getDataset({ dataset_id: params.id, view: "detailed" }); | ||
// We know that the server will return a DatasetDetails object because of the view parameter | ||
// but the type system doesn't, so we have to cast it. | ||
return data as unknown as DatasetDetails; | ||
} | ||
|
||
const updateHistoryDataset = fetcher.path("/api/histories/{history_id}/contents/{type}s/{id}").method("put").create(); | ||
|
||
export async function undeleteHistoryDataset(historyId: string, datasetId: string) { | ||
const { data } = await updateHistoryDataset({ | ||
history_id: historyId, | ||
id: datasetId, | ||
type: "dataset", | ||
deleted: false, | ||
}); | ||
return data; | ||
} | ||
|
||
const deleteHistoryDataset = fetcher | ||
.path("/api/histories/{history_id}/contents/{type}s/{id}") | ||
.method("delete") | ||
.create(); | ||
|
||
export async function purgeHistoryDataset(historyId: string, datasetId: string) { | ||
const { data } = await deleteHistoryDataset({ history_id: historyId, id: datasetId, type: "dataset", purge: true }); | ||
return data; | ||
} | ||
|
||
const datasetCopy = fetcher.path("/api/histories/{history_id}/contents/{type}s").method("post").create(); | ||
type HistoryContentsArgs = FetchArgType<typeof datasetCopy>; | ||
export async function copyDataset( | ||
datasetId: HistoryContentsArgs["content"], | ||
historyId: HistoryContentsArgs["history_id"], | ||
type: HistoryContentsArgs["type"] = "dataset", | ||
source: HistoryContentsArgs["source"] = "hda" | ||
) { | ||
const response = await datasetCopy({ | ||
history_id: historyId, | ||
type, | ||
source: source, | ||
content: datasetId, | ||
}); | ||
return response.data; | ||
} | ||
|
||
export function getCompositeDatasetLink(historyDatasetId: string, path: string) { | ||
return withPrefix(`/api/datasets/${historyDatasetId}/display?filename=${path}`); | ||
} |
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,13 @@ | ||
import { fetcher } from "@/api/schema"; | ||
|
||
export const datatypesFetcher = fetcher.path("/api/datatypes").method("get").create(); | ||
|
||
export const edamFormatsFetcher = fetcher.path("/api/datatypes/edam_formats/detailed").method("get").create(); | ||
export const edamDataFetcher = fetcher.path("/api/datatypes/edam_data/detailed").method("get").create(); | ||
|
||
const typesAndMappingsFetcher = fetcher.path("/api/datatypes/types_and_mapping").method("get").create(); | ||
|
||
export async function fetchDatatypesAndMappings(upload_only = true) { | ||
const { data } = await typesAndMappingsFetcher({ upload_only }); | ||
return data; | ||
} |
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,8 @@ | ||
/** | ||
* Historically, this API was used to get the list of genomes that were available | ||
* but now it is used to get the list of more generic "dbkeys". | ||
*/ | ||
|
||
import { fetcher } from "@/api/schema"; | ||
|
||
export const dbKeysFetcher = fetcher.path("/api/genomes").method("get").create(); |
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,9 @@ | ||
import axios from "axios"; | ||
|
||
import { components } from "@/api/schema"; | ||
|
||
type GroupModel = components["schemas"]["GroupModel"]; | ||
export async function getAllGroups(): Promise<GroupModel[]> { | ||
const { data } = await axios.get("/api/groups"); | ||
return data; | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import type { FetchArgType } from "openapi-typescript-fetch"; | ||
|
||
import { type components, fetcher } from "@/schema"; | ||
import { type components, fetcher } from "@/api/schema"; | ||
|
||
export type ArchivedHistorySummary = components["schemas"]["ArchivedHistorySummary"]; | ||
export type ArchivedHistoryDetailed = components["schemas"]["ArchivedHistoryDetailed"]; | ||
|
@@ -55,7 +55,7 @@ export async function fetchArchivedHistories( | |
}; | ||
} | ||
|
||
const archiveHistory = fetcher.path("/api/histories/{history_id}/archive").method("post").create(); | ||
const postArchiveHistory = fetcher.path("/api/histories/{history_id}/archive").method("post").create(); | ||
|
||
/** | ||
* Archive a history. | ||
|
@@ -64,20 +64,20 @@ const archiveHistory = fetcher.path("/api/histories/{history_id}/archive").metho | |
* @param purgeHistory Whether to purge the history after archiving. Can only be used in combination with an archive export record. | ||
* @returns The archived history summary. | ||
*/ | ||
export async function archiveHistoryById( | ||
export async function archiveHistory( | ||
historyId: string, | ||
archiveExportId?: string, | ||
purgeHistory?: boolean | ||
): Promise<ArchivedHistorySummary> { | ||
const { data } = await archiveHistory({ | ||
const { data } = await postArchiveHistory({ | ||
history_id: historyId, | ||
archive_export_id: archiveExportId, | ||
purge_history: purgeHistory, | ||
}); | ||
return data as ArchivedHistorySummary; | ||
} | ||
|
||
const unarchiveHistory = fetcher | ||
const putUnarchiveHistory = fetcher | ||
.path("/api/histories/{history_id}/archive/restore") | ||
.method("put") | ||
// @ts-ignore: workaround for optional query parameters in PUT. More info here https://github.com/ajaishankar/openapi-typescript-fetch/pull/55 | ||
Check warning on line 83 in client/src/api/histories.archived.ts GitHub Actions / client-unit-test (18)
|
||
|
@@ -89,8 +89,8 @@ const unarchiveHistory = fetcher | |
* @param force Whether to force un-archiving for purged histories. | ||
* @returns The restored history summary. | ||
*/ | ||
export async function unarchiveHistoryById(historyId: string, force?: boolean): Promise<ArchivedHistorySummary> { | ||
const { data } = await unarchiveHistory({ history_id: historyId, force }); | ||
export async function unarchiveHistory(historyId: string, force?: boolean): Promise<ArchivedHistorySummary> { | ||
const { data } = await putUnarchiveHistory({ history_id: historyId, force }); | ||
return data as ArchivedHistorySummary; | ||
} | ||
|
||
|
@@ -102,7 +102,7 @@ const reimportHistoryFromStore = fetcher.path("/api/histories/from_store_async") | |
* @param archivedHistory The archived history to reimport. It must have an associated export record. | ||
* @returns The async task result summary to track the reimport progress. | ||
*/ | ||
export async function reimportHistoryFromExportRecordAsync( | ||
export async function reimportArchivedHistoryFromExportRecord( | ||
archivedHistory: ArchivedHistorySummary | ||
): Promise<AsyncTaskResultSummary> { | ||
if (!archivedHistory.export_record_data) { | ||
|
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,6 @@ | ||
import { fetcher } from "@/api/schema"; | ||
|
||
export const historiesFetcher = fetcher.path("/api/histories").method("get").create(); | ||
export const archivedHistoriesFetcher = fetcher.path("/api/histories/archived").method("get").create(); | ||
export const undeleteHistory = fetcher.path("/api/histories/deleted/{history_id}/undelete").method("post").create(); | ||
export const purgeHistory = fetcher.path("/api/histories/{history_id}").method("delete").create(); |
14 changes: 13 additions & 1 deletion
14
client/src/stores/services/index.ts → client/src/api/index.ts
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,4 @@ | ||
import { fetcher } from "@/api/schema"; | ||
|
||
export const jobLockStatus = fetcher.path("/api/job_lock").method("get").create(); | ||
export const jobLockUpdate = fetcher.path("/api/job_lock").method("put").create(); |
Oops, something went wrong.