mirror of https://github.com/Budibase/budibase.git
10 changed files with 124 additions and 105 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,26 @@ |
|||||
|
const apiCall = method => async (url, body) => { |
||||
|
const headers = { |
||||
|
"Content-Type": "application/json", |
||||
|
} |
||||
|
const response = await fetch(url, { |
||||
|
method: method, |
||||
|
body: body && JSON.stringify(body), |
||||
|
headers, |
||||
|
}) |
||||
|
|
||||
|
return response |
||||
|
} |
||||
|
|
||||
|
export const post = apiCall("POST") |
||||
|
export const get = apiCall("GET") |
||||
|
export const patch = apiCall("PATCH") |
||||
|
export const del = apiCall("DELETE") |
||||
|
export const put = apiCall("PUT") |
||||
|
|
||||
|
export default { |
||||
|
post: apiCall("POST"), |
||||
|
get: apiCall("GET"), |
||||
|
patch: apiCall("PATCH"), |
||||
|
delete: apiCall("DELETE"), |
||||
|
put: apiCall("PUT"), |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
import api from "./api" |
||||
|
|
||||
|
export default async function fetchData(datasource) { |
||||
|
const { isModel, name } = datasource |
||||
|
|
||||
|
if (name) { |
||||
|
return isModel ? await fetchModelData() : await fetchViewData() |
||||
|
} |
||||
|
|
||||
|
async function fetchModelData() { |
||||
|
if (!name.startsWith("all_")) { |
||||
|
throw new Error("Incorrect model convention - must begin with all_") |
||||
|
} |
||||
|
const modelsResponse = await api.get(`/api/views/${name}`) |
||||
|
return await modelsResponse.json() |
||||
|
} |
||||
|
|
||||
|
async function fetchViewData() { |
||||
|
const { field, groupBy } = datasource |
||||
|
const params = new URLSearchParams() |
||||
|
|
||||
|
if (field) params.set("stats", true) |
||||
|
if (groupBy) params.set("group", groupBy) |
||||
|
let QUERY_VIEW_URL = |
||||
|
field && groupBy ? `/api/views/${name}?${params}` : `/api/views/${name}` |
||||
|
|
||||
|
const response = await api.get(QUERY_VIEW_URL) |
||||
|
return await response.json() |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue