import axios from 'axios'; export class OutlineService { private apiUrl: string; private apiToken: string; constructor() { this.apiUrl = process.env.OUTLINE_API_URL || 'https://chapitre.slm-lab.net/api'; this.apiToken = process.env.OUTLINE_API_KEY || ''; console.log('OutlineService initialized with URL:', this.apiUrl); console.log('Creating Outline collection with token length:', this.apiToken.length); } async createCollection(mission: any): Promise { if (!this.apiToken) { throw new Error('Outline API token is not configured'); } try { // Create a collection in Outline based on the mission const response = await axios.post( `${this.apiUrl}/collections`, { name: mission.label, description: mission.description || 'Mission documentation', color: '#4f46e5', // Indigo color as default permission: 'read_write', // Added extra fields that might be useful for identifying the collection private: false, external: true, meta: { missionId: mission.id, createdAt: new Date().toISOString() } }, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiToken}` } } ); if (response.data && response.data.data && response.data.data.id) { return response.data.data.id; } else { throw new Error('Failed to get collection ID from Outline API response'); } } catch (error) { if (axios.isAxiosError(error)) { if (error.response?.status === 401) { console.error('Outline authentication error:', error.response.data); throw new Error('Outline API authentication failed - check your token'); } else if (error.response?.status === 429) { console.error('Outline rate limit error:', error.response.data); throw new Error('Outline API rate limit exceeded - try again later'); } else { console.error('Outline API error:', error.response?.data || error.message); throw new Error(`Outline API error: ${error.response?.status} - ${error.message}`); } } throw error; } } async deleteCollection(collectionId: string): Promise { if (!this.apiToken) { throw new Error('Outline API token is not configured'); } try { // Delete the collection in Outline await axios.delete( `${this.apiUrl}/collections/${collectionId}`, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiToken}` } } ); return true; } catch (error) { console.error('Error deleting Outline collection:', error); return false; } } }