Skip to content

Commit 7c53fb6

Browse files
Merge pull request #46 from Coder-soft/main
fix(api): exclude certain categories from resource normalization
2 parents 99629d1 + f2b9bc9 commit 7c53fb6

1 file changed

Lines changed: 34 additions & 25 deletions

File tree

src/lib/api.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ const fetchJson = async <T>(url: string): Promise<T | null> => {
4747
}
4848
};
4949

50-
const normalizeCategory = (category: string): Resource["category"] => {
50+
const normalizeCategory = (category: string): Resource["category"] | null => {
5151
if (category === "mcicons") return "minecraft-icons";
5252
if (category === "mcsounds") return "mcsounds";
53-
if (category === "resources") return "images";
53+
if (category === "resources") return null; // Skip: these are not images and shouldn't appear in the Images tab
5454
return category as Resource["category"];
5555
};
5656

@@ -70,14 +70,15 @@ const inferSubcategory = (url: string, category: string): string | undefined =>
7070
return undefined;
7171
};
7272

73-
const normalizeApiResource = (item: ApiResource, category: string): Resource => {
73+
const normalizeApiResource = (item: ApiResource, category: string): Resource | null => {
7474
const normalizedCategory = normalizeCategory(category);
75+
if (!normalizedCategory) return null; // Skip excluded categories
7576
let subcategory: string | undefined = item.subcategory || undefined;
76-
77+
7778
if (!subcategory && category === "presets") {
7879
subcategory = inferSubcategory(item.url, category);
7980
}
80-
81+
8182
return {
8283
id: item.id,
8384
title: item.title,
@@ -106,57 +107,65 @@ export const fetchCategories = async (): Promise<ApiCategories | null> => {
106107
};
107108

108109
export const fetchCategory = async (category: string): Promise<Resource[]> => {
109-
const cacheKey = `${CATEGORY_CACHE_PREFIX}${category}`;
110+
const normalized = normalizeCategory(category);
111+
if (!normalized) return [];
112+
113+
const cacheKey = `${CATEGORY_CACHE_PREFIX}${normalized}`;
110114
const cached = readCache<ApiResource[]>(cacheKey);
111115
if (cached && cached.length > 0) {
112-
return cached.map(item => normalizeApiResource(item, category));
116+
return cached.map(item => normalizeApiResource(item, normalized)).filter((r): r is Resource => r !== null);
113117
}
114118

115-
const apiCategory = toApiCategory(category);
119+
const apiCategory = toApiCategory(normalized);
116120
const data = await fetchJson<{ category: string; files: ApiResource[] }>(
117121
`${API_BASE}/category/${apiCategory}`
118122
);
119123
if (data?.files && data.files.length > 0) {
120124
try {
121125
writeCache(cacheKey, data.files);
122126
} catch (e) {
123-
console.warn(`Failed to cache category ${category}:`, e);
127+
console.warn(`Failed to cache category ${normalized}:`, e);
124128
}
125-
return data.files.map(item => normalizeApiResource(item, category));
129+
return data.files.map(item => normalizeApiResource(item, normalized)).filter((r): r is Resource => r !== null);
126130
}
127-
console.error(`Failed to fetch category ${category} from API`);
131+
console.error(`Failed to fetch category ${normalized} from API`);
128132
return [];
129133
};
130134

131135
export const fetchAllResources = async (): Promise<Resource[]> => {
132136
const cached = readCache<ApiAllResources>(ALL_RESOURCES_CACHE_KEY);
133137
if (cached && cached.categories && Object.keys(cached.categories).length > 0) {
134-
const categoryNames = Object.keys(cached.categories);
138+
const categoryNames = Object.keys(cached.categories)
139+
.map(normalizeCategory)
140+
.filter((c): c is Resource["category"] => c !== null);
135141
const existingCategoriesCache = readCache<ApiCategories>(CATEGORIES_CACHE_KEY);
136142
if (!existingCategoriesCache) {
137-
writeCache(CATEGORIES_CACHE_KEY, {
138-
categories: categoryNames,
139-
total: categoryNames.length
143+
writeCache(CATEGORIES_CACHE_KEY, {
144+
categories: categoryNames,
145+
total: categoryNames.length
140146
});
141147
}
142148
return Object.entries(cached.categories).flatMap(([category, items]) =>
143-
items.map(item => normalizeApiResource(item, category))
149+
items.map(item => normalizeApiResource(item, category)).filter((r): r is Resource => r !== null)
144150
);
145151
}
146152

147153
const data = await fetchJson<ApiAllResources>(`${API_BASE}/all`);
148154
if (data?.categories && Object.keys(data.categories).length > 0) {
149155
try {
150156
writeCache(ALL_RESOURCES_CACHE_KEY, data);
151-
const categoryNames = Object.keys(data.categories);
152-
writeCache(CATEGORIES_CACHE_KEY, {
153-
categories: categoryNames,
154-
total: categoryNames.length
157+
const categoryNames = Object.keys(data.categories)
158+
.map(normalizeCategory)
159+
.filter((c): c is Resource["category"] => c !== null);
160+
writeCache(CATEGORIES_CACHE_KEY, {
161+
categories: categoryNames,
162+
total: categoryNames.length
155163
});
156164
Object.entries(data.categories).forEach(([category, items]) => {
157-
if (items && items.length > 0) {
165+
const normalized = normalizeCategory(category);
166+
if (normalized && items && items.length > 0) {
158167
try {
159-
writeCache(`${CATEGORY_CACHE_PREFIX}${normalizeCategory(category)}`, items);
168+
writeCache(`${CATEGORY_CACHE_PREFIX}${normalized}`, items);
160169
} catch {
161170
// Ignore quota errors for individual category caches
162171
}
@@ -166,7 +175,7 @@ export const fetchAllResources = async (): Promise<Resource[]> => {
166175
console.warn("Failed to cache all resources (likely quota exceeded):", e);
167176
}
168177
return Object.entries(data.categories).flatMap(([category, items]) =>
169-
items.map(item => normalizeApiResource(item, category))
178+
items.map(item => normalizeApiResource(item, category)).filter((r): r is Resource => r !== null)
170179
);
171180
}
172181
console.error("Failed to fetch all resources from API");
@@ -178,7 +187,7 @@ export const refreshAllResources = async (): Promise<Resource[]> => {
178187
if (data?.categories) {
179188
writeCache(ALL_RESOURCES_CACHE_KEY, data);
180189
return Object.entries(data.categories).flatMap(([category, items]) =>
181-
items.map(item => normalizeApiResource(item, category))
190+
items.map(item => normalizeApiResource(item, category)).filter((r): r is Resource => r !== null)
182191
);
183192
}
184193
return [];
@@ -190,7 +199,7 @@ export const getResourceByCategory = async (category: string): Promise<Resource[
190199
if (allCached?.categories?.[apiCategory]) {
191200
return allCached.categories[apiCategory].map(item =>
192201
normalizeApiResource(item, category)
193-
);
202+
).filter((r): r is Resource => r !== null);
194203
}
195204

196205
return fetchCategory(category);

0 commit comments

Comments
 (0)