AidanWelch / google-translate-api

An updated free and unlimited API for Google Translate :dollar: :no_entry_sign:
MIT License
137 stars 19 forks source link

Is proxying supported in react native? #42

Closed Seu07 closed 1 month ago

Seu07 commented 6 months ago

this "https-proxy-agent" is not support with react native. Could you give any suggestions?

AidanWelch commented 5 months ago

@Seu07 I'm not very experienced with react native, but I'm pretty sure react-native-blob-util can be used to polyfill xhr requests with trusty: true then you can do something like this with Axios:

import { translate } from 'google-translate-api-x';
import axios from 'axios';

function proxiedFetch(url, init) {
    // we essentially need to emulate fetch behavior with axios
    const axiosOptions = init.requestOptions ?? {};

    axiosOptions.data = axiosOptions.body; // axios calls the `body` in fetch `data`

    axiosOptions.proxy = {
        protocol: 'https',
        host: PROXY_URL,
        port: '5353', // or whatever port
        auth: { // if your proxy needs it
            username: PROXY_USERNAME,
            password: PROXY_PASSWORD,
        },
    };

    axios.transformResponse = res => res; // prevent axios from parsing json by default

    return axios(url, axiosOptions).then(res => {
        if (res.status >= 200 && res.status < 300) {
            res.ok = true;
        } else {
            res.ok = false;
        }

        res.text = () => res.data;
        res.json = () => JSON.parse(res.data);

        return res;
    });
}

const res = await translate('cat', {from: 'en', to: 'es', requestFunction: proxiedFetch});
console.log(res);

Please let me know if you get it to work!

AidanWelch commented 1 month ago

Stale