Complete LnbitsAPI migration to dependency injection pattern
- Convert LnbitsAPI from singleton to BaseService extension - Add LNBITS_API service token to DI container - Register LnbitsAPI service in base module with proper initialization order - Update AuthService to depend on injected LnbitsAPI instead of singleton - Fix BaseService to properly track LnbitsAPI dependency in getMissingDependencies - Update events API functions to use dependency injection - Resolve initialization timing issue preventing application startup 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
093846b351
commit
4a3d2012be
6 changed files with 69 additions and 18 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import type { Event, Ticket } from '../types/event'
|
||||
import { config } from '@/lib/config'
|
||||
import { lnbitsAPI } from './lnbits'
|
||||
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
|
||||
import type { LnbitsAPI } from './lnbits'
|
||||
|
||||
const API_BASE_URL = config.api.baseUrl || 'http://lnbits'
|
||||
const API_KEY = config.api.key
|
||||
|
|
@ -39,6 +40,9 @@ export async function fetchEvents(): Promise<Event[]> {
|
|||
|
||||
export async function purchaseTicket(eventId: string): Promise<{ payment_hash: string; payment_request: string }> {
|
||||
try {
|
||||
// Get injected LnbitsAPI service
|
||||
const lnbitsAPI = injectService(SERVICE_TOKENS.LNBITS_API) as LnbitsAPI
|
||||
|
||||
// Get current user to ensure authentication
|
||||
const user = await lnbitsAPI.getCurrentUser()
|
||||
if (!user) {
|
||||
|
|
@ -135,6 +139,9 @@ export async function checkPaymentStatus(eventId: string, paymentHash: string):
|
|||
|
||||
export async function fetchUserTickets(userId: string): Promise<Ticket[]> {
|
||||
try {
|
||||
// Get injected LnbitsAPI service
|
||||
const lnbitsAPI = injectService(SERVICE_TOKENS.LNBITS_API) as LnbitsAPI
|
||||
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/events/api/v1/tickets/user/${userId}`,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -61,16 +61,33 @@ interface User {
|
|||
}
|
||||
}
|
||||
|
||||
import { BaseService } from '@/core/base/BaseService'
|
||||
import { getApiUrl, getAuthToken, setAuthToken, removeAuthToken } from '@/lib/config/lnbits'
|
||||
|
||||
class LnbitsAPI {
|
||||
export class LnbitsAPI extends BaseService {
|
||||
// Service metadata
|
||||
protected readonly metadata = {
|
||||
name: 'LnbitsAPI',
|
||||
version: '1.0.0',
|
||||
dependencies: [] // No dependencies - this is a core infrastructure service
|
||||
}
|
||||
|
||||
private accessToken: string | null = null
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
// Try to load token from localStorage
|
||||
this.accessToken = getAuthToken()
|
||||
}
|
||||
|
||||
/**
|
||||
* Service-specific initialization (called by BaseService)
|
||||
*/
|
||||
protected async onInitialize(): Promise<void> {
|
||||
this.debug('LnbitsAPI initialized')
|
||||
// Service is ready to use
|
||||
}
|
||||
|
||||
private async request<T>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
|
|
@ -183,5 +200,5 @@ class LnbitsAPI {
|
|||
}
|
||||
}
|
||||
|
||||
export const lnbitsAPI = new LnbitsAPI()
|
||||
// Service is now registered in the DI container
|
||||
export type { LoginCredentials, RegisterData, AuthResponse, User }
|
||||
Loading…
Add table
Add a link
Reference in a new issue