HH-749: add CreatorHub react-admin frontend
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { HttpError } from 'ra-core'
|
||||
|
||||
async function request(path = '', options) {
|
||||
const response = await fetch(`/api/browsers${path}`, options)
|
||||
if (!response.ok) {
|
||||
const body = await response.json().catch(() => ({}))
|
||||
throw new HttpError(body.error || `请求失败 (${response.status})`, response.status, body)
|
||||
}
|
||||
return response.status === 204 ? null : response.json()
|
||||
}
|
||||
|
||||
const unsupported = operation => Promise.reject(new Error(`browsers 不支持 ${operation}`))
|
||||
|
||||
export const dataProvider = {
|
||||
async getList(resource) {
|
||||
if (resource !== 'browsers') return unsupported('getList')
|
||||
const browsers = await request()
|
||||
return {
|
||||
data: browsers.map(browser => ({ ...browser, id: browser.id || browser.name })),
|
||||
total: browsers.length,
|
||||
}
|
||||
},
|
||||
async create(resource, { data }) {
|
||||
if (resource !== 'browsers') return unsupported('create')
|
||||
const created = await request('', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
return { data: { ...data, ...created, id: created.id || created.name } }
|
||||
},
|
||||
getOne: () => unsupported('getOne'),
|
||||
getMany: () => unsupported('getMany'),
|
||||
getManyReference: () => unsupported('getManyReference'),
|
||||
update: () => unsupported('update'),
|
||||
updateMany: () => unsupported('updateMany'),
|
||||
delete: () => unsupported('delete'),
|
||||
deleteMany: () => unsupported('deleteMany'),
|
||||
async browserAction(name, action) {
|
||||
const paths = {
|
||||
start: [`/${encodeURIComponent(name)}/start`, 'POST'],
|
||||
stop: [`/${encodeURIComponent(name)}/stop`, 'POST'],
|
||||
recycle: [`/${encodeURIComponent(name)}`, 'DELETE'],
|
||||
}
|
||||
const target = paths[action]
|
||||
if (!target) throw new Error(`未知运行环境操作: ${action}`)
|
||||
await request(target[0], { method: target[1] })
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user