File tree Expand file tree Collapse file tree 6 files changed +108
-11
lines changed Expand file tree Collapse file tree 6 files changed +108
-11
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import Config from './configuration';
5
5
import Attribute from './attribute' ;
6
6
import deserialize from './util/deserialize' ;
7
7
import CollectionProxy from './collection-proxy' ;
8
+ import RecordProxy from './record-proxy' ;
8
9
import _extend from './util/extend' ;
9
10
import { camelize } from './util/string' ;
10
11
@@ -46,7 +47,7 @@ export default class Model {
46
47
return this . scope ( ) . all ( ) ;
47
48
}
48
49
49
- static find ( id : string | number ) : Promise < Model > {
50
+ static find ( id : string | number ) : Promise < RecordProxy < Model > > {
50
51
return this . scope ( ) . find ( id ) ;
51
52
}
52
53
Original file line number Diff line number Diff line change
1
+ import Model from './model' ;
2
+
3
+ class RecordProxy < T > {
4
+ private _raw_json : japiDoc ;
5
+ private _model : T ;
6
+
7
+ constructor ( raw_json : japiDoc = { data : [ ] } ) {
8
+ this . setRaw ( raw_json ) ;
9
+ }
10
+
11
+ get raw ( ) : japiDoc {
12
+ return this . _raw_json ;
13
+ }
14
+
15
+ get data ( ) : T {
16
+ return this . _model ;
17
+ }
18
+
19
+ get meta ( ) : Object {
20
+ return this . raw . meta || { } ;
21
+ }
22
+
23
+ private setRaw = ( json_payload : japiDoc ) => {
24
+ this . _raw_json = json_payload ;
25
+
26
+
27
+ if ( this . raw . data ) {
28
+ this . _model = Model . fromJsonapi ( this . raw . data , this . raw ) ;
29
+ }
30
+ }
31
+ }
32
+
33
+ export default RecordProxy ;
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import Config from './configuration';
3
3
import parameterize from './util/parameterize' ;
4
4
import IncludeDirective from './util/include-directive' ;
5
5
import CollectionProxy from './collection-proxy' ;
6
+ import RecordProxy from './record-proxy' ;
6
7
import Request from './request' ;
7
8
import colorize from './util/colorize' ;
8
9
@@ -28,9 +29,9 @@ export default class Scope {
28
29
} ) ;
29
30
}
30
31
31
- find ( id : string | number ) : Promise < Model > {
32
+ find ( id : string | number ) : Promise < RecordProxy < Model > > {
32
33
return this . _fetch ( this . model . url ( id ) ) . then ( ( json : japiDoc ) => {
33
- return Model . fromJsonapi ( json . data , json ) ;
34
+ return new RecordProxy ( json )
34
35
} ) ;
35
36
}
36
37
Original file line number Diff line number Diff line change @@ -8,8 +8,8 @@ after(function () {
8
8
} ) ;
9
9
10
10
let resultData = function ( promise ) {
11
- return promise . then ( function ( collection ) {
12
- return collection . data
11
+ return promise . then ( function ( proxyObject ) {
12
+ return proxyObject . data
13
13
} )
14
14
}
15
15
@@ -28,13 +28,13 @@ describe('Model finders', function() {
28
28
} ) ;
29
29
30
30
it ( 'returns a promise that resolves the correct instance' , function ( ) {
31
- return expect ( Person . find ( 1 ) ) . to . eventually
31
+ return expect ( resultData ( Person . find ( 1 ) ) ) . to . eventually
32
32
. be . instanceof ( Person ) . and
33
33
. have . property ( 'id' , '1' ) ;
34
34
} ) ;
35
35
36
36
it ( 'assigns attributes correctly' , function ( ) {
37
- return expect ( Person . find ( 1 ) ) . to . eventually
37
+ return expect ( resultData ( Person . find ( 1 ) ) ) . to . eventually
38
38
. have . property ( 'name' , 'John' )
39
39
} ) ;
40
40
@@ -50,7 +50,7 @@ describe('Model finders', function() {
50
50
} ) ;
51
51
52
52
it ( 'resolves to the correct class' , function ( ) {
53
- return expect ( Person . find ( 1 ) ) . to . eventually
53
+ return expect ( resultData ( Person . find ( 1 ) ) ) . to . eventually
54
54
. be . instanceof ( Author ) ;
55
55
} ) ;
56
56
} ) ;
Original file line number Diff line number Diff line change 1
- import { sinon } from '../../test/test-helper' ;
2
1
import { Person } from '../fixtures' ;
3
2
4
3
import CollectionProxy from '../../src/collection-proxy'
@@ -15,7 +14,6 @@ describe('CollectionProxy', function() {
15
14
} ,
16
15
}
17
16
] ,
18
- included : [ ] ,
19
17
meta : {
20
18
stats : {
21
19
total : {
@@ -43,7 +41,7 @@ describe('CollectionProxy', function() {
43
41
describe ( '#meta' , function ( ) {
44
42
it ( 'should get meta field from raw response' , function ( ) {
45
43
let collection = new CollectionProxy ( personData )
46
- expect ( collection . meta ) . to . eq ( personData . meta )
44
+ expect ( collection . meta ) . to . deep . eq ( personData . meta )
47
45
} )
48
46
49
47
describe ( 'meta is null' , function ( ) {
Original file line number Diff line number Diff line change
1
+ import { Person } from '../fixtures' ;
2
+
3
+ import RecordProxy from '../../src/record-proxy'
4
+
5
+ describe ( 'RecordProxy' , function ( ) {
6
+ let personData = {
7
+ data : {
8
+ id : '1' ,
9
+ type : 'people' ,
10
+ attributes : {
11
+ firstName : 'Donald' ,
12
+ lastName : 'Budge'
13
+ } ,
14
+ } ,
15
+ meta : {
16
+ stats : {
17
+ total : {
18
+ count : 3
19
+ } ,
20
+ average : {
21
+ salary : "$100k"
22
+ }
23
+ }
24
+ }
25
+ }
26
+
27
+ describe ( 'initialization' , function ( ) {
28
+ it ( 'should assign the response correctly' , function ( ) {
29
+ let record = new RecordProxy ( personData )
30
+ expect ( record . raw ) . to . deep . equal ( personData )
31
+ } )
32
+
33
+ it ( 'should assign the correct models to the data array' , function ( ) {
34
+ let record = new RecordProxy ( personData )
35
+ expect ( record . data ) . to . be . instanceof ( Person )
36
+ } )
37
+ } )
38
+
39
+ describe ( '#meta' , function ( ) {
40
+ it ( 'should get meta field from raw response' , function ( ) {
41
+ let record = new RecordProxy ( personData )
42
+ expect ( record . meta ) . to . deep . eq ( personData . meta )
43
+ } )
44
+
45
+ describe ( 'meta is null' , function ( ) {
46
+ let personData = {
47
+ data : {
48
+ id : '1' ,
49
+ type : 'people' ,
50
+ attributes : {
51
+ firstName : 'Donald' ,
52
+ lastName : 'Budge'
53
+ } ,
54
+ } ,
55
+ }
56
+
57
+ it ( 'should return an empty object' , function ( ) {
58
+ let record = new RecordProxy ( personData )
59
+ expect ( record . meta ) . to . deep . eq ( { } )
60
+ } )
61
+ } )
62
+ } )
63
+ } )
64
+
You can’t perform that action at this time.
0 commit comments