Skip to content

Commit 3162d52

Browse files
Moumoulsflovilmart
authored andcommitted
Add save Method for Parse.Config (#684)
* Add Save Method * Add tests and add fix CoreManager tests for the new method * Update to the latest JS standard + new save() strategy + Add save to CoreManager * Remove useMasterKey
1 parent e0c51ab commit 3162d52

File tree

4 files changed

+100
-16
lines changed

4 files changed

+100
-16
lines changed

src/CoreManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ module.exports = {
207207
},
208208

209209
setConfigController(controller: ConfigController) {
210-
requireMethods('ConfigController', ['current', 'get'], controller);
210+
requireMethods('ConfigController', ['current', 'get', 'save'], controller);
211211
config['ConfigController'] = controller;
212212
},
213213

src/ParseConfig.js

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import CoreManager from './CoreManager';
1313
import decode from './decode';
14+
import encode from './encode';
1415
import escape from './escape';
1516
import ParseError from './ParseError';
1617
import Storage from './Storage';
@@ -66,7 +67,7 @@ class ParseConfig {
6667
* exists, else an empty Parse.Config.
6768
*/
6869
static current() {
69-
var controller = CoreManager.getConfigController();
70+
const controller = CoreManager.getConfigController();
7071
return controller.current();
7172
}
7273

@@ -77,9 +78,25 @@ class ParseConfig {
7778
* configuration object when the get completes.
7879
*/
7980
static get() {
80-
var controller = CoreManager.getConfigController();
81+
const controller = CoreManager.getConfigController();
8182
return controller.get();
8283
}
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+
}
83100
}
84101

85102
var currentConfig = null;
@@ -88,7 +105,7 @@ var CURRENT_CONFIG_KEY = 'currentConfig';
88105

89106
function decodePayload(data) {
90107
try {
91-
var json = JSON.parse(data);
108+
const json = JSON.parse(data);
92109
if (json && typeof json === 'object') {
93110
return decode(json);
94111
}
@@ -103,14 +120,14 @@ var DefaultController = {
103120
return currentConfig;
104121
}
105122

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;
109126
if (!Storage.async()) {
110127
configData = Storage.getItem(storagePath);
111128

112129
if (configData) {
113-
var attributes = decodePayload(configData);
130+
const attributes = decodePayload(configData);
114131
if (attributes) {
115132
config.attributes = attributes;
116133
currentConfig = config;
@@ -121,7 +138,7 @@ var DefaultController = {
121138
// Return a promise for async storage controllers
122139
return Storage.getItemAsync(storagePath).then((configData) => {
123140
if (configData) {
124-
var attributes = decodePayload(configData);
141+
const attributes = decodePayload(configData);
125142
if (attributes) {
126143
config.attributes = attributes;
127144
currentConfig = config;
@@ -132,22 +149,22 @@ var DefaultController = {
132149
},
133150

134151
get() {
135-
var RESTController = CoreManager.getRESTController();
152+
const RESTController = CoreManager.getRESTController();
136153

137154
return RESTController.request(
138155
'GET', 'config', {}, {}
139156
).then((response) => {
140157
if (!response || !response.params) {
141-
var error = new ParseError(
158+
const error = new ParseError(
142159
ParseError.INVALID_JSON,
143160
'Config JSON response invalid.'
144161
);
145162
return Promise.reject(error);
146163
}
147164

148-
var config = new ParseConfig();
165+
const config = new ParseConfig();
149166
config.attributes = {};
150-
for (var attr in response.params) {
167+
for (const attr in response.params) {
151168
config.attributes[attr] = decode(response.params[attr]);
152169
}
153170
currentConfig = config;
@@ -158,6 +175,30 @@ var DefaultController = {
158175
return config;
159176
});
160177
});
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+
})
161202
}
162203
};
163204

src/__tests__/CoreManager-test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,16 @@ describe('CoreManager', () => {
8787

8888
expect(CoreManager.setConfigController.bind(null, {
8989
current: function() {},
90-
get: function() {}
90+
get: function() {},
91+
save : function() {}
9192
})).not.toThrow();
9293
});
9394

9495
it('can set and get ConfigController', () => {
9596
const controller = {
9697
current: function() {},
97-
get: function() {}
98+
get: function() {},
99+
save : function() {}
98100
};
99101

100102
CoreManager.setConfigController(controller);

src/__tests__/ParseConfig-test.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,48 @@ describe('ParseConfig', () => {
100100
});
101101
});
102102

103+
it('can save a config object with masterkey', (done) => {
104+
//Load a request that match the get() & save() request
105+
CoreManager.setRESTController({
106+
request() {
107+
return Promise.resolve({
108+
params : {
109+
str : 'hello2',
110+
num : 46
111+
},
112+
result: true
113+
});
114+
},
115+
ajax() {}
116+
});
117+
ParseConfig.save({str: 'hello2','num':46}).then((config) => {
118+
expect(config.get('str')).toBe('hello2');
119+
expect(config.get('num')).toBe(46);
120+
const path = Storage.generatePath('currentConfig');
121+
expect(JSON.parse(Storage.getItem(path))).toEqual({
122+
str: 'hello2',
123+
num: 46
124+
});
125+
done();
126+
});
127+
});
128+
129+
it('rejects save on invalid response', (done) => {
130+
CoreManager.setRESTController({
131+
request() {
132+
return Promise.resolve({result: false});
133+
},
134+
ajax() {}
135+
});
136+
ParseConfig.save({str: 'hello2','num':46}).then((config) => {
137+
expect(config).toBe(1)
138+
done();
139+
},(error) => {
140+
expect(error.code).toBe(1)
141+
done();
142+
});
143+
});
144+
103145
it('rejects the promise when an invalid payload comes back', (done) => {
104146
CoreManager.setRESTController({
105147
request() {
@@ -110,7 +152,6 @@ describe('ParseConfig', () => {
110152
ParseConfig.get().then(null, (error) => {
111153
expect(error.code).toBe(107);
112154
expect(error.message).toBe('Config JSON response invalid.');
113-
114155
done();
115156
});
116157
});

0 commit comments

Comments
 (0)