From 5b4b27dc167a28c61e275b80db70a5da42e91381 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 14 Jan 2025 12:51:27 +0100 Subject: [PATCH] Improve typings removing generics --- .../frontend-core/src/fetch/CustomFetch.ts | 2 +- .../frontend-core/src/fetch/GroupUserFetch.ts | 9 ++- .../src/fetch/NestedProviderFetch.ts | 2 +- .../frontend-core/src/fetch/QueryFetch.ts | 2 +- .../src/fetch/RelationshipFetch.ts | 2 +- .../frontend-core/src/fetch/TableFetch.ts | 2 +- packages/frontend-core/src/fetch/UserFetch.ts | 7 ++- packages/frontend-core/src/fetch/ViewFetch.ts | 2 +- .../frontend-core/src/fetch/ViewV2Fetch.ts | 2 +- packages/frontend-core/src/fetch/index.ts | 60 +++++++++++-------- 10 files changed, 55 insertions(+), 35 deletions(-) diff --git a/packages/frontend-core/src/fetch/CustomFetch.ts b/packages/frontend-core/src/fetch/CustomFetch.ts index 60ce1f50ec..ec119c9c5d 100644 --- a/packages/frontend-core/src/fetch/CustomFetch.ts +++ b/packages/frontend-core/src/fetch/CustomFetch.ts @@ -1,6 +1,6 @@ import BaseDataFetch from "./DataFetch" -interface CustomDatasource { +export interface CustomDatasource { type: "custom" data: any } diff --git a/packages/frontend-core/src/fetch/GroupUserFetch.ts b/packages/frontend-core/src/fetch/GroupUserFetch.ts index f3fdcf5ebb..505e6fb69b 100644 --- a/packages/frontend-core/src/fetch/GroupUserFetch.ts +++ b/packages/frontend-core/src/fetch/GroupUserFetch.ts @@ -7,14 +7,19 @@ interface GroupUserQuery { emailSearch: string } -interface GroupUserDatasource { +export interface GroupUserDatasource { type: "groupUser" tableId: TableNames.USERS } +interface GroupUserDefinition { + schema?: Record | null + primaryDisplay?: string +} + export default class GroupUserFetch extends BaseDataFetch< GroupUserDatasource, - {}, + GroupUserDefinition, GroupUserQuery > { constructor(opts: DataFetchParams) { diff --git a/packages/frontend-core/src/fetch/NestedProviderFetch.ts b/packages/frontend-core/src/fetch/NestedProviderFetch.ts index b4274e690f..42ae56b5a6 100644 --- a/packages/frontend-core/src/fetch/NestedProviderFetch.ts +++ b/packages/frontend-core/src/fetch/NestedProviderFetch.ts @@ -1,7 +1,7 @@ import { Row, TableSchema } from "@budibase/types" import BaseDataFetch from "./DataFetch" -interface NestedProviderDatasource { +export interface NestedProviderDatasource { type: "provider" value?: { schema: TableSchema diff --git a/packages/frontend-core/src/fetch/QueryFetch.ts b/packages/frontend-core/src/fetch/QueryFetch.ts index 179deb1a66..37ddb87aa9 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.ts +++ b/packages/frontend-core/src/fetch/QueryFetch.ts @@ -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 & { diff --git a/packages/frontend-core/src/fetch/RelationshipFetch.ts b/packages/frontend-core/src/fetch/RelationshipFetch.ts index c84c38e77c..f7af8dd123 100644 --- a/packages/frontend-core/src/fetch/RelationshipFetch.ts +++ b/packages/frontend-core/src/fetch/RelationshipFetch.ts @@ -1,7 +1,7 @@ import { Table } from "@budibase/types" import BaseDataFetch from "./DataFetch" -interface RelationshipDatasource { +export interface RelationshipDatasource { type: "link" tableId: string rowId: string diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index c39c9341fc..eff3797754 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -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 } diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index 2632337d64..517e38657d 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -9,12 +9,15 @@ interface UserFetchQuery { paginated: boolean } -interface UserDatasource { +export interface UserDatasource { type: "user" tableId: TableNames.USERS } -interface UserDefinition {} +interface UserDefinition { + schema?: Record | null + primaryDisplay?: string +} export default class UserFetch extends BaseDataFetch< UserDatasource, diff --git a/packages/frontend-core/src/fetch/ViewFetch.ts b/packages/frontend-core/src/fetch/ViewFetch.ts index 219f17ee2c..53f92c5372 100644 --- a/packages/frontend-core/src/fetch/ViewFetch.ts +++ b/packages/frontend-core/src/fetch/ViewFetch.ts @@ -1,7 +1,7 @@ import { Table } from "@budibase/types" import BaseDataFetch from "./DataFetch" -type ViewV1Datasource = { +export type ViewV1Datasource = { type: "view" name: string tableId: string diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index 029047c5fb..9639d2e13f 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -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 } diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 2bb6e09000..bacea080cb 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -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 = ({ +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 = ({ // Creates an empty fetch instance with no datasource configured, so no data // will initially be loaded -const createEmptyFetchInstance = ({ +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 = ({ } // 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 })