Please let HttpParams.set and .append accept one object-like arg with nested data
- Vorherrschende Sprache
- TypeScript
- Sterne
- 101k
- Forks
- 27.5k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 288
Beschreibung
## I'm submitting a...
[ x ] Feature request
## Current behavior
You can only set or append to HttpParams one by one.
## Expected behavior
With Http.get, .post, etc, you could pass any object with nested object-like data as options.params. First level keys would be mapped to query params, and values JSON encoded if they're object-like. I'd like to achieve the same without adding complexity by doing something like:
> new HttpParams.set( { where: { id: [ 1,2,3 ] } } )
## What is the motivation / use case for changing the behavior?
In migrating to HttpClient from Http, I had to add a helper method:
```
private anyToHttpParams(obj: any) { // requires lodash
return Object.entries(obj)
.reduce(
(params, [key, value]) => params.set(
key, _.isObjectLike(value) ? JSON.stringify(value) : value
),
new HttpParams()
);
}
// this.http.get(url, { params: this.anyToHttpParams({ where: { id: [ 1,2,3 ] } }) } );
```
For me, this was sort of a breaking change as well, as I didn't get a compiler error when I just replaced Http with HttpClient and passed objects with nested data to params like I used to with Http, but in network console my xhr URLs changed to something like `/action?[object object]`.
## Environment
Angular version: 5.0.1
Beitragsleitfaden
Rechercherichtung
Start with the HttpParams.set and append APIs and the HttpClient options.params path described in the issue. Done means one object-like argument maps first-level keys to query parameters and JSON-encodes object-like values, including nested data.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- api
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100