@@ -8,7 +8,7 @@ import 'rxjs/add/operator/catch';
8
8
import 'rxjs/add/observable/throw' ;
9
9
import { JsonApiModel } from '../models/json-api.model' ;
10
10
11
- export type ModelType = { new ( datastore : JsonApiDatastore , data : any ) : JsonApiModel ; } ;
11
+ export type ModelType < T extends JsonApiModel > = { new ( datastore : JsonApiDatastore , data : any ) : T ; } ;
12
12
13
13
@Injectable ( )
14
14
export class JsonApiDatastore {
@@ -19,28 +19,28 @@ export class JsonApiDatastore {
19
19
constructor ( private http : Http ) {
20
20
}
21
21
22
- query ( modelType : ModelType , params ?: any , headers ?: Headers ) : Observable < JsonApiModel [ ] > {
22
+ query < T extends JsonApiModel > ( modelType : ModelType < T > , params ?: any , headers ?: Headers ) : Observable < T [ ] > {
23
23
let options : RequestOptions = this . getOptions ( headers ) ;
24
24
let url : string = this . buildUrl ( modelType , params ) ;
25
25
return this . http . get ( url , options )
26
26
. map ( ( res : any ) => this . extractQueryData ( res , modelType ) )
27
27
. catch ( ( res : any ) => this . handleError ( res ) ) ;
28
28
}
29
29
30
- findRecord ( modelType : ModelType , id : string , params ?: any , headers ?: Headers ) : Observable < JsonApiModel > {
30
+ findRecord < T extends JsonApiModel > ( modelType : ModelType < T > , id : string , params ?: any , headers ?: Headers ) : Observable < T > {
31
31
let options : RequestOptions = this . getOptions ( headers ) ;
32
32
let url : string = this . buildUrl ( modelType , params , id ) ;
33
33
return this . http . get ( url , options )
34
34
. map ( ( res : any ) => this . extractRecordData ( res , modelType ) )
35
35
. catch ( ( res : any ) => this . handleError ( res ) ) ;
36
36
}
37
37
38
- createRecord ( modelType : ModelType , data ?: any ) : JsonApiModel {
38
+ createRecord < T extends JsonApiModel > ( modelType : ModelType < T > , data ?: any ) : T {
39
39
return new modelType ( this , { attributes : data } ) ;
40
40
}
41
41
42
- saveRecord ( attributesMetadata : any , model ?: any , params ?: any , headers ?: Headers ) : Observable < JsonApiModel > {
43
- let modelType = model . constructor ;
42
+ saveRecord < T extends JsonApiModel > ( attributesMetadata : any , model ?: T , params ?: any , headers ?: Headers ) : Observable < T > {
43
+ let modelType = < ModelType < T > > model . constructor ;
44
44
let typeName : string = Reflect . getMetadata ( 'JsonApiModelConfig' , modelType ) . type ;
45
45
let options : RequestOptions = this . getOptions ( headers ) ;
46
46
let relationships : any = ! model . id ? this . getRelationships ( model ) : undefined ;
@@ -75,12 +75,12 @@ export class JsonApiDatastore {
75
75
. catch ( ( res : any ) => this . handleError ( res ) ) ;
76
76
}
77
77
78
- peekRecord ( modelType : ModelType , id : string ) : JsonApiModel {
78
+ peekRecord < T extends JsonApiModel > ( modelType : ModelType < T > , id : string ) : T {
79
79
let type : string = Reflect . getMetadata ( 'JsonApiModelConfig' , modelType ) . type ;
80
80
return this . _store [ type ] ? this . _store [ type ] [ id ] : null ;
81
81
}
82
82
83
- peekAll ( modelType : ModelType ) : JsonApiModel [ ] {
83
+ peekAll < T extends JsonApiModel > ( modelType : ModelType < T > ) : T [ ] {
84
84
let type = Reflect . getMetadata ( 'JsonApiModelConfig' , modelType ) . type ;
85
85
return _ . values ( < JsonApiModel > this . _store [ type ] ) ;
86
86
}
@@ -89,7 +89,7 @@ export class JsonApiDatastore {
89
89
this . _headers = headers ;
90
90
}
91
91
92
- private buildUrl ( modelType : ModelType , params ?: any , id ?: string ) : string {
92
+ private buildUrl < T extends JsonApiModel > ( modelType : ModelType < T > , params ?: any , id ?: string ) : string {
93
93
let typeName : string = Reflect . getMetadata ( 'JsonApiModelConfig' , modelType ) . type ;
94
94
let baseUrl : string = Reflect . getMetadata ( 'JsonApiDatastoreConfig' , this . constructor ) . baseUrl ;
95
95
let idToken : string = id ? `/${ id } ` : null ;
@@ -115,11 +115,11 @@ export class JsonApiDatastore {
115
115
return relationships ;
116
116
}
117
117
118
- private extractQueryData ( res : any , modelType : ModelType ) : JsonApiModel [ ] {
118
+ private extractQueryData < T extends JsonApiModel > ( res : any , modelType : ModelType < T > ) : T [ ] {
119
119
let body : any = res . json ( ) ;
120
- let models : JsonApiModel [ ] = [ ] ;
120
+ let models : T [ ] = [ ] ;
121
121
body . data . forEach ( ( data : any ) => {
122
- let model : JsonApiModel = new modelType ( this , data ) ;
122
+ let model : T = new modelType ( this , data ) ;
123
123
this . addToStore ( model ) ;
124
124
if ( body . included ) {
125
125
model . syncRelationships ( data , body . included , 0 ) ;
@@ -130,7 +130,7 @@ export class JsonApiDatastore {
130
130
return models ;
131
131
}
132
132
133
- private extractRecordData ( res : any , modelType : ModelType , model ?: JsonApiModel ) : JsonApiModel {
133
+ private extractRecordData < T extends JsonApiModel > ( res : any , modelType : ModelType < T > , model ?: T ) : T {
134
134
let body : any = res . json ( ) ;
135
135
if ( model ) {
136
136
model . id = body . data . id ;
@@ -208,7 +208,7 @@ export class JsonApiDatastore {
208
208
return _ . keyBy ( modelsArray , 'id' ) ;
209
209
}
210
210
211
- private resetMetadataAttributes ( res : any , attributesMetadata : any , modelType : ModelType ) {
211
+ private resetMetadataAttributes < T extends JsonApiModel > ( res : any , attributesMetadata : any , modelType : ModelType < T > ) {
212
212
attributesMetadata = Reflect . getMetadata ( 'Attribute' , res ) ;
213
213
for ( let propertyName in attributesMetadata ) {
214
214
if ( attributesMetadata . hasOwnProperty ( propertyName ) ) {
0 commit comments