import { getTodoistClient } from '../client';
import {
ensureCacheDirectory,
tryReadFromCache,
createCachedResult,
writeToCache,
} from '../../cache/project-cache';
import { TodoistProject } from '../../types';
import { getErrorMessage } from '../../utils';
interface ProjectsResponse {
projects: TodoistProject[];
total_count: number;
}
// Helper function to fetch projects from API
async function fetchProjectsFromAPI(): Promise<ProjectsResponse> {
const todoistClient = getTodoistClient();
const apiResponse = await todoistClient.get<TodoistProject[]>('/projects');
return {
projects: apiResponse.data,
total_count: apiResponse.data.length,
};
}
// List projects function with caching - returns structured data
export async function listProjects(): Promise<
ProjectsResponse & { cached_at?: string }
> {
try {
ensureCacheDirectory();
const cachedData = tryReadFromCache();
if (cachedData) {
return cachedData;
}
const projectsData = await fetchProjectsFromAPI();
const result = createCachedResult(projectsData);
writeToCache(result);
return result;
} catch (error) {
throw new Error(`Failed to list projects: ${getErrorMessage(error)}`);
}
}
// Export types for testing - TodoistProject is now imported from types.ts
export type { ProjectsResponse };