Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Export buildUrl in the global angular namespace #3213

Closed
wants to merge 1 commit into from
Closed

Export buildUrl in the global angular namespace #3213

wants to merge 1 commit into from

Conversation

gdamjan
Copy link

@gdamjan gdamjan commented Jul 12, 2013

buildUrl could be used outside of $http too, for example if the user needs
to create urls for APIs that are not natively supported in angular (EventSource for ex.)

potentionally if it's not hidden in a closure inside $http,
it might be used in other parts of angular too

buildUrl could be used outside of $http too, for example if the user needs
to create urls for APIs that are not natively supported in angular (EventSource for ex.)

potentionally if it's not hidden in a closure inside $http,
it might be used in other parts of angular too
@pkozlowski-opensource
Copy link
Member

There is a related discussion in #2701

I still claim that this URL params parsing / formatting functionality should go into its own service.

@gdamjan
Copy link
Author

gdamjan commented Jul 12, 2013

I agree there's probably a bigger story about url handling. On the other hand this is a simple change that really doesn't change much. Maybe it's a good interim step?

@pkozlowski-opensource
Copy link
Member

@gdamjan The problem with this interim step is that it adds a method to the public API. We would have to maintain "forever".

@gdamjan
Copy link
Author

gdamjan commented Jul 15, 2013

I agree.
So what would be the proper place for this function? The $location service?

@petebacondarwin
Copy link
Contributor

I think that we need to look at a longer term solution for providing (de)serialization of urls and that we should not be adding to the public API in the interim. @gdamjan - do you want to look at #2701 and maybe put forward another PR that will be more generic and flexible in the long term?

@gdamjan
Copy link
Author

gdamjan commented Jul 15, 2013

I don't have much to add to #2701 and don't know about a more generic way to go.
In the end it's a small idempotent function, it can go anywhere.

@pkozlowski-opensource
Copy link
Member

@gdamjan It is not only about one function - serialization and deserialization go hand in hand. This is why I believe that we need a service that would encapsulate those 2 functions.

@blowsie
Copy link

blowsie commented Mar 18, 2014

@gdamjan FYI I have a use for this but I don't use the $location service

@SchizoDuckie
Copy link

I would love it if i can properly build a url for my JSONP request without having to resort to hacks. We need a solution for this, but I understand it's not just $location that's touched, it's a lot of changes deep into $http as well.

For people landing here from search, this is my current solution:

/**
 * Angular's private URL Builder method + unpublished dependencies converted to a public service
 * So we can properly build a GET url with parameters for a JSONP request.
 */
app.provider('URLBuilder', function() {

    function encodeUriQuery(val, pctEncodeSpaces) {
        return encodeURIComponent(val).
        replace(/%40/gi, '@').
        replace(/%3A/gi, ':').
        replace(/%24/g, '$').
        replace(/%2C/gi, ',').
        replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
    }

    /**
     * Angular's private buildUrl function, patched to refer to the public methods on the angular globals
     */
    function buildUrl(url, params) {
        if (!params) return url;
        var parts = [];
        angular.forEach(params, function(value, key) {
            if (value === null || angular.isUndefined(value)) return;
            if (!angular.isArray(value)) value = [value];

            angular.forEach(value, function(v) {
                if (angular.isObject(v)) {
                    v = angular.toJson(v);
                }
                parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(v));
            });
        });
        return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
    }

    this.$get = function() {
        return {
            build: function(url, params) {
                return buildUrl(url, params);
            }
        };

    };
}); ```

@subtubes-io
Copy link

+1

@blowsie
Copy link

blowsie commented May 27, 2015

Is there a solution for this in angular 1.3 ?

#7429

@parliament718
Copy link

I'm also waiting for this one, has it been included in latest angular yet?

@blowsie
Copy link

blowsie commented Jun 2, 2015

@parliament718 ;

As far as I know (from looking at the source etc), this is not in 1.3 but there may be a solution coming in 1.4, for now I am manually copying this changeset in to my copy of angular
f64afc7

@slavafomin
Copy link

👍

@blowsie
Copy link

blowsie commented Aug 12, 2015

Docs for 1.4+ found here.
https://docs.angularjs.org/api/ng/service/$httpParamSerializer

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

Successfully merging this pull request may close these issues.

9 participants