79 lines
2 KiB
TypeScript
79 lines
2 KiB
TypeScript
export interface GuanghuEnterpriseDomain {
|
|
accessState: string
|
|
id: string
|
|
name: string
|
|
}
|
|
|
|
export interface GuanghuEnterpriseStatus {
|
|
domains: GuanghuEnterpriseDomain[]
|
|
execution: string
|
|
hostState: string
|
|
nodeId: string
|
|
}
|
|
|
|
export type GuanghuEnterpriseState =
|
|
| {
|
|
checkedAt: null
|
|
error: null
|
|
phase: 'checking'
|
|
status: null
|
|
}
|
|
| {
|
|
checkedAt: number
|
|
error: null
|
|
phase: 'online'
|
|
status: GuanghuEnterpriseStatus
|
|
}
|
|
| {
|
|
checkedAt: number
|
|
error: string
|
|
phase: 'error'
|
|
status: null
|
|
}
|
|
|
|
export const INITIAL_GUANGHU_ENTERPRISE_STATE: GuanghuEnterpriseState = {
|
|
checkedAt: null,
|
|
error: null,
|
|
phase: 'checking',
|
|
status: null,
|
|
}
|
|
|
|
function record(value: unknown): Record<string, unknown> | null {
|
|
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
? value as Record<string, unknown>
|
|
: null
|
|
}
|
|
|
|
function requiredText(value: unknown): string | null {
|
|
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null
|
|
}
|
|
|
|
export function parseGuanghuEnterpriseStatus(value: unknown): GuanghuEnterpriseStatus {
|
|
const payload = record(value)
|
|
const nodeId = requiredText(payload?.node_id)
|
|
const hostState = requiredText(payload?.host_state)
|
|
const domains = Array.isArray(payload?.domains) ? payload.domains : null
|
|
if (!payload || !nodeId || !hostState || !domains) {
|
|
throw new Error('guanghu_enterprise_status_invalid')
|
|
}
|
|
|
|
const projectedDomains = domains.map(item => {
|
|
const domain = record(item)
|
|
const id = requiredText(domain?.id)
|
|
const name = requiredText(domain?.name)
|
|
const accessState = requiredText(domain?.access_state)
|
|
if (!domain || !id || !name || !accessState) {
|
|
throw new Error('guanghu_enterprise_status_invalid')
|
|
}
|
|
return { accessState, id, name }
|
|
})
|
|
|
|
return {
|
|
domains: projectedDomains,
|
|
execution: requiredText(payload.execution) ?? (
|
|
payload.raw_shell === 'rejected' ? 'disabled' : 'unknown'
|
|
),
|
|
hostState,
|
|
nodeId,
|
|
}
|
|
}
|