refactor: DeepL client uses fetch instead of axios
This commit is contained in:
@@ -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,
|
headers: {
|
||||||
source_lang: origin,
|
Authorization: `DeepL-Auth-Key ${this.deeplKey}`,
|
||||||
target_lang: target,
|
'Content-Type': 'application/json'
|
||||||
text
|
},
|
||||||
}
|
body: JSON.stringify({
|
||||||
const result = await axios.post(this.urlRequest, {
|
|
||||||
text: Array.isArray(text) ? text : [text],
|
text: Array.isArray(text) ? text : [text],
|
||||||
source_lang: origin,
|
source_lang: origin,
|
||||||
target_lang: target,
|
target_lang: target,
|
||||||
}, {
|
|
||||||
headers: {
|
|
||||||
Authorization: `DeepL-Auth-Key ${this.deeplKey}`,
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
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 = []
|
||||||
|
|||||||
Reference in New Issue
Block a user