Skip to content

Commit

Permalink
fix: change sync-fetch to node-fetch (#311)
Browse files Browse the repository at this point in the history
Co-authored-by: Christopher Fenner <[email protected]>
  • Loading branch information
ottolote and CFenner committed Jan 3, 2024
1 parent 3ab2ba0 commit 34a6643
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 33 deletions.
14 changes: 7 additions & 7 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,11 +31,11 @@ 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()
}).then(response => response.json())

if (result.error) {
throw new Error(result.error + ': ' + result.error_description)
Expand All @@ -58,7 +58,7 @@ module.exports = {
})
}
},
loadData: function (config) {
loadData: async function (config) {
const self = this
self.config = config

Expand All @@ -79,7 +79,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 +96,7 @@ module.exports = {
return
}

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

if (result.error) {
throw new Error(result.error.message)
Expand Down
26 changes: 13 additions & 13 deletions helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ describe('helper', () => {
delete moduleUnderTest.sendSocketNotification
})
describe('data', () => {
test('existing token', () => {
test('existing token', async () => {
// prepare
expect(moduleUnderTest).not.toHaveProperty('token')
moduleUnderTest.sendSocketNotification = jest.fn((type, payload) => {})
moduleUnderTest.authenticate({
await moduleUnderTest.authenticate({
apiBase,
authEndpoint,
refresh_token: process.env.REFRESH_TOKEN,
Expand All @@ -33,39 +33,39 @@ describe('helper', () => {
})
expect(moduleUnderTest).toHaveProperty('token')
// test
moduleUnderTest.loadData({
await moduleUnderTest.loadData({
apiBase,
dataEndpoint,
})
// assert
expect(moduleUnderTest.sendSocketNotification).toHaveBeenCalled()
})

test('with missing token', () => {
test('with missing token', async () => {
// moduleUnderTest.token = process.env.TOKEN
// prepare
moduleUnderTest.sendSocketNotification = jest.fn((type, payload) => {
expect(type).toBe(moduleUnderTest.notifications.DATA_RESPONSE)
expect(payload).toHaveProperty('status', 'INVALID_TOKEN')
})
// test
moduleUnderTest.loadData({
await moduleUnderTest.loadData({
apiBase,
dataEndpoint,
})
// assert
expect(moduleUnderTest.sendSocketNotification).toHaveBeenCalled()
})

test('with invalid token', () => {
test('with invalid token', async () => {
moduleUnderTest.token = 'something'
// prepare
moduleUnderTest.sendSocketNotification = jest.fn((type, payload) => {
expect(type).toBe(moduleUnderTest.notifications.DATA_RESPONSE)
expect(payload).toHaveProperty('status', 'INVALID_TOKEN')
})
// test
moduleUnderTest.loadData({
await moduleUnderTest.loadData({
apiBase,
dataEndpoint,
})
Expand All @@ -75,7 +75,7 @@ describe('helper', () => {
})

describe('authentication', () => {
test('with refresh_token from config', () => {
test('with refresh_token from config', async () => {
// prepare
moduleUnderTest.sendSocketNotification = jest.fn((type, payload) => {
expect(type).toBe(moduleUnderTest.notifications.AUTH_RESPONSE)
Expand All @@ -85,7 +85,7 @@ describe('helper', () => {
expect(moduleUnderTest).not.toHaveProperty('token_expires_in')
expect(moduleUnderTest).not.toHaveProperty('refresh_token')
// test
moduleUnderTest.authenticate({
await moduleUnderTest.authenticate({
apiBase,
authEndpoint,
refresh_token: process.env.REFRESH_TOKEN,
Expand All @@ -99,7 +99,7 @@ describe('helper', () => {
expect(moduleUnderTest.sendSocketNotification).toHaveBeenCalled()
})

test('with refresh_token from object', () => {
test('with refresh_token from object', async () => {
// prepare
moduleUnderTest.refresh_token = process.env.REFRESH_TOKEN
moduleUnderTest.sendSocketNotification = jest.fn((type, payload) => {
Expand All @@ -110,7 +110,7 @@ describe('helper', () => {
expect(moduleUnderTest).not.toHaveProperty('token_expires_in')
expect(moduleUnderTest).toHaveProperty('refresh_token')
// test
moduleUnderTest.authenticate({
await moduleUnderTest.authenticate({
apiBase,
authEndpoint,
refresh_token: '',
Expand All @@ -124,7 +124,7 @@ describe('helper', () => {
expect(moduleUnderTest.sendSocketNotification).toHaveBeenCalled()
})

test('without refresh_token', () => {
test('without refresh_token', async () => {
// prepare
moduleUnderTest.sendSocketNotification = jest.fn((type, payload) => {
expect(type).toBe(moduleUnderTest.notifications.AUTH_RESPONSE)
Expand All @@ -134,7 +134,7 @@ describe('helper', () => {
expect(moduleUnderTest).not.toHaveProperty('token_expires_in')
expect(moduleUnderTest).not.toHaveProperty('refresh_token')
// test
moduleUnderTest.authenticate({
await moduleUnderTest.authenticate({
apiBase,
authEndpoint,
refresh_token: '',
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 34a6643

Please sign in to comment.