@@ -73,7 +73,7 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
7373 add ( entity : T , options ?: HttpOptions ) : Observable < T > {
7474 const entityOrError =
7575 entity || new Error ( `No "${ this . entityName } " entity to add` ) ;
76- return this . execute ( 'POST' , this . entityUrl , entityOrError , options ) ;
76+ return this . execute ( 'POST' , this . entityUrl , entityOrError , null , options ) ;
7777 }
7878
7979 delete (
@@ -84,22 +84,29 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
8484 if ( key == null ) {
8585 err = new Error ( `No "${ this . entityName } " key to delete` ) ;
8686 }
87- return this . execute ( 'DELETE' , this . entityUrl + key , err , options ) . pipe (
87+
88+ return this . execute (
89+ 'DELETE' ,
90+ this . entityUrl + key ,
91+ err ,
92+ null ,
93+ options
94+ ) . pipe (
8895 // forward the id of deleted entity as the result of the HTTP DELETE
8996 map ( ( result ) => key as number | string )
9097 ) ;
9198 }
9299
93100 getAll ( options ?: HttpOptions ) : Observable < T [ ] > {
94- return this . execute ( 'GET' , this . entitiesUrl , options ) ;
101+ return this . execute ( 'GET' , this . entitiesUrl , null , options ) ;
95102 }
96103
97104 getById ( key : number | string , options ?: HttpOptions ) : Observable < T > {
98105 let err : Error | undefined ;
99106 if ( key == null ) {
100107 err = new Error ( `No "${ this . entityName } " key to get` ) ;
101108 }
102- return this . execute ( 'GET' , this . entityUrl + key , err , options ) ;
109+ return this . execute ( 'GET' , this . entityUrl + key , err , null , options ) ;
103110 }
104111
105112 getWithQuery (
@@ -127,14 +134,20 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
127134 id == null
128135 ? new Error ( `No "${ this . entityName } " update data or id` )
129136 : update . changes ;
130- return this . execute ( 'PUT' , this . entityUrl + id , updateOrError , options ) ;
137+ return this . execute (
138+ 'PUT' ,
139+ this . entityUrl + id ,
140+ updateOrError ,
141+ null ,
142+ options
143+ ) ;
131144 }
132145
133146 // Important! Only call if the backend service supports upserts as a POST to the target URL
134147 upsert ( entity : T , options ?: HttpOptions ) : Observable < T > {
135148 const entityOrError =
136149 entity || new Error ( `No "${ this . entityName } " entity to upsert` ) ;
137- return this . execute ( 'POST' , this . entityUrl , entityOrError , options ) ;
150+ return this . execute ( 'POST' , this . entityUrl , entityOrError , null , options ) ;
138151 }
139152
140153 protected execute (
@@ -144,9 +157,9 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
144157 options ?: any , // options or undefined/null
145158 httpOptions ?: HttpOptions // these override any options passed via options
146159 ) : Observable < any > {
147- let ngHttpClientOptions : any = undefined ;
160+ let entityActionHttpClientOptions : any = undefined ;
148161 if ( httpOptions ) {
149- ngHttpClientOptions = {
162+ entityActionHttpClientOptions = {
150163 headers : httpOptions ?. httpHeaders
151164 ? new HttpHeaders ( httpOptions ?. httpHeaders )
152165 : undefined ,
@@ -156,24 +169,29 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
156169 } ;
157170 }
158171
172+ // Now we may have:
173+ // options: containing headers, params, or any other allowed http options already in angular's api
174+ // entityActionHttpClientOptions: headers and params in angular's api
175+
176+ // We therefore need to merge these so that the action ones override the
177+ // existing keys where applicable.
178+
159179 // If any options have been specified, pass them to http client. Note
160180 // the new http options, if specified, will override any options passed
161181 // from the deprecated options parameter
162182 let mergedOptions : any = undefined ;
163- if ( options || ngHttpClientOptions ) {
164- if ( isDevMode ( ) && options && ngHttpClientOptions ) {
183+ if ( options || entityActionHttpClientOptions ) {
184+ if ( isDevMode ( ) && options && entityActionHttpClientOptions ) {
165185 console . warn (
166186 '@ngrx/data: options.httpParams will be merged with queryParams when both are are provided to getWithQuery(). In the event of a conflict HttpOptions.httpParams will override queryParams`. The queryParams parameter of getWithQuery() will be removed in next major release.'
167187 ) ;
168188 }
169189
170- mergedOptions = { } ;
171- if ( ngHttpClientOptions ?. headers ) {
172- mergedOptions . headers = ngHttpClientOptions ?. headers ;
173- }
174- if ( ngHttpClientOptions ?. params || options ?. params ) {
175- mergedOptions . params = ngHttpClientOptions ?. params ?? options ?. params ;
176- }
190+ mergedOptions = {
191+ ...options ,
192+ headers : entityActionHttpClientOptions ?. headers ?? options ?. headers ,
193+ params : entityActionHttpClientOptions ?. params ?? options ?. params ,
194+ } ;
177195 }
178196
179197 const req : RequestData = {
@@ -191,7 +209,7 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
191209
192210 switch ( method ) {
193211 case 'DELETE' : {
194- result$ = this . http . delete ( url , ngHttpClientOptions ) ;
212+ result$ = this . http . delete ( url , mergedOptions ) ;
195213 if ( this . saveDelay ) {
196214 result$ = result$ . pipe ( delay ( this . saveDelay ) ) ;
197215 }
@@ -205,15 +223,15 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
205223 break ;
206224 }
207225 case 'POST' : {
208- result$ = this . http . post ( url , data , ngHttpClientOptions ) ;
226+ result$ = this . http . post ( url , data , mergedOptions ) ;
209227 if ( this . saveDelay ) {
210228 result$ = result$ . pipe ( delay ( this . saveDelay ) ) ;
211229 }
212230 break ;
213231 }
214232 // N.B.: It must return an Update<T>
215233 case 'PUT' : {
216- result$ = this . http . put ( url , data , ngHttpClientOptions ) ;
234+ result$ = this . http . put ( url , data , mergedOptions ) ;
217235 if ( this . saveDelay ) {
218236 result$ = result$ . pipe ( delay ( this . saveDelay ) ) ;
219237 }
0 commit comments