// useFetch interceptors: https://nuxt.com/docs/api/composables/use-fetch
function httpFetch(url: string) {
return useFetch(url, {
onRequest({ request, options }) {
// Set the request headers
options.headers = options.headers || {};
// options.headers.authorization = "...";
},
onRequestError({ request, options, error }) {
// Handle the request errors
},
onResponse({ request, response, options }) {
// Process the response data
if (response.status === 200) {
return response._data;
}
},
onResponseError({ request, response, options }) {
// Handle the response errors
// reject(response);
},
});
}
export function http(url: string) {
return new Promise((resolve, reject) => {
const { data, pending, refresh, error, status } = httpFetch(url);
if (data.value) {
resolve(data.value);
}
if (error.value) {
reject(error.value);
}
});
}
Nuxt useFetch Promise
发表评论