UNPKG

@neosjs/create-app

Version:

帮助开发者快速创建Vue3应用并自动配置项目

39 lines (35 loc) 1.19 kB
import { type IRequestConfig, request, requestAll } from '@neosjs/request' import { merge } from 'lodash-es' const controller = new AbortController() const { VITE_API_URL = '/' } = import.meta.env /** * UseRequest * @param {string} url 接口地址 * @param {any} params 请求参数 * @param {IRequestConfig} config 请求配置 * @returns {Promise} */ export const useRequest = async (url: string | IRequestConfig, params?: any, config?: IRequestConfig) => { try { if (!window.navigator?.onLine) return Promise.reject(new Error('网络异常,请检查网络')) const options = merge( { baseURL: VITE_API_URL, // 接口地址 // 这里可以配置一些公共的配置 cache: false, // 禁止接口缓存 retry: 2, // 重试两次 headers: { }, withCredentials: true, signal: controller.signal }, config // 接口调用处传来的配置 ) options.params = params const { data } = await request(url, options) return data } catch (error: any) { return Promise.reject(new Error(error)) } } export const useRequestAll = (iterable: Fn[]) => requestAll(iterable)