refactor: 消除 es-toolkit/compat 依赖,isArray/isObject 替换为原生实现
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { SQL } from "bun";
|
||||
import { isError } from "es-toolkit";
|
||||
import { isArray } from "es-toolkit/compat";
|
||||
|
||||
import type { CheckResult, RawTargetConfig } from "../../types";
|
||||
import type { CheckerContext, CheckerDefinition, CheckerValidationInput, ResolveContext } from "../types";
|
||||
@@ -126,7 +125,7 @@ export class DbChecker implements CheckerDefinition<ResolvedDbTarget> {
|
||||
durationMs,
|
||||
failure: durationResult.failure,
|
||||
matched: false,
|
||||
statusDetail: `${isArray(rows) ? rows.length : 0} rows`,
|
||||
statusDetail: `${Array.isArray(rows) ? rows.length : 0} rows`,
|
||||
targetId: t.id,
|
||||
timestamp,
|
||||
};
|
||||
@@ -134,13 +133,13 @@ export class DbChecker implements CheckerDefinition<ResolvedDbTarget> {
|
||||
|
||||
// rowCount 断言
|
||||
if (t.expect?.rowCount) {
|
||||
const rowCountResult = checkRowCount(isArray(rows) ? rows.length : 0, t.expect.rowCount);
|
||||
const rowCountResult = checkRowCount(Array.isArray(rows) ? rows.length : 0, t.expect.rowCount);
|
||||
if (!rowCountResult.matched) {
|
||||
return {
|
||||
durationMs,
|
||||
failure: rowCountResult.failure,
|
||||
matched: false,
|
||||
statusDetail: `${isArray(rows) ? rows.length : 0} rows`,
|
||||
statusDetail: `${Array.isArray(rows) ? rows.length : 0} rows`,
|
||||
targetId: t.id,
|
||||
timestamp,
|
||||
};
|
||||
@@ -155,7 +154,7 @@ export class DbChecker implements CheckerDefinition<ResolvedDbTarget> {
|
||||
durationMs,
|
||||
failure: rowsResult.failure,
|
||||
matched: false,
|
||||
statusDetail: `${isArray(rows) ? rows.length : 0} rows`,
|
||||
statusDetail: `${Array.isArray(rows) ? rows.length : 0} rows`,
|
||||
targetId: t.id,
|
||||
timestamp,
|
||||
};
|
||||
@@ -163,7 +162,7 @@ export class DbChecker implements CheckerDefinition<ResolvedDbTarget> {
|
||||
}
|
||||
|
||||
if (t.expect?.result && t.expect.result.length > 0) {
|
||||
const rowCount = isArray(rows) ? rows.length : 0;
|
||||
const rowCount = Array.isArray(rows) ? rows.length : 0;
|
||||
const resultCheck = checkContentRules({ rowCount, rows }, t.expect.result, { path: "result", phase: "result" });
|
||||
if (!resultCheck.matched) {
|
||||
return {
|
||||
@@ -181,7 +180,7 @@ export class DbChecker implements CheckerDefinition<ResolvedDbTarget> {
|
||||
durationMs,
|
||||
failure: null,
|
||||
matched: true,
|
||||
statusDetail: `${isArray(rows) ? rows.length : 0} rows`,
|
||||
statusDetail: `${Array.isArray(rows) ? rows.length : 0} rows`,
|
||||
targetId: t.id,
|
||||
timestamp,
|
||||
};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { isPlainObject } from "es-toolkit";
|
||||
import { isArray } from "es-toolkit/compat";
|
||||
|
||||
import type { ExpectResult, KeyValueExpect, ValueMatcher } from "../../expect/types";
|
||||
|
||||
@@ -16,7 +15,7 @@ export function checkRowCount(actual: number, matcher: ValueMatcher): ExpectResu
|
||||
}
|
||||
|
||||
export function checkRows(rows: unknown, rules: KeyValueExpect[]): ExpectResult {
|
||||
if (!isArray(rows)) {
|
||||
if (!Array.isArray(rows)) {
|
||||
return {
|
||||
failure: mismatchFailure("row", "rows", rules, rows, "查询结果不是数组"),
|
||||
matched: false,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { isPlainObject, isString } from "es-toolkit";
|
||||
import { isArray } from "es-toolkit/compat";
|
||||
|
||||
import type { ConfigValidationIssue } from "../../schema/issues";
|
||||
import type { CheckerValidationInput } from "../types";
|
||||
@@ -54,7 +53,7 @@ function validateDbExpect(target: Record<string, unknown>, path: string): Config
|
||||
}
|
||||
|
||||
if (expect["rows"] !== undefined) {
|
||||
if (!isArray(expect["rows"])) {
|
||||
if (!Array.isArray(expect["rows"])) {
|
||||
issues.push(issue("invalid-type", joinPath(expectPath, "rows"), "必须为数组", targetName));
|
||||
} else {
|
||||
issues.push(...collectRowExpects(expect["rows"], joinPath(expectPath, "rows"), targetName));
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { isError } from "es-toolkit";
|
||||
import { isObject } from "es-toolkit/compat";
|
||||
|
||||
import type { CheckResult, RawTargetConfig } from "../../types";
|
||||
import type { CheckerContext, CheckerDefinition, CheckerValidationInput, ResolveContext } from "../types";
|
||||
@@ -161,7 +160,10 @@ function buildRedirectInit(init: RequestInit, statusCode: number, fromUrl: strin
|
||||
const method = init.method?.toUpperCase();
|
||||
|
||||
if (statusCode === 303 || ((statusCode === 301 || statusCode === 302) && method === "POST")) {
|
||||
const headers = isObject(init.headers) ? { ...(init.headers as Record<string, string>) } : undefined;
|
||||
const headers =
|
||||
typeof init.headers === "object" && init.headers !== null
|
||||
? { ...(init.headers as Record<string, string>) }
|
||||
: undefined;
|
||||
if (headers) {
|
||||
for (const key of Object.keys(headers)) {
|
||||
const lower = key.toLowerCase();
|
||||
@@ -176,7 +178,7 @@ function buildRedirectInit(init: RequestInit, statusCode: number, fromUrl: strin
|
||||
try {
|
||||
const fromOrigin = new URL(fromUrl).origin;
|
||||
const toOrigin = new URL(toUrl).origin;
|
||||
if (fromOrigin !== toOrigin && isObject(newInit.headers)) {
|
||||
if (fromOrigin !== toOrigin && typeof newInit.headers === "object" && newInit.headers !== null) {
|
||||
const headers = { ...(newInit.headers as Record<string, string>) };
|
||||
for (const key of Object.keys(headers)) {
|
||||
if (SENSITIVE_HEADERS.has(key.toLowerCase())) {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { isNumber, isString } from "es-toolkit";
|
||||
import { isArray } from "es-toolkit/compat";
|
||||
|
||||
import type { ConfigValidationIssue } from "../../schema/issues";
|
||||
import type { CheckerValidationInput } from "../types";
|
||||
@@ -76,7 +75,7 @@ function validateHttpExpect(target: Record<string, unknown>, path: string): Conf
|
||||
issues.push(...validateContentRules(expect["body"], joinPath(expectPath, "body"), targetName));
|
||||
}
|
||||
|
||||
if (isArray(expect["status"])) {
|
||||
if (Array.isArray(expect["status"])) {
|
||||
issues.push(...validateStatusValues(expect["status"], joinPath(expectPath, "status"), targetName));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { isBoolean, isNumber, isString } from "es-toolkit";
|
||||
import { isArray } from "es-toolkit/compat";
|
||||
|
||||
import type { ConfigValidationIssue } from "../../schema/issues";
|
||||
import type { CheckerValidationInput } from "../types";
|
||||
@@ -73,7 +72,7 @@ function validateLlmExpect(
|
||||
const issues: ConfigValidationIssue[] = [];
|
||||
const expectPath = joinPath(path, "expect");
|
||||
|
||||
if (isArray(expect["status"])) {
|
||||
if (Array.isArray(expect["status"])) {
|
||||
issues.push(...validateStatusValues(expect["status"], joinPath(expectPath, "status"), targetName));
|
||||
}
|
||||
if (expect["headers"] !== undefined) {
|
||||
@@ -144,7 +143,7 @@ function validateLlmOptions(options: unknown, path: string, targetName?: string)
|
||||
}
|
||||
|
||||
if (options["stopSequences"] !== undefined) {
|
||||
if (!isArray(options["stopSequences"])) {
|
||||
if (!Array.isArray(options["stopSequences"])) {
|
||||
issues.push(issue("invalid-type", joinPath(path, "stopSequences"), "必须为字符串数组", targetName));
|
||||
} else {
|
||||
for (let i = 0; i < options["stopSequences"].length; i++) {
|
||||
|
||||
Reference in New Issue
Block a user