2023-08-24 04:20:53 +08:00
|
|
|
import { makePersisted } from '@solid-primitives/storage'
|
|
|
|
import ky from 'ky'
|
|
|
|
import { createSignal } from 'solid-js'
|
|
|
|
import { themes } from '~/constants'
|
|
|
|
|
2023-09-02 20:37:08 +08:00
|
|
|
export const useRequest = () => {
|
|
|
|
const e = endpoint()
|
|
|
|
|
|
|
|
return ky.create({
|
|
|
|
prefixUrl: e?.url,
|
|
|
|
headers: {
|
|
|
|
Authorization: e?.secret ? `Bearer ${e.secret}` : '',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-24 04:20:53 +08:00
|
|
|
export const [selectedEndpoint, setSelectedEndpoint] = makePersisted(
|
|
|
|
createSignal(''),
|
|
|
|
{
|
|
|
|
name: 'selectedEndpoint',
|
|
|
|
storage: localStorage,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
export const [endpointList, setEndpointList] = makePersisted(
|
|
|
|
createSignal<
|
|
|
|
{
|
|
|
|
id: string
|
|
|
|
url: string
|
|
|
|
secret: string
|
|
|
|
}[]
|
|
|
|
>([]),
|
|
|
|
{ name: 'endpointList', storage: localStorage },
|
|
|
|
)
|
|
|
|
|
|
|
|
export const [curTheme, setCurTheme] = makePersisted(
|
2023-09-02 11:10:24 +08:00
|
|
|
createSignal<(typeof themes)[number]>('halloween'),
|
2023-08-24 04:20:53 +08:00
|
|
|
{ name: 'theme', storage: localStorage },
|
|
|
|
)
|
|
|
|
|
|
|
|
export const endpoint = () =>
|
|
|
|
endpointList().find(({ id }) => id === selectedEndpoint())
|
|
|
|
|
2023-08-27 23:30:13 +08:00
|
|
|
export const secret = () => endpoint()?.secret
|
|
|
|
|
2023-08-24 04:20:53 +08:00
|
|
|
export const wsEndpointURL = () => endpoint()?.url.replace('http', 'ws')
|