Nuxt useFetch Promise

// 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);
    }
  });
}
本条目发布于。属于软件分类。作者是

关于有个狸

2005年开始的一名站长,从事网站策划、运营,早期一批扎根阿里妈妈、Google Adsense的一员,目前司职前端与产品设计。

发表回复