This repository was archived by the owner on Sep 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
206 lines (183 loc) · 7.48 KB
/
index.js
File metadata and controls
206 lines (183 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const fetch = require('node-fetch')
const formurlencoded = require('form-urlencoded')
const sha512 = require('crypto-js/sha512')
const WebSocket = require('ws')
module.exports = class DeStreamAPI {
constructor(config) {
this._clientId = config.clientId
this._clientSecret = config.clientSecret
this.apiVersion = config.apiVersion ?? 2
this.baseURI = `https://destream.net/api/v${this.apiVersion}`
this.baseURIwebsocket = `https://destream.net/ws/v${this.apiVersion}`
}
static UserExistsException(api_response) {
this.apiResponse = api_response
}
static AccessTokenIncorrect(api_response) {
this.apiResponse = api_response
}
_getSignature() {
return sha512(this._clientId+getISO8601Date()+this._clientSecret)
}
async getTokensFromCode(authorizationCode, redirectUri) {
const accessTokenEndpoint = `${this.baseURI}/oauth2/token`
let params = formurlencoded({
grant_type: 'authorization_code',
client_id: this._clientId,
client_secret: this._clientSecret,
redirect_uri: redirectUri,
code: authorizationCode
})
let response = await fetch(`${accessTokenEndpoint}?${params}`, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
let parsedAPIResponse = await response.json()
return { ...parsedAPIResponse, http_status: response.status }
}
async refreshAccessToken(scope, refresh_token) {
const tokenRefreshingEndpoint = `${this.baseURI}/oauth2/token`
let params = formurlencoded({
grant_type: 'refresh_token',
client_id: this._clientId,
client_secret: this._clientSecret,
scope: scope,
refresh_token: refresh_token
})
let response = await fetch(`${tokenRefreshingEndpoint}?${params}`, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
let parsedAPIResponse = await response.json()
return { ...parsedAPIResponse, http_status: response.status }
}
async getUser(token_type, access_token) {
const userInfoEndpoint = `${this.baseURI}/users`
let response = await fetch(userInfoEndpoint, {
method: 'GET',
headers: {
'X-Api-ClientId': this._clientId,
'Authorization': `${token_type} ${access_token}`
}
})
let parsedAPIResponse = await response.json()
if(response.status === 401){ console.error('DeStreamAPI error', 'AccessTokenIncorrect', parsedAPIResponse); throw DeStreamAPI.AccessTokenIncorrect(parsedAPIResponse) }
return { ...parsedAPIResponse, http_status: response.status }
}
async registerUser(email) {
const registerUserEndpoint = `${this.baseURI}/users/register`
let response = await fetch(registerUserEndpoint, {
method: 'POST',
body: JSON.stringify({
email: email
}),
headers: {
'Content-Type': 'application/json',
'X-Api-ClientId': this._clientId,
'X-Api-RequestDate': getISO8601Date(),
'X-Api-Signature': this._getSignature()
}
})
let parsedAPIResponse = await response.json()
const HTTP_STATUS_USER_EXISTS = 409
if(response.status === HTTP_STATUS_USER_EXISTS){ console.error('DeStreamAPI error', 'UserExistsException', parsedAPIResponse); throw new DeStreamAPI.UserExistsException(parsedAPIResponse) }
return { ...parsedAPIResponse, http_status: response.status }
}
async getTips(tokens, offset, limit, sinceDate) {
const userInfoEndpoint = `${this.baseURI}/users/tips`
let params = formurlencoded({
offset: offset ?? 0,
limit: Math.max(0, Math.min(30, limit ?? 10)),
after_date: sinceDate instanceof Date ? getISO8601Date(sinceDate) : null
}, { ignorenull: true })
let response = await fetch(`${userInfoEndpoint}?${params}`, {
method: 'GET',
headers: {
'X-Api-ClientId': this._clientId,
'Authorization': `${tokens.token_type} ${tokens.access_token}`
}
})
let parsedAPIResponse = await response.json()
if(response.status === 401){ console.error('DeStreamAPI error', 'AccessTokenIncorrect', parsedAPIResponse); throw DeStreamAPI.AccessTokenIncorrect(parsedAPIResponse) }
if(response.status === 200){
parsedAPIResponse.next = async () => await this.getTips(tokens, (offset ?? 0)+(limit ?? 0), limit, sinceDate)
parsedAPIResponse.prev = async () => await this.getTips(tokens, Math.max(0, (offset ?? 0)-(limit ?? 0)), limit, sinceDate)
}
return { ...parsedAPIResponse, http_status: response.status }
}
async getInvoicesPayments(tokens, offset, limit, sinceDate, arrayOfIds) {
const userInfoEndpoint = `${this.baseURI}/payments`
let params = formurlencoded({
offset: offset ?? 0,
limit: Math.max(0, Math.min(30, limit ?? 10)),
after_date: sinceDate instanceof Date ? getISO8601Date(sinceDate) : null,
payment_id: arrayOfIds ? arrayOfIds.join(',') : null
}, { ignorenull: true })
let response = await fetch(`${userInfoEndpoint}?${params}`, {
method: 'GET',
headers: {
'X-Api-ClientId': this._clientId,
'X-Api-RequestDate': getISO8601Date(),
'X-Api-Signature': this._getSignature(),
'Authorization': `${tokens.token_type} ${tokens.access_token}`
}
})
let parsedAPIResponse = await response.json()
if(response.status === 401){ console.error('DeStreamAPI error', 'AccessTokenIncorrect', parsedAPIResponse); throw DeStreamAPI.AccessTokenIncorrect(parsedAPIResponse) }
if(response.status === 200){
parsedAPIResponse.next = async () => await this.getInvoicesPayments(tokens, offset+limit, limit, sinceDate)
parsedAPIResponse.prev = async () => await this.getInvoicesPayments(tokens, Math.max(0, offset-limit), limit, sinceDate)
}
return { ...parsedAPIResponse, http_status: response.status }
}
subscribeToEvents(access_token, callback) {
const websocketEndpoint = `${this.baseURIwebsocket}/donations`
let params = formurlencoded({
client_id: this._clientId,
access_token: access_token
})
let websocketURL = `${websocketEndpoint}?${params}`
console.log(websocketURL)
const ws = new WebSocket(websocketURL)
ws.on('message', callback)
}
async createInvoice(user_id, amount, currency, optionalData) {
const registerUserEndpoint = `${this.baseURI}/payments`
let response = await fetch(registerUserEndpoint, {
method: 'POST',
body: JSON.stringify({
user_id: user_id,
amount: amount,
currency: currency,
...optionalData
}),
headers: {
'Content-Type': 'application/json',
'X-Api-ClientId': this._clientId,
'X-Api-RequestDate': getISO8601Date(),
'X-Api-Signature': this._getSignature()
}
})
let parsedAPIResponse = await response.json()
return { ...parsedAPIResponse, http_status: response.status }
}
validateSignature(body, receivedSignature) {
return sha512(body+this._clientSecret) === receivedSignature
}
}
const getISO8601Date = (date) => {
const o0 = s => (`0${s}`).substr(-2)
date = date ?? new Date()
let year = date.getFullYear()
let month = o0(date.getMonth()+1)
let day = o0(date.getDate())
let hours = o0(date.getHours())
let minutes = o0(date.getMinutes())
let seconds = o0(date.getSeconds())
let tzOffset = -date.getTimezoneOffset();
let dif = tzOffset >= 0 ? '+' : '-';
let tzH = o0(tzOffset / 60)
let tzM = o0(tzOffset % 60)
// ISO8601 date + time + timezone example: 2021-05-28T20:34:37+00:00
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${dif}${tzH}:${tzM}`
}