Skip to content

Instantly share code, notes, and snippets.

@aungmyatmoethegreat
Created May 28, 2022 13:20
Show Gist options
  • Save aungmyatmoethegreat/8d4af8a760404d80b55a9686a01cbc4d to your computer and use it in GitHub Desktop.
Save aungmyatmoethegreat/8d4af8a760404d80b55a9686a01cbc4d to your computer and use it in GitHub Desktop.
Customization the $fetch hook to the easy to use hook that can use multiple http verbs
/**
* It's a wrapper around the `$fetch` function that allows you to pass in a URL and options object
* @param url {string} - The url to fetch
* @param [options] - {
* @example <caption> Example of useBaseFetch() in Nuxt app </caption>
* // returns response
* const response = await $fetch('/api/products', {
* method: 'POST',
* body: data,
* })
* @returns {Function} A function that takes two arguments, url and options.
*/
import {useRuntimeConfig} from "nuxt/app";
export default function useBaseFetch(url, options = {}) {
const config = useRuntimeConfig()
return $fetch(`${config.public.apiBase}${url}`, options)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment