Replies: 11 comments 7 replies
-
Thinking about this a little more, I believe there is a more flexible way to do this. I went through a list of APIs I commonly use, and see the following requirement sets:
I propose the following change. Modify export interface HttpResourceUrls {
/**
* The URL path for a single entity endpoint, e.g, `some-api-root/hero`
* such as you'd use to add a hero.
* Example: `httpClient.post<Hero>('some-api-root/hero', addedHero)`.
* note the lack of a trailing slash. If you require trailing slashes on your
* entity url, set the `entityResourceTrailingSlash` parameter to `true`.
*/
entityResourceUrl: string;
/**
* Adds a trailing slash to the end of the entity resource URL if true
* e.g.: `some-api-root/hero/` or `some-api-root/hero/{heroId}/`
* Removes trailing slash at the end of entity resource URL if false
* e.g.: `some-api-root/hero` or `some-api-root/hero/{heroId}`.
* defaults to false if not defined.
**/
entityResourceTrailingSlash?: boolean;
/**
* The URL path for a multiple-entity endpoint, e.g, `some-api-root/heroes`
* such as you'd use when getting all heroes.
* Example: `httpClient.get<Hero[]>('some-api-root/heroes')`
* note the lack of a trailing slash. If you require trailing slashes on your
* collection urls, set the `collectionResourceTrailingSlash` parameter to `true`.
*/
collectionResourceUrl: string;
/**
* Adds a trailing slash to the end of the collection resource URL if true
* e.g.: `some-api-root/heroes/`
* Removes trailing slash at the end of entity resource URL if false
* e.g.: `some-api-root/heroes`
* defaults to true if not defined.
**/
collectionResourceTrailingSlash?: boolean;
} HttpUrlGenerator is then modified to strip trailing slashes from |
Beta Was this translation helpful? Give feedback.
-
This will be useful. especially when your backend is based on django-rest-framework |
Beta Was this translation helpful? Give feedback.
-
It sounds like there is some interest in this, so I'll start working on a PR to implement this functionality. |
Beta Was this translation helpful? Give feedback.
-
@mxdmedia It is also important that you patch your service over the existing HttpUrlGenerator service in your module! { provide: HttpUrlGenerator, useClass: YourHttpUrlGenerator }, Modifying the |
Beta Was this translation helpful? Give feedback.
-
This isn't documented https://ngrx.io/guide/data/extension-points#replace-the-httpurlgenerator But it is really needed, I can't imagine anyone's real world rest api follows the out of the box conventions. In ours, we post to the plural endpoint e.g /api/heroes to create a new entity at that point. I had success by: ` import { DefaultHttpUrlGenerator, HttpResourceUrls, normalizeRoot, Pluralizer } from '@ngrx/data'; @Injectable()
} ` And then overriding the provider like:
|
Beta Was this translation helpful? Give feedback.
-
@httpete would you like to submit a PR to add the example to the docs? |
Beta Was this translation helpful? Give feedback.
-
We have a case where we need the root to be Why is And could there be a simpler way of disabling |
Beta Was this translation helpful? Give feedback.
-
Yeah, it breaks absolute paths (like mentioned above) and protocol-relative URLs (e.g. |
Beta Was this translation helpful? Give feedback.
-
I'll take a look into this soon. If anything we can look at making it configurable |
Beta Was this translation helpful? Give feedback.
-
Has there been any update on making this option configurable? I'm hitting an issue using AWS API Gateway, as that disallows URL endpoints with a trailing slash (not sure why) so the default Ngrx Data data service calls fail (with a 404). |
Beta Was this translation helpful? Give feedback.
-
Lost track on this one. If someone wants to submit a PR for making this configurable, that would be great. |
Beta Was this translation helpful? Give feedback.
-
While normally considered bad practice, there are APIs out there that require a trailing slash on endpoints, and as a front-end dev, we don't always have the ability to change this. I have been unable to use @ngrx/data with a particular API due to this, and looking through the
HttpUrlGenerator
code, it doesn't appear possible with the current version of @ngrx/data.I have working code which does the following:adds optionaltrailingSlashes: boolean
parameter toDefaultDataServiceConfig
interface.addstrailingSlashes: boolean
parameter toDefaultDataService<T>
class. Defaults tofalse
uses newDefaultDataService<T>.trailingSlashes
to append/
to all urls constructed in.add
,.delete
, etc.It works great, allowing the use of APIs that require trailing slashes. I am just finishing up writing tests for it, and then can submit a PR.My code was a bit limiting. See new proposal in comment below.
Describe any alternatives/workarounds you're currently using
One workaround is to use an interceptor to always add a trailing slash. However this feels like overkill, and becomes tricky if you need to use httpClient for other non-trailing-slash endpoints/apis/urls. It seems it would be easier to inject the slash if required when building the url.
Other information:
If accepted, I would be willing to submit a PR for this feature
[X] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No
Beta Was this translation helpful? Give feedback.
All reactions