Skip to content

Commit

Permalink
Code Style & Linting Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelahern committed Aug 25, 2024
1 parent b4afd77 commit 24aa978
Show file tree
Hide file tree
Showing 7 changed files with 751 additions and 622 deletions.
18 changes: 16 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import eslint from '@eslint/js';
import stylistic from '@stylistic/eslint-plugin';
import tseslint from 'typescript-eslint';

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
eslint.configs.recommended,
stylistic.configs.customize({
arrowParens: false,
blockSpacing: true,
braceStyle: 'stroustrup',
commaDangle: 'never',
flat: true,
indent: 4,
jsx: false,
quoteProps: 'consistent-as-needed',
quotes: 'single',
semi: true
}),
...tseslint.configs.strict,
...tseslint.configs.stylistic
);
130 changes: 130 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"simple-oauth2": "~5.1"
},
"devDependencies": {
"@stylistic/eslint-plugin": "^2",
"@types/node": "^20",
"@types/simple-oauth2": "~5.0",
"eslint": "^9",
Expand Down
102 changes: 51 additions & 51 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
import axios from "axios";
import { AccessToken, ClientCredentials } from "simple-oauth2";
import axios from 'axios';
import { AccessToken, ClientCredentials } from 'simple-oauth2';

export class AirthingsApi {
private accessToken?: AccessToken;
private accessToken?: AccessToken;

private readonly client?: ClientCredentials;
private readonly tokenScope: string;
private readonly client?: ClientCredentials;
private readonly tokenScope: string;

constructor(tokenScope: string, clientId?: string, clientSecret?: string) {
this.tokenScope = tokenScope;
constructor(tokenScope: string, clientId?: string, clientSecret?: string) {
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"
}
};
if (clientId == null || clientSecret == null) {
return;
}

this.client = new ClientCredentials(config);
}
const config = {
client: {
id: clientId,
secret: clientSecret
},
auth: {
tokenHost: 'https://accounts.airthings.com',
tokenPath: 'https://accounts-api.airthings.com/v1/token'
}
};

public async getLatestSamples(id: string) {
if (this.client == null) {
throw new Error("Airthings API Client not initialized due to invalid configuration...");
this.client = new ClientCredentials(config);
}

if (this.accessToken == null || this.accessToken?.expired(300)) {
const tokenParams = {
scope: this.tokenScope
};
this.accessToken = await this.client.getToken(tokenParams);
}
public async getLatestSamples(id: string) {
if (this.client == null) {
throw new Error('Airthings API Client not initialized due to invalid configuration...');
}

const requestConfig = {
headers: { "Authorization": `${this.accessToken.token.access_token}` }
};
if (this.accessToken == null || this.accessToken?.expired(300)) {
const tokenParams = {
scope: this.tokenScope
};
this.accessToken = await this.client.getToken(tokenParams);
}

const response = await axios.get<AirthingsApiDeviceSample>(`https://ext-api.airthings.com/v1/devices/${id}/latest-samples`, requestConfig);
return response.data;
}
const requestConfig = {
headers: { Authorization: `${this.accessToken.token.access_token}` }
};

const response = await axios.get<AirthingsApiDeviceSample>(`https://ext-api.airthings.com/v1/devices/${id}/latest-samples`, requestConfig);
return response.data;
}
}

export interface AirthingsApiDeviceSample {
data: {
battery?: number;
co2?: number;
humidity?: number;
mold?: number;
pm1?: number;
pm25?: number;
pressure?: number;
radonShortTermAvg?: number;
temp?: number;
time?: number;
voc?: number;
}
data: {
battery?: number;
co2?: number;
humidity?: number;
mold?: number;
pm1?: number;
pm25?: number;
pressure?: number;
radonShortTermAvg?: number;
temp?: number;
time?: number;
voc?: number;
};
}
Loading

0 comments on commit 24aa978

Please sign in to comment.