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"
|
2021-05-07 14:30:51 +02:00
|
|
|
|
2021-05-11 17:51:01 +02:00
|
|
|
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 }
|
|
|
|
}
|