Add NostrmarketService to dependency injection container

- Introduce NOSTRMARKET_SERVICE token in the DI container for better service management.
- Update market module to create and register NostrmarketService instance using the new DI pattern.
- Refactor components and composables to inject NostrmarketService via the DI container, enhancing modularity and testability.

These changes improve the architecture by ensuring consistent service injection and eliminating legacy singleton references, aligning with the overall dependency injection strategy.
This commit is contained in:
padreug 2025-09-07 01:31:24 +02:00
parent 31a9c0a9dc
commit 14d6bc6329
6 changed files with 26 additions and 36 deletions

View file

@ -137,6 +137,9 @@ export const SERVICE_TOKENS = {
// Invoice services // Invoice services
INVOICE_SERVICE: Symbol('invoiceService'), INVOICE_SERVICE: Symbol('invoiceService'),
// Nostrmarket services
NOSTRMARKET_SERVICE: Symbol('nostrmarketService'),
} as const } as const
// Type-safe injection helpers // Type-safe injection helpers

View file

@ -320,11 +320,13 @@ import {
BarChart3 BarChart3
} from 'lucide-vue-next' } from 'lucide-vue-next'
import type { OrderStatus } from '@/stores/market' import type { OrderStatus } from '@/stores/market'
import { nostrmarketService } from '../services/nostrmarketService' import type { NostrmarketService } from '../services/nostrmarketService'
import { auth } from '@/composables/useAuthService' import { auth } from '@/composables/useAuthService'
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
const router = useRouter() const router = useRouter()
const marketStore = useMarketStore() const marketStore = useMarketStore()
const nostrmarketService = injectService(SERVICE_TOKENS.NOSTRMARKET_SERVICE) as NostrmarketService
// Local state // Local state
const isGeneratingInvoice = ref<string | null>(null) const isGeneratingInvoice = ref<string | null>(null)

View file

@ -2,7 +2,7 @@ import { ref, computed, onMounted, onUnmounted, readonly } from 'vue'
import { useMarketStore } from '../stores/market' import { useMarketStore } from '../stores/market'
import { injectService, SERVICE_TOKENS } from '@/core/di-container' import { injectService, SERVICE_TOKENS } from '@/core/di-container'
import { config } from '@/lib/config' import { config } from '@/lib/config'
import { nostrmarketService } from '../services/nostrmarketService' import type { NostrmarketService } from '../services/nostrmarketService'
import { nip04 } from 'nostr-tools' import { nip04 } from 'nostr-tools'
import { useAsyncOperation } from '@/core/composables/useAsyncOperation' import { useAsyncOperation } from '@/core/composables/useAsyncOperation'
import { auth } from '@/composables/useAuthService' import { auth } from '@/composables/useAuthService'
@ -19,6 +19,7 @@ export function useMarket() {
const marketStore = useMarketStore() const marketStore = useMarketStore()
const relayHub = injectService(SERVICE_TOKENS.RELAY_HUB) as any const relayHub = injectService(SERVICE_TOKENS.RELAY_HUB) as any
const authService = injectService(SERVICE_TOKENS.AUTH_SERVICE) as any const authService = injectService(SERVICE_TOKENS.AUTH_SERVICE) as any
const nostrmarketService = injectService(SERVICE_TOKENS.NOSTRMARKET_SERVICE) as NostrmarketService
if (!relayHub) { if (!relayHub) {
throw new Error('RelayHub not available. Make sure base module is installed.') throw new Error('RelayHub not available. Make sure base module is installed.')

View file

@ -1,7 +1,7 @@
import type { App } from 'vue' import type { App } from 'vue'
import type { ModulePlugin } from '@/core/types' import type { ModulePlugin } from '@/core/types'
import type { RouteRecordRaw } from 'vue-router' import type { RouteRecordRaw } from 'vue-router'
import { container } from '@/core/di-container' import { container, SERVICE_TOKENS } from '@/core/di-container'
import { eventBus } from '@/core/event-bus' import { eventBus } from '@/core/event-bus'
// Import components // Import components
@ -17,9 +17,8 @@ import ShoppingCart from './components/ShoppingCart.vue'
import { useMarket } from './composables/useMarket' import { useMarket } from './composables/useMarket'
import { useMarketPreloader } from './composables/useMarketPreloader' import { useMarketPreloader } from './composables/useMarketPreloader'
// Define service tokens // Import services
export const MARKET_SERVICE_TOKEN = Symbol('marketService') import { NostrmarketService } from './services/nostrmarketService'
export const NOSTRMARKET_SERVICE_TOKEN = Symbol('nostrmarketService')
export interface MarketModuleConfig { export interface MarketModuleConfig {
defaultCurrency: string defaultCurrency: string
@ -45,8 +44,9 @@ export const marketModule: ModulePlugin = {
throw new Error('Market module requires configuration') throw new Error('Market module requires configuration')
} }
// Import the singleton instance // Create and register NostrmarketService instance
const { nostrmarketService } = await import('./services/nostrmarketService') const nostrmarketService = new NostrmarketService()
container.provide(SERVICE_TOKENS.NOSTRMARKET_SERVICE, nostrmarketService)
// Initialize the service (will handle dependency injection) // Initialize the service (will handle dependency injection)
await nostrmarketService.initialize({ await nostrmarketService.initialize({
@ -56,9 +56,6 @@ export const marketModule: ModulePlugin = {
console.warn('🛒 NostrmarketService initialization deferred:', error) console.warn('🛒 NostrmarketService initialization deferred:', error)
// Service will auto-initialize when dependencies are available // Service will auto-initialize when dependencies are available
}) })
// Register the service
container.provide(NOSTRMARKET_SERVICE_TOKEN, nostrmarketService)
// Register global components // Register global components
app.component('MarketSettings', MarketSettings) app.component('MarketSettings', MarketSettings)
@ -77,7 +74,7 @@ export const marketModule: ModulePlugin = {
console.log('🗑️ Uninstalling market module...') console.log('🗑️ Uninstalling market module...')
// Clean up services // Clean up services
container.remove(NOSTRMARKET_SERVICE_TOKEN) container.remove(SERVICE_TOKENS.NOSTRMARKET_SERVICE)
console.log('✅ Market module uninstalled') console.log('✅ Market module uninstalled')
}, },
@ -137,7 +134,7 @@ export const marketModule: ModulePlugin = {
}, },
services: { services: {
nostrmarket: NOSTRMARKET_SERVICE_TOKEN nostrmarket: SERVICE_TOKENS.NOSTRMARKET_SERVICE
} }
} }

View file

@ -1,7 +1,6 @@
import { finalizeEvent, type EventTemplate, nip04 } from 'nostr-tools' import { finalizeEvent, type EventTemplate, nip04 } from 'nostr-tools'
import { BaseService } from '@/core/base/BaseService' import { BaseService } from '@/core/base/BaseService'
import type { Stall, Product, Order } from '@/stores/market' import type { Stall, Product, Order } from '@/stores/market'
import { auth } from '@/composables/useAuthService'
export interface NostrmarketStall { export interface NostrmarketStall {
id: string id: string
@ -107,30 +106,20 @@ export class NostrmarketService extends BaseService {
} }
private getAuth() { private getAuth() {
// Check global auth first // Use injected AuthService only
if (!auth.isAuthenticated.value) { if (!this.authService?.isAuthenticated.value) {
throw new Error('User not authenticated') throw new Error('User not authenticated')
} }
// Try to get keys from global auth first, fallback to injected auth service const user = this.authService.user.value
const globalUser = auth.currentUser.value const pubkey = user?.pubkey
const serviceUser = this.authService?.user?.value const prvkey = user?.prvkey
const pubkey = globalUser?.pubkey || serviceUser?.pubkey
const prvkey = globalUser?.prvkey || serviceUser?.prvkey
if (!pubkey || !prvkey) { if (!pubkey || !prvkey) {
this.debug('Auth check failed:', { this.debug('Auth check failed:', {
globalUser: { userExists: !!user,
exists: !!globalUser, hasPubkey: !!pubkey,
hasPubkey: !!globalUser?.pubkey, hasPrvkey: !!prvkey
hasPrvkey: !!globalUser?.prvkey
},
serviceUser: {
exists: !!serviceUser,
hasPubkey: !!serviceUser?.pubkey,
hasPrvkey: !!serviceUser?.prvkey
}
}) })
throw new Error('Nostr keys not available. Please ensure your Nostr identity is configured in your profile.') throw new Error('Nostr keys not available. Please ensure your Nostr identity is configured in your profile.')
} }
@ -517,6 +506,3 @@ export class NostrmarketService extends BaseService {
return results return results
} }
} }
// Export singleton instance
export const nostrmarketService = new NostrmarketService()

View file

@ -1,10 +1,10 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { ref, computed, readonly, watch } from 'vue' import { ref, computed, readonly, watch } from 'vue'
import { paymentMonitor } from '../services/paymentMonitor' import { paymentMonitor } from '../services/paymentMonitor'
import { nostrmarketService } from '../services/nostrmarketService'
import { useAuth } from '@/composables/useAuthService' import { useAuth } from '@/composables/useAuthService'
import { injectService, SERVICE_TOKENS } from '@/core/di-container' import { injectService, SERVICE_TOKENS } from '@/core/di-container'
import type { LightningInvoice, InvoiceService } from '@/core/services/invoiceService' import type { LightningInvoice, InvoiceService } from '@/core/services/invoiceService'
import type { NostrmarketService } from '../services/nostrmarketService'
import type { import type {
@ -18,6 +18,7 @@ export const useMarketStore = defineStore('market', () => {
const auth = useAuth() const auth = useAuth()
const storageService = injectService(SERVICE_TOKENS.STORAGE_SERVICE) as any const storageService = injectService(SERVICE_TOKENS.STORAGE_SERVICE) as any
const invoiceService = injectService(SERVICE_TOKENS.INVOICE_SERVICE) as InvoiceService const invoiceService = injectService(SERVICE_TOKENS.INVOICE_SERVICE) as InvoiceService
const nostrmarketService = injectService(SERVICE_TOKENS.NOSTRMARKET_SERVICE) as NostrmarketService
// Core market state // Core market state
const markets = ref<Market[]>([]) const markets = ref<Market[]>([])
const stalls = ref<Stall[]>([]) const stalls = ref<Stall[]>([])