Type view endpoints

This commit is contained in:
Andrew Kingston 2024-12-04 11:39:20 +00:00
parent 170d45320b
commit 1b467f2e74
No known key found for this signature in database
5 changed files with 24 additions and 15 deletions

View File

@ -56,10 +56,7 @@
}
const exportAllData = async () => {
return await API.exportView({
viewName: view,
format: exportFormat,
})
return await API.exportView(view, exportFormat)
}
const exportFilteredData = async () => {

View File

@ -43,8 +43,7 @@
return
}
try {
data = await API.fetchViewData({
name,
data = await API.fetchViewData(name, {
calculation,
field,
groupBy,

View File

@ -30,6 +30,7 @@ import { SelfEndpoints } from "./self"
import { TableEndpoints } from "./tables"
import { TemplateEndpoints } from "./templates"
import { UserEndpoints } from "./user"
import { ViewEndpoints } from "./views"
export enum HTTPMethod {
POST = "POST",
@ -130,4 +131,5 @@ export type APIClient = BaseAPIClient &
SelfEndpoints &
TableEndpoints &
TemplateEndpoints &
UserEndpoints & { rowActions: RowActionEndpoints; [key: string]: any }
UserEndpoints &
ViewEndpoints & { rowActions: RowActionEndpoints; [key: string]: any }

View File

@ -1,4 +1,15 @@
export const buildViewEndpoints = API => ({
import { Row } from "@budibase/types"
import { BaseAPIClient } from "./types"
export interface ViewEndpoints {
// Missing request or response types
fetchViewData: (name: string, opts: any) => Promise<Row[]>
exportView: (name: string, format: string) => Promise<any>
saveView: (view: any) => Promise<any>
deleteView: (name: string) => Promise<any>
}
export const buildViewEndpoints = (API: BaseAPIClient): ViewEndpoints => ({
/**
* Fetches all rows in a view
* @param name the name of the view
@ -6,7 +17,7 @@ export const buildViewEndpoints = API => ({
* @param groupBy the field to group by
* @param calculation the calculation to perform
*/
fetchViewData: async ({ name, field, groupBy, calculation }) => {
fetchViewData: async (name, { field, groupBy, calculation }) => {
const params = new URLSearchParams()
if (calculation) {
params.set("field", field)
@ -23,11 +34,11 @@ export const buildViewEndpoints = API => ({
/**
* Exports a view for download
* @param viewName the view to export
* @param name the view to export
* @param format the format to download
*/
exportView: async ({ viewName, format }) => {
const safeViewName = encodeURIComponent(viewName)
exportView: async (name, format) => {
const safeViewName = encodeURIComponent(name)
return await API.get({
url: `/api/views/export?view=${safeViewName}&format=${format}`,
parseResponse: async response => {
@ -51,9 +62,9 @@ export const buildViewEndpoints = API => ({
* Deletes a view.
* @param viewName the name of the view to delete
*/
deleteView: async viewName => {
deleteView: async name => {
return await API.delete({
url: `/api/views/${encodeURIComponent(viewName)}`,
url: `/api/views/${encodeURIComponent(name)}`,
})
},
})

View File

@ -8,7 +8,7 @@ export default class ViewFetch extends DataFetch {
async getData() {
const { datasource } = this.options
try {
const res = await this.API.fetchViewData(datasource)
const res = await this.API.fetchViewData(datasource.name)
return { rows: res || [] }
} catch (error) {
return { rows: [] }