From 2003948a9a79ffb98054e51596738ba8cb0fd690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Fri, 3 Jul 2026 13:34:38 +0400 Subject: [PATCH] refactor: DeepL client uses fetch instead of axios --- src/api/parole/services/parole.js | 35 ++++++++++++++----------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/api/parole/services/parole.js b/src/api/parole/services/parole.js index eabab3d..fb637e3 100644 --- a/src/api/parole/services/parole.js +++ b/src/api/parole/services/parole.js @@ -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, { + 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, - }, { - headers: { - Authorization: `DeepL-Auth-Key ${this.deeplKey}`, - 'Content-Type': 'application/json' - } }) + }) - 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 = []