Composables
useSanctumFetch
How to use useSanctumFetch to fetch data from Laravel API
Usage
Besides useSanctumClient you can directly send a request by using a module-specific
version of fetch composable - useSanctumFetch.
This composable implements an API-specific useFetch, so you can check more details here.
Composable accepts 2 arguments:
url: target endpoint to call (type:MaybeRefOrGetter<string>)options: fetch options for client (type:UseFetchOptions<T>)
const { data, status, error, refresh } = await useSanctumFetch("/api/users")
// or
const { data, status, error, refresh } = await useSanctumFetch("/api/users", {
pick: ["id"],
method: "GET",
query: {
is_active: true,
},
})
You can also use type casting to work with the response as an interface:
interface MyResponse {
name: string
}
const { data } = await useSanctumFetch<MyResponse>("/api/endpoint")
const name = data.value.name // augmented by MyResponse interface