[API] Fix bug, rename param set -> data (BREAKING)

This commit is contained in:
syuilo 2017-03-19 15:47:36 +09:00
parent 135f0818d6
commit e8e220b410
2 changed files with 15 additions and 15 deletions

View file

@ -17,16 +17,14 @@ import event from '../../../event';
* @return {Promise<any>} * @return {Promise<any>}
*/ */
module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) => { module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) => {
// Get 'set' parameter // Get 'data' parameter
const [set, setError] = $(params.set).optional.object() const [data, dataError] = $(params.data).optional.object()
.pipe(obj => { .pipe(obj => {
return Object.entries(obj).some(kv => { const hasInvalidData = Object.entries(obj).some(([k, v]) =>
const k = kv[0]; $(k).string().match(/^[a-z_]+$/).isNg() && $(v).string().isNg());
const v = kv[1]; return !hasInvalidData;
return $(k).string().match(/[a-z_]+/).isNg() && $(v).string().isNg();
});
}).$; }).$;
if (setError) return rej('invalid set param'); if (dataError) return rej('invalid data param');
// Get 'key' parameter // Get 'key' parameter
const [key, keyError] = $(params.key).optional.string().match(/[a-z_]+/).$; const [key, keyError] = $(params.key).optional.string().match(/[a-z_]+/).$;
@ -36,16 +34,18 @@ module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) =
const [value, valueError] = $(params.value).optional.string().$; const [value, valueError] = $(params.value).optional.string().$;
if (valueError) return rej('invalid value param'); if (valueError) return rej('invalid value param');
let data = {}; let set = {};
if (set) { if (data) {
data = set; Object.entries(data).forEach(([k, v]) => {
set['data.' + k] = v;
});
} else { } else {
data[key] = value; set['data.' + key] = value;
} }
if (isSecure) { if (isSecure) {
const _user = await User.findOneAndUpdate(user._id, { const _user = await User.findOneAndUpdate(user._id, {
$set: { data } $set: set
}); });
res(204); res(204);
@ -63,7 +63,7 @@ module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) =
app_id: app._id, app_id: app._id,
user_id: user._id user_id: user._id
}, { }, {
$set: { data } $set: set
}), { }), {
upsert: true upsert: true
}); });

View file

@ -157,7 +157,7 @@ function fetchme(token, cb) {
function init() { function init() {
const data = generateDefaultUserdata(); const data = generateDefaultUserdata();
api(token, 'i/appdata/set', { api(token, 'i/appdata/set', {
set: data data
}).then(() => { }).then(() => {
me.data = data; me.data = data;
done(); done();