Information
This serves as an extension to ohmyfetch for nuxt. This works similar to nuxt/http
and nuxtjs-alt/axios
except it utilizes ohmyfetch. All property options will be under http
. This module is required in order for @nuxt-alt/auth
to function.
Setup
- Add
@nuxt-alt/http
dependency to your project
yarn add @nuxt-alt/http
- Add
@nuxt-alt/http
to the modules
section of nuxt.config.ts
export default defineNuxtConfig({
modules: [
'@nuxt-alt/http'
],
http: {
}
});
Example
Remember this is a mix of ofetch
and nuxt/http
so to use methods you would use as an example:
await $http.$get('/api', options)
await $http.$get({ url: '/api', ...options })
await $http.get('/api', options)
await $http.get({ url: '/api', ...options })
await $http.request({ url: '/api', ...options })
await $http.request('/api', options)
await $http.raw({ url: '/api', ...options })
await $http.raw('/api', options)
await $http.native({ url: '/api', ...options })
await $http.native('/api', options)
Composable
A useHttp
composable is avaialble, it works like useFetch
except uses this module under the hood.
Interceptors
The interceptors should work exactly like how axios has it so to access them you would use:
$http.interceptors.request.use(config)
$http.interceptors.response.use(response)
@nuxtjs-axios based functions have also been added:
$http.onRequest(config)
$http.onResponse(response)
$http.onRequestError(err)
$http.onResponseError(err)
$http.onError(err)
You can also opt to use $fetch
interceptors if you prefer:
await $http.$get('/api', {
async onRequest({ request, options }) {
console.log("[fetch request]", request, options);
options.query = options.query || {};
options.query.t = new Date();
},
})
await useHttp('/api', {
async onRequest({ request, options }) {
console.log("[fetch request]", request, options);
options.query = options.query || {};
options.query.t = new Date();
},
})
Options
Config options are taken from the http module, with minor adjustments.
interface ModuleOptions extends Omit<FetchConfig, 'credentials'> {
baseURL?: string;
browserBaseURL?: string;
host?: string;
prefix?: string;
port?: string | number;
https?: boolean;
proxyHeaders?: boolean;
proxyHeadersIgnore?: string[];
serverTimeout?: number,
clientTimeout?: number,
retry?: number;
credentials?: 'same-origin' | 'omit' | 'include';
headers?: HeadersInit;
debug?: boolean;
}