mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.3 KiB
88 lines
2.3 KiB
2 years ago
|
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
|
||
|
import { Api } from 'nocodb-sdk'
|
||
2 years ago
|
import type { Ref } from 'vue'
|
||
2 years ago
|
import type { EventHook, MaybeRef } from '@vueuse/core'
|
||
|
import { addAxiosInterceptors } from './interceptors'
|
||
|
import { createEventHook, ref, unref, useNuxtApp } from '#imports'
|
||
|
import type { NuxtApp } from '#app'
|
||
2 years ago
|
|
||
|
interface UseApiReturn<D = any, R = any> {
|
||
|
api: Api<any>
|
||
|
isLoading: Ref<boolean>
|
||
|
error: Ref<AxiosError<D, R> | null>
|
||
|
response: Ref<AxiosResponse<D, R> | null>
|
||
|
onError: EventHook<AxiosError<D, R>>['on']
|
||
|
onResponse: EventHook<AxiosResponse<D, R>>['on']
|
||
|
}
|
||
|
|
||
2 years ago
|
export function createApiInstance(app: NuxtApp, baseURL = 'http://localhost:8080') {
|
||
|
const api = new Api({
|
||
|
baseURL,
|
||
|
})
|
||
|
|
||
|
addAxiosInterceptors(api, app)
|
||
|
|
||
|
return api
|
||
|
}
|
||
|
|
||
2 years ago
|
/** todo: add props? */
|
||
2 years ago
|
interface UseApiProps<D = any> {
|
||
|
axiosConfig?: MaybeRef<AxiosRequestConfig<D>>
|
||
|
useGlobalInstance?: MaybeRef<boolean>
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
export function useApi<Data = any, RequestConfig = any>(props: UseApiProps<Data> = {}): UseApiReturn<Data, RequestConfig> {
|
||
2 years ago
|
const isLoading = ref(false)
|
||
|
|
||
|
const error = ref(null)
|
||
|
|
||
|
const response = ref<any>(null)
|
||
|
|
||
|
const errorHook = createEventHook<AxiosError<Data, RequestConfig>>()
|
||
|
|
||
|
const responseHook = createEventHook<AxiosResponse<Data, RequestConfig>>()
|
||
|
|
||
2 years ago
|
const api = unref(props.useGlobalInstance) ? useNuxtApp().$api : createApiInstance(useNuxtApp())
|
||
2 years ago
|
|
||
2 years ago
|
api.instance.interceptors.request.use(
|
||
2 years ago
|
(config) => {
|
||
|
error.value = null
|
||
|
response.value = null
|
||
|
isLoading.value = true
|
||
|
|
||
2 years ago
|
return {
|
||
|
...config,
|
||
|
...unref(props),
|
||
|
}
|
||
2 years ago
|
},
|
||
|
(requestError) => {
|
||
|
errorHook.trigger(requestError)
|
||
|
error.value = requestError
|
||
|
response.value = null
|
||
|
isLoading.value = false
|
||
2 years ago
|
|
||
|
return requestError
|
||
2 years ago
|
},
|
||
|
)
|
||
|
|
||
2 years ago
|
api.instance.interceptors.response.use(
|
||
2 years ago
|
(apiResponse) => {
|
||
|
responseHook.trigger(apiResponse as AxiosResponse<Data, RequestConfig>)
|
||
|
// can't properly typecast
|
||
|
response.value = apiResponse
|
||
|
isLoading.value = false
|
||
2 years ago
|
|
||
|
return apiResponse
|
||
2 years ago
|
},
|
||
|
(apiError) => {
|
||
|
errorHook.trigger(apiError)
|
||
|
error.value = apiError
|
||
|
isLoading.value = false
|
||
2 years ago
|
|
||
|
return apiError
|
||
2 years ago
|
},
|
||
|
)
|
||
|
|
||
2 years ago
|
return { api, isLoading, response, error, onError: errorHook.on, onResponse: responseHook.on }
|
||
2 years ago
|
}
|