angular-token
angular-token copied to clipboard
Stop adding a final slash to apiBase URL
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 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?
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.
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":
-
With Trailing Slash:
export const baseUrl = 'http://example.com'; export const usersUrl = '/v1/users'; -
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