UNPKG

homebridge-airthings

Version:

A Homebridge plugin for Airthings air quality monitors via the Airthings Consumer API.

41 lines 1.43 kB
import axios from 'axios'; import { ClientCredentials } from 'simple-oauth2'; export class AirthingsApi { accessToken; client; tokenScope; constructor(tokenScope, clientId, clientSecret) { this.tokenScope = tokenScope; if (clientId == null || clientSecret == null) { return; } const config = { client: { id: clientId, secret: clientSecret }, auth: { tokenHost: 'https://accounts.airthings.com', tokenPath: 'https://accounts-api.airthings.com/v1/token' } }; this.client = new ClientCredentials(config); } async getLatestSamples(id) { if (this.client == null) { throw new Error('Airthings API Client not initialized due to invalid configuration...'); } if (this.accessToken == null || this.accessToken?.expired(300)) { const tokenParams = { scope: this.tokenScope }; this.accessToken = await this.client.getToken(tokenParams); } const requestConfig = { headers: { Authorization: `${this.accessToken.token.access_token}` } }; const response = await axios.get(`https://ext-api.airthings.com/v1/devices/${id}/latest-samples`, requestConfig); return response.data; } } //# sourceMappingURL=api.js.map