From 14d6bc6329a6d3a0889a84ff25f726ae67af5ca9 Mon Sep 17 00:00:00 2001 From: padreug Date: Sun, 7 Sep 2025 01:31:24 +0200 Subject: [PATCH] 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. --- src/core/di-container.ts | 3 ++ .../market/components/MerchantStore.vue | 4 ++- src/modules/market/composables/useMarket.ts | 3 +- src/modules/market/index.ts | 19 +++++------- .../market/services/nostrmarketService.ts | 30 +++++-------------- src/modules/market/stores/market.ts | 3 +- 6 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/core/di-container.ts b/src/core/di-container.ts index 753f28f..ea58e1a 100644 --- a/src/core/di-container.ts +++ b/src/core/di-container.ts @@ -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 diff --git a/src/modules/market/components/MerchantStore.vue b/src/modules/market/components/MerchantStore.vue index af1d749..e8a60cd 100644 --- a/src/modules/market/components/MerchantStore.vue +++ b/src/modules/market/components/MerchantStore.vue @@ -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(null) diff --git a/src/modules/market/composables/useMarket.ts b/src/modules/market/composables/useMarket.ts index 415e908..ee31aac 100644 --- a/src/modules/market/composables/useMarket.ts +++ b/src/modules/market/composables/useMarket.ts @@ -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.') diff --git a/src/modules/market/index.ts b/src/modules/market/index.ts index 897f5e0..adcfb95 100644 --- a/src/modules/market/index.ts +++ b/src/modules/market/index.ts @@ -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 } } diff --git a/src/modules/market/services/nostrmarketService.ts b/src/modules/market/services/nostrmarketService.ts index 409c628..268373d 100644 --- a/src/modules/market/services/nostrmarketService.ts +++ b/src/modules/market/services/nostrmarketService.ts @@ -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() diff --git a/src/modules/market/stores/market.ts b/src/modules/market/stores/market.ts index e49a945..9b8e167 100644 --- a/src/modules/market/stores/market.ts +++ b/src/modules/market/stores/market.ts @@ -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([]) const stalls = ref([])