fix: remove fetch requests

This commit is contained in:
cihatata authored and cihat committed 2025-02-11 07:43:39 +03:00
1 parent 158a3f14ea
commit 7b6eed96f4
6 files changed
+6 -450

No files matched your search

+2 -102
View File
@@ -12,9 +12,8 @@ const UPROCK_ENDPOINT = "https://api.uprock.com/checkmate/push";
*/
class NetworkService {
static SERVICE_NAME = SERVICE_NAME;
static POEDITOR_BASE_URL = 'https://api.poeditor.com/v2';
constructor(axios, ping, logger, http, Docker, net) {
constructor(axios, ping, logger, http, Docker, net, stringService) {
this.TYPE_PING = "ping";
this.TYPE_HTTP = "http";
this.TYPE_PAGESPEED = "pagespeed";
@@ -31,17 +30,7 @@ class NetworkService {
this.http = http;
this.Docker = Docker;
this.net = net;
this.apiToken = process.env.POEDITOR_API_TOKEN;
this.projectId = process.env.POEDITOR_PROJECT_ID;
if (!this.apiToken || !this.projectId) {
this.logger.error({
message: 'POEditor API token or project ID is missing in environment variables',
service: this.SERVICE_NAME,
method: 'constructor'
});
}
this.stringService = stringService;
}
/**
@@ -410,95 +399,6 @@ class NetworkService {
return this.handleUnsupportedType(type);
}
}
async getPoEditorLanguages() {
try {
const params = new URLSearchParams();
params.append('api_token', this.apiToken);
params.append('id', this.projectId);
const response = await this.axios.post(`${NetworkService.POEDITOR_BASE_URL}/languages/list`, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
return response.data.result.languages.map(lang => lang.code);
} catch (error) {
error.service = this.SERVICE_NAME;
error.method = "getPoEditorLanguages";
throw error;
}
}
async exportPoEditorTranslations(language) {
try {
const params = new URLSearchParams();
params.append('api_token', this.apiToken);
params.append('id', this.projectId);
params.append('language', language);
params.append('type', 'key_value_json');
const exportResponse = await this.axios.post(`${NetworkService.POEDITOR_BASE_URL}/projects/export`, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
const { url } = exportResponse.data.result;
const translationsResponse = await this.axios.get(url);
return translationsResponse.data;
} catch (error) {
error.service = this.SERVICE_NAME;
error.method = "exportPoEditorTranslations";
throw error;
}
}
async getPoEditorTerms() {
try {
const params = new URLSearchParams();
params.append('api_token', this.apiToken);
params.append('id', this.projectId);
const response = await this.axios.post(`${NetworkService.POEDITOR_BASE_URL}/terms/list`, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
return response.data.result?.terms?.map(term => term.term.trim()) || [];
} catch (error) {
error.service = this.SERVICE_NAME;
error.method = "getPoEditorTerms";
throw error;
}
}
async addPoEditorTerms(terms) {
try {
const formattedTerms = terms.map(termObj => ({
term: Object.keys(termObj)[0]
}));
const params = new URLSearchParams();
params.append('api_token', this.apiToken);
params.append('id', this.projectId);
params.append('data', JSON.stringify(formattedTerms));
const response = await this.axios.post(`${NetworkService.POEDITOR_BASE_URL}/terms/add`, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
return response.data;
} catch (error) {
error.service = this.SERVICE_NAME;
error.method = "addPoEditorTerms";
throw error;
}
}
}
export default NetworkService;
+2 -199
View File
@@ -4,9 +4,8 @@ import path from 'path';
class TranslationService {
static SERVICE_NAME = 'TranslationService';
constructor(logger, networkService) {
constructor(logger) {
this.logger = logger;
this.networkService = networkService;
this.translations = {};
this._language = 'en';
this.localesDir = path.join(process.cwd(), 'locales');
@@ -22,10 +21,8 @@ class TranslationService {
async initialize() {
try {
await this.loadTranslations();
await this.loadFromFiles();
// Yeni eklenen terimleri POEditor'e gönder
await this.syncTermsWithPOEditor();
} catch (error) {
this.logger.error({
message: error.message,
@@ -73,113 +70,6 @@ class TranslationService {
}
}
async loadTranslations() {
let hasError = false;
try {
const languages = await this.getLanguages();
for (const language of languages) {
try {
const translations = await this.exportTranslations(language);
this.translations[language] = translations;
} catch (error) {
hasError = true;
this.logger.error({
message: `Failed to fetch translations from POEditor for language ${language}: ${error.message}`,
service: 'TranslationService',
method: 'loadTranslations',
stack: error.stack
});
}
}
if (hasError || Object.keys(this.translations[this._language]).length === 0) {
this.logger.error({
message: 'Failed to fetch translations from POEditor, using default.json',
service: 'TranslationService',
method: 'loadTranslations'
});
// Load translations from default.json in locales directory
const defaultFilePath = path.join(this.localesDir, 'default.json');
if (fs.existsSync(defaultFilePath)) {
const content = fs.readFileSync(defaultFilePath, 'utf8');
this.translations['en'] = JSON.parse(content);
} else {
throw new Error('default.json file not found in locales directory');
}
}
await this.saveTranslations();
} catch (error) {
hasError = true;
this.logger.error({
message: error.message,
service: 'TranslationService',
method: 'loadTranslations',
stack: error.stack
});
}
}
async getLanguages() {
try {
return await this.networkService.getPoEditorLanguages();
} catch (error) {
this.logger.error({
message: error.message,
service: 'TranslationService',
method: 'getLanguages',
stack: error.stack
});
return ['en'];
}
}
async exportTranslations(language) {
try {
return await this.networkService.exportPoEditorTranslations(language);
} catch (error) {
this.logger.error({
message: error.message,
service: 'TranslationService',
method: 'exportTranslations',
stack: error.stack
});
return {};
}
}
async saveTranslations() {
try {
if (!fs.existsSync(this.localesDir)) {
fs.mkdirSync(this.localesDir);
}
for (const [language, translations] of Object.entries(this.translations)) {
const filePath = path.join(this.localesDir, `${language}.json`);
fs.writeFileSync(filePath, JSON.stringify(translations, null, 2));
}
const defaultFilePath = path.join(this.localesDir, 'default.json');
const enTranslations = this.translations['en'] || {};
fs.writeFileSync(defaultFilePath, JSON.stringify(enTranslations, null, 2));
this.logger.info({
message: 'Translations saved to files successfully',
service: 'TranslationService',
method: 'saveTranslations'
});
} catch (error) {
this.logger.error({
message: error.message,
service: 'TranslationService',
method: 'saveTranslations',
stack: error.stack
});
}
}
getTranslation(key) {
let language = this._language;
@@ -195,93 +85,6 @@ class TranslationService {
return key;
}
}
async getTermsFromPOEditor() {
try {
return await this.networkService.getPoEditorTerms();
} catch (error) {
this.logger.error({
message: error.message,
service: 'TranslationService',
method: 'getTermsFromPOEditor',
stack: error.stack
});
return [];
}
}
async addTermsToPOEditor(terms) {
try {
if (!terms.length) return;
const response = await this.networkService.addPoEditorTerms(terms);
if (response.response?.status === 'fail') {
throw new Error(response.response.message || 'Failed to add terms to POEditor');
}
this.logger.info({
message: `${terms.length} new terms added to POEditor`,
service: 'TranslationService',
method: 'addTermsToPOEditor',
response: response
});
return response;
} catch (error) {
this.logger.error({
message: `Failed to add terms to POEditor: ${error.message}`,
service: 'TranslationService',
method: 'addTermsToPOEditor',
stack: error.stack,
terms: terms
});
throw error;
}
}
async syncTermsWithPOEditor() {
try {
const defaultFilePath = path.join(this.localesDir, 'default.json');
const enTranslations = JSON.parse(fs.readFileSync(defaultFilePath, 'utf8'));
const localTerms = Object.keys(enTranslations)
.map(term => term);
const poeditorTerms = await this.getTermsFromPOEditor();
const newTerms = localTerms?.filter(term => !poeditorTerms?.includes(term));
this.logger.info({
message: `Comparison results - New terms found: ${newTerms.length}`,
sampleNewTerms: newTerms.slice(0, 5),
service: 'TranslationService',
method: 'syncTermsWithPOEditor'
});
if (newTerms.length > 0) {
const formattedTerms = newTerms.map(term => ({
[term]: enTranslations[term] || '',
}));
await this.addTermsToPOEditor(formattedTerms);
} else {
this.logger.info({
message: 'No new terms found to synchronize',
service: 'TranslationService',
method: 'syncTermsWithPOEditor'
});
}
} catch (error) {
this.logger.error({
message: error.message,
service: 'TranslationService',
method: 'syncTermsWithPOEditor',
stack: error.stack
});
}
}
}
export default TranslationService;