Skip to content

Commit

Permalink
fix(helper): change from sync-fetch to node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ottolote committed Dec 30, 2023
1 parent 3361324 commit 8e3c514
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
24 changes: 13 additions & 11 deletions helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
const fs = require('fs')
const path = require('path')
const fetch = require('sync-fetch')
const fetch = require('node-fetch')
const URLSearchParams = require('@ungap/url-search-params')

module.exports = {
Expand All @@ -20,7 +20,7 @@ module.exports = {
console.log('Netatmo helper started ...')
this.token = null
},
authenticate: function (config) {
authenticate: async function (config) {
const self = this
self.config = config

Expand All @@ -31,20 +31,22 @@ module.exports = {
params.append('client_secret', self.config.clientSecret)

try {
const result = fetch('https://' + self.config.apiBase + self.config.authEndpoint, {
const result = await fetch('https://' + self.config.apiBase + self.config.authEndpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: params,
}).json()
})

if (result.error) {
throw new Error(result.error + ': ' + result.error_description)
}

console.log('UPDATING TOKEN ' + result.access_token)
self.token = result.access_token
self.token_expires_in = result.expires_in
self.refresh_token = result.refresh_token
authData = await result.json()

console.log('UPDATING TOKEN ' + authData.access_token)
self.token = authData.access_token
self.token_expires_in = authData.expires_in
self.refresh_token = authData.refresh_token
// we got a new token, save it to main file to allow it to request the datas
self.sendSocketNotification(self.notifications.AUTH_RESPONSE, {
status: 'OK',
Expand All @@ -58,7 +60,7 @@ module.exports = {
})
}
},
loadData: function (config) {
loadData: async function (config) {
const self = this
self.config = config

Expand All @@ -79,7 +81,7 @@ module.exports = {
}

try {
let result = fetch('https://' + self.config.apiBase + self.config.dataEndpoint, {
let result = await fetch('https://' + self.config.apiBase + self.config.dataEndpoint, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${self.token}`,
Expand All @@ -96,7 +98,7 @@ module.exports = {
return
}

result = result.json()
result = await result.json()

if (result.error) {
throw new Error(result.error.message)
Expand Down
13 changes: 1 addition & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@
"snyk": true,
"dependencies": {
"@ungap/url-search-params": "0.2.2",
"sync-fetch": "0.5.2"
"node-fetch": "^2.7.0"
}
}

0 comments on commit 8e3c514

Please sign in to comment.