mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Restructure removing remote
This commit is contained in:
87
src/api/general.ts
Normal file
87
src/api/general.ts
Normal file
@ -0,0 +1,87 @@
|
||||
import axios from 'axios'
|
||||
import chalk from 'chalk'
|
||||
|
||||
const ctx = new chalk.Instance({ level: 3 })
|
||||
|
||||
export type Params = {
|
||||
method: 'get' | 'post' | 'put' | 'delete'
|
||||
domain?: string
|
||||
url: string
|
||||
params?: {
|
||||
[key: string]: string | number | boolean
|
||||
}
|
||||
headers?: { [key: string]: string }
|
||||
body?: FormData | Object
|
||||
}
|
||||
|
||||
const apiGeneral = async <T = unknown>({
|
||||
method,
|
||||
domain,
|
||||
url,
|
||||
params,
|
||||
headers,
|
||||
body
|
||||
}: Params): Promise<{ body: T }> => {
|
||||
if (!domain) {
|
||||
return Promise.reject()
|
||||
}
|
||||
|
||||
console.log(
|
||||
ctx.bgGreen.bold(' API general ') +
|
||||
' ' +
|
||||
domain +
|
||||
' ' +
|
||||
method +
|
||||
ctx.green(' -> ') +
|
||||
`/${url}` +
|
||||
(params ? ctx.green(' -> ') : ''),
|
||||
params ? params : ''
|
||||
)
|
||||
|
||||
return axios({
|
||||
timeout: method === 'post' ? 1000 * 60 : 1000 * 15,
|
||||
method,
|
||||
baseURL: `https://${domain}/`,
|
||||
url,
|
||||
params,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...headers
|
||||
},
|
||||
...(body && { data: body })
|
||||
})
|
||||
.then(response => {
|
||||
return Promise.resolve({
|
||||
body: response.data
|
||||
})
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.response) {
|
||||
// The request was made and the server responded with a status code
|
||||
// that falls out of the range of 2xx
|
||||
console.error(
|
||||
ctx.bold(' API general '),
|
||||
ctx.bold('response'),
|
||||
error.response.status,
|
||||
error.response.data.error
|
||||
)
|
||||
return Promise.reject(error.response)
|
||||
} else if (error.request) {
|
||||
// The request was made but no response was received
|
||||
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
||||
// http.ClientRequest in node.js
|
||||
console.error(ctx.bold(' API general '), ctx.bold('request'), error)
|
||||
return Promise.reject()
|
||||
} else {
|
||||
console.error(
|
||||
ctx.bold(' API general '),
|
||||
ctx.bold('internal'),
|
||||
error.message,
|
||||
url
|
||||
)
|
||||
return Promise.reject()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default apiGeneral
|
@ -5,22 +5,8 @@ import li from 'li'
|
||||
|
||||
const ctx = new chalk.Instance({ level: 3 })
|
||||
|
||||
const client = async <T = unknown>({
|
||||
method,
|
||||
instance,
|
||||
localIndex,
|
||||
instanceDomain,
|
||||
version = 'v1',
|
||||
url,
|
||||
params,
|
||||
headers,
|
||||
body,
|
||||
onUploadProgress
|
||||
}: {
|
||||
export type Params = {
|
||||
method: 'get' | 'post' | 'put' | 'delete'
|
||||
instance: 'local' | 'remote'
|
||||
localIndex?: number
|
||||
instanceDomain?: string
|
||||
version?: 'v1' | 'v2'
|
||||
url: string
|
||||
params?: {
|
||||
@ -29,30 +15,37 @@ const client = async <T = unknown>({
|
||||
headers?: { [key: string]: string }
|
||||
body?: FormData
|
||||
onUploadProgress?: (progressEvent: any) => void
|
||||
}): Promise<{ body: T; links: { prev?: string; next?: string } }> => {
|
||||
const { store } = require('@root/store')
|
||||
const state = (store.getState() as RootState).instances
|
||||
const theLocalIndex =
|
||||
localIndex !== undefined ? localIndex : state.local.activeIndex
|
||||
}
|
||||
|
||||
let domain = null
|
||||
let token = null
|
||||
if (instance === 'remote') {
|
||||
domain = instanceDomain || state.remote.url
|
||||
const apiInstance = async <T = unknown>({
|
||||
method,
|
||||
version = 'v1',
|
||||
url,
|
||||
params,
|
||||
headers,
|
||||
body,
|
||||
onUploadProgress
|
||||
}: Params): Promise<{ body: T; links: { prev?: string; next?: string } }> => {
|
||||
const { store } = require('@root/store')
|
||||
const state = store.getState() as RootState
|
||||
const instanceActive = state.instances.instances.findIndex(
|
||||
instance => instance.active
|
||||
)
|
||||
|
||||
let domain
|
||||
let token
|
||||
if (instanceActive !== -1 && state.instances.instances[instanceActive]) {
|
||||
domain = state.instances.instances[instanceActive].url
|
||||
token = state.instances.instances[instanceActive].token
|
||||
} else {
|
||||
if (theLocalIndex !== null && state.local.instances[theLocalIndex]) {
|
||||
domain = state.local.instances[theLocalIndex].url
|
||||
token = state.local.instances[theLocalIndex].token
|
||||
} else {
|
||||
console.error(
|
||||
ctx.bgRed.white.bold(' API ') + ' ' + 'No instance domain is provided'
|
||||
)
|
||||
return Promise.reject()
|
||||
}
|
||||
console.error(
|
||||
ctx.bgRed.white.bold(' API ') + ' ' + 'No instance domain is provided'
|
||||
)
|
||||
return Promise.reject()
|
||||
}
|
||||
|
||||
console.log(
|
||||
ctx.bgGreen.bold(' API ') +
|
||||
ctx.bgGreen.bold(' API instance ') +
|
||||
' ' +
|
||||
domain +
|
||||
' ' +
|
||||
@ -97,7 +90,7 @@ const client = async <T = unknown>({
|
||||
// The request was made and the server responded with a status code
|
||||
// that falls out of the range of 2xx
|
||||
console.error(
|
||||
ctx.bold(' API '),
|
||||
ctx.bold(' API instance '),
|
||||
ctx.bold('response'),
|
||||
error.response.status,
|
||||
error.response.data.error
|
||||
@ -107,11 +100,11 @@ const client = async <T = unknown>({
|
||||
// The request was made but no response was received
|
||||
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
||||
// http.ClientRequest in node.js
|
||||
console.error(ctx.bold(' API '), ctx.bold('request'), error)
|
||||
console.error(ctx.bold(' API instance '), ctx.bold('request'), error)
|
||||
return Promise.reject()
|
||||
} else {
|
||||
console.error(
|
||||
ctx.bold(' API '),
|
||||
ctx.bold(' API instance '),
|
||||
ctx.bold('internal'),
|
||||
error.message,
|
||||
url
|
||||
@ -121,4 +114,4 @@ const client = async <T = unknown>({
|
||||
})
|
||||
}
|
||||
|
||||
export default client
|
||||
export default apiInstance
|
@ -1,7 +1,7 @@
|
||||
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
|
||||
import {
|
||||
getLocalInstance,
|
||||
updateLocalNotification
|
||||
getInstance,
|
||||
updateInstanceNotification
|
||||
} from '@utils/slices/instancesSlice'
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { useQueryClient } from 'react-query'
|
||||
@ -18,7 +18,7 @@ const useWebsocket = ({
|
||||
const queryClient = useQueryClient()
|
||||
const dispatch = useDispatch()
|
||||
const localInstance = useSelector(
|
||||
getLocalInstance,
|
||||
getInstance,
|
||||
(prev, next) =>
|
||||
prev?.urls.streaming_api === next?.urls.streaming_api &&
|
||||
prev?.token === next?.token
|
||||
@ -39,7 +39,7 @@ const useWebsocket = ({
|
||||
case 'notification':
|
||||
const payload: Mastodon.Notification = JSON.parse(message.payload)
|
||||
dispatch(
|
||||
updateLocalNotification({ latestTime: payload.created_at })
|
||||
updateInstanceNotification({ latestTime: payload.created_at })
|
||||
)
|
||||
const queryKey: QueryKeyTimeline = [
|
||||
'Timeline',
|
||||
|
Reference in New Issue
Block a user