LaVOZs

The World’s Largest Online Community for Developers

'; javascript - How do I access axios-oauth-client response? - LavOzs.Com

I'm using axios-oauth-client in my TypeScript project.

I'm calling it like (oauth = axios-oauth-client):

async function getOauthToken(): Promise<any> {
    let getClientCredentials;
    try {
                getClientCredentials = oauth.client(axios.create(), {
                    url: url,
                    grant_type: 'client_credentials',
                    client_id: 'id',
                    client_secret: 'secret',
                    scope: 'read'
                });
            } catch (error) {
                logger.error('Oauth Request Error: ' + error.toString());
           }
    }
    return getClientCredentials;
}

When I call .then() on oauth.client I get a runtime error, saying .then does not exist. When I return getClientCredentials and print out the value I get () => axios(config).then(res => res.data).

But I have no idea how to access the data here?!

If I return getClientCredentials() (note the braces), then the JSON is returned no problem and I simply call the key I want. But this fails during jest test runs, saying getClientCredentials is not a function.

Where am I going wrong?

Did you try this:

   async function getOauthToken(): Promise<any> {
        let getClientCredentials;
        try {
                    getClientCredentials = oauth.client(axios.create(), {
                        url: url,
                        grant_type: 'client_credentials',
                        client_id: 'id',
                        client_secret: 'secret',
                        scope: 'read'
                    });
                 const auth = await getClientCredentials();
                } catch (error) {
                    logger.error('Oauth Request Error: ' + error.toString());
               }
        }
        return auth;
    }
Related
How do JavaScript closures work?
How do I check if an element is hidden in jQuery?
How do I remove a property from a JavaScript object?
How do I redirect to another webpage?
How do I check whether a checkbox is checked in jQuery?
How do I include a JavaScript file in another JavaScript file?
How to check whether a string contains a substring in JavaScript?
Why does Google prepend while(1); to their JSON responses?
How can I remove a specific item from an array?
How do I return the response from an asynchronous call?