I need some help in replacing fetch with axios here
export const fetchSecret = async (secretName) => {
const token = await getFunctionAppToken();
const headers = {
Authorization: `Bearer ${token}`,
};
const url = `${blbl.GetSecretFromVault}?secretName=${secretName}`;
return Promise.all([fetch(url, { method: "get", headers })])
.then(([response]) => {
const res = response.text();
return res;
})
.catch((error) => {
return Promise.reject(error);
});
};
I am stuck at the line of const res = response.text() for axios.
return Promise.all([axios.get(url, { headers })])
.then(([response]) => {
const res = ??????????
return res;
Axios automatically parses the response body stream as data, see response schema.
You can also inform Axios to expect a text response via the responseType option.
Finally, I always preference using the params option for passing query parameters as it ensures proper URL-encoding.
export const fetchSecret = async (secretName) =>
(await axios.get(blbl.GetSecretFromVault, {
headers: {
Authorization: `Bearer ${await getFunctionAppToken()}`
},
params: { secretName },
responseType: "text"
})).data
I don't know why you were using Promise.all() so have omitted it.
Your .catch() was also redundant so it's not included either.
The (await axios.get(...)).data is just a shorter version of
const response = await axios.get(...)
return response.data
or without await...
return axios.get(...).then(response => response.data)