the-traveler
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -98,2 +98,3 @@ "use strict"; | ||
MembershipType[MembershipType["PSN"] = 2] = "PSN"; | ||
MembershipType[MembershipType["PC"] = 4] = "PC"; | ||
MembershipType[MembershipType["Bungie"] = 254] = "Bungie"; | ||
@@ -100,0 +101,0 @@ })(MembershipType = exports.MembershipType || (exports.MembershipType = {})); |
@@ -5,2 +5,3 @@ "use strict"; | ||
var rp = require("request-promise-native"); | ||
var querystring = require('querystring'); | ||
/** | ||
@@ -13,7 +14,12 @@ * Entry class for accessing the Destiny 2 API | ||
this.apikey = config.apikey; | ||
this.userAgent = config.userAgent; | ||
this.oauthConfig = { | ||
clientId: config.oauthClientId, | ||
clientSecret: config.oauthClientSecret, | ||
}; | ||
this.apibase = 'https://www.bungie.net/Platform/Destiny2'; | ||
this.assetbase = 'https://www.bungie.net/'; | ||
this.rootURL = 'https://www.bungie.net/'; | ||
this.options = { | ||
headers: { | ||
'User-Agent': config.userAgent, | ||
'User-Agent': this.userAgent, | ||
'X-API-Key': this.apikey, | ||
@@ -53,4 +59,6 @@ }, | ||
* <li>2: PSN</li> | ||
* <li>4: PC (Blizzard)</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* Keep in mind that `-1` or `MembershipType.All` is only applicable on this endpoint. | ||
* @return {Promise.Object} When fulfilled returns an object containing information about the found user | ||
@@ -74,8 +82,5 @@ */ | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint.. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -104,8 +109,5 @@ * @param queryStringParameters An object containing key/value query parameters for this endpoint. Following keys are valid: | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param characterId ID of the character | ||
@@ -155,8 +157,5 @@ * @param destinyMembershipId The Destiny ID (Account ID) | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -186,8 +185,5 @@ * @param itemInstanceId: ID of the Destiny Item | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -217,8 +213,5 @@ * @param characterId ID of the character for whom to get the vendor info | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -266,3 +259,3 @@ * @param characterId ID of the character for whom to get the vendor info | ||
/** | ||
* Get historical stats definitions. This contains the values for the `statId` key. | ||
* Get historical stats definitions. This contains the values for the `<br` key. | ||
* @async | ||
@@ -293,4 +286,4 @@ */ | ||
* <li>maxtop {number}: Maximum number of top players to return. Use a large number to get entire leaderboard | ||
* <li>statid {string}: ID of stat to return rather than returning all Leaderboard stats. <br /> | ||
* {@link https://alexanderwe.github.io/the-traveler/enums/statid.html|StatIDs} for available ids</li> | ||
* <li><statId {string}: ID of stat to return rather than returning all Leaderboard stats. <br /> | ||
* {@link https://alexanderwe.github.io/the-traveler/enums/statid.html|StatIds} for available ids</li> | ||
* </ul> | ||
@@ -339,8 +332,5 @@ * @return {Promise.object} When fulfilled returns an object containing leaderboards for a clan | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -353,4 +343,4 @@ * @param queryStringParameters An object containing key/value query parameters for this endpoint. Following keys are valid: | ||
* <li>maxtop {number}: Maximum number of top players to return. Use a large number to get entire leaderboard | ||
* <li>statid {string}: ID of stat to return rather than returning all Leaderboard stats. <br /> | ||
* See {@link https://github.com/alexanderwe/the-traveler/blob/master/docs/globals.html|StatIDs} for available ids</li> | ||
* <li><statId {string}: ID of stat to return rather than returning all Leaderboard stats. <br /> | ||
* See {@link https://alexanderwe.github.io/the-traveler/enums/statid.html|StatIds} for available ids</li> | ||
* </ul> | ||
@@ -375,8 +365,5 @@ * @return {Promise.object} When fulfilled returns an object containing the leaderboard | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -390,4 +377,4 @@ * @param characterId ID of the character | ||
* <li>maxtop {number}: Maximum number of top players to return. Use a large number to get entire leaderboard | ||
* <li>statid {string}: ID of stat to return rather than returning all Leaderboard stats. <br /> | ||
* See {@link https://github.com/alexanderwe/the-traveler/blob/master/docs/globals.html|StatIDs} for available ids</li> | ||
* <li><statId {string}: ID of stat to return rather than returning all Leaderboard stats. <br /> | ||
* See {@link https://github.com/alexanderwe/the-traveler/blob/master/docs/globals.html|<brs} for available ids</li> | ||
* </ul> | ||
@@ -435,8 +422,5 @@ * @return {Promise.object} When fulfilled returns an object containing the leaderboard | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -476,8 +460,5 @@ * @param characterId ID of the character | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -507,8 +488,5 @@ * @param queryStringParameters An object containing key/value query parameters for this endpoint. Following keys are valid: | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -542,8 +520,5 @@ * @param characterId ID of the character | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -569,8 +544,5 @@ * @param characterId ID of the character | ||
* @async | ||
* @param membershipType A valid non-BungieNet membership type, or All <ul> | ||
* <li>-1: ALL</li> | ||
* <li>1: Xbox</li> | ||
* <li>2: PSN</li> | ||
* <li>254: Bungie</li> | ||
* </ul> | ||
* @param membershipType A valid non-BungieNet membership type. It has to match the type which the `destinyMembershipId` is belonging to. <br /> | ||
* Keep in mind that `-1 / MembershipType.All` is <strong> not applicable here </strong> <br/> | ||
* Ex: If the `destinyMembershipId` is a PSN account then use `'2'` or `MembershipType.PSN` for this endpoint. | ||
* @param destinyMembershipId The Destiny ID (Account ID) | ||
@@ -630,2 +602,93 @@ * @param characterId ID of the character | ||
/** | ||
* Generates the OAuthURL where your users need to sign up to give your application access to | ||
* authorized endpoints. | ||
*/ | ||
Traveler.prototype.generateOAuthURL = function () { | ||
if (this.oauthConfig.clientId !== undefined) { | ||
return "https://www.bungie.net/en/OAuth/Authorize?client_id=" + this.oauthConfig.clientId + "&response_type=code"; | ||
} | ||
else { | ||
throw new Error('You did not specify a oauth client id'); | ||
} | ||
}; | ||
/** | ||
* Retreive the Oauth access token from the authorization code | ||
* @param code The authorization code from the oauth redirect url | ||
*/ | ||
Traveler.prototype.getAccessToken = function (code) { | ||
var _this = this; | ||
var options; | ||
if (this.oauthConfig.clientSecret) { | ||
options = { | ||
body: querystring.stringify({ | ||
client_id: this.oauthConfig.clientId, | ||
code: "" + code, | ||
grant_type: 'authorization_code', | ||
}), | ||
headers: { | ||
'authorization': "Basic " + new Buffer(this.oauthConfig.clientId + ":" + this.oauthConfig.clientSecret).toString('base64'), | ||
'content-type': 'application/x-www-form-urlencoded', | ||
}, | ||
json: true, | ||
method: 'POST', | ||
uri: 'https://www.bungie.net/platform/app/oauth/token/', | ||
}; | ||
} | ||
else { | ||
options = { | ||
body: querystring.stringify({ | ||
client_id: this.oauthConfig.clientId, | ||
code: "" + code, | ||
grant_type: 'authorization_code', | ||
}), | ||
headers: { | ||
'content-type': 'application/x-www-form-urlencoded', | ||
}, | ||
json: true, | ||
method: 'POST', | ||
uri: 'https://www.bungie.net/platform/app/oauth/token/', | ||
}; | ||
} | ||
return new Promise(function (resolve, reject) { | ||
_this.makeRequest(options) | ||
.then(function (response) { | ||
resolve(response); | ||
}) | ||
.catch(function (err) { | ||
reject(err); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Use the refreshToken to retrieve a new valid access_token. | ||
* Please keep the expiration durations in mind. | ||
* <strong>This is only possible with a confidential app, as only this will get a refresh token to use</strong> | ||
* @param refreshToken | ||
*/ | ||
Traveler.prototype.refreshToken = function (refreshToken) { | ||
var _this = this; | ||
var options = { | ||
body: querystring.stringify({ | ||
grant_type: 'refresh_token', | ||
refresh_token: "" + refreshToken, | ||
}), | ||
headers: { | ||
'authorization': "Basic " + new Buffer(this.oauthConfig.clientId + ":" + this.oauthConfig.clientSecret).toString('base64'), | ||
'content-type': 'application/x-www-form-urlencoded', | ||
}, | ||
json: true, | ||
method: 'POST', | ||
uri: 'https://www.bungie.net/platform/app/oauth/token/', | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
_this.makeRequest(options) | ||
.then(function (response) { | ||
resolve(response); | ||
}) | ||
.catch(function (err) { | ||
reject(err); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Base function for making the request to the API endpoint. We need this, because we can do error validation, or agregated functions here. | ||
@@ -637,3 +700,2 @@ * @async | ||
Traveler.prototype.makeRequest = function (options) { | ||
var _this = this; | ||
if (this.debug) { | ||
@@ -643,5 +705,8 @@ console.log('\x1b[33m%s\x1b[0m', 'Debug url:' + options.uri); | ||
return new Promise(function (resolve, reject) { | ||
rp(_this.options) | ||
rp(options) | ||
.then(function (response) { | ||
if (response.ErrorCode !== 1) { | ||
if (response.access_token) { | ||
resolve(response); | ||
} | ||
else if (response.ErrorCode !== 1) { | ||
reject(response); | ||
@@ -648,0 +713,0 @@ } |
{ | ||
"name": "the-traveler", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "A Node.js API wrapper for the Destiny 2 API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,2 +7,3 @@ Table of Contents | ||
* [Prerequisites](#prerequisites) | ||
* [OAuth](#oauth) | ||
* [Notices](#notices) | ||
@@ -44,7 +45,7 @@ * [Typical Response](#typical-response) | ||
import Traveler from 'the-traveler'; | ||
import { ComponentType, anyOtherEnum} from 'the-traveler/build/enums'; | ||
import { ComponentType } from 'the-traveler/build/enums'; | ||
const traveler = new Traveler({ | ||
apikey: 'pasteYourAPIkey', | ||
userAgent: 'yourUserAgent' //used to identify your request to the API | ||
userAgent: 'yourUserAgent', //used to identify your request to the API | ||
}); | ||
@@ -78,3 +79,84 @@ ``` | ||
## OAuth | ||
If you want to use OAuth to also get access to endpoints which require authentications, provide the `Traveler` object with the needed OAuth `clientId` and if you are using a confidential client type the `clientSecret`. | ||
``` | ||
import Traveler from 'the-traveler'; | ||
const traveler = new Traveler({ | ||
apikey: 'pasteYourAPIkey', | ||
userAgent: 'yourUserAgent', //used to identify your request to the API | ||
oauthClientId: 'yourClientId', | ||
oauthClientSecret: 'yourClientSecret', | ||
}); | ||
``` | ||
Also ensure that you specify an `redirectURL` in your Application settings on [https://www.bungie.net/en/Application](https://www.bungie.net/en/Application) | ||
After that you can generate the authentication URL which has to be visited by your users to approve your app and give it access. This follows the following schema: `https://www.bungie.net/en/OAuth/Authorize?client_id={yourClientID}&response_type=code`. | ||
``` | ||
const authUrl = traveler.traveler.generateOAuthURL(); // The URL your users have to visit to give your application access | ||
``` | ||
If the users visit this site and approve your application they will be redirected to the `redirectURL` you specified, with a URL query parameter `code` appended: `https://www.example.com/?code=hereComesTheCode` | ||
This is the code you need to get the OAuth Access token. Use it with the `getAccessToken()` | ||
``` | ||
traveler.getAccessToken(hereComesTheCode).then(oauth => { | ||
// Provide your traveler object with the oauth object. This is later used for making authenticated calls | ||
traveler.oauth = oauth; | ||
}).catch(err => { | ||
console.log(err) | ||
}) | ||
``` | ||
The oauth response schema depends on if you are using a `public` or `confidential`client type. With a `public` type the response does **not** contain an `refresh_token`. This means that a user has to authenticate everytime again after the OAuth access token has expired. Such an response looks like this: | ||
_Response_: | ||
``` | ||
{ access_token: '', | ||
token_type: 'Bearer', | ||
expires_in: 3600, | ||
membership_id: ''} | ||
``` | ||
If you are using a `confidential` client type the response will contain an `refresh_token` which can be used to get a new `access_token` without requiring the user to approve your app again. Use this `refresh_token` to prevent getting errors because the `access_token` has expired. In the following you can see such a response with the method to renew the token. | ||
_Response_: | ||
``` | ||
{ access_token: '', | ||
token_type: 'Bearer', | ||
expires_in: 3600, | ||
refresh_token: ', | ||
refresh_expires_in: 7776000, | ||
membership_id: '' } | ||
``` | ||
_Use refresh_: | ||
``` | ||
traveler.refreshToken(traveler.oauth.refresh_token).then(oauth => { | ||
// Provide your traveler object with the oauth object. This is later used for making authenticated calls | ||
traveler.oauth = oauth; | ||
}).catch(err => { | ||
console.log(err) | ||
}) | ||
``` | ||
To wrap this up, the flow is the following: | ||
* Provide `clientId` and `clientSecret (only for confidential)` | ||
* Generate `authUrl` and redirect users to it | ||
* Grab the `code` parameter from your `redirectURL` | ||
* Use `code` to get the `oauth Object` and apply it to the `traveler object` | ||
* **FOR Public** Reauthenticate your users after the access token has expired, so they have to visit the `authUrl`again | ||
* **FOR Confidential** Use `traveler.oauth.refreshtoken` to renew the `accessToken`, without user interaction | ||
* After the oauth object is set on the traveler object you can query the endpoints which require authentiation | ||
* Keep in mind that it would be very useful to store the tokens for your users _**securely**_! | ||
## Notices | ||
@@ -176,4 +258,2 @@ | ||
``` | ||
@@ -180,0 +260,0 @@ _Response (First level):_ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75476
1078
336