destream-api
Advanced tools
Comparing version 1.0.2 to 1.0.3
42
index.js
@@ -15,2 +15,10 @@ const fetch = require('node-fetch') | ||
static UserExistsException(api_response) { | ||
this.apiResponse = api_response | ||
} | ||
static AccessTokenIncorrect(api_response) { | ||
this.apiResponse = api_response | ||
} | ||
_getSignature() { | ||
@@ -34,3 +42,3 @@ return sha512(this._clientId+getISO8601Date()+this._clientSecret) | ||
let parsedAPIResponse = await response.json() | ||
return { ...parsedAPIResponse } | ||
return { ...parsedAPIResponse, http_status: response.status } | ||
} | ||
@@ -52,3 +60,3 @@ | ||
let parsedAPIResponse = await response.json() | ||
return { ...parsedAPIResponse } | ||
return { ...parsedAPIResponse, http_status: response.status } | ||
} | ||
@@ -62,4 +70,2 @@ | ||
'X-Api-ClientId': this._clientId, | ||
'X-Api-RequestDate': getISO8601Date(), | ||
'X-Api-Signature': this._getSignature(), | ||
'Authorization': `${token_type} ${access_token}` | ||
@@ -69,3 +75,4 @@ } | ||
let parsedAPIResponse = await response.json() | ||
return { ...parsedAPIResponse } | ||
if(response.status === 401){ throw DeStreamAPI.AccessTokenIncorrect(parsedAPIResponse) } | ||
return { ...parsedAPIResponse, http_status: response.status } | ||
} | ||
@@ -87,4 +94,9 @@ | ||
}) | ||
let parsedAPIResponse = await response.json() | ||
return { ...parsedAPIResponse, status: response.status } | ||
const HTTP_STATUS_USER_EXISTS = 409 | ||
if(response.status === HTTP_STATUS_USER_EXISTS){ throw new DeStreamAPI.UserExistsException(parsedAPIResponse) } | ||
return { ...parsedAPIResponse, http_status: response.status } | ||
} | ||
@@ -103,4 +115,2 @@ | ||
'X-Api-ClientId': this._clientId, | ||
'X-Api-RequestDate': getISO8601Date(), | ||
'X-Api-Signature': this._getSignature(), | ||
'Authorization': `${tokens.token_type} ${tokens.access_token}` | ||
@@ -110,2 +120,3 @@ } | ||
let parsedAPIResponse = await response.json() | ||
if(response.status === 401){ throw DeStreamAPI.AccessTokenIncorrect(parsedAPIResponse) } | ||
if(response.status === 200){ | ||
@@ -115,3 +126,3 @@ parsedAPIResponse.next = () => this.getTips(offset+limit, limit, sinceDate) | ||
} | ||
return { ...parsedAPIResponse } | ||
return { ...parsedAPIResponse, http_status: response.status } | ||
} | ||
@@ -137,7 +148,8 @@ | ||
let parsedAPIResponse = await response.json() | ||
if(response.status === 401){ throw DeStreamAPI.AccessTokenIncorrect(parsedAPIResponse) } | ||
if(response.status === 200){ | ||
parsedAPIResponse.next = () => this.getTips(offset+limit, limit, sinceDate) | ||
parsedAPIResponse.prev = () => this.getTips(Math.max(0, offset-limit), limit, sinceDate) | ||
parsedAPIResponse.next = () => this.getInvoicesPayments(offset+limit, limit, sinceDate) | ||
parsedAPIResponse.prev = () => this.getInvoicesPayments(Math.max(0, offset-limit), limit, sinceDate) | ||
} | ||
return { ...parsedAPIResponse } | ||
return { ...parsedAPIResponse, http_status: response.status } | ||
} | ||
@@ -176,4 +188,8 @@ | ||
let parsedAPIResponse = await response.json() | ||
return { ...parsedAPIResponse } | ||
return { ...parsedAPIResponse, http_status: response.status } | ||
} | ||
validateSignature(body, receivedSignature) { | ||
return sha512(body+this._clientSecret) === receivedSignature | ||
} | ||
} | ||
@@ -180,0 +196,0 @@ |
{ | ||
"name": "destream-api", | ||
"version": "1.0.2", | ||
"description": "Destream API wrapper for NodeJS with ES6 promises support [BETA]", | ||
"version": "1.0.3", | ||
"description": "Full featured DeStream API NodeJS wrapper [BETA]", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
126
README.md
@@ -1,2 +0,2 @@ | ||
# DeStream-API — Full featured DeStream API NodeJS wrapper | ||
# DeStream-API — Full featured DeStream API NodeJS wrapper [BETA] | ||
@@ -12,2 +12,77 @@ destream-api currently supports: | ||
## Code with/without using destream-api library | ||
### Refresh Access Token with hardcoded refresh token | ||
```javascript | ||
/* Without using destream-api */ | ||
const fetch = require('node-fetch') | ||
const formurlencoded = require('form-urlencoded') | ||
let params = formurlencoded({ | ||
grant_type: 'refresh_token', | ||
client_id: 12345, | ||
client_secret: 'secret-secret', | ||
scope: 'profile+tips', | ||
refresh_token: 'refresh_token' | ||
}) | ||
fetch(`https://destream.net/api/v2/oauth2/token?${params}`, { | ||
method: 'POST', | ||
headers: {'Content-Type': 'application/x-www-form-urlencoded'} | ||
}) | ||
``` | ||
```javascript | ||
/* Using destream-api */ | ||
const DeStreamAPI = require('destream-api') | ||
let destream = new DeStreamAPI({clientId: '12345', clientSecret: 'secret-secret'}) | ||
destream.refreshAccessToken('profile+tips', 'refresh_token' }) | ||
``` | ||
### Getting an array of all tips | ||
```javascript | ||
/* Without using destream-api */ | ||
const fetch = require('node-fetch') | ||
const limit = 10; let tipsArray = [], offset = 0; | ||
while(true){ | ||
fetch(`https://destream.net/api/v2/users/tips?offset=${offset}`, { | ||
method: 'GET', | ||
headers: { | ||
'X-Api-ClientId': '12345', | ||
'Authorization': `access_token jahs62d123` | ||
} | ||
}).then(result => result.json()).then(result => { | ||
if(result.data.length === 0){ | ||
break; | ||
} else { | ||
tipsArray.push(...result.data) | ||
} | ||
offset += limit | ||
}) | ||
} | ||
``` | ||
```javascript | ||
/* Using destream-api */ | ||
const DeStreamAPI = require('destream-api') | ||
let destream = new DeStreamAPI({clientId: '12345', clientSecret: 'secret-secret'}) | ||
let tipsArray = [] | ||
let tips = await destream.getTips({ tokenType: 'access_token', access_token: 'jahs62d123' }) | ||
while(tips.next){ | ||
let tips = await tips.next() | ||
tipsArray.push(...tips.data) | ||
} | ||
``` | ||
[More examples](/examples/) | ||
## Installation | ||
@@ -27,2 +102,4 @@ | ||
Each method except for subscribeToEvents() and validateSignature() has `http_status` property in returned object; object is parsed JSON response. | ||
```javascript | ||
@@ -52,3 +129,3 @@ const DeStreamAPI = require('destream-api') | ||
```javascript | ||
let { access_token, refresh_token, token_type } = await destream.getTokensFromCode('lk12j3a1p', 'https://destream.ru/') | ||
let { access_token, refresh_token, token_type, http_status } = await destream.getTokensFromCode('lk12j3a1p', 'https://destream.ru/') | ||
``` | ||
@@ -63,3 +140,3 @@ | ||
```javascript | ||
let { access_token, refresh_token, token_type } = await destream.refreshAccessToken('profile+tips', '2QwlfWHU7OYs') | ||
let { access_token, refresh_token, token_type, http_status } = await destream.refreshAccessToken('profile+tips', '2QwlfWHU7OYs') | ||
``` | ||
@@ -71,3 +148,3 @@ | ||
Gets user which gave your app access to its account. | ||
Gets user which gave your app access to its account. If Access Token expired or incorrect, will throw DeStreamAPI.AccessTokenIncorrect exception for 401 http status with API response included in `apiResponse` property in it. | ||
@@ -81,15 +158,5 @@ Example usage: | ||
#### async getTokensFromCode(authorizationCode, redirectUri) | ||
Exchanges authorization code from oauth to access token, refresh token, token type. | ||
Example usage: | ||
```javascript | ||
let { access_token, refresh_token, token_type } = await destream.getTokensFromCode('lk12j3a1p', 'https://destream.ru/') | ||
``` | ||
#### async registerUser(email) | ||
Register new user on destream with specified email. | ||
Register new user on destream with specified email. If user exists, throws an DeStreamAPI.UserExistsException exception with API response included in it in `apiResponse` property. | ||
@@ -99,4 +166,3 @@ Example usage: | ||
```javascript | ||
let newUser = await destream.registerUser('help@gmail.com') | ||
if(newUser.status === 409) { throw 'User exists!' } | ||
let { http_status } = await destream.registerUser('help@gmail.com') | ||
console.log('User created!', newUser.data.user_id) | ||
@@ -109,4 +175,6 @@ ``` | ||
Gets latest tips. Tokens is an object: { token_type: 'string', access_token: 'string' }; Everything else is optional: offset and limit are Numbers; sinceDate is Date object | ||
Gets latest tips. Tokens is an object: `{ token_type: 'string', access_token: 'string' }`; Everything else is optional: offset and limit are Numbers; sinceDate is Date object | ||
If Access Token expired or incorrect, will throw DeStreamAPI.AccessTokenIncorrect exception for 401 http status with API response included in `apiResponse` property in it. | ||
Example usage: | ||
@@ -119,3 +187,3 @@ | ||
#### async subscribeToEvents(access_token, callback) | ||
#### subscribeToEvents(access_token, callback) | ||
@@ -149,4 +217,6 @@ Subscribes you to websocket server which sends you messages such as donationReceived. Callback function is called every time with exactly 1 argument: message text. | ||
Gets information about created invoices. Tokens is an object: { token_type: 'string', access_token: 'string' }; Everything else is optional: offset and limit are Numbers; sinceDate is Date object; arrayOfIds must be an array of numbers (payment_ids) | ||
Gets information about created invoices. Tokens is an object: `{ token_type: 'string', access_token: 'string' }`; Everything else is optional: offset and limit are Numbers; sinceDate is Date object; arrayOfIds must be an array of numbers (payment_ids) | ||
If Access Token expired or incorrect, will throw DeStreamAPI.AccessTokenIncorrect exception for 401 http status with API response included in `apiResponse` property in it. | ||
Example usage: | ||
@@ -157,2 +227,18 @@ | ||
console.log('You created these invoices: ', ...tenCreatedInvoices.data.map(invoice => invoice.payment_id)) | ||
``` | ||
### Notifications on webhook | ||
You have to setup server by yourself, but this library provides useful methods for webhook. | ||
#### validateSignature(body, receivedSignature) | ||
Concatenates body with clientSecret and hashes to SHA512. Returns true if equal to receivedSignature, false otherwise. | ||
Example usage: | ||
```javascript | ||
if(!destream.validateSignature(req.body, req.headers['X-Signature'])){ | ||
throw 'Signature invalid!!' | ||
} | ||
``` |
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
15366
182
233