hello.js: Is it possible to set the provider's settings dynamically?

730 views Asked by At

I have implemented a new module for hello.js. I need the auth, grant and base to be dynamic. Is there a way to set/override these values from the hello.init() call?

My module looks like this:

(function(hello) {

    hello.init({

        my_service_name: {

            name: 'My-Service-Name',

            oauth: {
                version: 2,
                auth: 'http://mydomain/oauth/authorize',
                grant: 'http://mydomain/oauth/token'
            },

            scope: {
                basic: ['basic_scope']
            },

            base: 'http://mydomain/',

            xhr: function(p) {

                if (p.method !== 'get' && p.data) {

                    // Serialize payload as JSON
                    p.headers = p.headers || {};
                    p.headers['Content-Type'] = 'application/json';
                    if (typeof (p.data) === 'object') {
                        p.data = JSON.stringify(p.data);
                    }
                }

                return true;
            }
        }
    });

})(hello);

and my hello.init() call:

hello.init({
    my_service_name: server.consumerKey
}, {
    redirect_uri : server.callbackUrl,
});

The use-case is that the application I am developing will communicate with several servers, so I cannot hardcode the URLs in the module's file.

1

There are 1 answers

0
eyettea On

I figured out that I can override the default settings by passing the whole oauth object in the hello.init() call:

hello.init({
    my_service_name: {
        id: server.consumerKey,
        oauth: {
            version: 2,
            auth: server.auth_url,
            grant: server.token_url
        },
        base: server.baseUrl
    }
}, {
    redirect_uri : server.callbackUrl
});