1- const axios = require ( 'axios' )
21const { getRandom } = require ( 'random-useragent' )
32const { load } = require ( 'cheerio' )
43
5- const available = require ( './languages/available.js' )
6- const compatibility = require ( './languages/compatibility.js' )
7- const SupportedLanguages = require ( './entities/languages.js' )
8-
9- axios . interceptors . request . use (
10- ( config ) => {
11- config . headers [ 'Accept' ] = '*/*'
12- config . headers [ 'Connection' ] = 'keep-alive'
13- config . headers [ 'User-Agent' ] = getRandom ( )
14-
15- return config
16- } ,
17- ( error ) => {
18- return Promise . reject ( error )
19- }
20- )
4+ const available = require ( './utils/languages/available.js' )
5+ const compatibility = require ( './utils/languages/compatibility.js' )
6+ const SupportedLanguages = require ( './enums/languages.js' )
7+ const transformResponse = require ( './utils/transform-response' )
218
229module . exports = class Reverso {
2310 /** @private */
@@ -34,17 +21,6 @@ module.exports = class Reverso {
3421 /** @private */
3522 CONJUGATION_URL = 'https://conjugator.reverso.net/conjugation-'
3623
37- /**
38- * @private
39- * Whether to use the insecure HTTP parser in Axios.
40- *
41- * This HTTP parser accepts certain headers that do not strictly follow the specification in
42- * https://datatracker.ietf.org/doc/html/rfc2616#section-4.1. The Reverso API occasionally
43- * returns headers that do not end with CRLF. Enable this to support accept these malformed
44- * responses. See https://github.com/axios/axios#request-config
45- */
46- insecureHTTPParser = false
47-
4824 /**
4925 * @public
5026 * @param {insecureHTTPParser: boolean }
@@ -470,6 +446,12 @@ module.exports = class Reverso {
470446 return result
471447 }
472448
449+ /**
450+ * @param text
451+ * @param source
452+ * @param cb
453+ * @returns {Promise<{ok: boolean, message}|{verbForms: *[], infinitive: (*|jQuery|string), ok: boolean}|{ok: boolean, message: string}> }
454+ */
473455 async getConjugation ( text , source = SupportedLanguages . ENGLISH , cb = null ) {
474456 source = source . toLowerCase ( )
475457
@@ -554,12 +536,22 @@ module.exports = class Reverso {
554536 * @returns {Promise<any> }
555537 */
556538 async #request( config ) {
557- try {
558- const { data } = await axios ( {
559- insecureHTTPParser : this . insecureHTTPParser ,
560- ...config ,
561- } )
539+ const headers = {
540+ Accept : '*/*' ,
541+ Connection : 'keep-alive' ,
542+ 'User-Agent' : getRandom ( ) ,
543+ ...config . headers ,
544+ }
562545
546+ const requestOptions = {
547+ method : config . method ,
548+ headers,
549+ body : config . data ? JSON . stringify ( config . data ) : undefined ,
550+ }
551+
552+ try {
553+ const response = await fetch ( config . url , requestOptions )
554+ const data = await transformResponse ( response )
563555 return { success : true , data }
564556 } catch ( error ) {
565557 return { success : false , error }
0 commit comments