Make English the repository working language (#5)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,7 +19,7 @@ export function badRequest(message: string): HttpError {
|
||||
export function pathParam(c: Context, name: string): string {
|
||||
const v = c.req.param(name);
|
||||
if (v === undefined || v === "") {
|
||||
throw new HttpError(404, "not_found", "路径参数缺失。");
|
||||
throw new HttpError(404, "not_found", "Missing path parameter.");
|
||||
}
|
||||
return v;
|
||||
}
|
||||
@@ -35,7 +35,7 @@ export function pathParam(c: Context, name: string): string {
|
||||
export function requireValidId(c: Context, name: string): string {
|
||||
const v = pathParam(c, name);
|
||||
if (!isValidId(v)) {
|
||||
throw new HttpError(404, "not_found", "资源不存在或无权访问。");
|
||||
throw new HttpError(404, "not_found", "Resource does not exist or you do not have access.");
|
||||
}
|
||||
return v;
|
||||
}
|
||||
@@ -43,7 +43,7 @@ export function requireValidId(c: Context, name: string): string {
|
||||
/** Parse a positive-integer path parameter (e.g. Trace file index). */
|
||||
export function positiveIntParam(c: Context, name: string): number {
|
||||
const v = Number.parseInt(pathParam(c, name), 10);
|
||||
if (!Number.isInteger(v) || v < 1) throw badRequest(`${name} 必须是正整数。`);
|
||||
if (!Number.isInteger(v) || v < 1) throw badRequest(`${name} must be a positive integer.`);
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -51,9 +51,10 @@ export function positiveIntParam(c: Context, name: string): number {
|
||||
export function paginationQuery(c: Context): { offset: number; limit: number } {
|
||||
const offset = Number.parseInt(c.req.query("offset") ?? "0", 10);
|
||||
const limit = Number.parseInt(c.req.query("limit") ?? "200", 10);
|
||||
if (!Number.isInteger(offset) || offset < 0) throw badRequest("offset 必须是非负整数。");
|
||||
if (!Number.isInteger(offset) || offset < 0)
|
||||
throw badRequest("offset must be a non-negative integer.");
|
||||
if (!Number.isInteger(limit) || limit < 1 || limit > 1000) {
|
||||
throw badRequest("limit 必须是 1~1000 的整数。");
|
||||
throw badRequest("limit must be an integer between 1 and 1000.");
|
||||
}
|
||||
return { offset, limit };
|
||||
}
|
||||
@@ -64,10 +65,10 @@ export async function readJson(c: Context): Promise<Record<string, unknown>> {
|
||||
try {
|
||||
body = await c.req.json();
|
||||
} catch {
|
||||
throw badRequest("请求体必须是合法 JSON。");
|
||||
throw badRequest("Request body must be valid JSON.");
|
||||
}
|
||||
if (body === null || typeof body !== "object" || Array.isArray(body)) {
|
||||
throw badRequest("请求体必须是 JSON 对象。");
|
||||
throw badRequest("Request body must be a JSON object.");
|
||||
}
|
||||
return body as Record<string, unknown>;
|
||||
}
|
||||
@@ -87,15 +88,15 @@ export function requireString(
|
||||
): string {
|
||||
const v = obj[key];
|
||||
const label = rule.label ?? key;
|
||||
if (typeof v !== "string") throw badRequest(`${label} 必须是字符串。`);
|
||||
if (typeof v !== "string") throw badRequest(`${label} must be a string.`);
|
||||
if (rule.minLen !== undefined && v.length < rule.minLen) {
|
||||
throw badRequest(`${label} 长度至少 ${rule.minLen} 个字符。`);
|
||||
throw badRequest(`${label} must be at least ${rule.minLen} characters.`);
|
||||
}
|
||||
if (rule.maxLen !== undefined && v.length > rule.maxLen) {
|
||||
throw badRequest(`${label} 长度不能超过 ${rule.maxLen} 个字符。`);
|
||||
throw badRequest(`${label} must be at most ${rule.maxLen} characters.`);
|
||||
}
|
||||
if (rule.pattern !== undefined && !rule.pattern.test(v)) {
|
||||
throw badRequest(`${label} 格式不合法。`);
|
||||
throw badRequest(`${label} has an invalid format.`);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
@@ -117,7 +118,7 @@ export function requireEnum<T extends string>(
|
||||
): T {
|
||||
const v = obj[key];
|
||||
if (typeof v !== "string" || !(values as readonly string[]).includes(v)) {
|
||||
throw badRequest(`${label} 必须是 ${values.join(" / ")} 之一。`);
|
||||
throw badRequest(`${label} must be one of ${values.join(" / ")}.`);
|
||||
}
|
||||
return v as T;
|
||||
}
|
||||
@@ -150,12 +151,12 @@ export function optionalNumber(
|
||||
const v = obj[key];
|
||||
if (v === undefined) return undefined;
|
||||
const label = rule.label ?? key;
|
||||
if (typeof v !== "number" || !Number.isFinite(v)) throw badRequest(`${label} 必须是数字。`);
|
||||
if (rule.integer && !Number.isInteger(v)) throw badRequest(`${label} 必须是整数。`);
|
||||
if (typeof v !== "number" || !Number.isFinite(v)) throw badRequest(`${label} must be a number.`);
|
||||
if (rule.integer && !Number.isInteger(v)) throw badRequest(`${label} must be an integer.`);
|
||||
if (rule.positiveOrMinusOne && !(v > 0 || v === -1)) {
|
||||
throw badRequest(`${label} 必须大于 0 或为 -1。`);
|
||||
throw badRequest(`${label} must be greater than 0 or equal to -1.`);
|
||||
}
|
||||
if (rule.nonNegative && v < 0) throw badRequest(`${label} 不能为负数。`);
|
||||
if (rule.nonNegative && v < 0) throw badRequest(`${label} must not be negative.`);
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -163,7 +164,7 @@ export function optionalNumber(
|
||||
export function optionalDateParam(value: string | undefined, label: string): string | undefined {
|
||||
if (value === undefined || value === "") return undefined;
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
||||
throw badRequest(`${label} 必须是 YYYY-MM-DD 格式。`);
|
||||
throw badRequest(`${label} must be in YYYY-MM-DD format.`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user