Skip to content
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

Access to a resource with an end slash #25

Open
pirhoo opened this issue Jan 7, 2013 · 2 comments
Open

Access to a resource with an end slash #25

pirhoo opened this issue Jan 7, 2013 · 2 comments

Comments

@pirhoo
Copy link

pirhoo commented Jan 7, 2013

Hi folks,

The module is great but I found a little issue with endpoints terminating by a slash.

Not works:

site.v1.resource["1"].get(console.log) // Fermata dislikes the 501 redirection

Works:

site.v1.resource["1/"].get(console.log) // Note the slash after the identifier 

I didn't find option to add a slash automatically... Any help ?

Cheers,

@pirhoo
Copy link
Author

pirhoo commented Jan 7, 2013

I mad this workaround (using a plugin) :

fermata.registerPlugin("slashed", function (transport, baseURL) {

    this.base = baseURL;                            
    transport = transport.using('statusCheck').using('autoConvert', "application/json");        

    return function (request, callback) {                        
        // If the last caracter of the path isn't a slash
        if( String(request.path[request.path.length - 1]).substr(-1,1) !== "/") {
            // Automatically adding a tail slash to the URL's end
            request.path[request.path.length - 1] += "/";
            // It's also possible to add an empty string
            // request.path.push("");
        }

        transport(request, callback);
    };
});


var site = fermata.slashed("http://api.example.org");
site.v1.instance["1"].get(console.log); // Works !
site.v1.instance["1/"].get(console.log); // Works too !

@natevw
Copy link
Owner

natevw commented Jan 7, 2013

Cool, glad you were able to get a plugin working for this — I probably don't promote this enough in the docs, but plugins are intended to be lightweight enough to make one for every site whenever desirable.

There's two ways you could get a slash at the end of a URL right now, by avoiding escaping with an array (site.v1.resource(["1/"])) or by appending a blank component (site.v1.resource['']).

I'll leave this open for now in case there's any good clean general solutions, but a plugin might be the most appropriate solution in this sort of situation.

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

No branches or pull requests

2 participants