收紧后端 TypeScript:统一 Json 类型与 TaskContext 契约
关闭 allowJs;集中 JsonObject 定义并替换分散 any 别名;parseTaskContext 返回 TaskContext;任务详情 API 标注 AdminTaskDetailView;测试减少 as any。
This commit is contained in:
@@ -6,6 +6,7 @@ import util from 'node:util'
|
||||
import { PROJECT_ROOT, runtimeConfig } from '../config/runtime.js'
|
||||
import type { HttpErrorLike } from './http.js'
|
||||
import { maskSecret } from './masking.js'
|
||||
import { asJsonObject } from '../types/json.js'
|
||||
|
||||
type LogLevel = 'debug' | 'info' | 'warn' | 'error'
|
||||
type LogChannel = 'app' | 'integration'
|
||||
@@ -362,7 +363,7 @@ function isSensitiveLogKey(key: unknown): boolean {
|
||||
function normalizeExternalHttpPacketDetail(
|
||||
detail: ExternalHttpPacketDetail,
|
||||
): ExternalHttpPacketDetail {
|
||||
const source = detail && typeof detail === 'object' && !Array.isArray(detail) ? detail : {}
|
||||
const source = asJsonObject(detail)
|
||||
const normalized: ExternalHttpPacketDetail = {}
|
||||
|
||||
for (const [key, value] of Object.entries(source)) {
|
||||
|
||||
@@ -1,26 +1,34 @@
|
||||
export type JsonRecord = Record<string, any>
|
||||
import type { JsonObject, JsonRecord } from '../types/json.js'
|
||||
import { asJsonObject, isPlainObject } from '../types/json.js'
|
||||
import type { TaskContext } from '../types/task-context.js'
|
||||
|
||||
export function parseJsonObject(value: unknown): JsonRecord {
|
||||
export type { JsonRecord, JsonObject }
|
||||
|
||||
export function parseJsonObject(value: unknown): JsonObject {
|
||||
if (!value) {
|
||||
return {}
|
||||
}
|
||||
|
||||
if (typeof value === 'object' && !Array.isArray(value)) {
|
||||
return value as JsonRecord
|
||||
if (isPlainObject(value)) {
|
||||
return value
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(String(value || '{}'))
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {}
|
||||
const parsed = JSON.parse(String(value || '{}')) as unknown
|
||||
return asJsonObject(parsed)
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
export function parseTaskContext(task: { context_json?: unknown } | null | undefined): JsonRecord {
|
||||
return parseJsonObject(task?.context_json)
|
||||
export function parseTaskContext(
|
||||
task: { context_json?: unknown } | null | undefined,
|
||||
): TaskContext {
|
||||
return parseJsonObject(task?.context_json) as TaskContext
|
||||
}
|
||||
|
||||
export function parseTaskState(task: { state_json?: unknown } | null | undefined): JsonRecord {
|
||||
export function parseTaskState(
|
||||
task: { state_json?: unknown } | null | undefined,
|
||||
): JsonObject {
|
||||
return parseJsonObject(task?.state_json)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user