-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from MiracleUFO/perf/translation-core-package
perf(proxying): fix Buffer not found issue by using proxy
- Loading branch information
Showing
8 changed files
with
86 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
NODE_ENV=development | ||
|
||
TRANSLATE_API_PROXY= | ||
TEST_TRANSLATE_API_PROXY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { HttpsProxyAgent } from 'https-proxy-agent'; | ||
import getErrorInTranslationMessage from './getErrorInTranslationMessage'; | ||
import { | ||
IS_DEVELOPMENT_OR_TEST, | ||
PROXY_URL, | ||
PROXY_URL_ALT, | ||
PROXY_URL_TEST, | ||
} from '../constants'; | ||
import language from '../types/language'; | ||
|
||
const translate = async (text: string, from?: language, to?: language) => { | ||
// option for development / testing | ||
// in case of `TooManyRequestsError` (Error Code `429`) | ||
const fetchOptions = ( | ||
IS_DEVELOPMENT_OR_TEST && PROXY_URL_TEST && { agent: new HttpsProxyAgent(PROXY_URL_TEST) } | ||
); | ||
|
||
let response; | ||
try { | ||
response = await fetch(PROXY_URL, { | ||
credentials: 'omit', | ||
mode: 'cors', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify( | ||
{ | ||
text, | ||
from, | ||
to, | ||
fetchOptions, | ||
}, | ||
), | ||
}); | ||
} catch (error) { | ||
// If the first request failed, try with the second proxy | ||
response = await fetch(PROXY_URL_ALT, { | ||
credentials: 'omit', | ||
mode: 'cors', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify( | ||
{ | ||
text, | ||
from, | ||
to, | ||
fetchOptions, | ||
}, | ||
), | ||
}); | ||
} | ||
|
||
if (response.status === 200) return response.json(); | ||
|
||
const error = new Error(`${response.status} - ${response.statusText}`); | ||
throw getErrorInTranslationMessage(error); | ||
}; | ||
|
||
export default translate; |