Skip to content

Instantly share code, notes, and snippets.

@Fanreza
Created August 8, 2023 06:36
Show Gist options
  • Save Fanreza/a47438822d43ba802bbb121fcd095226 to your computer and use it in GitHub Desktop.
Save Fanreza/a47438822d43ba802bbb121fcd095226 to your computer and use it in GitHub Desktop.
Nuxt composable $fetch with global options and interceptor
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