@@ -9,32 +9,33 @@ import { JsonApiModel } from '../models/json-api.model';
99export class JsonApiDatastore {
1010
1111 private http : Http ;
12+ private _headers : Headers ;
1213
1314 constructor ( ) {
1415 let injector = ReflectiveInjector . resolveAndCreate ( [ HTTP_PROVIDERS ] ) ;
1516 this . http = injector . get ( Http ) ;
1617 }
1718
18- query ( type : { new ( data : any ) : JsonApiModel ; } , params ?: any ) : Observable < JsonApiModel [ ] > {
19- let options = this . getOptions ( ) ;
19+ query ( type : { new ( data : any ) : JsonApiModel ; } , params ?: any , headers ?: Headers ) : Observable < JsonApiModel [ ] > {
20+ let options = this . getOptions ( headers ) ;
2021 let url = this . buildUrl ( type , params ) ;
2122 return this . http . get ( url , options )
2223 . map ( ( res : any ) => this . extractQueryData ( res , type ) )
2324 . catch ( ( res : any ) => this . handleError ( res ) ) ;
2425 }
2526
26- findRecord ( type : { new ( data : any ) : JsonApiModel ; } , id : number , params ?: any ) : Observable < JsonApiModel > {
27- let options = this . getOptions ( ) ;
27+ findRecord ( type : { new ( data : any ) : JsonApiModel ; } , id : number , params ?: any , headers ?: Headers ) : Observable < JsonApiModel > {
28+ let options = this . getOptions ( headers ) ;
2829 let url = this . buildUrl ( type , params , id ) ;
2930 return this . http . get ( url , options )
3031 . map ( ( res : any ) => this . extractRecordData ( res , type ) )
3132 . catch ( ( res : any ) => this . handleError ( res ) ) ;
3233 }
3334
34- createRecord ( type : { new ( data : any ) : JsonApiModel ; } , data ?: any ) {
35+ createRecord ( type : { new ( data : any ) : JsonApiModel ; } , data ?: any , headers ?: Headers ) {
3536 let typeName = Reflect . getMetadata ( 'JsonApiModelConfig' , type ) . type ;
3637 let baseUrl = Reflect . getMetadata ( 'JsonApiDatastoreConfig' , this . constructor ) . baseUrl ;
37- let options = this . getOptions ( ) ;
38+ let options = this . getOptions ( headers ) ;
3839 let relationships : any ;
3940 for ( let key in data ) {
4041 if ( data . hasOwnProperty ( key ) ) {
@@ -62,6 +63,10 @@ export class JsonApiDatastore {
6263 . catch ( ( res : any ) => this . handleError ( res ) )
6364 }
6465
66+ set headers ( headers : Headers ) {
67+ this . _headers = headers ;
68+ }
69+
6570 private buildUrl ( type : { new ( data : any ) : JsonApiModel ; } , params : any = { } , id ?: number ) {
6671 let typeName = Reflect . getMetadata ( 'JsonApiModelConfig' , type ) . type ;
6772 if ( params . include && typeof params . include === 'function' ) {
@@ -101,8 +106,15 @@ export class JsonApiDatastore {
101106 return Observable . throw ( errMsg ) ;
102107 }
103108
104- private getOptions ( ) {
105- let headers = new Headers ( { 'Accept' : 'application/vnd.api+json' , 'Content-Type' : 'application/vnd.api+json' } ) ;
109+ private getOptions ( customHeaders ?: Headers ) {
110+ let headers : Headers = this . _headers ? this . _headers : new Headers ( ) ;
111+ headers . append ( 'Accept' , 'application/vnd.api+json' ) ;
112+ headers . append ( 'Content-Type' , 'application/vnd.api+json' ) ;
113+ if ( customHeaders ) {
114+ customHeaders . forEach ( function ( values , name ) {
115+ headers . append ( name , values [ 0 ] ) ;
116+ } ) ;
117+ }
106118 return new RequestOptions ( { headers : headers } ) ;
107119 }
108120
0 commit comments