refactor: DeepL client uses fetch instead of axios

This commit is contained in:
2026-07-03 13:34:38 +04:00
parent cac88c39b2
commit 2003948a9a
+16 -19
View File
@@ -1,6 +1,5 @@
'use strict'; 'use strict';
const qs = require('qs') const qs = require('qs')
const axios = require('axios')
const Diff = require('diff') const Diff = require('diff')
const { createCoreService } = require('@strapi/strapi').factories; const { createCoreService } = require('@strapi/strapi').factories;
@@ -52,34 +51,33 @@ class Translator {
} }
async get(origin, target, text) { async get(origin, target, text) {
try { const response = await fetch(this.urlRequest, {
const data = { method: 'POST',
auth_key: this.deeplKey,
source_lang: origin,
target_lang: target,
text
}
const result = await axios.post(this.urlRequest, {
text: Array.isArray(text) ? text : [text],
source_lang: origin,
target_lang: target,
}, {
headers: { headers: {
Authorization: `DeepL-Auth-Key ${this.deeplKey}`, Authorization: `DeepL-Auth-Key ${this.deeplKey}`,
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} },
body: JSON.stringify({
text: Array.isArray(text) ? text : [text],
source_lang: origin,
target_lang: target,
})
}) })
return result.data if (!response.ok) {
} catch (error) { const body = await response.text()
console.error('DeepL error:', error?.response?.data || error); console.error('DeepL error:', body)
throw new Error(`DeepL ${response.status}: ${body}`)
} }
return await response.json()
} }
} }
const translator = new Translator()
module.exports = createCoreService('api::parole.parole', ({strapi}) => ({ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
async translate(origin, target, text) { async translate(origin, target, text) {
const translator = new Translator()
const data = await translator.get(origin, target, text) const data = await translator.get(origin, target, text)
return data.translations[0].text return data.translations[0].text
}, },
@@ -222,7 +220,6 @@ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
start += pageSize start += pageSize
} }
const translator = new Translator()
const translated = [] const translated = []
const skipped = [] const skipped = []
const errors = [] const errors = []