Initial commit
This commit is contained in:
108
src/stores/providersStore.ts
Normal file
108
src/stores/providersStore.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* Providers Store
|
||||
*/
|
||||
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { providerService } from '../services'
|
||||
import { ProviderObject } from '../models/provider'
|
||||
import type { SourceSelector } from '../types'
|
||||
|
||||
export const useProvidersStore = defineStore('mailProvidersStore', () => {
|
||||
// State
|
||||
const _providers = ref<Record<string, ProviderObject>>({})
|
||||
const transceiving = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
// Getters
|
||||
const count = computed(() => Object.keys(_providers.value).length)
|
||||
const has = computed(() => count.value > 0)
|
||||
|
||||
/**
|
||||
* Get providers as an array
|
||||
* @returns Array of provider objects
|
||||
*/
|
||||
const providers = computed(() => Object.values(_providers.value))
|
||||
|
||||
/**
|
||||
* Get a specific provider by identifier from cache
|
||||
* @param identifier - Provider identifier
|
||||
* @returns Provider object or null
|
||||
*/
|
||||
function provider(identifier: string): ProviderObject | null {
|
||||
return _providers.value[identifier] || null
|
||||
}
|
||||
|
||||
// Actions
|
||||
/**
|
||||
* Retrieve all or specific providers
|
||||
*/
|
||||
async function list(sources?: SourceSelector): Promise<Record<string, ProviderObject>> {
|
||||
transceiving.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const response = await providerService.list({ sources })
|
||||
|
||||
console.debug('[Mail Manager](Store) - Successfully retrieved', Object.keys(response).length, 'providers')
|
||||
|
||||
_providers.value = response
|
||||
return response
|
||||
} catch (err: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to retrieve providers:', err)
|
||||
error.value = err.message
|
||||
throw err
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a specific provider
|
||||
*/
|
||||
async function fetch(identifier: string): Promise<ProviderObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
return await providerService.fetch({ identifier })
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to fetch provider:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check which providers exist/are available
|
||||
*/
|
||||
async function extant(sources: SourceSelector) {
|
||||
transceiving.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const response = await providerService.extant({ sources })
|
||||
console.debug('[Mail Manager](Store) - Successfully checked', sources ? Object.keys(sources).length : 0, 'providers')
|
||||
return response
|
||||
} catch (err: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to check providers:', err)
|
||||
error.value = err.message
|
||||
throw err
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// Return public API
|
||||
return {
|
||||
// State
|
||||
transceiving: readonly(transceiving),
|
||||
error: readonly(error),
|
||||
// computed
|
||||
count,
|
||||
has,
|
||||
providers,
|
||||
provider,
|
||||
// functions
|
||||
list,
|
||||
fetch,
|
||||
extant,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user