mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 17:24:10 +00:00
Create the complete TypeScript foundation for the Vue 3 migration in a parallel scripts-v2/ directory. 72 files, 5430 lines, zero any types, strict mode. - types/ (21 files): Domain interfaces for all 17 entities derived from actual Laravel models and API resources. Enums for all statuses. Generic API response wrappers. - api/ (29 files): Typed axios client with interceptors, endpoint constants from routes/api.php, 25 typed service classes covering every API endpoint. - composables/ (14 files): Vue 3 composition functions for auth, notifications, dialogs, modals, pagination, filters, currency, dates, theme, sidebar, company context, and permissions. - utils/ (5 files): Pure typed utilities for money formatting, date formatting (date-fns), localStorage, and error handling. - config/ (3 files): Typed ability constants, app constants. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { ref } from 'vue'
|
|
import type { Ref } from 'vue'
|
|
import { handleApiError } from '../utils/error-handling'
|
|
import type { NormalizedApiError } from '../utils/error-handling'
|
|
|
|
export interface UseApiReturn<T> {
|
|
data: Ref<T | null>
|
|
loading: Ref<boolean>
|
|
error: Ref<NormalizedApiError | null>
|
|
execute: (...args: unknown[]) => Promise<T | null>
|
|
reset: () => void
|
|
}
|
|
|
|
/**
|
|
* Generic API call wrapper composable.
|
|
* Manages loading, error, and data state for an async API function.
|
|
*
|
|
* @param apiFn - An async function that performs the API call
|
|
* @returns Reactive refs for data, loading, and error plus an execute function
|
|
*/
|
|
export function useApi<T>(
|
|
apiFn: (...args: never[]) => Promise<T>
|
|
): UseApiReturn<T> {
|
|
const data = ref<T | null>(null) as Ref<T | null>
|
|
const loading = ref<boolean>(false)
|
|
const error = ref<NormalizedApiError | null>(null) as Ref<NormalizedApiError | null>
|
|
|
|
async function execute(...args: unknown[]): Promise<T | null> {
|
|
loading.value = true
|
|
error.value = null
|
|
|
|
try {
|
|
const result = await (apiFn as (...a: unknown[]) => Promise<T>)(...args)
|
|
data.value = result
|
|
return result
|
|
} catch (err: unknown) {
|
|
const normalized = handleApiError(err)
|
|
error.value = normalized
|
|
return null
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
function reset(): void {
|
|
data.value = null
|
|
loading.value = false
|
|
error.value = null
|
|
}
|
|
|
|
return { data, loading, error, execute, reset }
|
|
}
|