mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Standardize on fetch and remove axios dependency
This commit is contained in:
1 parent
264451a1f7
commit
8233fec1a9
9 files changed
+379
-367
No files matched your search
@@ -1,4 +1,3 @@
|
||||
import axios from 'axios'
|
||||
import type {
|
||||
SettingsResponse,
|
||||
UpdateSettingsRequest,
|
||||
@@ -8,47 +7,50 @@ import type {
|
||||
UpdateOpenCodeConfigRequest
|
||||
} from './types/settings'
|
||||
import { API_BASE_URL } from '@/config'
|
||||
import { fetchWrapper, FetchError } from './fetchWrapper'
|
||||
|
||||
export const settingsApi = {
|
||||
getSettings: async (userId = 'default'): Promise<SettingsResponse> => {
|
||||
const { data } = await axios.get(`${API_BASE_URL}/api/settings`, {
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings`, {
|
||||
params: { userId },
|
||||
})
|
||||
return data
|
||||
},
|
||||
|
||||
updateSettings: async (
|
||||
updates: UpdateSettingsRequest,
|
||||
userId = 'default'
|
||||
): Promise<SettingsResponse> => {
|
||||
const { data } = await axios.patch(`${API_BASE_URL}/api/settings`, updates, {
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings`, {
|
||||
method: 'PATCH',
|
||||
params: { userId },
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(updates),
|
||||
})
|
||||
return data
|
||||
},
|
||||
|
||||
resetSettings: async (userId = 'default'): Promise<SettingsResponse> => {
|
||||
const { data } = await axios.delete(`${API_BASE_URL}/api/settings`, {
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings`, {
|
||||
method: 'DELETE',
|
||||
params: { userId },
|
||||
})
|
||||
return data
|
||||
},
|
||||
|
||||
getOpenCodeConfigs: async (userId = 'default'): Promise<OpenCodeConfigResponse> => {
|
||||
const { data } = await axios.get(`${API_BASE_URL}/api/settings/opencode-configs`, {
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-configs`, {
|
||||
params: { userId },
|
||||
})
|
||||
return data
|
||||
},
|
||||
|
||||
createOpenCodeConfig: async (
|
||||
request: CreateOpenCodeConfigRequest,
|
||||
userId = 'default'
|
||||
): Promise<OpenCodeConfig> => {
|
||||
const { data } = await axios.post(`${API_BASE_URL}/api/settings/opencode-configs`, request, {
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-configs`, {
|
||||
method: 'POST',
|
||||
params: { userId },
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(request),
|
||||
})
|
||||
return data
|
||||
},
|
||||
|
||||
updateOpenCodeConfig: async (
|
||||
@@ -56,21 +58,27 @@ export const settingsApi = {
|
||||
request: UpdateOpenCodeConfigRequest,
|
||||
userId = 'default'
|
||||
): Promise<OpenCodeConfig> => {
|
||||
const { data } = await axios.put(
|
||||
return fetchWrapper(
|
||||
`${API_BASE_URL}/api/settings/opencode-configs/${encodeURIComponent(configName)}`,
|
||||
request,
|
||||
{ params: { userId } }
|
||||
{
|
||||
method: 'PUT',
|
||||
params: { userId },
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(request),
|
||||
}
|
||||
)
|
||||
return data
|
||||
},
|
||||
|
||||
deleteOpenCodeConfig: async (
|
||||
configName: string,
|
||||
userId = 'default'
|
||||
): Promise<boolean> => {
|
||||
await axios.delete(
|
||||
await fetchWrapper(
|
||||
`${API_BASE_URL}/api/settings/opencode-configs/${encodeURIComponent(configName)}`,
|
||||
{ params: { userId } }
|
||||
{
|
||||
method: 'DELETE',
|
||||
params: { userId },
|
||||
}
|
||||
)
|
||||
return true
|
||||
},
|
||||
@@ -79,46 +87,56 @@ export const settingsApi = {
|
||||
configName: string,
|
||||
userId = 'default'
|
||||
): Promise<OpenCodeConfig> => {
|
||||
const { data } = await axios.post(
|
||||
return fetchWrapper(
|
||||
`${API_BASE_URL}/api/settings/opencode-configs/${encodeURIComponent(configName)}/set-default`,
|
||||
{},
|
||||
{ params: { userId } }
|
||||
{
|
||||
method: 'POST',
|
||||
params: { userId },
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({}),
|
||||
}
|
||||
)
|
||||
return data
|
||||
},
|
||||
|
||||
getDefaultOpenCodeConfig: async (userId = 'default'): Promise<OpenCodeConfig | null> => {
|
||||
try {
|
||||
const { data } = await axios.get(`${API_BASE_URL}/api/settings/opencode-configs/default`, {
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-configs/default`, {
|
||||
params: { userId },
|
||||
})
|
||||
return data
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
},
|
||||
|
||||
restartOpenCodeServer: async (): Promise<{ success: boolean; message: string; details?: string }> => {
|
||||
const { data } = await axios.post(`${API_BASE_URL}/api/settings/opencode-restart`)
|
||||
return data
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-restart`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
},
|
||||
|
||||
reloadOpenCodeConfig: async (): Promise<{ success: boolean; message: string; details?: string }> => {
|
||||
try {
|
||||
const { data } = await axios.post(`${API_BASE_URL}/api/settings/opencode-reload`)
|
||||
return data
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-reload`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error) && error.response?.status === 404) {
|
||||
const result = await axios.post(`${API_BASE_URL}/api/settings/opencode-restart`)
|
||||
return result.data
|
||||
if (error instanceof FetchError && error.statusCode === 404) {
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-restart`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
}
|
||||
throw error
|
||||
}
|
||||
},
|
||||
|
||||
rollbackOpenCodeConfig: async (): Promise<{ success: boolean; message: string; configName?: string }> => {
|
||||
const { data } = await axios.post(`${API_BASE_URL}/api/settings/opencode-rollback`)
|
||||
return data
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-rollback`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
},
|
||||
|
||||
getOpenCodeVersions: async (): Promise<{
|
||||
@@ -130,8 +148,7 @@ export const settingsApi = {
|
||||
}>
|
||||
currentVersion: string | null
|
||||
}> => {
|
||||
const { data } = await axios.get(`${API_BASE_URL}/api/settings/opencode-versions`)
|
||||
return data
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-versions`)
|
||||
},
|
||||
|
||||
installOpenCodeVersion: async (version: string): Promise<{
|
||||
@@ -142,8 +159,11 @@ export const settingsApi = {
|
||||
recovered?: boolean
|
||||
recoveryMessage?: string
|
||||
}> => {
|
||||
const { data } = await axios.post(`${API_BASE_URL}/api/settings/opencode-install-version`, { version })
|
||||
return data
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-install-version`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ version }),
|
||||
})
|
||||
},
|
||||
|
||||
upgradeOpenCode: async (): Promise<{
|
||||
@@ -155,22 +175,25 @@ export const settingsApi = {
|
||||
recovered?: boolean
|
||||
recoveryMessage?: string
|
||||
}> => {
|
||||
const { data } = await axios.post(`${API_BASE_URL}/api/settings/opencode-upgrade`)
|
||||
return data
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/opencode-upgrade`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
},
|
||||
|
||||
getAgentsMd: async (): Promise<{ content: string }> => {
|
||||
const { data } = await axios.get(`${API_BASE_URL}/api/settings/agents-md`)
|
||||
return data
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/agents-md`)
|
||||
},
|
||||
|
||||
getDefaultAgentsMd: async (): Promise<{ content: string }> => {
|
||||
const { data } = await axios.get(`${API_BASE_URL}/api/settings/agents-md/default`)
|
||||
return data
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/agents-md/default`)
|
||||
},
|
||||
|
||||
updateAgentsMd: async (content: string): Promise<{ success: boolean }> => {
|
||||
const { data } = await axios.put(`${API_BASE_URL}/api/settings/agents-md`, { content })
|
||||
return data
|
||||
return fetchWrapper(`${API_BASE_URL}/api/settings/agents-md`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ content }),
|
||||
})
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user