elk/scripts/registerApps.ts

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-11-15 14:29:46 +00:00
import fs from 'fs-extra'
import { $fetch } from 'ohmyfetch'
2022-11-15 15:48:23 +00:00
import { APP_NAME } from '~/constants'
import type { AppInfo } from '~/types'
2022-11-15 14:29:46 +00:00
const KNOWN_SERVERS = [
'mastodon.social',
'mas.to',
'fosstodon.org',
]
2022-11-15 15:48:23 +00:00
const KNOWN_DOMAINS = [
'http://localhost:3000',
'https://nuxtodon.netlify.app',
]
2022-11-15 14:29:46 +00:00
const filename = 'public/registered-apps.json'
2022-11-15 15:48:23 +00:00
let registeredApps: Record<string, AppInfo> = {}
2022-11-15 14:29:46 +00:00
if (fs.existsSync(filename))
registeredApps = await fs.readJSON(filename)
for (const server of KNOWN_SERVERS) {
2022-11-15 15:48:23 +00:00
const redirect_uris = [
'urn:ietf:wg:oauth:2.0:oob',
...KNOWN_DOMAINS.map(d => `${d}/api/${server}/oauth`),
].join('\n')
if (!registeredApps[server] || registeredApps[server].redirect_uri !== redirect_uris) {
const app = await $fetch(`https://${server}/api/v1/apps`, {
method: 'POST',
body: {
client_name: APP_NAME,
redirect_uris,
scopes: 'read write follow push',
},
})
registeredApps[server] = app
console.log(`Registered app for ${server}`)
}
2022-11-15 14:29:46 +00:00
}
await fs.writeJSON(filename, registeredApps, { spaces: 2, EOL: '\n' })