Skip to content

DSHttpAdapter default $http config #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kenjiqq opened this issue Aug 6, 2014 · 7 comments
Closed

DSHttpAdapter default $http config #111

kenjiqq opened this issue Aug 6, 2014 · 7 comments
Assignees

Comments

@kenjiqq
Copy link

kenjiqq commented Aug 6, 2014

I think it would be good if you could provide some default config for $http in the DSHttpAdapter.
So you can add stuff like interceptors if you want to handle certain errors etc, for example that the user is not authenticated with the api.

I was thinking about these changes to the DSHttpAdapterProvider

    var defaults = this.defaults = {
        /**
         * @doc property
         * @id DSHttpAdapterProvider.properties:defaults.queryTransform
         * @name defaults.queryTransform
         * @description
         * Transform the angular-data query to something your server understands. You might just do this on the server instead.
         *
         * @param {string} resourceName The name of the resource.
         * @param {object} params Params sent through from `$http()`.
         * @returns {*} Returns `params` as-is.
         */
        queryTransform: function(resourceName, params) {
            return params;
        },
        httpConfig: {}
    };
function HTTP(config) {
    var start = new Date().getTime();

    config = DSUtils.deepMixIn(this.defaults.httpConfig, config);
    return $http(config).then(function(data) {
        $log.debug(data.config.method + ' request:' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms', arguments);
        return data;
    });
}

Then one could add config to all $http requests that angular-data executes

DSHttpAdapter.defaults.httpConfig = {
    interceptor: /* ... */
};

The reason for not just configuring $http globally would be if you wanted to do other $http requests to other sources that is not your API

@jmdobry
Copy link
Member

jmdobry commented Aug 6, 2014

👍

@kenjiqq
Copy link
Author

kenjiqq commented Aug 11, 2014

Think this line needs to be the other way like this:
config = DSUtils.deepMixIn(config, this.defaults.httpConfig);

@jmdobry
Copy link
Member

jmdobry commented Aug 11, 2014

Yep

@jmdobry jmdobry self-assigned this Aug 14, 2014
@pennersr
Copy link

Interceptors cannot be added by tweaking the config (see issue #139). So, given that the goal of this ticket was to be able to plug in interceptors, should this issue be re-opened?

@jmdobry
Copy link
Member

jmdobry commented Oct 31, 2014

The fix for #139 was to remove the documentation that indicated that it was possible to add an interceptor to $http via Angular-data's API. The only way to add interceptors is via $httpProvider.interceptors.push(...).

@perrierism
Copy link

Sorry how are interceptors added then? I see this was added in 0.10.5 (changelog) / ffae052

But the more recent comment above says there is no angular-data API to do this. I can get an interceptor working with $httpProvider.interceptors.push(...) but is there a way to make an interceptor which is specific to DS?

@jmdobry
Copy link
Member

jmdobry commented Apr 26, 2015

is there a way to make an interceptor which is specific to DS

No, not if you're going to use the http adapter that comes with js-data-angular (that's not how Angular's DI works).

However, you can load js-data-http which uses axios for http (instead of $http). You can add interceptors to axios via axios' api, which will not be used by $http.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants