1
+ import { utils , DataStore , Mapper } from 'js-data' ;
2
+ import { HttpAdapter } from 'js-data-http' ;
3
+ import { Adapter } from 'js-data-adapter' ;
4
+ import { jsonApiDeserialize , wrapDeserialize } from './deserializer' ;
5
+ import { jsonApiSerialize , wrapSerialize } from './serializer' ;
6
+ import { ERROR } from './strings' ;
7
+
8
+ export class JsonApiAdapter extends HttpAdapter {
9
+ private store : DataStore ;
10
+ private options : any ;
11
+
12
+ constructor ( options ?:any ) {
13
+ options = utils . deepMixIn ( {
14
+ // Some default
15
+ } , options || { } )
16
+
17
+ if ( ! options . store ) {
18
+ throw new Error ( ERROR . FORCE_STORE_OPTION )
19
+ }
20
+
21
+ if ( options . serialize || options . deserialize ) {
22
+ throw new Error ( ERROR . PREVENT_SERIALIZE_DESERIALIZE_OPTIONS )
23
+ }
24
+
25
+ super ( options ) ;
26
+
27
+ this . store = options . store ;
28
+ this . options = options ;
29
+
30
+ this . serialize = wrapSerialize ( this ) ;
31
+ this . deserialize = wrapDeserialize ( this ) ;
32
+ }
33
+
34
+ private warn ( ...args :any [ ] ) {
35
+ console . warn . apply ( null , arguments ) ;
36
+ return ;
37
+ }
38
+
39
+ private handleResponse ( opts ?:any ) { return function ( response :any ) : Promise < any > {
40
+ if ( opts && opts . raw ) {
41
+ response . meta = response . data . meta ;
42
+ response . data = response . data . result ;
43
+ }
44
+
45
+ // @todo #6 need to handle errors here
46
+
47
+ return response ;
48
+ } }
49
+
50
+ private handleBeforeLifecycle ( opts ?:any ) : Promise < void > {
51
+ if ( opts && ( opts . serialize || opts . deserialize ) ) {
52
+ return Promise . reject ( new Error ( ERROR . PREVENT_SERIALIZE_DESERIALIZE_OPTIONS ) )
53
+ }
54
+
55
+ return Promise . resolve ( ) ;
56
+ }
57
+
58
+ public find ( mapper : Mapper , id : string | number , opts ?: any ) : Promise < any > {
59
+ return this . handleBeforeLifecycle ( opts ) . then ( ( ) => {
60
+ return HttpAdapter . prototype . find . call ( this , mapper , id , opts ) ;
61
+ } ) . then ( this . handleResponse ( opts ) ) ;
62
+ }
63
+
64
+ public findAll ( mapper : Mapper , query ?: any , opts ?: any ) : Promise < any > {
65
+ return this . handleBeforeLifecycle ( opts ) . then ( ( ) => {
66
+ return HttpAdapter . prototype . findAll . call ( this , mapper , query , opts ) ;
67
+ } ) . then ( this . handleResponse ( opts ) ) ;
68
+ }
69
+
70
+ public create ( mapper : Mapper , props : any , opts ?: any ) : Promise < any > {
71
+ return this . handleBeforeLifecycle ( opts ) . then ( ( ) => {
72
+ return HttpAdapter . prototype . create . call ( this , mapper , props , opts ) ;
73
+ } ) . then ( this . handleResponse ( opts ) )
74
+ }
75
+
76
+ public createMany ( mapper : Mapper , props : any , opts ?: any ) : Promise < any > {
77
+ return Promise . reject ( new Error ( 'JSONApi doesn\'t support creating in batch.' ) ) ;
78
+ }
79
+
80
+ public update ( mapper : Mapper , id : any , props : any , opts ?: any ) : Promise < any > {
81
+ // Ensure id is properly set
82
+ props [ mapper . idAttribute ] = id ;
83
+
84
+ return this . handleBeforeLifecycle ( opts ) . then ( ( ) => {
85
+ return HttpAdapter . prototype . update . call ( this , mapper , id , props , opts )
86
+ } ) . then ( this . handleResponse ( opts ) )
87
+ }
88
+
89
+ public updateAll ( mapper : Mapper , props : any , query : any , opts ?: any ) : Promise < any > {
90
+ return Promise . reject ( new Error ( 'JSONApi doesn\'t support updating in batch.' ) ) ;
91
+ }
92
+
93
+ public updateMany ( mapper : Mapper , records : any , opts ?: any ) : Promise < any > {
94
+ return Promise . reject ( new Error ( 'JSONApi doesn\'t support updating in batch.' ) ) ;
95
+ }
96
+
97
+ public destroy ( mapper : Mapper , id : string | number , opts ?: any ) : Promise < any > {
98
+ return this . handleBeforeLifecycle ( opts ) . then ( ( ) => {
99
+ return HttpAdapter . prototype . destroy . call ( this , mapper , id , opts ) ;
100
+ } ) . then ( this . handleResponse ( opts ) )
101
+ }
102
+
103
+ public destroyAll ( mapper : Mapper , query : any , opts ?: any ) : Promise < any > {
104
+ return Promise . reject ( new Error ( 'JSONApi doesn\'t support destroying in batch.' ) ) ;
105
+ }
106
+ }
0 commit comments