refactor: DeepL client uses fetch instead of axios
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
const qs = require('qs')
|
||||
const axios = require('axios')
|
||||
const Diff = require('diff')
|
||||
|
||||
const { createCoreService } = require('@strapi/strapi').factories;
|
||||
@@ -52,34 +51,33 @@ class Translator {
|
||||
}
|
||||
|
||||
async get(origin, target, text) {
|
||||
try {
|
||||
const data = {
|
||||
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,
|
||||
}, {
|
||||
const response = await fetch(this.urlRequest, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `DeepL-Auth-Key ${this.deeplKey}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text: Array.isArray(text) ? text : [text],
|
||||
source_lang: origin,
|
||||
target_lang: target,
|
||||
})
|
||||
})
|
||||
|
||||
return result.data
|
||||
} catch (error) {
|
||||
console.error('DeepL error:', error?.response?.data || error);
|
||||
if (!response.ok) {
|
||||
const body = await response.text()
|
||||
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}) => ({
|
||||
async translate(origin, target, text) {
|
||||
const translator = new Translator()
|
||||
const data = await translator.get(origin, target, text)
|
||||
return data.translations[0].text
|
||||
},
|
||||
@@ -222,7 +220,6 @@ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
|
||||
start += pageSize
|
||||
}
|
||||
|
||||
const translator = new Translator()
|
||||
const translated = []
|
||||
const skipped = []
|
||||
const errors = []
|
||||
|
||||
Reference in New Issue
Block a user