Initial commit
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import type { Ref, ComputedRef } from 'vue'
|
||||
import type { ProviderInterface, ProviderRecord, ProviderCapabilitiesInterface } from '../types/provider'
|
||||
import type { SourceSelector } from '../types/common'
|
||||
import { providerService } from '../services/providerService'
|
||||
import { ProviderObject } from '../models/provider'
|
||||
|
||||
export const useProvidersStore = defineStore('fileProviders', () => {
|
||||
const providers: Ref<Record<string, ProviderObject>> = ref({})
|
||||
const loading = ref(false)
|
||||
const error: Ref<string | null> = ref(null)
|
||||
const initialized = ref(false)
|
||||
|
||||
const providerList: ComputedRef<ProviderObject[]> = computed(() =>
|
||||
Object.values(providers.value)
|
||||
)
|
||||
|
||||
const providerIds: ComputedRef<string[]> = computed(() =>
|
||||
Object.keys(providers.value)
|
||||
)
|
||||
|
||||
const getProvider = (id: string): ProviderObject | undefined => {
|
||||
return providers.value[id]
|
||||
}
|
||||
|
||||
const hasProvider = (id: string): boolean => {
|
||||
return id in providers.value
|
||||
}
|
||||
|
||||
const isCapable = (providerId: string, capability: keyof ProviderCapabilitiesInterface): boolean => {
|
||||
const provider = providers.value[providerId]
|
||||
return provider ? provider.capable(capability) : false
|
||||
}
|
||||
|
||||
const setProviders = (data: ProviderRecord) => {
|
||||
const hydrated: Record<string, ProviderObject> = {}
|
||||
for (const [id, providerData] of Object.entries(data)) {
|
||||
hydrated[id] = new ProviderObject().fromJson(providerData)
|
||||
}
|
||||
providers.value = hydrated
|
||||
initialized.value = true
|
||||
}
|
||||
|
||||
const addProvider = (id: string, provider: ProviderInterface) => {
|
||||
providers.value[id] = new ProviderObject().fromJson(provider)
|
||||
}
|
||||
|
||||
const removeProvider = (id: string) => {
|
||||
delete providers.value[id]
|
||||
}
|
||||
|
||||
const clearProviders = () => {
|
||||
providers.value = {}
|
||||
initialized.value = false
|
||||
}
|
||||
|
||||
// API actions
|
||||
const fetchProviders = async (sources?: SourceSelector): Promise<void> => {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const data = await providerService.list(sources)
|
||||
setProviders(data)
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to fetch providers'
|
||||
throw e
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const checkProviderExtant = async (sources: SourceSelector): Promise<Record<string, boolean>> => {
|
||||
try {
|
||||
return await providerService.extant(sources)
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to check providers'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
providers,
|
||||
loading,
|
||||
error,
|
||||
initialized,
|
||||
// Computed
|
||||
providerList,
|
||||
providerIds,
|
||||
// Getters
|
||||
getProvider,
|
||||
hasProvider,
|
||||
isCapable,
|
||||
// Setters
|
||||
setProviders,
|
||||
addProvider,
|
||||
removeProvider,
|
||||
clearProviders,
|
||||
// Actions
|
||||
fetchProviders,
|
||||
checkProviderExtant,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user