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

20 lines
514 B
JavaScript
Raw Normal View History

import { writable } from 'svelte/store'
import api from "builderStore/api"
export default function (url) {
const store = writable({status: 'LOADING', data: {}, error: {}})
async function get() {
store.update(u => ({...u, status: 'SUCCESS'}))
try {
const response = await api.get(url)
store.set({data: await response.json(), status: 'SUCCESS'})
} catch(e) {
store.set({data: {}, error: e, status: 'ERROR'})
}
}
get()
2021-05-07 14:52:58 +02:00
return {subscribe: store.subscribe, refresh: get}
}