This code compiles the source code using the Babel compiler and prints the result to the console. To use the React-RAPID-API, the code first sets the processing flag to true and sets the REACT_APP_RAPID_API_URL and REACT_APP_RAPID_API_KEY environmental variables. Then, it makes an HTTP POST request to the specified url using the method "POST" with the following parameters:
-
url: process.env.REACT_APP_RAPID_API_URL
-
params: { base64_encoded: "true", fields: "*" }
-
headers: {
-
"content-type": "application/json",
-
"Content-Type": "application/json",
-
"X-RapidAPI-Host": process.env.REACT_APP_RAPID_API_HOST,
-
"X-RapidAPI-Key": process.env.REACT_APP_RAPID_API_KEY
After making the request, the code sets the processing flag to false to indicate that the compilation is finished. The code then prints the result of the compilation to the
const handleCompile = () => {
setProcessing(true);
const formData = {
language_id: language.id,
// encode source code in base64
source_code: btoa(code),
stdin: btoa(customInput),
};
const options = {
method: "POST",
url: process.env.REACT_APP_RAPID_API_URL,
params: { base64_encoded: "true", fields: "*" },
headers: {
"content-type": "application/json",
"Content-Type": "application/json",
"X-RapidAPI-Host": process.env.REACT_APP_RAPID_API_HOST,
"X-RapidAPI-Key": process.env.REACT_APP_RAPID_API_KEY,
},
data: formData,
};
axios
.request(options)
.then(function (response) {
console.log("res.data", response.data);
const token = response.data.token;
checkStatus(token);
})
.catch((err) => {
let error = err.response ? err.response.data : err;
setProcessing(false);
console.log(error);
});
};