mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
@ -21,14 +21,14 @@ const apiGeneral = async <T = unknown>({
|
||||
body
|
||||
}: Params): Promise<PagedResponse<T>> => {
|
||||
console.log(
|
||||
ctx.bgGreen.bold(' API general ') +
|
||||
ctx.bgMagenta.bold(' General ') +
|
||||
' ' +
|
||||
domain +
|
||||
' ' +
|
||||
method +
|
||||
ctx.green(' -> ') +
|
||||
ctx.magenta(' -> ') +
|
||||
`/${url}` +
|
||||
(params ? ctx.green(' -> ') : ''),
|
||||
(params ? ctx.magenta(' -> ') : ''),
|
||||
params ? params : ''
|
||||
)
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { getAccountDetails } from '@utils/storage/actions'
|
||||
import { StorageGlobal } from '@utils/storage/global'
|
||||
import axios, { AxiosRequestConfig } from 'axios'
|
||||
import { ctx, handleError, PagedResponse, parseHeaderLinks, userAgent } from './helpers'
|
||||
|
||||
export type Params = {
|
||||
account?: StorageGlobal['account.active']
|
||||
method: 'get' | 'post' | 'put' | 'delete' | 'patch'
|
||||
version?: 'v1' | 'v2'
|
||||
url: string
|
||||
@ -15,6 +17,7 @@ export type Params = {
|
||||
}
|
||||
|
||||
const apiInstance = async <T = unknown>({
|
||||
account,
|
||||
method,
|
||||
version = 'v1',
|
||||
url,
|
||||
@ -23,7 +26,7 @@ const apiInstance = async <T = unknown>({
|
||||
body,
|
||||
extras
|
||||
}: Params): Promise<PagedResponse<T>> => {
|
||||
const accountDetails = getAccountDetails(['auth.domain', 'auth.token'])
|
||||
const accountDetails = getAccountDetails(['auth.domain', 'auth.token'], account)
|
||||
if (!accountDetails) {
|
||||
console.warn(ctx.bgRed.white.bold(' API instance '), 'No account detail available')
|
||||
return Promise.reject()
|
||||
@ -35,9 +38,9 @@ const apiInstance = async <T = unknown>({
|
||||
}
|
||||
|
||||
console.log(
|
||||
ctx.bgGreen.bold(' API instance '),
|
||||
ctx.bgBlue.bold(' Instance '),
|
||||
accountDetails['auth.domain'],
|
||||
method + ctx.green(' -> ') + `/${url}` + (params ? ctx.green(' -> ') : ''),
|
||||
method + ctx.blue(' -> ') + `/${url}` + (params ? ctx.blue(' -> ') : ''),
|
||||
params ? params : ''
|
||||
)
|
||||
|
||||
|
@ -26,7 +26,7 @@ const apiTooot = async <T = unknown>({
|
||||
body
|
||||
}: Params): Promise<{ body: T }> => {
|
||||
console.log(
|
||||
ctx.bgGreen.bold(' API tooot ') +
|
||||
ctx.bgGreen.bold(' tooot ') +
|
||||
' ' +
|
||||
method +
|
||||
ctx.green(' -> ') +
|
||||
|
@ -6,9 +6,9 @@ const log = (type: 'log' | 'warn' | 'error', func: string, message: string) => {
|
||||
switch (type) {
|
||||
case 'log':
|
||||
console.log(
|
||||
ctx.bgBlue.white.bold(' Start up ') +
|
||||
ctx.bgGrey.white.bold(' Start up ') +
|
||||
' ' +
|
||||
ctx.bgBlueBright.black(` ${func} `) +
|
||||
ctx.bgGrey.black(` ${func} `) +
|
||||
' ' +
|
||||
message
|
||||
)
|
||||
|
@ -228,6 +228,41 @@ export const setAccount = async (account: string) => {
|
||||
queryClient.clear()
|
||||
}
|
||||
|
||||
export type ReadableAccountType = {
|
||||
acct: string
|
||||
key: string
|
||||
active: boolean
|
||||
}
|
||||
export const getReadableAccounts = (): ReadableAccountType[] => {
|
||||
const accountActive = getGlobalStorage.string('account.active')
|
||||
const accounts = getGlobalStorage.object('accounts')?.sort((a, b) => a.localeCompare(b))
|
||||
accounts?.splice(
|
||||
accounts.findIndex(a => a === accountActive),
|
||||
1
|
||||
)
|
||||
accounts?.unshift(accountActive || '')
|
||||
return (
|
||||
accounts?.map(account => {
|
||||
const details = getAccountDetails(
|
||||
['auth.account.acct', 'auth.account.domain', 'auth.domain', 'auth.account.id'],
|
||||
account
|
||||
)
|
||||
if (details) {
|
||||
return {
|
||||
acct: `@${details['auth.account.acct']}@${details['auth.account.domain']}`,
|
||||
key: generateAccountKey({
|
||||
domain: details['auth.domain'],
|
||||
id: details['auth.account.id']
|
||||
}),
|
||||
active: account === accountActive
|
||||
}
|
||||
} else {
|
||||
return { acct: '', key: '', active: false }
|
||||
}
|
||||
}) || []
|
||||
).filter(a => a.acct.length)
|
||||
}
|
||||
|
||||
export const removeAccount = async (account: string) => {
|
||||
const currAccounts: NonNullable<StorageGlobal['accounts']> = JSON.parse(
|
||||
storage.global.getString('accounts') || '[]'
|
||||
|
Reference in New Issue
Block a user