Browse Source

feat(gui-v2): add option to use global api instance in `useApi`

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

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

@ -3,7 +3,7 @@ import { Api } from 'nocodb-sdk'
import type { Ref } from 'vue' import type { Ref } from 'vue'
import { addAxiosInterceptors } from './interceptors' import { addAxiosInterceptors } from './interceptors'
import type { CreateApiOptions, UseApiProps, UseApiReturn } from './types' import type { CreateApiOptions, UseApiProps, UseApiReturn } from './types'
import { createEventHook, ref, unref, useCounter, useGlobal } from '#imports' import { createEventHook, ref, unref, useCounter, useGlobal, useNuxtApp } from '#imports'
export function createApiInstance<SecurityDataType = any>(options: CreateApiOptions = {}): Api<SecurityDataType> { export function createApiInstance<SecurityDataType = any>(options: CreateApiOptions = {}): Api<SecurityDataType> {
return addAxiosInterceptors( return addAxiosInterceptors(
@ -28,7 +28,11 @@ export function createApiInstance<SecurityDataType = any>(options: CreateApiOpti
* const { token } = await api.auth.signIn(form) * const { token } = await api.auth.signIn(form)
* } * }
*/ */
export function useApi<Data = any, RequestConfig = any>(props: UseApiProps<Data> = {}): UseApiReturn<Data, RequestConfig> { export function useApi<Data = any, RequestConfig = any>({
useGlobalInstance = false,
apiOptions,
axiosConfig,
}: UseApiProps<Data> = {}): UseApiReturn<Data, RequestConfig> {
const state = useGlobal() const state = useGlobal()
/** /**
@ -50,8 +54,11 @@ export function useApi<Data = any, RequestConfig = any>(props: UseApiProps<Data>
const responseHook = createEventHook<AxiosResponse<Data, RequestConfig>>() const responseHook = createEventHook<AxiosResponse<Data, RequestConfig>>()
/** fresh api instance - with interceptors for token refresh already bound */ /** global api instance */
const api = createApiInstance(props.apiOptions) const $api = useNuxtApp().$api
/** api instance - with interceptors for token refresh already bound */
const api = useGlobalInstance && !!$api ? $api : createApiInstance(apiOptions)
/** set loading to true and increment local and global request counter */ /** set loading to true and increment local and global request counter */
function onRequestStart() { function onRequestStart() {
@ -96,7 +103,7 @@ export function useApi<Data = any, RequestConfig = any>(props: UseApiProps<Data>
return { return {
...config, ...config,
...unref(props), ...unref(axiosConfig),
} }
}, },
(requestError) => { (requestError) => {

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

@ -22,4 +22,5 @@ export interface UseApiProps<D = any> {
axiosConfig?: MaybeRef<AxiosRequestConfig<D>> axiosConfig?: MaybeRef<AxiosRequestConfig<D>>
/** {@link Api} options */ /** {@link Api} options */
apiOptions?: CreateApiOptions apiOptions?: CreateApiOptions
useGlobalInstance?: boolean
} }

Loading…
Cancel
Save