From 9df503022886f79a446241826b22c790fca61937 Mon Sep 17 00:00:00 2001 From: Pascal Bourque Date: Mon, 2 Jun 2025 07:04:45 -0400 Subject: [PATCH] feat: Added getDeviceFirmwares API (#10) --- example/main.ts | 4 +++- src/api/MysaApiClient.ts | 24 +++++++++++++++++++++--- src/types/rest/Firmwares.ts | 8 ++++++++ src/types/rest/index.ts | 1 + 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/types/rest/Firmwares.ts diff --git a/example/main.ts b/example/main.ts index afcaa2a..6a8b3d4 100644 --- a/example/main.ts +++ b/example/main.ts @@ -92,4 +92,6 @@ async function main() { } } -main().catch(rootLogger.error); +main().catch((error) => { + rootLogger.error(error, 'Error in main'); +}); diff --git a/src/api/MysaApiClient.ts b/src/api/MysaApiClient.ts index 54bf2d0..a6bede8 100644 --- a/src/api/MysaApiClient.ts +++ b/src/api/MysaApiClient.ts @@ -6,7 +6,7 @@ import { ChangeDeviceState } from '@/types/mqtt/in/ChangeDeviceState'; import { InMessageType } from '@/types/mqtt/in/InMessageType'; import { StartPublishingDeviceStatus } from '@/types/mqtt/in/StartPublishingDeviceStatus'; import { OutMessageType } from '@/types/mqtt/out/OutMessageType'; -import { Devices } from '@/types/rest/Devices'; +import { Devices, Firmwares } from '@/types/rest'; import { fromCognitoIdentityPool } from '@aws-sdk/credential-providers'; import { AuthenticationDetails, @@ -184,7 +184,25 @@ export class MysaApiClient { const response = await this._fetcher(`${MysaApiBaseUrl}/devices`, { headers: { - Authorization: `${session.getAccessToken().getJwtToken()}` + Authorization: `${session.getIdToken().getJwtToken()}` + } + }); + + if (!response.ok) { + throw new MysaApiError(response); + } + + return response.json(); + } + + async getDeviceFirmwares(): Promise { + this._logger.debug(`Fetching device firmwares...`); + + const session = await this.getFreshSession(); + + const response = await this._fetcher(`${MysaApiBaseUrl}/devices/firmware`, { + headers: { + Authorization: `${session.getIdToken().getJwtToken()}` } }); @@ -320,7 +338,7 @@ export class MysaApiClient { if ( this._cognitoUserSession.isValid() && - dayjs.unix(this._cognitoUserSession.getAccessToken().getExpiration()).isAfter() + dayjs.unix(this._cognitoUserSession.getIdToken().getExpiration()).isAfter() ) { this._logger.info('Session is valid, no need to refresh'); return Promise.resolve(this._cognitoUserSession); diff --git a/src/types/rest/Firmwares.ts b/src/types/rest/Firmwares.ts new file mode 100644 index 0000000..7e649ac --- /dev/null +++ b/src/types/rest/Firmwares.ts @@ -0,0 +1,8 @@ +export interface FirmwareDevice { + Device: string; + InstalledVersion: string; +} + +export interface Firmwares { + Firmware: Record; +} diff --git a/src/types/rest/index.ts b/src/types/rest/index.ts index 35b5f86..efa93be 100644 --- a/src/types/rest/index.ts +++ b/src/types/rest/index.ts @@ -1 +1,2 @@ export * from './Devices'; +export * from './Firmwares';