Reuse type

This commit is contained in:
Adria Navarro 2025-01-09 15:04:56 +01:00
parent 7df69f154a
commit e25f26d28d
1 changed files with 7 additions and 14 deletions

View File

@ -12,6 +12,8 @@ import CustomFetch from "./CustomFetch"
import QueryArrayFetch from "./QueryArrayFetch" import QueryArrayFetch from "./QueryArrayFetch"
import { APIClient } from "../api/types" import { APIClient } from "../api/types"
export type DataFetchType = keyof typeof DataFetchMap
export const DataFetchMap = { export const DataFetchMap = {
table: TableFetch, table: TableFetch,
view: ViewFetch, view: ViewFetch,
@ -31,8 +33,7 @@ export const DataFetchMap = {
// Constructs a new fetch model for a certain datasource // Constructs a new fetch model for a certain datasource
export const fetchData = ({ API, datasource, options }: any) => { export const fetchData = ({ API, datasource, options }: any) => {
const Fetch = const Fetch = DataFetchMap[datasource?.type as DataFetchType] || TableFetch
DataFetchMap[datasource?.type as keyof typeof DataFetchMap] || TableFetch
const fetch = new Fetch({ API, datasource, ...options }) const fetch = new Fetch({ API, datasource, ...options })
// Initially fetch data but don't bother waiting for the result // Initially fetch data but don't bother waiting for the result
@ -43,18 +44,14 @@ export const fetchData = ({ API, datasource, options }: any) => {
// Creates an empty fetch instance with no datasource configured, so no data // Creates an empty fetch instance with no datasource configured, so no data
// will initially be loaded // will initially be loaded
const createEmptyFetchInstance = < const createEmptyFetchInstance = <TDatasource extends { type: DataFetchType }>({
TDatasource extends {
type: keyof typeof DataFetchMap
}
>({
API, API,
datasource, datasource,
}: { }: {
API: APIClient API: APIClient
datasource: TDatasource datasource: TDatasource
}) => { }) => {
const handler = DataFetchMap[datasource?.type as keyof typeof DataFetchMap] const handler = DataFetchMap[datasource?.type as DataFetchType]
if (!handler) { if (!handler) {
return null return null
} }
@ -63,9 +60,7 @@ const createEmptyFetchInstance = <
// Fetches the definition of any type of datasource // Fetches the definition of any type of datasource
export const getDatasourceDefinition = async < export const getDatasourceDefinition = async <
TDatasource extends { TDatasource extends { type: DataFetchType }
type: keyof typeof DataFetchMap
}
>({ >({
API, API,
datasource, datasource,
@ -79,9 +74,7 @@ export const getDatasourceDefinition = async <
// Fetches the schema of any type of datasource // Fetches the schema of any type of datasource
export const getDatasourceSchema = < export const getDatasourceSchema = <
TDatasource extends { TDatasource extends { type: DataFetchType }
type: keyof typeof DataFetchMap
}
>({ >({
API, API,
datasource, datasource,