Created
August 8, 2023 06:36
-
-
Save Fanreza/a47438822d43ba802bbb121fcd095226 to your computer and use it in GitHub Desktop.
Nuxt composable $fetch with global options and interceptor
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
import { useRuntimeConfig } from "nuxt/app"; | |
import { $fetch, FetchOptions } from "ofetch"; | |
export const useRawHttp = async (req: string, options: FetchOptions = {}) => { | |
const config = useRuntimeConfig(); | |
options.baseURL = config.public.baseUrl; // set your base url on runtime config | |
options.onResponse = ({ request, response, options }) => {}; | |
options.onRequest = ({ request, response, options }) => {}; | |
options.onResponseError = ({ request, response, options }) => {}; | |
options.onRequestError = ({ request, response, options }) => {}; | |
return $fetch(req, options); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment