Improve typings removing generics
This commit is contained in:
parent
7d284ae50e
commit
5b4b27dc16
|
@ -1,6 +1,6 @@
|
|||
import BaseDataFetch from "./DataFetch"
|
||||
|
||||
interface CustomDatasource {
|
||||
export interface CustomDatasource {
|
||||
type: "custom"
|
||||
data: any
|
||||
}
|
||||
|
|
|
@ -7,14 +7,19 @@ interface GroupUserQuery {
|
|||
emailSearch: string
|
||||
}
|
||||
|
||||
interface GroupUserDatasource {
|
||||
export interface GroupUserDatasource {
|
||||
type: "groupUser"
|
||||
tableId: TableNames.USERS
|
||||
}
|
||||
|
||||
interface GroupUserDefinition {
|
||||
schema?: Record<string, any> | null
|
||||
primaryDisplay?: string
|
||||
}
|
||||
|
||||
export default class GroupUserFetch extends BaseDataFetch<
|
||||
GroupUserDatasource,
|
||||
{},
|
||||
GroupUserDefinition,
|
||||
GroupUserQuery
|
||||
> {
|
||||
constructor(opts: DataFetchParams<GroupUserDatasource, GroupUserQuery>) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Row, TableSchema } from "@budibase/types"
|
||||
import BaseDataFetch from "./DataFetch"
|
||||
|
||||
interface NestedProviderDatasource {
|
||||
export interface NestedProviderDatasource {
|
||||
type: "provider"
|
||||
value?: {
|
||||
schema: TableSchema
|
||||
|
|
|
@ -3,7 +3,7 @@ import { Helpers } from "@budibase/bbui"
|
|||
import { ExecuteQueryRequest, Query } from "@budibase/types"
|
||||
import { get } from "svelte/store"
|
||||
|
||||
interface QueryDatasource {
|
||||
export interface QueryDatasource {
|
||||
type: "query"
|
||||
_id: string
|
||||
fields: Record<string, any> & {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Table } from "@budibase/types"
|
||||
import BaseDataFetch from "./DataFetch"
|
||||
|
||||
interface RelationshipDatasource {
|
||||
export interface RelationshipDatasource {
|
||||
type: "link"
|
||||
tableId: string
|
||||
rowId: string
|
||||
|
|
|
@ -2,7 +2,7 @@ import { get } from "svelte/store"
|
|||
import BaseDataFetch from "./DataFetch"
|
||||
import { SortOrder, Table } from "@budibase/types"
|
||||
|
||||
interface TableDatasource {
|
||||
export interface TableDatasource {
|
||||
type: "table"
|
||||
tableId: string
|
||||
}
|
||||
|
|
|
@ -9,12 +9,15 @@ interface UserFetchQuery {
|
|||
paginated: boolean
|
||||
}
|
||||
|
||||
interface UserDatasource {
|
||||
export interface UserDatasource {
|
||||
type: "user"
|
||||
tableId: TableNames.USERS
|
||||
}
|
||||
|
||||
interface UserDefinition {}
|
||||
interface UserDefinition {
|
||||
schema?: Record<string, any> | null
|
||||
primaryDisplay?: string
|
||||
}
|
||||
|
||||
export default class UserFetch extends BaseDataFetch<
|
||||
UserDatasource,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Table } from "@budibase/types"
|
||||
import BaseDataFetch from "./DataFetch"
|
||||
|
||||
type ViewV1Datasource = {
|
||||
export type ViewV1Datasource = {
|
||||
type: "view"
|
||||
name: string
|
||||
tableId: string
|
||||
|
|
|
@ -3,7 +3,7 @@ import BaseDataFetch from "./DataFetch"
|
|||
import { get } from "svelte/store"
|
||||
import { helpers } from "@budibase/shared-core"
|
||||
|
||||
interface ViewDatasource {
|
||||
export interface ViewDatasource {
|
||||
type: "viewV2"
|
||||
id: string
|
||||
}
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
import TableFetch from "./TableFetch"
|
||||
import ViewFetch from "./ViewFetch"
|
||||
import ViewV2Fetch from "./ViewV2Fetch"
|
||||
import QueryFetch from "./QueryFetch"
|
||||
import RelationshipFetch from "./RelationshipFetch"
|
||||
import NestedProviderFetch from "./NestedProviderFetch"
|
||||
import FieldFetch from "./FieldFetch"
|
||||
import TableFetch, { TableDatasource } from "./TableFetch"
|
||||
import ViewFetch, { ViewV1Datasource } from "./ViewFetch"
|
||||
import ViewV2Fetch, { ViewDatasource } from "./ViewV2Fetch"
|
||||
import QueryFetch, { QueryDatasource } from "./QueryFetch"
|
||||
import RelationshipFetch, { RelationshipDatasource } from "./RelationshipFetch"
|
||||
import NestedProviderFetch, {
|
||||
NestedProviderDatasource,
|
||||
} from "./NestedProviderFetch"
|
||||
import FieldFetch, { FieldDatasource } from "./FieldFetch"
|
||||
import JSONArrayFetch from "./JSONArrayFetch"
|
||||
import UserFetch from "./UserFetch"
|
||||
import GroupUserFetch from "./GroupUserFetch"
|
||||
import CustomFetch from "./CustomFetch"
|
||||
import UserFetch, { UserDatasource } from "./UserFetch"
|
||||
import GroupUserFetch, { GroupUserDatasource } from "./GroupUserFetch"
|
||||
import CustomFetch, { CustomDatasource } from "./CustomFetch"
|
||||
import QueryArrayFetch from "./QueryArrayFetch"
|
||||
import { APIClient } from "../api/types"
|
||||
import { Table, ViewV2Enriched } from "@budibase/types"
|
||||
|
@ -47,6 +49,20 @@ export type DataFetch =
|
|||
| JSONArrayFetch
|
||||
| QueryArrayFetch
|
||||
|
||||
export type DataFetchDatasource =
|
||||
| TableDatasource
|
||||
| ViewV1Datasource
|
||||
| ViewDatasource
|
||||
| QueryDatasource
|
||||
| RelationshipDatasource
|
||||
| UserDatasource
|
||||
| GroupUserDatasource
|
||||
| CustomDatasource
|
||||
| NestedProviderDatasource
|
||||
| FieldDatasource<"field" | "queryarray" | "jsonarray">
|
||||
// | FieldDatasource<"field" | "queryarray" | "jsonarray">
|
||||
// | FieldDatasource<"field" | "queryarray" | "jsonarray">
|
||||
|
||||
export type DataFetchDefinition =
|
||||
| Table
|
||||
| ViewV2Enriched
|
||||
|
@ -56,16 +72,16 @@ export type DataFetchDefinition =
|
|||
}
|
||||
|
||||
// Constructs a new fetch model for a certain datasource
|
||||
export const fetchData = <TDatasource extends { type: DataFetchType }>({
|
||||
export const fetchData = ({
|
||||
API,
|
||||
datasource,
|
||||
options,
|
||||
}: {
|
||||
API: APIClient
|
||||
datasource: TDatasource
|
||||
datasource: DataFetchDatasource
|
||||
options: any
|
||||
}) => {
|
||||
const Fetch = DataFetchMap[datasource?.type as DataFetchType] || TableFetch
|
||||
const Fetch = DataFetchMap[datasource?.type] || TableFetch
|
||||
const fetch = new Fetch({ API, datasource, ...options })
|
||||
|
||||
// Initially fetch data but don't bother waiting for the result
|
||||
|
@ -76,14 +92,14 @@ export const fetchData = <TDatasource extends { type: DataFetchType }>({
|
|||
|
||||
// Creates an empty fetch instance with no datasource configured, so no data
|
||||
// will initially be loaded
|
||||
const createEmptyFetchInstance = <TDatasource extends { type: DataFetchType }>({
|
||||
const createEmptyFetchInstance = ({
|
||||
API,
|
||||
datasource,
|
||||
}: {
|
||||
API: APIClient
|
||||
datasource: TDatasource
|
||||
datasource: DataFetchDatasource
|
||||
}) => {
|
||||
const handler = DataFetchMap[datasource?.type as DataFetchType]
|
||||
const handler = DataFetchMap[datasource?.type]
|
||||
if (!handler) {
|
||||
return null
|
||||
}
|
||||
|
@ -95,29 +111,25 @@ const createEmptyFetchInstance = <TDatasource extends { type: DataFetchType }>({
|
|||
}
|
||||
|
||||
// Fetches the definition of any type of datasource
|
||||
export const getDatasourceDefinition = async <
|
||||
TDatasource extends { type: DataFetchType }
|
||||
>({
|
||||
export const getDatasourceDefinition = async ({
|
||||
API,
|
||||
datasource,
|
||||
}: {
|
||||
API: APIClient
|
||||
datasource: TDatasource
|
||||
datasource: DataFetchDatasource
|
||||
}) => {
|
||||
const instance = createEmptyFetchInstance({ API, datasource })
|
||||
return await instance?.getDefinition()
|
||||
}
|
||||
|
||||
// Fetches the schema of any type of datasource
|
||||
export const getDatasourceSchema = <
|
||||
TDatasource extends { type: DataFetchType }
|
||||
>({
|
||||
export const getDatasourceSchema = ({
|
||||
API,
|
||||
datasource,
|
||||
definition,
|
||||
}: {
|
||||
API: APIClient
|
||||
datasource: TDatasource
|
||||
datasource: DataFetchDatasource
|
||||
definition?: any
|
||||
}) => {
|
||||
const instance = createEmptyFetchInstance({ API, datasource })
|
||||
|
|
Loading…
Reference in New Issue