Browse Source

refactor(gui-v2): move `useApi` related types to separate file

pull/2877/head
braks 2 years ago
parent
commit
25f2b65412
  1. 25
      packages/nc-gui-v2/composables/useApi/index.ts
  2. 25
      packages/nc-gui-v2/composables/useApi/types.ts

25
packages/nc-gui-v2/composables/useApi/index.ts

@ -1,24 +1,10 @@
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
import type { AxiosError, AxiosResponse } from 'axios'
import { Api } from 'nocodb-sdk'
import type { Ref } from 'vue'
import type { EventHook, MaybeRef } from '@vueuse/core'
import { addAxiosInterceptors } from './interceptors'
import type { CreateApiOptions, UseApiProps, UseApiReturn } from './types'
import { createEventHook, ref, unref, useCounter, useGlobal } from '#imports'
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']
}
/** {@link Api} options */
interface CreateApiOptions {
baseURL?: string
}
export function createApiInstance<SecurityDataType = any>(options: CreateApiOptions = {}): Api<SecurityDataType> {
return addAxiosInterceptors(
new Api<SecurityDataType>({
@ -27,13 +13,6 @@ export function createApiInstance<SecurityDataType = any>(options: CreateApiOpti
)
}
interface UseApiProps<D = any> {
/** additional axios config for requests */
axiosConfig?: MaybeRef<AxiosRequestConfig<D>>
/** {@link Api} options */
apiOptions?: CreateApiOptions
}
/**
* Api composable that provides loading, error and response refs, as well as event hooks for error and response.
*

25
packages/nc-gui-v2/composables/useApi/types.ts

@ -0,0 +1,25 @@
import type { Api } from 'nocodb-sdk'
import type { Ref } from 'vue'
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
import type { EventHook, MaybeRef } from '@vueuse/core'
export 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']
}
/** {@link Api} options */
export interface CreateApiOptions {
baseURL?: string
}
export interface UseApiProps<D = any> {
/** additional axios config for requests */
axiosConfig?: MaybeRef<AxiosRequestConfig<D>>
/** {@link Api} options */
apiOptions?: CreateApiOptions
}
Loading…
Cancel
Save