budibase/packages/frontend-core/src/fetch/index.ts

97 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-01-09 14:49:07 +01:00
import TableFetch from "./TableFetch"
import ViewFetch from "./ViewFetch"
import ViewV2Fetch from "./ViewV2Fetch"
2025-01-07 11:47:10 +01:00
import QueryFetch from "./QueryFetch"
2025-01-07 10:14:18 +01:00
import RelationshipFetch from "./RelationshipFetch"
2025-01-07 13:34:42 +01:00
import NestedProviderFetch from "./NestedProviderFetch"
2025-01-03 12:34:36 +01:00
import FieldFetch from "./FieldFetch"
2025-01-07 12:41:37 +01:00
import JSONArrayFetch from "./JSONArrayFetch"
2025-01-09 14:49:07 +01:00
import UserFetch from "./UserFetch"
2025-01-07 11:02:09 +01:00
import GroupUserFetch from "./GroupUserFetch"
2025-01-07 13:46:00 +01:00
import CustomFetch from "./CustomFetch"
2025-01-09 14:49:07 +01:00
import QueryArrayFetch from "./QueryArrayFetch"
import { APIClient } from "../api/types"
2025-01-09 14:49:07 +01:00
export const DataFetchMap = {
table: TableFetch,
view: ViewFetch,
2023-07-19 10:16:12 +02:00
viewV2: ViewV2Fetch,
query: QueryFetch,
link: RelationshipFetch,
user: UserFetch,
2023-05-04 18:38:04 +02:00
groupUser: GroupUserFetch,
2023-10-05 15:42:26 +02:00
custom: CustomFetch,
// Client specific datasource types
provider: NestedProviderFetch,
field: FieldFetch,
jsonarray: JSONArrayFetch,
queryarray: QueryArrayFetch,
}
// Constructs a new fetch model for a certain datasource
2025-01-07 11:22:22 +01:00
export const fetchData = ({ API, datasource, options }: any) => {
2024-12-31 13:24:26 +01:00
const Fetch =
DataFetchMap[datasource?.type as keyof typeof DataFetchMap] || TableFetch
2025-01-09 15:58:59 +01:00
const fetch = new Fetch({ API, datasource, ...options })
// Initially fetch data but don't bother waiting for the result
fetch.getInitialData()
return fetch
}
// Creates an empty fetch instance with no datasource configured, so no data
// will initially be loaded
2025-01-08 14:27:13 +01:00
const createEmptyFetchInstance = <
TDatasource extends {
type: keyof typeof DataFetchMap
}
>({
2024-12-31 13:24:26 +01:00
API,
datasource,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
2025-01-08 14:27:13 +01:00
datasource: TDatasource
2024-12-31 13:24:26 +01:00
}) => {
const handler = DataFetchMap[datasource?.type as keyof typeof DataFetchMap]
if (!handler) {
return null
}
2025-01-07 11:22:22 +01:00
return new handler({ API, datasource: null as any, query: null as any })
}
// Fetches the definition of any type of datasource
2025-01-08 14:27:13 +01:00
export const getDatasourceDefinition = async <
TDatasource extends {
type: keyof typeof DataFetchMap
}
>({
2024-12-31 13:24:26 +01:00
API,
datasource,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
2025-01-08 14:27:13 +01:00
datasource: TDatasource
2024-12-31 13:24:26 +01:00
}) => {
const instance = createEmptyFetchInstance({ API, datasource })
2025-01-08 14:27:13 +01:00
return await instance?.getDefinition()
}
// Fetches the schema of any type of datasource
2025-01-08 14:27:13 +01:00
export const getDatasourceSchema = <
TDatasource extends {
type: keyof typeof DataFetchMap
}
>({
2024-12-31 13:24:26 +01:00
API,
datasource,
definition,
}: {
2025-01-07 11:22:22 +01:00
API: APIClient
2025-01-08 14:27:13 +01:00
datasource: TDatasource
2025-01-08 14:17:10 +01:00
definition?: any
2024-12-31 13:24:26 +01:00
}) => {
const instance = createEmptyFetchInstance({ API, datasource })
2025-01-08 12:57:25 +01:00
return instance?.getSchema(definition)
}