NeahNew/lib/services/outline-service.ts
2025-05-06 17:18:33 +02:00

89 lines
2.8 KiB
TypeScript

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<string> {
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.create`,
{
name: mission.label,
description: mission.description || 'Mission documentation',
color: '#4f46e5', // Indigo color as default,
permission: 'read_write'
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiToken}`
}
}
);
console.log('Outline API response:', JSON.stringify(response.data, null, 2));
if (response.data && response.data.data && response.data.data.id) {
return response.data.data.id;
} else if (response.data && response.data.id) {
return response.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<boolean> {
if (!this.apiToken) {
throw new Error('Outline API token is not configured');
}
try {
// Delete the collection in Outline
await axios.post(
`${this.apiUrl}/collections.delete`,
{
id: collectionId
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiToken}`
}
}
);
return true;
} catch (error) {
console.error('Error deleting Outline collection:', error);
return false;
}
}
}