UNPKG

i18n-management-cli

Version:

CLI tool for i18n management platform - import, export, and sync translations

135 lines (115 loc) 3.65 kB
const axios = require('axios'); const Config = require('./config'); class ApiClient { constructor(config) { this.config = config || new Config(); this.axios = axios.create({ baseURL: this.config.get('apiUrl'), timeout: 30000, headers: { 'Content-Type': 'application/json', } }); // Add auth token if available const token = this.config.get('token'); if (token) { this.axios.defaults.headers.common['Authorization'] = `Bearer ${token}`; } // Response interceptor for error handling this.axios.interceptors.response.use( (response) => response, (error) => { if (error.response) { const message = error.response.data?.error || error.response.statusText; throw new Error(`API Error (${error.response.status}): ${message}`); } else if (error.request) { throw new Error('Network Error: Could not connect to API server'); } else { throw new Error(`Request Error: ${error.message}`); } } ); } async getProjects() { const response = await this.axios.get('/projects'); return response.data; } async getProject(projectId) { const response = await this.axios.get(`/projects/${projectId}`); return response.data; } async getProjectLanguages(projectId) { const response = await this.axios.get(`/projects/${projectId}/languages`); return response.data; } async getProjectModules(projectId) { const response = await this.axios.get(`/projects/${projectId}/modules`); return response.data; } async createModule(projectId, name, description) { const response = await this.axios.post(`/projects/${projectId}/modules`, { name, description }); return response.data; } async exportTranslations(projectId, langCode, moduleId = null) { const params = { projectId, lang: langCode }; if (moduleId) { params.moduleId = moduleId; } const response = await this.axios.get('/translations/export', { params }); return response.data; } async importTranslations(projectId, moduleId, langCode, translations, createMissing = true) { const response = await this.axios.post('/translations/import', { projectId, moduleId, langCode, translations, createMissing }); return response.data; } async getTranslations(filter = {}) { const response = await this.axios.get('/translations', { params: filter }); return response.data; } async batchUpdateTranslations(translations) { const response = await this.axios.post('/translations/batch', { translations }); return response.data; } // Authentication methods async login(username, password) { const response = await this.axios.post('/auth/login', { username, password }); return response.data; } async logout() { if (this.axios.defaults.headers.common['Authorization']) { const response = await this.axios.post('/auth/logout'); delete this.axios.defaults.headers.common['Authorization']; return response.data; } return { success: true, message: 'Already logged out' }; } async getCurrentUser() { const response = await this.axios.get('/auth/me'); return response.data; } setToken(token) { if (token) { this.axios.defaults.headers.common['Authorization'] = `Bearer ${token}`; } else { delete this.axios.defaults.headers.common['Authorization']; } } hasToken() { return !!this.axios.defaults.headers.common['Authorization']; } } module.exports = ApiClient;