82 lines
2.0 KiB
TypeScript
82 lines
2.0 KiB
TypeScript
import { getToken } from '@/utils/auth'
|
|
|
|
export interface TaskLogSummary {
|
|
id: string
|
|
source: 'awx' | 'wayne'
|
|
service: string
|
|
name: string
|
|
runner: string
|
|
status: string
|
|
status_text: string
|
|
status_class: string
|
|
business_line_id: number
|
|
reference_id: string
|
|
created_at: string
|
|
updated_at: string
|
|
}
|
|
|
|
export interface TaskLogLine {
|
|
time: string
|
|
message: string
|
|
class: string
|
|
}
|
|
|
|
export interface TaskLogListParams {
|
|
source?: string
|
|
businessLineId?: number
|
|
}
|
|
|
|
export const taskLogApi = {
|
|
async list(params: TaskLogListParams = {}): Promise<TaskLogSummary[]> {
|
|
const query = new URLSearchParams()
|
|
if (params.source && params.source !== 'all') {
|
|
query.set('source', params.source)
|
|
}
|
|
if (params.businessLineId) {
|
|
query.set('business_line_id', String(params.businessLineId))
|
|
}
|
|
const suffix = query.toString() ? `?${query.toString()}` : ''
|
|
const data = await authRequest(`/auth/api/v1/task-logs${suffix}`)
|
|
return Array.isArray(data.items) ? data.items : []
|
|
},
|
|
|
|
async get(id: string): Promise<{ task: TaskLogSummary; lines: TaskLogLine[] }> {
|
|
const data = await authRequest(`/auth/api/v1/task-logs/${encodeURIComponent(id)}`)
|
|
return {
|
|
task: data.task,
|
|
lines: Array.isArray(data.lines) ? data.lines : [],
|
|
}
|
|
},
|
|
}
|
|
|
|
async function authRequest(path: string, init: RequestInit = {}) {
|
|
const token = getToken()
|
|
const response = await fetch(path, {
|
|
...init,
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
...init.headers,
|
|
},
|
|
})
|
|
const text = await response.text()
|
|
const data = parseResponseBody(text)
|
|
if (!response.ok) {
|
|
const message = data?.error || data?.message || text || `HTTP ${response.status}`
|
|
throw new Error(message)
|
|
}
|
|
return data || {}
|
|
}
|
|
|
|
function parseResponseBody(text: string) {
|
|
if (!text.trim()) {
|
|
return {}
|
|
}
|
|
try {
|
|
return JSON.parse(text)
|
|
} catch {
|
|
return { error: text }
|
|
}
|
|
}
|