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_SERVICE: Symbol('invoiceService'),
// Nostrmarket services
NOSTRMARKET_SERVICE: Symbol('nostrmarketService'),
} as const
// Type-safe injection helpers

View file

@ -320,11 +320,13 @@ import {
BarChart3
} from 'lucide-vue-next'
import type { OrderStatus } from '@/stores/market'
import { nostrmarketService } from '../services/nostrmarketService'
import type { NostrmarketService } from '../services/nostrmarketService'
import { auth } from '@/composables/useAuthService'
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
const router = useRouter()
const marketStore = useMarketStore()
const nostrmarketService = injectService(SERVICE_TOKENS.NOSTRMARKET_SERVICE) as NostrmarketService
// Local state
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 { injectService, SERVICE_TOKENS } from '@/core/di-container'
import { config } from '@/lib/config'
import { nostrmarketService } from '../services/nostrmarketService'
import type { NostrmarketService } from '../services/nostrmarketService'
import { nip04 } from 'nostr-tools'
import { useAsyncOperation } from '@/core/composables/useAsyncOperation'
import { auth } from '@/composables/useAuthService'
@ -19,6 +19,7 @@ export function useMarket() {
const marketStore = useMarketStore()
const relayHub = injectService(SERVICE_TOKENS.RELAY_HUB) as any
const authService = injectService(SERVICE_TOKENS.AUTH_SERVICE) as any
const nostrmarketService = injectService(SERVICE_TOKENS.NOSTRMARKET_SERVICE) as NostrmarketService
if (!relayHub) {
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 { ModulePlugin } from '@/core/types'
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 components
@ -17,9 +17,8 @@ import ShoppingCart from './components/ShoppingCart.vue'
import { useMarket } from './composables/useMarket'
import { useMarketPreloader } from './composables/useMarketPreloader'
// Define service tokens
export const MARKET_SERVICE_TOKEN = Symbol('marketService')
export const NOSTRMARKET_SERVICE_TOKEN = Symbol('nostrmarketService')
// Import services
import { NostrmarketService } from './services/nostrmarketService'
export interface MarketModuleConfig {
defaultCurrency: string
@ -45,8 +44,9 @@ export const marketModule: ModulePlugin = {
throw new Error('Market module requires configuration')
}
// Import the singleton instance
const { nostrmarketService } = await import('./services/nostrmarketService')
// Create and register NostrmarketService instance
const nostrmarketService = new NostrmarketService()
container.provide(SERVICE_TOKENS.NOSTRMARKET_SERVICE, nostrmarketService)
// Initialize the service (will handle dependency injection)
await nostrmarketService.initialize({
@ -56,9 +56,6 @@ export const marketModule: ModulePlugin = {
console.warn('🛒 NostrmarketService initialization deferred:', error)
// Service will auto-initialize when dependencies are available
})
// Register the service
container.provide(NOSTRMARKET_SERVICE_TOKEN, nostrmarketService)
// Register global components
app.component('MarketSettings', MarketSettings)
@ -77,7 +74,7 @@ export const marketModule: ModulePlugin = {
console.log('🗑️ Uninstalling market module...')
// Clean up services
container.remove(NOSTRMARKET_SERVICE_TOKEN)
container.remove(SERVICE_TOKENS.NOSTRMARKET_SERVICE)
console.log('✅ Market module uninstalled')
},
@ -137,7 +134,7 @@ export const marketModule: ModulePlugin = {
},
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 { BaseService } from '@/core/base/BaseService'
import type { Stall, Product, Order } from '@/stores/market'
import { auth } from '@/composables/useAuthService'
export interface NostrmarketStall {
id: string
@ -107,30 +106,20 @@ export class NostrmarketService extends BaseService {
}
private getAuth() {
// Check global auth first
if (!auth.isAuthenticated.value) {
// Use injected AuthService only
if (!this.authService?.isAuthenticated.value) {
throw new Error('User not authenticated')
}
// Try to get keys from global auth first, fallback to injected auth service
const globalUser = auth.currentUser.value
const serviceUser = this.authService?.user?.value
const pubkey = globalUser?.pubkey || serviceUser?.pubkey
const prvkey = globalUser?.prvkey || serviceUser?.prvkey
const user = this.authService.user.value
const pubkey = user?.pubkey
const prvkey = user?.prvkey
if (!pubkey || !prvkey) {
this.debug('Auth check failed:', {
globalUser: {
exists: !!globalUser,
hasPubkey: !!globalUser?.pubkey,
hasPrvkey: !!globalUser?.prvkey
},
serviceUser: {
exists: !!serviceUser,
hasPubkey: !!serviceUser?.pubkey,
hasPrvkey: !!serviceUser?.prvkey
}
userExists: !!user,
hasPubkey: !!pubkey,
hasPrvkey: !!prvkey
})
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
}
}
// Export singleton instance
export const nostrmarketService = new NostrmarketService()

View file

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