Giter Site home page Giter Site logo

Comments (1)

AidanWelch avatar AidanWelch commented on July 24, 2024

@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!

from google-translate-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.