f6cb90cd70
- main.tsx 挂 AuthProvider 门控:未登录→登录页;must_change_password→强制改密页 - 登录页/改密页/顶栏身份(头像+姓名+角色徽章+退出)按 A·Data-console token 样式 - 全局 fetch 拦截器:任意业务请求 401 自动回登录页(会话过期/被撤销不留死表单) - 系统设置新增「成员与角色」分区(仅 admin 可见):名单、角色调整、启停、重置临时密码、新增成员 - 消灭全部手填操作人:审核/封批/确认计划集/范围规则/来源调度/UI 设置/开始处理/批量重试/ 目录差异确认共 9 处输入框删除,请求体不再携带 actor 字段(服务端从会话注入) - 测试断言方向翻转:显式验证请求体不含 operator_ref/created_by 等字段 - 附带修复:登录失败计数 SQL 参数类型无法推断导致 500(pgx 扩展协议),显式 cast 解决 前端门禁:tsc 0 错、125 用例全绿、build 通过
378 lines
16 KiB
TypeScript
378 lines
16 KiB
TypeScript
import { newOperationID } from './operationKey'
|
||
export type SourceConnectionType = 'pp' | 'alist' | 'dingtalk' | 'manual'
|
||
export type SourceHealthStatus = 'unknown' | 'healthy' | 'degraded' | 'unhealthy'
|
||
export type SourceScanState = 'pending' | 'running' | 'completed' | 'failed'
|
||
|
||
export interface SourceScanSummary {
|
||
id: string
|
||
state: SourceScanState
|
||
observed_items: number
|
||
unchanged_items: number
|
||
added_items: number
|
||
updated_items: number
|
||
over_limit_items: number
|
||
missing_items: number
|
||
planned_uploads: number
|
||
error_code: string
|
||
created_at: string
|
||
completed_at: string | null
|
||
}
|
||
|
||
export interface TriggeredSourceScan {
|
||
id: string
|
||
source_connection_id: string
|
||
source_type: SourceConnectionType
|
||
state: SourceScanState
|
||
created_at: string
|
||
}
|
||
|
||
export type SourceScanChangeType = 'added' | 'updated' | 'unavailable'
|
||
|
||
export interface SourceScanChangeView {
|
||
id: string
|
||
source_item_id: string | null
|
||
change_type: SourceScanChangeType
|
||
observed_size_bytes: number | null
|
||
created_at: string
|
||
}
|
||
|
||
export interface SourceScanDetail extends SourceScanSummary {
|
||
changes: SourceScanChangeView[]
|
||
}
|
||
|
||
export interface SourceScanPage {
|
||
items: SourceScanSummary[]
|
||
total: number
|
||
limit: number
|
||
offset: number
|
||
}
|
||
|
||
export interface SourceConnectionView {
|
||
id: string
|
||
source_type: SourceConnectionType
|
||
name: string
|
||
enabled: boolean
|
||
schedule_configured: boolean
|
||
health_status: SourceHealthStatus
|
||
item_count: number
|
||
updated_at: string
|
||
last_scan?: SourceScanSummary | null
|
||
}
|
||
|
||
export interface SourceConnectionSummary {
|
||
total: number
|
||
enabled: number
|
||
disabled: number
|
||
healthy: number
|
||
degraded: number
|
||
unhealthy: number
|
||
unknown: number
|
||
}
|
||
|
||
export interface SourceConnectionPage {
|
||
items: SourceConnectionView[]
|
||
summary: SourceConnectionSummary
|
||
}
|
||
|
||
const sourceTypes = new Set<SourceConnectionType>(['pp', 'alist', 'dingtalk', 'manual'])
|
||
const healthStatuses = new Set<SourceHealthStatus>(['unknown', 'healthy', 'degraded', 'unhealthy'])
|
||
const scanStates = new Set<SourceScanState>(['pending', 'running', 'completed', 'failed'])
|
||
const scanChangeTypes = new Set<SourceScanChangeType>(['added', 'updated', 'unavailable'])
|
||
const summaryKeys: ReadonlyArray<keyof SourceConnectionSummary> = [
|
||
'total', 'enabled', 'disabled', 'healthy', 'degraded', 'unhealthy', 'unknown',
|
||
]
|
||
|
||
export function parseSourceConnectionsResponse(value: unknown): SourceConnectionPage {
|
||
if (!isRecord(value) || !Array.isArray(value.data) || !isRecord(value.summary)) {
|
||
throw new Error('invalid source connections response')
|
||
}
|
||
const items = value.data.map((raw): SourceConnectionView => {
|
||
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.source_type !== 'string' ||
|
||
!sourceTypes.has(raw.source_type as SourceConnectionType) || typeof raw.name !== 'string' ||
|
||
typeof raw.enabled !== 'boolean' || typeof raw.schedule_configured !== 'boolean' ||
|
||
typeof raw.health_status !== 'string' || !healthStatuses.has(raw.health_status as SourceHealthStatus) ||
|
||
!isCount(raw.item_count) || typeof raw.updated_at !== 'string') {
|
||
throw new Error('invalid source connection item')
|
||
}
|
||
const lastScan = parseSourceScanSummary(raw.last_scan)
|
||
return {
|
||
id: raw.id,
|
||
source_type: raw.source_type as SourceConnectionType,
|
||
name: raw.name,
|
||
enabled: raw.enabled,
|
||
schedule_configured: raw.schedule_configured,
|
||
health_status: raw.health_status as SourceHealthStatus,
|
||
item_count: raw.item_count,
|
||
updated_at: raw.updated_at,
|
||
last_scan: lastScan,
|
||
}
|
||
})
|
||
const summary = {} as SourceConnectionSummary
|
||
for (const key of summaryKeys) {
|
||
const count = value.summary[key]
|
||
if (!isCount(count)) throw new Error('invalid source connection summary')
|
||
summary[key] = count
|
||
}
|
||
return { items, summary }
|
||
}
|
||
|
||
export interface SourceConnectionListItem {
|
||
id: string
|
||
source_type: SourceConnectionType
|
||
name: string
|
||
enabled: boolean
|
||
schedule_configured: boolean
|
||
health_status: string
|
||
item_count: number
|
||
updated_at: string
|
||
next_refresh_at: string | null
|
||
}
|
||
|
||
function parseConnectionList(raw: unknown): SourceConnectionListItem[] {
|
||
if (!Array.isArray(raw)) throw new Error('invalid source connections list')
|
||
return raw.map((item): SourceConnectionListItem => {
|
||
if (!isRecord(item) || typeof item.id !== 'string' || typeof item.source_type !== 'string' ||
|
||
typeof item.name !== 'string' || typeof item.enabled !== 'boolean' ||
|
||
typeof item.schedule_configured !== 'boolean' || typeof item.item_count !== 'number' ||
|
||
typeof item.updated_at !== 'string' ||
|
||
(item.next_refresh_at !== null && typeof item.next_refresh_at !== 'string')) {
|
||
throw new Error('invalid source connection item')
|
||
}
|
||
return {
|
||
id: item.id, source_type: item.source_type as SourceConnectionType, name: item.name,
|
||
enabled: item.enabled, schedule_configured: item.schedule_configured,
|
||
health_status: typeof item.health_status === 'string' ? item.health_status : '',
|
||
item_count: item.item_count, updated_at: item.updated_at,
|
||
next_refresh_at: (item.next_refresh_at as string | null) ?? null,
|
||
}
|
||
})
|
||
}
|
||
|
||
export async function fetchSourceConnectionList(signal?: AbortSignal): Promise<SourceConnectionListItem[]> {
|
||
const response = await fetch('/api/v1/source-connections', { headers: { Accept: 'application/json' }, signal })
|
||
if (!response.ok) throw new Error(`加载来源连接失败(HTTP ${response.status})`)
|
||
const value = await response.json() as unknown
|
||
if (!isRecord(value) || !Array.isArray(value.data)) throw new Error('invalid source connections response')
|
||
return parseConnectionList(value.data)
|
||
}
|
||
|
||
export async function fetchSourceConnections(signal?: AbortSignal): Promise<SourceConnectionPage> {
|
||
const response = await fetch('/api/v1/source-connections', {
|
||
headers: { Accept: 'application/json' },
|
||
signal,
|
||
})
|
||
if (!response.ok) {
|
||
throw new Error(`加载来源连接失败(HTTP ${response.status})`)
|
||
}
|
||
return parseSourceConnectionsResponse(await response.json())
|
||
}
|
||
|
||
export async function triggerSourceScan(connectionID: string): Promise<TriggeredSourceScan> {
|
||
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/scan`, {
|
||
method: 'POST',
|
||
headers: { Accept: 'application/json', 'Idempotency-Key': newOperationID() },
|
||
})
|
||
if (!response.ok) {
|
||
throw new Error(response.status === 409 ? '该来源已有扫描正在运行' : `触发来源扫描失败(HTTP ${response.status})`)
|
||
}
|
||
const raw = await response.json() as unknown
|
||
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.source_connection_id !== 'string' ||
|
||
typeof raw.source_type !== 'string' || !sourceTypes.has(raw.source_type as SourceConnectionType) ||
|
||
typeof raw.state !== 'string' || !scanStates.has(raw.state as SourceScanState) || typeof raw.created_at !== 'string') {
|
||
throw new Error('invalid triggered source scan response')
|
||
}
|
||
return {
|
||
id: raw.id,
|
||
source_connection_id: raw.source_connection_id,
|
||
source_type: raw.source_type as SourceConnectionType,
|
||
state: raw.state as SourceScanState,
|
||
created_at: raw.created_at,
|
||
}
|
||
}
|
||
|
||
export function parseSourceScansResponse(value: unknown): SourceScanPage {
|
||
if (!isRecord(value) || !Array.isArray(value.data) || !isCount(value.total) ||
|
||
!isCount(value.limit) || value.limit < 1 || !isCount(value.offset)) {
|
||
throw new Error('invalid source scans response')
|
||
}
|
||
return {
|
||
items: value.data.map((item) => {
|
||
const parsed = parseSourceScanSummary(item)
|
||
if (!parsed) throw new Error('invalid source scan summary')
|
||
return parsed
|
||
}),
|
||
total: value.total,
|
||
limit: value.limit,
|
||
offset: value.offset,
|
||
}
|
||
}
|
||
|
||
export function parseSourceScanDetailResponse(value: unknown): SourceScanDetail {
|
||
if (!isRecord(value) || !isRecord(value.data) || !Array.isArray(value.data.changes)) {
|
||
throw new Error('invalid source scan detail response')
|
||
}
|
||
const summary = parseSourceScanSummary(value.data)
|
||
if (!summary) throw new Error('invalid source scan detail response')
|
||
const changes = value.data.changes.map((raw): SourceScanChangeView => {
|
||
if (!isRecord(raw) || typeof raw.id !== 'string' ||
|
||
(raw.source_item_id !== null && typeof raw.source_item_id !== 'string') ||
|
||
typeof raw.change_type !== 'string' || !scanChangeTypes.has(raw.change_type as SourceScanChangeType) ||
|
||
(raw.observed_size_bytes !== null && !isCount(raw.observed_size_bytes)) || typeof raw.created_at !== 'string') {
|
||
throw new Error('invalid source scan change')
|
||
}
|
||
return {
|
||
id: raw.id,
|
||
source_item_id: raw.source_item_id as string | null,
|
||
change_type: raw.change_type as SourceScanChangeType,
|
||
observed_size_bytes: raw.observed_size_bytes as number | null,
|
||
created_at: raw.created_at,
|
||
}
|
||
})
|
||
return { ...summary, changes }
|
||
}
|
||
|
||
export async function fetchSourceScans(connectionID: string, limit = 20, offset = 0, signal?: AbortSignal): Promise<SourceScanPage> {
|
||
const params = new URLSearchParams({ limit: String(limit), offset: String(offset) })
|
||
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/scans?${params}`, {
|
||
headers: { Accept: 'application/json' }, signal,
|
||
})
|
||
if (!response.ok) throw new Error(`加载扫描历史失败(HTTP ${response.status})`)
|
||
return parseSourceScansResponse(await response.json())
|
||
}
|
||
|
||
export async function fetchSourceScanDetail(connectionID: string, scanID: string, signal?: AbortSignal): Promise<SourceScanDetail> {
|
||
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/scans/${encodeURIComponent(scanID)}`, {
|
||
headers: { Accept: 'application/json' }, signal,
|
||
})
|
||
if (!response.ok) throw new Error(`加载扫描详情失败(HTTP ${response.status})`)
|
||
return parseSourceScanDetailResponse(await response.json())
|
||
}
|
||
|
||
function parseSourceScanSummary(raw: unknown): SourceScanSummary | null {
|
||
if (raw === null || raw === undefined) return null
|
||
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.state !== 'string' ||
|
||
!scanStates.has(raw.state as SourceScanState) || typeof raw.error_code !== 'string' ||
|
||
typeof raw.created_at !== 'string' || (raw.completed_at !== null && typeof raw.completed_at !== 'string')) {
|
||
throw new Error('invalid source scan summary')
|
||
}
|
||
const countKeys = ['observed_items', 'unchanged_items', 'added_items', 'updated_items', 'over_limit_items', 'missing_items', 'planned_uploads'] as const
|
||
for (const key of countKeys) {
|
||
if (!isCount(raw[key])) throw new Error('invalid source scan summary')
|
||
}
|
||
return {
|
||
id: raw.id,
|
||
state: raw.state as SourceScanState,
|
||
observed_items: raw.observed_items as number,
|
||
unchanged_items: raw.unchanged_items as number,
|
||
added_items: raw.added_items as number,
|
||
updated_items: raw.updated_items as number,
|
||
over_limit_items: raw.over_limit_items as number,
|
||
missing_items: raw.missing_items as number,
|
||
planned_uploads: raw.planned_uploads as number,
|
||
error_code: raw.error_code,
|
||
created_at: raw.created_at,
|
||
completed_at: raw.completed_at as string | null,
|
||
}
|
||
}
|
||
|
||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||
return typeof value === 'object' && value !== null
|
||
}
|
||
|
||
function isCount(value: unknown): value is number {
|
||
return typeof value === 'number' && Number.isInteger(value) && value >= 0
|
||
}
|
||
|
||
// V1 source settings revisions -------------------------------------------------
|
||
|
||
export type SourceSettingsRevisionState = 'validated' | 'active' | 'retired' | 'failed'
|
||
|
||
export interface SourceSettingsRevision {
|
||
id: string
|
||
revision_number: number
|
||
state: SourceSettingsRevisionState
|
||
name: string
|
||
endpoint: string
|
||
credential_ref: string
|
||
enabled: boolean
|
||
refresh_interval_seconds: string
|
||
created_by: string
|
||
created_at: string
|
||
validated_at: string | null
|
||
activated_at: string | null
|
||
retired_at: string | null
|
||
}
|
||
|
||
export interface SourceSettingsDraftRequest {
|
||
source_type: SourceConnectionType
|
||
name: string
|
||
endpoint: string
|
||
credential_ref: string
|
||
enabled: boolean
|
||
schedule: { refresh_interval_seconds?: number | null }
|
||
}
|
||
|
||
const revisionStates = new Set<SourceSettingsRevisionState>(['validated', 'active', 'retired', 'failed'])
|
||
|
||
function parseSettingsRevision(raw: unknown): SourceSettingsRevision {
|
||
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.revision_number !== 'number' ||
|
||
typeof raw.state !== 'string' || !revisionStates.has(raw.state as SourceSettingsRevisionState) ||
|
||
typeof raw.name !== 'string' || typeof raw.endpoint !== 'string' || typeof raw.credential_ref !== 'string' ||
|
||
typeof raw.enabled !== 'boolean' || typeof raw.refresh_interval_seconds !== 'string' ||
|
||
typeof raw.created_by !== 'string' || typeof raw.created_at !== 'string' ||
|
||
(raw.validated_at !== null && typeof raw.validated_at !== 'string') ||
|
||
(raw.activated_at !== null && typeof raw.activated_at !== 'string') ||
|
||
(raw.retired_at !== null && typeof raw.retired_at !== 'string')) {
|
||
throw new Error('invalid source settings revision')
|
||
}
|
||
return {
|
||
id: raw.id, revision_number: raw.revision_number, state: raw.state as SourceSettingsRevisionState,
|
||
name: raw.name, endpoint: raw.endpoint, credential_ref: raw.credential_ref, enabled: raw.enabled,
|
||
refresh_interval_seconds: raw.refresh_interval_seconds, created_by: raw.created_by, created_at: raw.created_at,
|
||
validated_at: (raw.validated_at as string | null) ?? null,
|
||
activated_at: (raw.activated_at as string | null) ?? null,
|
||
retired_at: (raw.retired_at as string | null) ?? null,
|
||
}
|
||
}
|
||
|
||
export async function fetchSourceSettingsRevisions(connectionID: string, signal?: AbortSignal): Promise<SourceSettingsRevision[]> {
|
||
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions`, {
|
||
headers: { Accept: 'application/json' },
|
||
signal,
|
||
})
|
||
if (!response.ok) throw new Error(`加载来源设置历史失败(HTTP ${response.status})`)
|
||
const value = await response.json() as unknown
|
||
if (!isRecord(value) || !Array.isArray(value.data)) throw new Error('invalid source settings revisions response')
|
||
return value.data.map(parseSettingsRevision)
|
||
}
|
||
|
||
async function postSourceSettings(path: string, body: string, errorMessage: string): Promise<{ operation: string; id: string | null }> {
|
||
const response = await fetch(path, {
|
||
method: 'POST',
|
||
headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-Governance-Action': 'source-settings', 'Idempotency-Key': newOperationID() },
|
||
body,
|
||
})
|
||
if (!response.ok) {
|
||
let detail = ''
|
||
try {
|
||
const parsed = await response.json() as unknown
|
||
if (isRecord(parsed) && typeof parsed.error === 'string') detail = parsed.error
|
||
} catch { /* keep status-only message */ }
|
||
throw new Error(detail ? `${errorMessage}:${detail}` : `${errorMessage}(HTTP ${response.status})`)
|
||
}
|
||
const value = await response.json() as unknown
|
||
if (!isRecord(value) || (value.operation !== 'created' && value.operation !== 'replayed') || !isRecord(value.data)) throw new Error('invalid source settings response')
|
||
const id = typeof value.data.id === 'string' ? value.data.id : null
|
||
return { operation: value.operation, id }
|
||
}
|
||
|
||
export async function createSourceSettingsRevision(connectionID: string, draft: SourceSettingsDraftRequest): Promise<{ operation: string; id: string }> {
|
||
const result = await postSourceSettings(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions`, JSON.stringify(draft), '保存来源设置草稿失败')
|
||
if (!result.id) throw new Error('创建来源设置草稿未返回修订号')
|
||
return result as { operation: string; id: string }
|
||
}
|
||
|
||
export async function activateSourceSettingsRevision(connectionID: string, revisionID: string): Promise<{ operation: string }> {
|
||
return postSourceSettings(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions/${encodeURIComponent(revisionID)}/activate`, '{}', '启用来源设置修订失败')
|
||
}
|