Browse Source

refactor(gui-v2): remove `useGlobal` dependency from `useApi`

pull/3109/head
braks 2 years ago
parent
commit
7466cc8e92
  1. 15
      packages/nc-gui-v2/composables/useApi/index.ts

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

@ -1,9 +1,9 @@
import type { AxiosError, AxiosResponse } from 'axios' import type { AxiosError, AxiosResponse } from 'axios'
import { Api } from 'nocodb-sdk' import { Api } from 'nocodb-sdk'
import type { Ref } from 'vue' import type { Ref } from 'vue'
import { addAxiosInterceptors } from './interceptors'
import type { CreateApiOptions, UseApiProps, UseApiReturn } from './types' import type { CreateApiOptions, UseApiProps, UseApiReturn } from './types'
import { createEventHook, ref, unref, useCounter, useGlobal, useNuxtApp } from '#imports' import { addAxiosInterceptors } from './interceptors'
import { createEventHook, ref, unref, useCounter, 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(
@ -33,8 +33,6 @@ export function useApi<Data = any, RequestConfig = any>({
apiOptions, apiOptions,
axiosConfig, axiosConfig,
}: UseApiProps<Data> = {}): UseApiReturn<Data, RequestConfig> { }: UseApiProps<Data> = {}): UseApiReturn<Data, RequestConfig> {
const state = useGlobal()
/** /**
* Local state of running requests, do not confuse with global state of running requests * Local state of running requests, do not confuse with global state of running requests
* This state is only counting requests made by this instance of `useApi` and not by other instances. * This state is only counting requests made by this instance of `useApi` and not by other instances.
@ -54,11 +52,10 @@ export function useApi<Data = any, RequestConfig = any>({
const responseHook = createEventHook<AxiosResponse<Data, RequestConfig>>() const responseHook = createEventHook<AxiosResponse<Data, RequestConfig>>()
/** global api instance */ const nuxtApp = useNuxtApp()
const $api = useNuxtApp().$api
/** api instance - with interceptors for token refresh already bound */ /** api instance - with interceptors for token refresh already bound */
const api = useGlobalInstance && !!$api ? $api : createApiInstance(apiOptions) const api = useGlobalInstance && !!nuxtApp.$api ? nuxtApp.$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() {
@ -68,7 +65,7 @@ export function useApi<Data = any, RequestConfig = any>({
inc() inc()
/** global count */ /** global count */
state.runningRequests.inc() nuxtApp.$state.runningRequests.inc()
} }
/** decrement local and global request counter and check if we can stop loading */ /** decrement local and global request counter and check if we can stop loading */
@ -76,7 +73,7 @@ export function useApi<Data = any, RequestConfig = any>({
/** local count */ /** local count */
dec() dec()
/** global count */ /** global count */
state.runningRequests.dec() nuxtApp.$state.runningRequests.dec()
/** try to stop loading */ /** try to stop loading */
stopLoading() stopLoading()

Loading…
Cancel
Save