11
11
12
12
import CoreManager from './CoreManager' ;
13
13
import decode from './decode' ;
14
+ import encode from './encode' ;
14
15
import escape from './escape' ;
15
16
import ParseError from './ParseError' ;
16
17
import Storage from './Storage' ;
@@ -66,7 +67,7 @@ class ParseConfig {
66
67
* exists, else an empty Parse.Config.
67
68
*/
68
69
static current ( ) {
69
- var controller = CoreManager . getConfigController ( ) ;
70
+ const controller = CoreManager . getConfigController ( ) ;
70
71
return controller . current ( ) ;
71
72
}
72
73
@@ -77,9 +78,25 @@ class ParseConfig {
77
78
* configuration object when the get completes.
78
79
*/
79
80
static get ( ) {
80
- var controller = CoreManager . getConfigController ( ) ;
81
+ const controller = CoreManager . getConfigController ( ) ;
81
82
return controller . get ( ) ;
82
83
}
84
+
85
+ /**
86
+ * Save value keys to the server.
87
+ * @static
88
+ * @return {Promise } A promise that is resolved with a newly-created
89
+ * configuration object or with the current with the update.
90
+ */
91
+ static save ( attrs ) {
92
+ const controller = CoreManager . getConfigController ( ) ;
93
+ //To avoid a mismatch with the local and the cloud config we get a new version
94
+ return controller . save ( attrs ) . then ( ( ) => {
95
+ return controller . get ( ) ;
96
+ } , ( error ) => {
97
+ return Promise . reject ( error ) ;
98
+ } ) ;
99
+ }
83
100
}
84
101
85
102
var currentConfig = null ;
@@ -88,7 +105,7 @@ var CURRENT_CONFIG_KEY = 'currentConfig';
88
105
89
106
function decodePayload ( data ) {
90
107
try {
91
- var json = JSON . parse ( data ) ;
108
+ const json = JSON . parse ( data ) ;
92
109
if ( json && typeof json === 'object' ) {
93
110
return decode ( json ) ;
94
111
}
@@ -103,14 +120,14 @@ var DefaultController = {
103
120
return currentConfig ;
104
121
}
105
122
106
- var config = new ParseConfig ( ) ;
107
- var storagePath = Storage . generatePath ( CURRENT_CONFIG_KEY ) ;
108
- var configData ;
123
+ const config = new ParseConfig ( ) ;
124
+ const storagePath = Storage . generatePath ( CURRENT_CONFIG_KEY ) ;
125
+ let configData ;
109
126
if ( ! Storage . async ( ) ) {
110
127
configData = Storage . getItem ( storagePath ) ;
111
128
112
129
if ( configData ) {
113
- var attributes = decodePayload ( configData ) ;
130
+ const attributes = decodePayload ( configData ) ;
114
131
if ( attributes ) {
115
132
config . attributes = attributes ;
116
133
currentConfig = config ;
@@ -121,7 +138,7 @@ var DefaultController = {
121
138
// Return a promise for async storage controllers
122
139
return Storage . getItemAsync ( storagePath ) . then ( ( configData ) => {
123
140
if ( configData ) {
124
- var attributes = decodePayload ( configData ) ;
141
+ const attributes = decodePayload ( configData ) ;
125
142
if ( attributes ) {
126
143
config . attributes = attributes ;
127
144
currentConfig = config ;
@@ -132,22 +149,22 @@ var DefaultController = {
132
149
} ,
133
150
134
151
get ( ) {
135
- var RESTController = CoreManager . getRESTController ( ) ;
152
+ const RESTController = CoreManager . getRESTController ( ) ;
136
153
137
154
return RESTController . request (
138
155
'GET' , 'config' , { } , { }
139
156
) . then ( ( response ) => {
140
157
if ( ! response || ! response . params ) {
141
- var error = new ParseError (
158
+ const error = new ParseError (
142
159
ParseError . INVALID_JSON ,
143
160
'Config JSON response invalid.'
144
161
) ;
145
162
return Promise . reject ( error ) ;
146
163
}
147
164
148
- var config = new ParseConfig ( ) ;
165
+ const config = new ParseConfig ( ) ;
149
166
config . attributes = { } ;
150
- for ( var attr in response . params ) {
167
+ for ( const attr in response . params ) {
151
168
config . attributes [ attr ] = decode ( response . params [ attr ] ) ;
152
169
}
153
170
currentConfig = config ;
@@ -158,6 +175,30 @@ var DefaultController = {
158
175
return config ;
159
176
} ) ;
160
177
} ) ;
178
+ } ,
179
+
180
+ save ( attrs ) {
181
+ var RESTController = CoreManager . getRESTController ( ) ;
182
+ const encodedAttrs = { } ;
183
+ for ( const key in attrs ) {
184
+ encodedAttrs [ key ] = encode ( attrs [ key ] )
185
+ }
186
+ return RESTController . request (
187
+ 'PUT' ,
188
+ 'config' ,
189
+ { params : encodedAttrs } ,
190
+ { useMasterKey : true }
191
+ ) . then ( response => {
192
+ if ( response && response . result ) {
193
+ return Promise . resolve ( )
194
+ } else {
195
+ const error = new ParseError (
196
+ ParseError . INTERNAL_SERVER_ERROR ,
197
+ 'Error occured updating Config.'
198
+ ) ;
199
+ return Promise . reject ( error )
200
+ }
201
+ } )
161
202
}
162
203
} ;
163
204
0 commit comments