Created
May 28, 2022 13:20
-
-
Save theaungmyatmoe/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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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