-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
Copy pathParseConfig.js
214 lines (192 loc) · 5.52 KB
/
ParseConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
import CoreManager from './CoreManager';
import decode from './decode';
import encode from './encode';
import escape from './escape';
import ParseError from './ParseError';
import Storage from './Storage';
import type { RequestOptions } from './RESTController';
/**
* Parse.Config is a local representation of configuration data that
* can be set from the Parse dashboard.
*
* @alias Parse.Config
*/
class ParseConfig {
attributes: { [key: string]: any };
_escapedAttributes: { [key: string]: any };
constructor() {
this.attributes = {};
this._escapedAttributes = {};
}
/**
* Gets the value of an attribute.
* @param {String} attr The name of an attribute.
*/
get(attr: string): any {
return this.attributes[attr];
}
/**
* Gets the HTML-escaped value of an attribute.
* @param {String} attr The name of an attribute.
*/
escape(attr: string): string {
const html = this._escapedAttributes[attr];
if (html) {
return html;
}
const val = this.attributes[attr];
let escaped = '';
if (val != null) {
escaped = escape(val.toString());
}
this._escapedAttributes[attr] = escaped;
return escaped;
}
/**
* Retrieves the most recently-fetched configuration object, either from
* memory or from local storage if necessary.
*
* @static
* @return {Config} The most recently-fetched Parse.Config if it
* exists, else an empty Parse.Config.
*/
static current() {
const controller = CoreManager.getConfigController();
return controller.current();
}
/**
* Gets a new configuration object from the server.
* @static
* @param {Object} options
* Valid options are:<ul>
* <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
* be used for this request.
* </ul>
* @return {Promise} A promise that is resolved with a newly-created
* configuration object when the get completes.
*/
static get(options: RequestOptions = {}) {
const controller = CoreManager.getConfigController();
return controller.get(options);
}
/**
* Save value keys to the server.
* @static
* @return {Promise} A promise that is resolved with a newly-created
* configuration object or with the current with the update.
*/
static save(attrs: { [key: string]: any }) {
const controller = CoreManager.getConfigController();
//To avoid a mismatch with the local and the cloud config we get a new version
return controller.save(attrs).then(() => {
return controller.get();
},(error) => {
return Promise.reject(error);
});
}
}
let currentConfig = null;
const CURRENT_CONFIG_KEY = 'currentConfig';
function decodePayload(data) {
try {
const json = JSON.parse(data);
if (json && typeof json === 'object') {
return decode(json);
}
} catch(e) {
return null;
}
}
const DefaultController = {
current() {
if (currentConfig) {
return currentConfig;
}
const config = new ParseConfig();
const storagePath = Storage.generatePath(CURRENT_CONFIG_KEY);
let configData;
if (!Storage.async()) {
configData = Storage.getItem(storagePath);
if (configData) {
const attributes = decodePayload(configData);
if (attributes) {
config.attributes = attributes;
currentConfig = config;
}
}
return config;
}
// Return a promise for async storage controllers
return Storage.getItemAsync(storagePath).then((configData) => {
if (configData) {
const attributes = decodePayload(configData);
if (attributes) {
config.attributes = attributes;
currentConfig = config;
}
}
return config;
});
},
get(options: RequestOptions = {}) {
const RESTController = CoreManager.getRESTController();
return RESTController.request(
'GET', 'config', {}, options
).then((response) => {
if (!response || !response.params) {
const error = new ParseError(
ParseError.INVALID_JSON,
'Config JSON response invalid.'
);
return Promise.reject(error);
}
const config = new ParseConfig();
config.attributes = {};
for (const attr in response.params) {
config.attributes[attr] = decode(response.params[attr]);
}
currentConfig = config;
return Storage.setItemAsync(
Storage.generatePath(CURRENT_CONFIG_KEY),
JSON.stringify(response.params)
).then(() => {
return config;
});
});
},
save(attrs: { [key: string]: any }) {
const RESTController = CoreManager.getRESTController();
const encodedAttrs = {};
for(const key in attrs){
encodedAttrs[key] = encode(attrs[key])
}
return RESTController.request(
'PUT',
'config',
{ params: encodedAttrs },
{ useMasterKey: true }
).then(response => {
if(response && response.result){
return Promise.resolve()
} else {
const error = new ParseError(
ParseError.INTERNAL_SERVER_ERROR,
'Error occured updating Config.'
);
return Promise.reject(error)
}
})
}
};
CoreManager.setConfigController(DefaultController);
export default ParseConfig;