angular-token icon indicating copy to clipboard operation
angular-token copied to clipboard

Stop adding a final slash to apiBase URL

Open Chosko opened this issue 8 years ago • 3 comments

Hi, I use constants to build my URLs.

   export const baseUrl = 'http://example.com';
   export const usersUrl = '/v1/users';

because for me it's easier to make calls such as this.http.get(baseUrl + usersUrl), obtaining GET http://example.com/v1/users

However, although I configured angular2-token with apiUrl: baseUrl (which doesn't have a trailing slash), for some reason this._tokenService.get(usersUrl) adds another slash to the URL, which then becomes GET http://example.com//v1/users.

I know that to make it work I could define my constants as

   export const baseUrl = 'http://example.com';
   export const usersUrl = 'v1/users';

but the drawback is that, where I use Angular's Http, I would need to change the whole application to manually add the missing slash: this.http.get(baseUrl + '/' + usersUrl) which is not super clean, and I don't like it too much as it breaks the DRY principle.

IMO the library would be clearer without deciding to make changes to your provided URLs...

Chosko avatar Mar 20 '17 11:03 Chosko

@Chosko thanks for your detailed explanation. I know that the route building is kinda messy, especially the naming. Do you know if there's some kind of standard for this?

neroniaky avatar Mar 27 '17 06:03 neroniaky

Could the library safely replace double slashes after the protocol? // => /? Not sure of a use case when you would have double slashes in a URL or if this is defined in a standard someplace as legal.

raysuelzer avatar Dec 15 '17 03:12 raysuelzer

Great question. What is the standard practice for URLs or paths that are constants? Do they include trailing slashes or not?

Which one is the "standard practice":

  1. With Trailing Slash:

    export const baseUrl  = 'http://example.com';
    export const usersUrl = '/v1/users';
    
  2. Without Trailing Slash:

    export const baseUrl  = 'http://example.com/';
    export const usersUrl = 'v1/users';
    

The following StackOverflow entry seems to address it but is certainly not definitive:

https://softwareengineering.stackexchange.com/questions/344930/should-i-configure-my-urls-with-a-leading-and-or-trailing-slash

joshuapinter avatar Jul 01 '18 17:07 joshuapinter