feat: use memory/fs/kv storage drivers for server details (#34)
This commit is contained in:
parent
2ece5f5619
commit
521ad7a332
7 changed files with 111 additions and 74 deletions
|
@ -1,26 +1,69 @@
|
|||
// @ts-expect-error unstorage needs to provide backwards-compatible subpath types
|
||||
import _fs from 'unstorage/drivers/fs'
|
||||
// @ts-expect-error unstorage needs to provide backwards-compatible subpath types
|
||||
import _kv from 'unstorage/drivers/cloudflare-kv-http'
|
||||
|
||||
import { $fetch } from 'ohmyfetch'
|
||||
import type { Storage } from 'unstorage'
|
||||
|
||||
import cached from './cache-driver'
|
||||
|
||||
import type { AppInfo } from '~/types'
|
||||
import { APP_NAME } from '~/constants'
|
||||
|
||||
export const registeredApps: Record<string, AppInfo> = {}
|
||||
const fs = _fs as typeof import('unstorage/dist/drivers/fs')['default']
|
||||
const kv = _kv as typeof import('unstorage/dist/drivers/cloudflare-kv-http')['default']
|
||||
|
||||
const runtimeConfig = useRuntimeConfig()
|
||||
const promise = $fetch(runtimeConfig.registedAppsUrl, { responseType: 'json' })
|
||||
.then((r) => {
|
||||
Object.assign(registeredApps, r)
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`\n${Object.keys(registeredApps).length} registered apps loaded from ${runtimeConfig.registedAppsUrl.split(/\/+/g)[1]}`)
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`${Object.keys(registeredApps).map(i => ` - ${i}`).join('\n')}\n`)
|
||||
})
|
||||
.catch((e) => {
|
||||
if (process.dev)
|
||||
console.error('Failed to fetch registered apps,\nyou may need to run `nr register-apps` first')
|
||||
else
|
||||
console.error('Failed to fetch registered apps')
|
||||
console.error(e)
|
||||
const storage = useStorage() as Storage
|
||||
|
||||
if (process.dev) {
|
||||
storage.mount('servers', fs({ base: 'node_modules/.cache/servers' }))
|
||||
}
|
||||
else {
|
||||
const config = useRuntimeConfig()
|
||||
storage.mount('servers', cached(kv({
|
||||
accountId: config.cloudflare.accountId,
|
||||
namespaceId: config.cloudflare.namespaceId,
|
||||
apiToken: config.cloudflare.apiToken,
|
||||
})))
|
||||
}
|
||||
|
||||
const KNOWN_DOMAINS = [
|
||||
'http://localhost:3000',
|
||||
'https://elk.netlify.app',
|
||||
'https://elk.zone',
|
||||
]
|
||||
|
||||
async function fetchAppInfo(server: string) {
|
||||
const redirect_uris = [
|
||||
'urn:ietf:wg:oauth:2.0:oob',
|
||||
...KNOWN_DOMAINS.map(d => `${d}/api/${server}/oauth`),
|
||||
].join('\n')
|
||||
|
||||
const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, {
|
||||
method: 'POST',
|
||||
body: {
|
||||
client_name: APP_NAME,
|
||||
redirect_uris,
|
||||
scopes: 'read write follow push',
|
||||
},
|
||||
})
|
||||
return app
|
||||
}
|
||||
|
||||
const serverKey = (server: string) => `servers:${server}.json`
|
||||
|
||||
export async function getApp(server: string) {
|
||||
await promise
|
||||
return registeredApps[server]
|
||||
const key = serverKey(server)
|
||||
if (await storage.hasItem(key))
|
||||
return storage.getItem(key) as Promise<AppInfo>
|
||||
|
||||
try {
|
||||
const appInfo = await fetchAppInfo(server)
|
||||
await storage.setItem(key, appInfo)
|
||||
return appInfo
|
||||
}
|
||||
catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue