Improve typings removing generics

This commit is contained in:
Adria Navarro 2025-01-14 12:51:27 +01:00
parent 7d284ae50e
commit 5b4b27dc16
10 changed files with 55 additions and 35 deletions

View File

@ -1,6 +1,6 @@
import BaseDataFetch from "./DataFetch" import BaseDataFetch from "./DataFetch"
interface CustomDatasource { export interface CustomDatasource {
type: "custom" type: "custom"
data: any data: any
} }

View File

@ -7,14 +7,19 @@ interface GroupUserQuery {
emailSearch: string emailSearch: string
} }
interface GroupUserDatasource { export interface GroupUserDatasource {
type: "groupUser" type: "groupUser"
tableId: TableNames.USERS tableId: TableNames.USERS
} }
interface GroupUserDefinition {
schema?: Record<string, any> | null
primaryDisplay?: string
}
export default class GroupUserFetch extends BaseDataFetch< export default class GroupUserFetch extends BaseDataFetch<
GroupUserDatasource, GroupUserDatasource,
{}, GroupUserDefinition,
GroupUserQuery GroupUserQuery
> { > {
constructor(opts: DataFetchParams<GroupUserDatasource, GroupUserQuery>) { constructor(opts: DataFetchParams<GroupUserDatasource, GroupUserQuery>) {

View File

@ -1,7 +1,7 @@
import { Row, TableSchema } from "@budibase/types" import { Row, TableSchema } from "@budibase/types"
import BaseDataFetch from "./DataFetch" import BaseDataFetch from "./DataFetch"
interface NestedProviderDatasource { export interface NestedProviderDatasource {
type: "provider" type: "provider"
value?: { value?: {
schema: TableSchema schema: TableSchema

View File

@ -3,7 +3,7 @@ import { Helpers } from "@budibase/bbui"
import { ExecuteQueryRequest, Query } from "@budibase/types" import { ExecuteQueryRequest, Query } from "@budibase/types"
import { get } from "svelte/store" import { get } from "svelte/store"
interface QueryDatasource { export interface QueryDatasource {
type: "query" type: "query"
_id: string _id: string
fields: Record<string, any> & { fields: Record<string, any> & {

View File

@ -1,7 +1,7 @@
import { Table } from "@budibase/types" import { Table } from "@budibase/types"
import BaseDataFetch from "./DataFetch" import BaseDataFetch from "./DataFetch"
interface RelationshipDatasource { export interface RelationshipDatasource {
type: "link" type: "link"
tableId: string tableId: string
rowId: string rowId: string

View File

@ -2,7 +2,7 @@ import { get } from "svelte/store"
import BaseDataFetch from "./DataFetch" import BaseDataFetch from "./DataFetch"
import { SortOrder, Table } from "@budibase/types" import { SortOrder, Table } from "@budibase/types"
interface TableDatasource { export interface TableDatasource {
type: "table" type: "table"
tableId: string tableId: string
} }

View File

@ -9,12 +9,15 @@ interface UserFetchQuery {
paginated: boolean paginated: boolean
} }
interface UserDatasource { export interface UserDatasource {
type: "user" type: "user"
tableId: TableNames.USERS tableId: TableNames.USERS
} }
interface UserDefinition {} interface UserDefinition {
schema?: Record<string, any> | null
primaryDisplay?: string
}
export default class UserFetch extends BaseDataFetch< export default class UserFetch extends BaseDataFetch<
UserDatasource, UserDatasource,

View File

@ -1,7 +1,7 @@
import { Table } from "@budibase/types" import { Table } from "@budibase/types"
import BaseDataFetch from "./DataFetch" import BaseDataFetch from "./DataFetch"
type ViewV1Datasource = { export type ViewV1Datasource = {
type: "view" type: "view"
name: string name: string
tableId: string tableId: string

View File

@ -3,7 +3,7 @@ import BaseDataFetch from "./DataFetch"
import { get } from "svelte/store" import { get } from "svelte/store"
import { helpers } from "@budibase/shared-core" import { helpers } from "@budibase/shared-core"
interface ViewDatasource { export interface ViewDatasource {
type: "viewV2" type: "viewV2"
id: string id: string
} }

View File

@ -1,14 +1,16 @@
import TableFetch from "./TableFetch" import TableFetch, { TableDatasource } from "./TableFetch"
import ViewFetch from "./ViewFetch" import ViewFetch, { ViewV1Datasource } from "./ViewFetch"
import ViewV2Fetch from "./ViewV2Fetch" import ViewV2Fetch, { ViewDatasource } from "./ViewV2Fetch"
import QueryFetch from "./QueryFetch" import QueryFetch, { QueryDatasource } from "./QueryFetch"
import RelationshipFetch from "./RelationshipFetch" import RelationshipFetch, { RelationshipDatasource } from "./RelationshipFetch"
import NestedProviderFetch from "./NestedProviderFetch" import NestedProviderFetch, {
import FieldFetch from "./FieldFetch" NestedProviderDatasource,
} from "./NestedProviderFetch"
import FieldFetch, { FieldDatasource } from "./FieldFetch"
import JSONArrayFetch from "./JSONArrayFetch" import JSONArrayFetch from "./JSONArrayFetch"
import UserFetch from "./UserFetch" import UserFetch, { UserDatasource } from "./UserFetch"
import GroupUserFetch from "./GroupUserFetch" import GroupUserFetch, { GroupUserDatasource } from "./GroupUserFetch"
import CustomFetch from "./CustomFetch" import CustomFetch, { CustomDatasource } from "./CustomFetch"
import QueryArrayFetch from "./QueryArrayFetch" import QueryArrayFetch from "./QueryArrayFetch"
import { APIClient } from "../api/types" import { APIClient } from "../api/types"
import { Table, ViewV2Enriched } from "@budibase/types" import { Table, ViewV2Enriched } from "@budibase/types"
@ -47,6 +49,20 @@ export type DataFetch =
| JSONArrayFetch | JSONArrayFetch
| QueryArrayFetch | 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 = export type DataFetchDefinition =
| Table | Table
| ViewV2Enriched | ViewV2Enriched
@ -56,16 +72,16 @@ export type DataFetchDefinition =
} }
// Constructs a new fetch model for a certain datasource // Constructs a new fetch model for a certain datasource
export const fetchData = <TDatasource extends { type: DataFetchType }>({ export const fetchData = ({
API, API,
datasource, datasource,
options, options,
}: { }: {
API: APIClient API: APIClient
datasource: TDatasource datasource: DataFetchDatasource
options: any options: any
}) => { }) => {
const Fetch = DataFetchMap[datasource?.type as DataFetchType] || TableFetch const Fetch = DataFetchMap[datasource?.type] || 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
@ -76,14 +92,14 @@ export const fetchData = <TDatasource extends { type: DataFetchType }>({
// 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 = <TDatasource extends { type: DataFetchType }>({ const createEmptyFetchInstance = ({
API, API,
datasource, datasource,
}: { }: {
API: APIClient API: APIClient
datasource: TDatasource datasource: DataFetchDatasource
}) => { }) => {
const handler = DataFetchMap[datasource?.type as DataFetchType] const handler = DataFetchMap[datasource?.type]
if (!handler) { if (!handler) {
return null return null
} }
@ -95,29 +111,25 @@ const createEmptyFetchInstance = <TDatasource extends { type: DataFetchType }>({
} }
// 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 { type: DataFetchType }
>({
API, API,
datasource, datasource,
}: { }: {
API: APIClient API: APIClient
datasource: TDatasource datasource: DataFetchDatasource
}) => { }) => {
const instance = createEmptyFetchInstance({ API, datasource }) const instance = createEmptyFetchInstance({ API, datasource })
return await instance?.getDefinition() return await instance?.getDefinition()
} }
// Fetches the schema of any type of datasource // Fetches the schema of any type of datasource
export const getDatasourceSchema = < export const getDatasourceSchema = ({
TDatasource extends { type: DataFetchType }
>({
API, API,
datasource, datasource,
definition, definition,
}: { }: {
API: APIClient API: APIClient
datasource: TDatasource datasource: DataFetchDatasource
definition?: any definition?: any
}) => { }) => {
const instance = createEmptyFetchInstance({ API, datasource }) const instance = createEmptyFetchInstance({ API, datasource })