Skip to content

Commit a2cce49

Browse files
committed
feat(leaderboard): add options.overwrite for #updateStatistics
1 parent 190aaf4 commit a2cce49

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

src/leaderboard.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,25 @@ AV.Leaderboard.getStatistics = (user, { statisticNames } = {}, authOptions) =>
174174
* Update Statistics for the specified user.
175175
* @param {AV.User} user The specified AV.User pointer.
176176
* @param {Object} statistics A name-value pair representing the statistics to update.
177-
* @param {AuthOptions} [authOptions]
177+
* @param {AuthOptions} [options]
178178
* @return {Promise<Statistic[]>}
179179
*/
180-
AV.Leaderboard.updateStatistics = (user, statistics, authOptions) =>
180+
AV.Leaderboard.updateStatistics = (user, statistics, options = {}) =>
181181
Promise.resolve().then(() => {
182182
if (!(user && user.id)) throw new Error('user must be an AV.User');
183183
const data = _.map(statistics, (value, key) => ({
184184
statisticName: key,
185185
statisticValue: value,
186186
}));
187+
const { overwrite } = options;
187188
return request({
188189
method: 'POST',
189190
path: `/leaderboard/users/${user.id}/statistics`,
191+
query: {
192+
overwrite: overwrite ? 1 : undefined,
193+
},
190194
data,
191-
authOptions,
195+
authOptions: options,
192196
}).then(({ results }) => results.map(parseStatisticData));
193197
});
194198

src/utils/index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,30 @@ const ajax = ({
1616
onprogress,
1717
timeout,
1818
}) => {
19-
const count = requestsCount++;
19+
const flattenedQuery = {};
20+
if (query) {
21+
for (const k in query) {
22+
const value = query[k];
23+
if (value === undefined) continue;
24+
if (typeof value === 'object') {
25+
flattenedQuery[k] = JSON.stringify(value);
26+
} else {
27+
flattenedQuery[k] = value;
28+
}
29+
}
30+
}
2031

32+
const count = requestsCount++;
2133
debugRequest(
2234
'request(%d) %s %s %o %o %o',
2335
count,
2436
method,
2537
url,
26-
query,
38+
flattenedQuery,
2739
data,
2840
headers
2941
);
3042

31-
const flattenedQuery = {};
32-
if (query) {
33-
for (const k in query) {
34-
if (typeof query[k] === 'object') {
35-
flattenedQuery[k] = JSON.stringify(query[k]);
36-
} else {
37-
flattenedQuery[k] = query[k];
38-
}
39-
}
40-
}
41-
4243
return new Promise((resolve, reject) => {
4344
const req = request(method, url)
4445
.set(headers)

storage.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,10 @@ declare interface Ranking {
838838
includedStatistics?: Statistic[];
839839
}
840840

841+
declare interface UpdateStatisticsOptions extends AuthOptions {
842+
overwrite?: boolean;
843+
}
844+
841845
export class Leaderboard {
842846
statisticName: string;
843847
order?: LeaderboardOrder;
@@ -868,7 +872,7 @@ export class Leaderboard {
868872
static updateStatistics(
869873
user: User,
870874
statistics: { [name: string]: number },
871-
authOptions?: AuthOptions
875+
options?: UpdateStatisticsOptions
872876
): Promise<Statistic[]>;
873877

874878
fetch(authOptions?: AuthOptions): Promise<Leaderboard>;

0 commit comments

Comments
 (0)