Browse Source

feat: Allow usage of key to fetch mastodon data

tags/v0.2.0
Dale 3 months ago
parent
commit
ecc3bad81d
3 changed files with 25 additions and 12 deletions
  1. 2
    1
      .gitignore
  2. 9
    0
      src/config.ts
  3. 14
    11
      src/lib/mastodon.ts

+ 2
- 1
.gitignore View File

@@ -1,3 +1,4 @@
node_modules/
dist/
tmp/
tmp/
config.json

+ 9
- 0
src/config.ts View File

@@ -0,0 +1,9 @@
import * as configFile from "./config.json";

interface IConfig {
'MASTODON_API_KEY': string;
}

const config: IConfig = configFile
export default config;


+ 14
- 11
src/lib/mastodon.ts View File

@@ -2,6 +2,8 @@ import axios from 'axios';
import * as R from 'ramda';
import * as tls from 'tls';

import config from '../config';

(tls as any).DEFAULT_ECDH_CURVE = 'auto';

interface MastodonStatus {
@@ -13,15 +15,16 @@ interface MastodonStatus {
}

export default function getMastodonStatus(address: string): PromiseLike<MastodonStatus> {
return axios.get(`${address}/api/v1/instance`)
.then((resp) => ({
online: true,
version: R.pathOr('N/A', ['data', 'version'], resp),
totalUsers: R.pathOr(0, ['data', 'stats', 'user_count'], resp),
totalStatuses: R.pathOr(0, ['data', 'stats', 'status_count'], resp),
knownDomains: R.pathOr(0, ['data', 'stats', 'domain_count'], resp),
}))
.catch(() => ({
online: false,
}))
return axios.get(`${address}/api/v1/instance`, {
headers: { 'Authorization': `Bearer ${config.MASTODON_API_KEY}`},
}).then((resp) => ({
online: true,
version: R.pathOr('N/A', ['data', 'version'], resp),
totalUsers: R.pathOr(0, ['data', 'stats', 'user_count'], resp),
totalStatuses: R.pathOr(0, ['data', 'stats', 'status_count'], resp),
knownDomains: R.pathOr(0, ['data', 'stats', 'domain_count'], resp),
}))
.catch((e: any) => ({
online: false,
}));
}

Loading…
Cancel
Save