2.2.0 • Published 4 months ago
nuxt-custom-fetch v2.2.0
Nuxt Module
简单封装 useAsyncData
version > 3.0.0
注意
请求的使用形式,类似于之前的ajax
,修改参数后,重新调用封装方法。
- 在 onMounted 中请求无效(version > 3.0.0)
- 在下一个 nextTick 中执行,或者设置{server: false},watch pending 变化
refresh
和execute
使用上也需要注意,参数不是响应式的,所以会一直是第一次请求的参数如果传入响应式对象,使用
watch
监听,会自动重新请求
安装
pnpm add nuxt-custom-fetch
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['nuxt-custom-fetch']
})
使用
实例化全局 CustomFetch
const ajax = new CustomFetch({
baseURL: ''
})
// `http|get|post`
ajax.get('/api/user')
ajax.post('/api/user')
ajax.http({
method: 'GET'
})
类型
useAsyncDataType
export declare class CustomFetch {
baseURL: any
immutableKey: boolean
showLogs: boolean
params: HTTPConfig
baseHandler: HTTPConfig['handler']
interceptors: Interceptors
offline: typeof Noop
constructor (config?: HTTPConfig)
private baseConfig
http<DataT, ErrorT = Error | null>(url: NitroFetchRequest, config: HTTPConfig & {
method: FetchMethod
}, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, ErrorT>
get<DataT, ErrorT = Error | null>(url: NitroFetchRequest, config?: HTTPConfig, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, ErrorT>
post<DataT, ErrorT = Error | null>(url: NitroFetchRequest, config?: HTTPConfig, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, ErrorT>
}
export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'post' | 'head' | 'patch' | 'put' | 'delete' | 'connect' | 'trace' | undefined
export interface HTTPConfig extends Omit<FetchOptions, 'method'> {
key?: string
immutableKey?: boolean
baseURL?: string
useHandler?: boolean
handler?: (params: Record<string, any>) => Record<string, any>
offline?: () => void
}
export interface Interceptors {
onRequest?: FetchOptions['onRequest']
onRequestError?: FetchOptions['onRequestError']
onResponse?: FetchOptions['onResponse']
onResponseError?: FetchOptions['onResponseError']
}
示例
// api.ts
export const getInfo = (params: Record<string, any>) =>
ajax.get<DataT>('/api/get-ip', { params }, {})
// index.vue
import { getInfo } from './ajax'
const { data, error, pending } = await getInfo({
sign: 123
})
console.log(data.value)
Development
- Run
npm run dev:prepare
to generate type stubs. - Use
npm run dev
to start playground in development mode.
2.1.2
7 months ago
2.2.0
4 months ago
2.1.4
4 months ago
2.1.3
4 months ago
2.1.1
12 months ago
2.1.0
1 year ago
2.0.4
1 year ago
2.0.3
1 year ago
2.0.2
1 year ago
2.0.1
1 year ago
2.0.0
1 year ago
1.1.7
1 year ago
1.1.6
1 year ago
1.1.5
1 year ago
1.1.4
1 year ago
1.1.3
1 year ago
1.1.2
1 year ago
1.1.1
2 years ago
1.1.0
2 years ago
1.0.8
2 years ago
1.0.7
2 years ago
1.0.6
2 years ago
1.0.6-alpha.1
2 years ago
1.0.6-alpha.0
2 years ago
1.0.5
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago
0.0.2-alpha.0
2 years ago
0.0.1
2 years ago