Permalink
Browse files

notFoundAsError and completeScores options

  • Loading branch information...
1 parent 35c3f66 commit ceb4bfc4271bd058dde68ead017ecd9e36c04aaa @brussell98 committed Oct 6, 2016
Showing with 56 additions and 15 deletions.
  1. +24 −2 README.md
  2. +31 −12 lib/Api.js
  3. +1 −1 package.json
View
@@ -1,4 +1,4 @@
-# node-osu
+# node-osu [![Code Climate](https://codeclimate.com/github/brussell98/node-osu/badges/gpa.svg)](https://codeclimate.com/github/brussell98/node-osu)
Making the osu api easy to use.
ES6 because it's awesome :wink:
@@ -50,7 +50,11 @@ All methods return a Promise.
#### Constructor
```js
-var osuApi = new osu.Api('A3tGREAemXk213gfJJUewH9675g')
+var osuApi = new osu.Api('A3tGREAemXk213gfJJUewH9675g', {
+ // baseUrl: sets the base api url (default: https://osu.ppy.sh/api)
+ notFoundAsError: true, // Reject on not found instead of returning nothing. (default: true)
+ completeScores: false // When fetching scores also return the beatmap (default: false)
+})
```
#### apiCall(endpoint, options)
@@ -83,6 +87,12 @@ Returns an array of osu.Score objects.
osuApi.getScores({b: '1036655'}).then(scores => {
console.log(scores[0].score);
});
+
+// or with completeScores set to true
+osuApi.getScores({b: '1036655'}).then((scores, beatmap) => {
+ console.log(scores[0].score);
+ console.log(beatmap.title);
+});
```
#### getUserBest(options)
@@ -91,6 +101,12 @@ Returns an array of osu.Score objects.
osuApi.getUserBest({u: 'brussell98'}).then(scores => {
console.log(scores[0].score);
});
+
+// or with completeScores set to true
+osuApi.getUserBest({u: 'brussell98'}).then(scores => {
+ console.log(scores[0][0].score);
+ console.log(scores[0][1].title);
+});
```
#### getUserRecent(options)
@@ -99,6 +115,12 @@ Returns an array of osu.Score objects.
osuApi.getUserRecent({u: 'brussell98'}).then(scores => {
console.log(scores[0].score);
});
+
+// or with completeScores set to true
+osuApi.getUserRecent({u: 'brussell98'}).then(scores => {
+ console.log(scores[0][0].score);
+ console.log(scores[0][1].title);
+});
```
#### getMatch(options)
View
@@ -9,9 +9,11 @@ const request = require('superagent'),
class Api {
- constructor(apiKey) {
+ constructor(apiKey, options = {}) {
this.apiKey = apiKey;
- this.baseUrl = 'https://osu.ppy.sh/api'
+ this.baseUrl = options.baseUrl || 'https://osu.ppy.sh/api';
+ this.notFoundAsError = options.notFoundAsError === undefined ? true : !!options.notFoundAsError;
+ this.completeScores = !!options.completeScores;
}
apiCall(endpoint, options) {
@@ -35,7 +37,7 @@ class Api {
return new Promise((resolve, reject) => {
this.apiCall('/get_beatmaps', options).then(response => {
if (response.length === 0)
- return reject(new Error('Beatmap not found'));
+ return this.notFoundAsError ? reject(new Error('Beatmap not found')) : resolve(response);
resolve(response.map(bm => new Beatmap(bm)));
}).catch(reject);
});
@@ -45,7 +47,7 @@ class Api {
return new Promise((resolve, reject) => {
this.apiCall('/get_user', options).then(response => {
if (response.length === 0)
- return reject(new Error('User not found'));
+ return this.notFoundAsError ? reject(new Error('User not found')) : resolve(response);
resolve(new User(response[0]));
}).catch(reject);
});
@@ -55,8 +57,13 @@ class Api {
return new Promise((resolve, reject) => {
this.apiCall('/get_scores', options).then(response => {
if (response.length === 0)
- return reject(new Error('No scores'));
- resolve(response.map(sc => new Score(sc)));
+ return this.notFoundAsError ? reject(new Error('No scores')) : resolve(response);
+ if (this.completeScores === false)
+ return resolve(response.map(sc => new Score(sc)));
+
+ this.getBeatmaps({b: options.b}).then(beatmap => {
+ return resolve(response.map(sc => new Score(sc)), beatmap[0]);
+ }).catch(reject);
}).catch(reject);
});
}
@@ -65,8 +72,14 @@ class Api {
return new Promise((resolve, reject) => {
this.apiCall('/get_user_best', options).then(response => {
if (response.length === 0)
- return reject(new Error('No scores'));
- resolve(response.map(sc => new Score(sc)));
+ return this.notFoundAsError ? reject(new Error('No scores')) : resolve(response);
+ if (this.completeScores === false)
+ return resolve(response.map(sc => new Score(sc)));
+
+ let scores = response.map(sc => new Score(sc));
+ Promise.all(scores.map(sc => this.getBeatmaps({b: sc.beatmapId}))).then(beatmaps => {
+ return resolve(scores.map((sc, i) => [sc, beatmaps[i][0]]));
+ }).catch(reject);
}).catch(reject);
});
}
@@ -75,8 +88,14 @@ class Api {
return new Promise((resolve, reject) => {
this.apiCall('/get_user_recent', options).then(response => {
if (response.length === 0)
- return reject(new Error('No scores'));
- resolve(response.map(sc => new Score(sc)));
+ return this.notFoundAsError ? reject(new Error('No scores')) : resolve(response);
+ if (this.completeScores === false)
+ return resolve(response.map(sc => new Score(sc)));
+
+ let scores = response.map(sc => new Score(sc));
+ Promise.all(scores.map(sc => this.getBeatmaps({b: sc.beatmapId}))).then(beatmaps => {
+ return resolve(scores.map((sc, i) => [sc, beatmaps[i][0]]));
+ }).catch(reject);
}).catch(reject);
});
}
@@ -85,9 +104,9 @@ class Api {
return new Promise((resolve, reject) => {
this.apiCall('/get_match', options).then(response => {
if (response.length === 0)
- return reject(new Error('Match not found'));
+ return this.notFoundAsError ? reject(new Error('Match not found')) : resolve(response);
if (response[0].match === 0)
- return reject(new Error('Match too old'));
+ return this.notFoundAsError ? reject(new Error('Match too old')) : resolve(response);
resolve(new Match(response[0]));
}).catch(reject);
});
View
@@ -1,6 +1,6 @@
{
"name": "node-osu",
- "version": "1.0.0",
+ "version": "1.1.0",
"description": "An object-oriented library for easy interaction with the osu api",
"main": "index.js",
"scripts": {

0 comments on commit ceb4bfc

Please sign in to comment.