budibase/packages/builder/src/helpers/fetchData.js

21 lines
495 B
JavaScript
Raw Normal View History

2021-05-14 16:09:07 +02:00
import { writable } from "svelte/store"
2022-01-20 20:27:15 +01:00
import { API } from "api"
export default function (url) {
2021-05-14 16:09:07 +02:00
const store = writable({ status: "LOADING", data: {}, error: {} })
async function get() {
2021-05-17 13:24:09 +02:00
store.update(u => ({ ...u, status: "LOADING" }))
2021-05-14 16:09:07 +02:00
try {
2022-01-20 20:27:15 +01:00
const data = await API.get({ url })
store.set({ data, status: "SUCCESS" })
2021-05-14 16:09:07 +02:00
} catch (e) {
store.set({ data: {}, error: e, status: "ERROR" })
}
}
get()
return { subscribe: store.subscribe, refresh: get }
}