Skip to content

Instantly share code, notes, and snippets.

@aungmyatmoethegreat
Created May 28, 2022 07:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aungmyatmoethegreat/ddadd563102f45e37846d9f2dc9c3594 to your computer and use it in GitHub Desktop.
Save aungmyatmoethegreat/ddadd563102f45e37846d9f2dc9c3594 to your computer and use it in GitHub Desktop.
Using custom hook/composable to fetch data in nuxt3
/**
* It takes a URL and options, and returns a response object
* @param url - The URL to fetch.
* @param [options] - The options object that will be passed to the fetch function.
* @returns The return value of the useFetch hook.
*/
import {useFetch, useRuntimeConfig} from "nuxt/app";
export default function useFetchApi(url, options = {}) {
const config = useRuntimeConfig();
return useFetch(url, {
...options,
baseURL: config.public.apiBase,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment