Skip to content

Commit

Permalink
bumping versions
Browse files Browse the repository at this point in the history
  • Loading branch information
bassrock committed May 10, 2020
1 parent a20077b commit 088914d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flair-api-ts",
"version": "0.0.14",
"version": "0.0.15",
"description": "A ts-node implementation of the Flair Smart Vent API",
"author": "bassrock",
"main": "lib/index.js",
Expand Down
17 changes: 10 additions & 7 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,21 @@ class Client {
private async updateAccessToken(): Promise<Token> {
if (this.currentToken === undefined) {
return this.getRefreshToken();
} else if (this.currentToken.expires_at.isBefore(new Date())) {
} else if (this.currentToken.expires_at.isBefore(moment().subtract(20, 'seconds'))) {
const requestURL = '/oauth/token?' + querystring.stringify({
...this.refreshTokenConfig,
refresh_token: this.currentToken!.refresh_token
});
const response = await axios.post(requestURL)
if (response.status !== 200) {
throw new Error("Refreshing access token failed.")
try {
const response = await axios.post(requestURL)
if (response.status !== 200) {
return this.getRefreshToken();
}
response.data.expires_at = moment().add(response.data.expires_in, 'seconds')
this.currentToken = response.data
} catch (e) {
return this.getRefreshToken();
}

response.data.expires_at = moment().add(response.data.expires_in, 'seconds')
this.currentToken = response.data
}

return this.currentToken!
Expand Down

0 comments on commit 088914d

Please sign in to comment.