Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
6b61a56
refactor: extract shared useApiRequest composable for axios services
mattmillerai Jul 2, 2026
58f541b
test: cover comfyRegistryService and comfyManagerService request wrap…
mattmillerai Jul 2, 2026
4e0298d
fix: make useApiRequest loading state concurrency-safe
mattmillerai Jul 17, 2026
4647dce
Merge remote-tracking branch 'origin/main' into matt/be-2254-extract-…
mattmillerai Aug 26, 2026
b736b37
fix: address review findings on error mapping and queue-start propaga…
mattmillerai Aug 26, 2026
5ee55c5
Merge remote-tracking branch 'origin/main' into matt/be-2254-extract-…
mattmillerai Aug 26, 2026
15f87af
Merge remote-tracking branch 'origin/main' into matt/be-2254-extract-…
mattmillerai Sep 1, 2026
6f7ddba
fix: report queue-start failure instead of throwing
mattmillerai Sep 1, 2026
f6b559b
fix: derive queue-start outcome per request instead of shared error s…
mattmillerai Sep 1, 2026
d7931f6
Merge origin/main into matt/be-2254-extract-api-request-composable
mattmillerai Oct 19, 2026
bd1955e
fix: narrow axios errors with isAxiosError<T> instead of casting
mattmillerai Oct 19, 2026
e6286fd
fix: keep a stale customer-events failure from overwriting a newer re…
mattmillerai Oct 19, 2026
a8d8746
Merge remote-tracking branch 'origin/main' into matt/be-2254-extract-…
mattmillerai Oct 22, 2026
5cf569d
fix: treat axios cancellations as aborts in useApiRequest
mattmillerai Oct 22, 2026
f045413
Merge remote-tracking branch 'origin/main' into matt/be-2254-extract-…
mattmillerai Oct 30, 2026
d5b9722
Merge remote-tracking branch 'origin/main' into matt/be-2254-extract-…
mattmillerai Oct 30, 2026
e82889d
Merge remote-tracking branch 'origin/main' into matt/be-2254-extract-…
mattmillerai Nov 2, 2026
d238397
refactor: drop the unused isCurrent option from useApiRequest
mattmillerai Nov 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions src/composables/useApiRequest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import type { AxiosResponse } from 'axios'
import axios, { AxiosHeaders, CanceledError } from 'axios'
import { describe, expect, it, vi } from 'vitest'

import { useApiRequest } from '@/composables/useApiRequest'

const client = axios.create()

function response<T>(data: T): AxiosResponse<T> {
return {
data,
status: 200,
statusText: 'OK',
headers: {},
config: { headers: new AxiosHeaders() }
}
}

describe('useApiRequest', () => {
it('returns response data and toggles loading state', async () => {
const { isLoading, error, executeRequest } = useApiRequest({
client,
mapError: vi.fn()
})
expect(isLoading.value).toBe(false)

const pending = executeRequest(async () => response('ok'), {
errorContext: 'ctx'
})
expect(isLoading.value).toBe(true)

await expect(pending).resolves.toBe('ok')
expect(isLoading.value).toBe(false)
expect(error.value).toBeNull()
})

it('stays loading until every concurrent request settles', async () => {
const { isLoading, executeRequest } = useApiRequest({
client,
mapError: vi.fn()
})

let resolveFirst!: (value: AxiosResponse<string>) => void
const first = executeRequest(
() => new Promise<AxiosResponse<string>>((res) => (resolveFirst = res)),
{ errorContext: 'ctx' }
)
const second = executeRequest(async () => response('second'), {
errorContext: 'ctx'
})

await second
expect(isLoading.value).toBe(true)

resolveFirst(response('first'))
await first
expect(isLoading.value).toBe(false)
})

it('passes the injected client to the api call', async () => {
const apiCall = vi.fn(async () => response(1))
const { executeRequest } = useApiRequest({ client, mapError: vi.fn() })

await executeRequest(apiCall, { errorContext: 'ctx' })

expect(apiCall).toHaveBeenCalledWith(client)
})

it('maps errors through the injected mapper and stores the message', async () => {
const mapError = vi.fn(() => 'mapped message')
const routeSpecificErrors = { 404: 'nope' }
const boom = new Error('boom')
const { error, executeRequest } = useApiRequest({ client, mapError })

const result = await executeRequest(
() => {
throw boom
},
{ errorContext: 'ctx', routeSpecificErrors }
)

expect(result).toBeNull()
expect(mapError).toHaveBeenCalledWith(boom, 'ctx', routeSpecificErrors)
expect(error.value).toBe('mapped message')
})

it.for([
{
kind: 'DOM AbortError',
cancellation: new DOMException('aborted', 'AbortError')
},
{ kind: 'axios CanceledError', cancellation: new CanceledError() }
])('swallows a $kind without mapping an error', async ({ cancellation }) => {
const mapError = vi.fn(() => 'should not run')
const { error, executeRequest } = useApiRequest({ client, mapError })

const result = await executeRequest(
() => {
throw cancellation
},
{ errorContext: 'ctx' }
)

expect(result).toBeNull()
expect(mapError).not.toHaveBeenCalled()
expect(error.value).toBeNull()
})
})
62 changes: 62 additions & 0 deletions src/composables/useApiRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { AxiosInstance, AxiosResponse } from 'axios'
import axios from 'axios'
import { computed, ref } from 'vue'

import { isAbortError } from '@/utils/typeGuardUtil'

/**
* Maps a caught request error to a user-facing message string.
* Each service injects its own mapper so it keeps control of the exact
* status-to-message copy it presents.
*/
export type ApiErrorMapper = (
err: unknown,
errorContext: string,
routeSpecificErrors?: Record<number, string>
) => string

export interface ExecuteRequestOptions {
errorContext: string
routeSpecificErrors?: Record<number, string>
}

/**
* Shared axios request wrapper: owns the `isLoading`/`error` state and the
* try/catch/finally plumbing, while the caller injects the axios instance and
* an error mapper. Cancellations are swallowed (no error set, `null` returned).
*/
export function useApiRequest({
client,
mapError
}: {
client: AxiosInstance
mapError: ApiErrorMapper
}) {
const pendingCount = ref(0)
const isLoading = computed(() => pendingCount.value > 0)
const error = ref<string | null>(null)

Comment thread
mattmillerai marked this conversation as resolved.
Resolved
async function executeRequest<T>(
apiCall: (client: AxiosInstance) => Promise<AxiosResponse<T>>,
options: ExecuteRequestOptions
): Promise<T | null> {
const { errorContext, routeSpecificErrors } = options

pendingCount.value++
error.value = null

try {
const response = await apiCall(client)
return response.data
} catch (err) {
if (isAbortError(err) || axios.isCancel(err)) return null

error.value = mapError(err, errorContext, routeSpecificErrors)
return null
} finally {
pendingCount.value--
}
}

return { isLoading, error, executeRequest }
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ describe('useConflictDetection', () => {
const mockComfyManagerService = {
getImportFailInfoBulk: vi.fn(),
listInstalledPacks: vi.fn(async () => ({})),
isLoading: ref(false),
isLoading: computed(() => false),
error: ref<string | null>(null)
} as Partial<ReturnType<typeof useComfyManagerService>> as ReturnType<
typeof useComfyManagerService
>

const mockRegistryService = {
getBulkNodeVersions: vi.fn(),
isLoading: ref(false),
isLoading: computed(() => false),
error: ref<string | null>(null)
} as Partial<ReturnType<typeof useComfyRegistryService>> as ReturnType<
typeof useComfyRegistryService
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { InternalAxiosRequestConfig } from 'axios'
import axios from 'axios'
import { describe, expect, it, vi } from 'vitest'
import { computed } from 'vue'

import { api } from '@/scripts/api'
import { useManagerState } from '@/platform/customNodes/manager/composables/useManagerState'

vi.mock(import('axios'), { spy: true })
vi.mock(import('@/scripts/api'))
vi.mock(import('@/platform/customNodes/manager/composables/useManagerState'))

const requestedUrl = vi.fn<(url: string) => void>()

async function captureRequestUrl(config: InternalAxiosRequestConfig) {
requestedUrl(axios.getUri(config))
return { data: {}, status: 200, statusText: 'OK', headers: {}, config }
}

vi.mocked(axios.create).mockImplementationOnce((config) =>
axios.create({ ...config, adapter: captureRequestUrl })
)

const { useComfyManagerService } =
await import('@/platform/customNodes/manager/services/comfyManagerService')

describe('useComfyManagerService', () => {
it('sends the request to the API base configured after import', async () => {
api.api_base = '/changed-base'
vi.mocked(useManagerState()).isNewManagerUI = computed(() => true)

await useComfyManagerService().listInstalledPacks()

expect(requestedUrl).toHaveBeenCalledWith(
'/changed-base/api/v2/customnode/installed'
)
})
})
Loading
Loading