diff --git a/package.json b/package.json index d4a71ee..9b10342 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "main": "dist/index.js", "scripts": { "lint": "eslint src/**.ts --max-warnings=0", + "fix": "yarn lint --fix", "watch": "npm run build && npm link && nodemon", "build": "rimraf ./dist && tsc", "prepublishOnly": "npm run lint && npm run build" diff --git a/src/SalusConnect.ts b/src/SalusConnect.ts index 2d49820..70b58e6 100644 --- a/src/SalusConnect.ts +++ b/src/SalusConnect.ts @@ -34,18 +34,18 @@ export function getKnownProperties(props: SalusProperty[]): Record { const token = await this.getToken(); - const response = await axios(this.buildUrl(`apiv1/dsns/${id}/properties/${makeProp(prop)}`), { - method: 'GET', - headers: { - 'Authorization': `Bearer ${token}`, - }, - }); - if(!retried && response.status === 401) { - await this.refreshToken(); - return this.getProperty({id, prop, retried: true}); - } - if(response.status !== 200) { - throw new Error(`'Wrong response on getProperty(${JSON.stringify({ id, prop })}): ${response.data}'`); + try { + const response = await axios(this.buildUrl(`apiv1/dsns/${id}/properties/${makeProp(prop)}`), { + method: 'GET', + headers: { + 'Authorization': `Bearer ${token}`, + }, + }); + return response.data; + + } catch (error) { + if (axios.isAxiosError(error)) { + const status = error.response?.status; + if (!retried && status === 401) { + await this.refreshToken(); + return this.getProperty({ id, prop, retried: true }); + } + throw new Error(`'Wrong response on getProperty(${JSON.stringify({ id, prop })}): ${error.response?.data}'`); + } + throw error; } - return response.data; } async getAllProperties({ id, retried }: { id: string; retried?: boolean }): Promise { const token = await this.getToken(); - const response = await axios(this.buildUrl(`apiv1/dsns/${id}/properties`), { - method: 'GET', - headers: { - 'Authorization': `Bearer ${token}`, - }, - }); - if(!retried && response.status === 401) { - await this.refreshToken(); - return this.getAllProperties({id, retried: true}); - } - if(response.status !== 200) { - throw new Error(`'Wrong response on getAllProperties(${JSON.stringify({ id })}): ${response.data}'`); - } - return response.data.map(data => data.property) as SalusProperty[]; - } - - async setProperty({ id, prop, value, retried }: { id: string; prop: Props; value: CharacteristicValue; retried?: boolean }): - Promise<{ value: CharacteristicValue }> { - const token = await this.getToken(); - this.log?.debug(`setProperty(${JSON.stringify({ id, prop, value })})`); - const response = await axios.post(this.buildUrl(`apiv1/dsns/${id}/properties/${makeProp(prop)}/datapoints`), - { 'datapoint': { value } }, { + try { + const response = await axios(this.buildUrl(`apiv1/dsns/${id}/properties`), { + method: 'GET', headers: { 'Authorization': `Bearer ${token}`, }, }); - if(!retried && response.status === 401) { - await this.refreshToken(); - return this.setProperty({id, prop, value, retried: true}); + return response.data.map(data => data.property) as SalusProperty[]; + } catch (error) { + if (axios.isAxiosError(error)) { + const status = error.response?.status; + if (!retried && status === 401) { + await this.refreshToken(); + return this.getAllProperties({ id, retried: true }); + } + throw new Error(`'Wrong response on getAllProperties(${JSON.stringify({ id })}): ${error.response?.data}'`); + } + throw error; } - if(response.status !== 201) { - throw new Error(`'Wrong response on setProperty(${JSON.stringify({ id, prop, value })}): ${response.data}'`); + } + + async setProperty({ id, prop, value, retried }: { id: string; prop: Props; value: CharacteristicValue; retried?: boolean }): + Promise<{ value: CharacteristicValue }> { + const token = await this.getToken(); + try { + + const response = await axios.post(this.buildUrl(`apiv1/dsns/${id}/properties/${makeProp(prop)}/datapoints`), + { 'datapoint': { value } }, { + headers: { + 'Authorization': `Bearer ${token}`, + }, + }); + this.log?.debug(`Response: ${JSON.stringify(response.data, undefined, 4)}`); + return response.data.datapoint; + } catch (error) { + if (axios.isAxiosError(error)) { + const status = error.response?.status; + if (!retried && status === 401) { + await this.refreshToken(); + return this.setProperty({ id, prop, value, retried: true }); + } + throw new Error(`'Wrong response on setProperty(${JSON.stringify({ id, prop, value })}): ${error.response?.data}'`); + } + throw error; } - this.log?.debug(`Response: ${JSON.stringify(response.data, undefined, 4)}`); - return response.data.datapoint; } buildUrl(url: string) {