useSanctumFetch

Besides useSanctumClient you can directly send a request by using a module-specific version of fetch composable - useSanctumFetch .

This composable implements a similar interface to useFetch, so you can check more details here.

Composable accepts 3 arguments:

  • endpoint URL to call

  • SanctumFetchOptions to pass to the Sanctum client (ofetch)

  • AsyncDataOptions to pass to useAsyncData under the hood

components/YourComponent.vue
const { data, status, error, refresh } = await useSanctumFetch('/api/users');

// or

const { data, status, error, refresh } = await useSanctumFetch(
  '/api/users',
  {
    method: 'GET',
    query: {
      is_active: true,
    },
  },
  {
    pick: ['id'],
  },
);

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

Last updated