angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

$resource doesn't provide any options to transform/serialize parameters when calling an action.

Open Sean-Der opened this issue 12 years ago • 10 comments

I am using $resource heavily, and am having a hard time finding a way to serialize params when calling actions. I can transform the body with transformRequest, but there is no facility like this for params.

Right now https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L416 just sets the values, and it would be nice to have a way to just case off of the type!

My use case is that I pass dates when calling .query, and I don't want to implement a service OR replace toString on my dates.

I have this implemented on a personal project, but is this something that would be welcomed to angular? If so, how would you like it implemented?

A.) Do you want the function to operate on all params, or serialize one at a time. B.) Do you want this custom function to handle JUST serialization, or should it take over building the query string as well.

I would really like to fire off the pull request myself, seems like a good start to get involved.

Sean-Der avatar Dec 23 '13 20:12 Sean-Der

+1 we need the same feature for our project, right now we're monkeypatching the resource and it's ugly

arty-name avatar Jan 02 '14 16:01 arty-name

+1 We're using moment.js and have various API endpoints that accept date ranges. It would tidy up our code considerably if we could just handle moment objects and transform them in a standard way in our resource declarations. Currently we're repeating the same formatting in controllers, routes, etc.

robfletcher avatar Mar 11 '14 14:03 robfletcher

+1

We ran into this as well. We ended up using a request transformer, but it feels quite hacky and fragile.

lord2800 avatar Mar 11 '14 20:03 lord2800

@lord2800 a request transformer can only transform the post body, can't it? Not parameters.

robfletcher avatar Mar 12 '14 09:03 robfletcher

Erhm, I meant a request interceptor. My mistake.

lord2800 avatar Mar 14 '14 19:03 lord2800

I created a small module for improving the interface of $http that includes this feature. https://gist.github.com/ashaffer/11385932

It also allows you to pass around partial configurations for the $http service. Like this:

angular.factory('api', ['chttp', 'authentication', function(chttp, authentication) {
  return chttp({
    url: 'http://myapi.com',
    transformParams: [function(params) {
      if(authentication.token()) {
        params.access_token = authentication.token();
      }
      return params;
    }]
  });
}]);

angular.factory('userModel', ['api', function(api) {
  var user = api('/user');
  return {
    me: user.get('/me').then(constructModel(User));
  };
});

And the 'me' method will send requests (with access token) to 'http://myapi.com/user/me'.

@lord2800 sup <-- darawk

EDIT: I should point out that I literally just wrote this and have tested it barely at all. I'll probably package it up into a formal module at some point, but right now, no guarantees about any amount of workingness.

EDIT2: This module depends on the presence of underscore/lodash

ashaffer avatar Apr 28 '14 22:04 ashaffer

+1

I see the issue being that params are compiled into the URL prior to $http, and interceptor/transform are part of $http, not $resource.

Adding a transformParams: function (params, actionUrl, config) or even formatParams that gets called as part of the $resource configuration (pre $http) rather than what get's passed to $http might be the solution.

Though I would suggest it be passed as part of the options (where stripTrailingSlashes lives) would probably be the better place, that way, globally for that $resource you could format do things like format dates a specific way.

Doing it per action seems like it could be a lot more configuration that's not present.

halkon avatar Jun 13 '14 15:06 halkon

+1 and look forward to seeing changes by @Sean-Der merged into master.

Intercepting $http globally is problematic since the $http calls could entirely come from the third party modules of the app, which may rely on a certain behavior of $http, so it's definitely safer to define them in the resource.

michaelwsk avatar Oct 03 '14 04:10 michaelwsk

@michaelwsk I hope this gets merged eventually, but it really doesn't look like it :/

I just noticed that the PR doesn't apply cleanly, so fixing that now. Hopefully it gets attention from a maintainer eventually.

Sean-Der avatar Oct 03 '14 04:10 Sean-Der

:+1: This was in Oct 3, 2014. Today, Jan 26, 2015 still without at least response about it. I am using Angular 1.2.26 and hopefully upgrade to 1.3 and I need this feature too. I am building some application who needs a lot of dates in the URL so is a pain to do it without something like this ... I can patch, I can create my own module but we need that in the core.

@btford @caitp someone, @Sean-Der did a pretty good job, testing and updating the documentation so I don't see the problem with this PR.

yordis avatar Jan 26 '15 20:01 yordis