useFetch
Based on vueuse/useFetch, with support for new features like caching, auto updates, conditional requests, and streams
Usage
javascript
import { useFetch } from "fluth-vue";
const { data, loading, error, promise$ } = useFetch("https://example.com");
Use Cases
useFetch is used to handle relationships between asynchronous data, generally with the following three use cases:
Declarative + Reactive Relationship
For example, data2
and data1
in the following example:
javascript
import { useFetch } from "fluth-vue";
const useFetchApi = (payload: Ref<Record<string, any>>) =>
useFetch("https://example.com", { refetch: true }).post(payload).json();
const data1 = ref({ a: 1 });
// Usage
const { data: data2 } = useFetchApi(data1);
data2
anddata1
form a declarative relationship through theuseFetchApi
function, without needing to care about the details of producingdata2
data2
anddata1
form a reactive relationship through theuseFetchApi
function,data2
will change asdata1
changes
Declarative Relationship
For example, data2
and data1
in the following example:
javascript
import { useFetch } from "fluth-vue";
const useFetchApi = (payload: Ref<Record<string, any>>) =>
useFetch("https://example.com").post(payload).json();
const data1 = ref({ a: 1 });
const { data: data2, execute: fetchData2 } = useFetchApi(data1);
data2
anddata1
form a declarative relationship through theuseFetchApi
function- Update
data2
by calling thefetchData2
function, which will use the latestdata1
as the request parameter
Call Relationship
For example, data2
and data1
in the following example:
javascript
import { useFetch } from "fluth-vue";
const useFetchApi = (payload: Record<string, any>) =>
useFetch("https://example.com").post(payload).json();
const data1 = ref({ a: 1 });
const { data: data2 } = await useFetchApi(data1.value);
data2
is obtained by calling theuseFetchApi
async function and taking the current value ofdata1
as the request parameter in real-time
Stream
useFetch provides not only reactive data input and reactive data output, but also supports fluth streams. Streams can be used as both input and output for useFetch, allowing async requests to be handled as nodes in stream programming. See promise$ for details.