UNPKG

logggai

Version:

AI-powered CLI for transforming your development work into professional content

204 lines 8.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.apiClient = void 0; const axios_1 = require("axios"); const config = require('./config'); const errors_1 = require("./errors"); const chalk = require("chalk"); class ApiClient { constructor() { this.baseURL = config.getApiUrl(); } getHeaders() { const sessionToken = config.getSessionToken(); const userId = config.getUserId(); const headers = { 'Content-Type': 'application/json', }; if (sessionToken) { headers['Authorization'] = `Bearer ${sessionToken}`; } if (userId) { headers['x-clerk-user-id'] = userId; } headers['User-Agent'] = 'devlog-cli/1.0.0'; return headers; } handleApiError(error, message) { if (error.response) { const status = error.response.status; const data = error.response.data; if (status === 401 || status === 403) { console.error(chalk.default.red.bold('\nAuthentication Failed')); console.error(chalk.default.yellow('Your session has likely expired.')); console.error(chalk.default.white('Please run the following command to refresh your credentials:')); console.error(chalk.default.cyan('\n npx logggai logout && npx logggai login\n')); process.exit(1); } if (data?.type === 'LIMIT_EXCEEDED') { const limitError = (0, errors_1.parseLimitExceededResponse)(data, this.baseURL); throw limitError; } console.error(chalk.default.red(`API Error ${status}: ${data.error || message}`)); console.error(chalk.default.gray(data.message || 'No additional details provided.')); } else if (error.request) { // Handle network errors or other request-level errors console.error(chalk.default.red('Network error or server not responding.')); console.error(chalk.default.gray(error.message || 'No additional details provided.')); } else { // Handle errors that don't have a response or request property console.error(chalk.default.red('An unexpected error occurred.')); console.error(chalk.default.gray(error.message || 'No additional details provided.')); } process.exit(1); } async verifyAuth(sessionToken) { try { const response = await axios_1.default.post(`${this.baseURL}/api/auth/cli`, { sessionToken }, { headers: { 'x-api-key': process.env.LOGGGAI_API_KEY || 'dev-key' } }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to verify authentication'); } } async refreshToken(sessionToken) { try { const response = await axios_1.default.post(`${this.baseURL}/api/auth/refresh`, { session_token: sessionToken }, { headers: { 'Content-Type': 'application/json', 'User-Agent': 'devlog-cli/1.0.0', 'Authorization': `Bearer ${sessionToken}` } }); return response.data; } catch (error) { if (error.response?.status === 401) { // Session token expired or invalid throw new Error('REFRESH_TOKEN_EXPIRED'); } this.handleApiError(error, 'Failed to refresh token'); } } async createPost(data) { try { const response = await axios_1.default.post(`${this.baseURL}/api/posts`, data, { headers: this.getHeaders() }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to create post'); } } async getPosts(limit = 10, offset = 0) { try { const response = await axios_1.default.get(`${this.baseURL}/api/posts?limit=${limit}&offset=${offset}`, { headers: this.getHeaders() }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to fetch posts'); } } async getPrompts() { try { const response = await axios_1.default.get(`${this.baseURL}/api/prompts`, { headers: this.getHeaders() }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to fetch prompts'); } } async getDefaultPrompt() { try { const response = await axios_1.default.get(`${this.baseURL}/api/user/default-prompt`, { headers: this.getHeaders() }); return response.data; } catch (error) { // If no default prompt, return null return { promptId: null }; } } async getIntegrations() { try { const response = await axios_1.default.get(`${this.baseURL}/api/integrations`, { headers: this.getHeaders() }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to fetch integrations'); } } async publishToSocial(articleId, platforms) { try { const response = await axios_1.default.post(`${this.baseURL}/api/integrations/publish`, { articleId, platforms }, { headers: this.getHeaders() }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to publish'); } } async createProject(data) { try { const response = await axios_1.default.post(`${this.baseURL}/api/projects`, data, { headers: this.getHeaders() }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to create project'); } } async syncCommits(data) { try { const response = await axios_1.default.post(`${this.baseURL}/api/sync`, data, { headers: this.getHeaders() }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to sync commits'); } } // Organizations and Context methods async getUserOrganizations() { try { const response = await axios_1.default.get(`${this.baseURL}/api/organizations`, { headers: this.getHeaders() }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to fetch organizations'); } } async getCurrentContext() { try { const response = await axios_1.default.get(`${this.baseURL}/api/user/context`, { headers: this.getHeaders() }); return response.data; } catch (error) { this.handleApiError(error, 'Failed to fetch context'); } } async switchContext(organizationId) { try { const payload = organizationId ? { organizationId } : { organizationId: null }; const response = await axios_1.default.post(`${this.baseURL}/api/user/context`, payload, { headers: this.getHeaders() }); return response.data; } catch (error) { if (error?.response?.status === 404 && error?.response?.data?.error === 'User not found') { console.error('\n' + chalk.default.red.bold('Your session appears to be invalid or your account has been deleted.')); console.error(chalk.default.yellow('Please run:')); console.error(chalk.default.cyan(' npx logggai logout && npx logggai login\n')); console.error(chalk.default.gray('to reset your session.')); process.exit(1); } this.handleApiError(error, 'Failed to switch context'); } } } exports.apiClient = new ApiClient(); //# sourceMappingURL=api.js.map