Restrict schema parsing to a subset of fields

This commit is contained in:
CPTProgrammer 2026-07-15 09:58:36 +08:00
parent f2f5db5c17
commit 79d21571d9
No known key found for this signature in database
8 changed files with 22 additions and 21 deletions

View File

@ -9,16 +9,5 @@ describe("getMod", () => {
test("GET /v1/mods/{modId}", async () => {
const response = await getMod(client, 238222);
const mod = response.data;
expect(mod.id).toBe(238222);
expect(mod.name).toBeTruthy();
expect(mod.slug).toBeTruthy();
expect(typeof mod.summary).toBe("string");
expect(typeof mod.downloadCount).toBe("number");
expect(Array.isArray(mod.authors)).toBe(true);
expect(mod.authors.length).toBeGreaterThan(0);
expect(Array.isArray(mod.categories)).toBe(true);
expect(Array.isArray(mod.latestFiles)).toBe(true);
expect(Array.isArray(mod.latestFilesIndexes)).toBe(true);
expect(mod.logo).toBeDefined();
});
});

View File

@ -9,12 +9,6 @@ describe("getProject", () => {
test("GET /project/{id|slug} returns a valid Project", async () => {
const project = await getProject(client, "fabric-api");
expect(project).toBeDefined();
expect(typeof project.id).toBe("string");
expect(project.slug).toBe("fabric-api");
expect(Array.isArray(project.versions)).toBe(true);
expect(Array.isArray(project.game_versions)).toBe(true);
expect(Array.isArray(project.loaders)).toBe(true);
expect(Array.isArray(project.gallery)).toBe(true);
});
});
@ -23,9 +17,5 @@ describe("getProjects", () => {
const projects = await getProjects(client, ["fabric-api", "sodium"]);
expect(Array.isArray(projects)).toBe(true);
expect(projects.length).toBe(2);
for (const project of projects) {
expect(typeof project.id).toBe("string");
expect(Array.isArray(project.versions)).toBe(true);
}
});
});

View File

@ -171,6 +171,8 @@ export const CurseForgeFileSchema = z.object({
fileFingerprint: z.number().int(),
/** 文件模块列表 */
modules: z.array(FileModuleSchema),
}).pick({
fileName: true,
});
export type CurseForgeFile = z.infer<typeof CurseForgeFileSchema>;
// endregion

View File

@ -8,6 +8,9 @@ export const GameVersionTypeSchema = z.object({
name: z.string(),
/** 类型标识 */
slug: z.string(),
}).pick({
id: true,
name: true,
});
export type GameVersionType = z.infer<typeof GameVersionTypeSchema>;
// endregion
@ -24,6 +27,10 @@ export const GameVersionSchema = z.object({
slug: z.string(),
/** API 版本号,无则为 null 或空字符串 */
apiVersion: z.string().nullable().optional(),
}).pick({
id: true,
gameVersionTypeID: true,
name: true,
});
export type GameVersion = z.infer<typeof GameVersionSchema>;
// endregion
@ -50,6 +57,8 @@ export const MinecraftGameVersionSchema = z.object({
gameVersionStatus: z.union([z.literal(1), z.literal(2), z.literal(3)]),
/** 版本类型状态1=Normal 2=Deleted */
gameVersionTypeStatus: z.union([z.literal(1), z.literal(2)]),
}).pick({
versionString: true,
});
export type MinecraftGameVersion = z.infer<typeof MinecraftGameVersionSchema>;
// endregion

View File

@ -167,6 +167,8 @@ export const ModSchema = z.object({
thumbsUpCount: z.number().int(),
/** 评分,无则为 null 或被省略 */
rating: z.number().nullable().optional(),
}).pick({
name: true,
});
export type Mod = z.infer<typeof ModSchema>;
// endregion

View File

@ -137,6 +137,8 @@ export const ProjectSchema = z.object({
loaders: z.array(z.string()),
/** 图库图片列表 */
gallery: z.array(GalleryImageSchema),
}).pick({
title: true,
});
export type Project = z.infer<typeof ProjectSchema>;
// endregion

View File

@ -8,6 +8,8 @@ export const LoaderTagSchema = z.object({
name: z.string(),
/** 该加载器适用的项目类型列表 */
supported_project_types: z.array(z.string()),
}).pick({
name: true,
});
export type LoaderTag = z.infer<typeof LoaderTagSchema>;
// endregion
@ -22,6 +24,9 @@ export const GameVersionTagSchema = z.object({
date: z.string(),
/** 是否为主要版本,用于 Featured Versions 标记 */
major: z.boolean(),
}).pick({
version: true,
version_type: true,
});
export type GameVersionTag = z.infer<typeof GameVersionTagSchema>;
// endregion

View File

@ -113,6 +113,8 @@ export const VersionSchema = BaseVersionSchema.extend({
changelog_url: z.string().nullable(),
/** 该版本的所有文件列表 */
files: z.array(VersionFileSchema),
}).pick({
version_number: true,
});
export type Version = z.infer<typeof VersionSchema>;
// endregion