fix: minor type changes for fonts

This commit is contained in:
Ilia Mashkov
2026-01-13 19:54:56 +03:00
parent 5fcb381b11
commit 4c9b9f631f
2 changed files with 21 additions and 11 deletions

View File

@@ -4,6 +4,10 @@
* Handles API requests to Fontshare API for fetching font metadata.
* Provides error handling, pagination support, and type-safe responses.
*
* Pagination: The Fontshare API DOES support pagination via `page` and `limit` parameters.
* However, the current implementation uses `fetchAllFontshareFonts()` to fetch all fonts upfront.
* For future optimization, consider implementing incremental pagination for large datasets.
*
* @see https://fontshare.com
*/
@@ -38,7 +42,7 @@ export interface FontshareParams extends QueryParams {
/**
* Search query to filter fonts
*/
search?: string;
q?: string;
}
/**
@@ -77,7 +81,7 @@ export async function fetchFontshareFonts(
params: FontshareParams = {},
): Promise<FontshareResponse> {
const queryString = buildQueryString(params);
const url = `https://api.fontshare.com/v2${queryString}`;
const url = `https://api.fontshare.com/v2/fonts${queryString}`;
try {
const response = await api.get<FontshareResponse>(url);
@@ -107,7 +111,7 @@ export async function fetchFontshareFontBySlug(
slug: string,
): Promise<FontshareFont | undefined> {
const response = await fetchFontshareFonts();
return response.items.find(font => font.slug === slug);
return response.fonts.find(font => font.slug === slug);
}
/**
@@ -120,7 +124,7 @@ export async function fetchFontshareFontBySlug(
* @example
* ```ts
* const allFonts = await fetchAllFontshareFonts();
* console.log(`Found ${allFonts.items.length} fonts`);
* console.log(`Found ${allFonts.fonts.length} fonts`);
* ```
*/
export async function fetchAllFontshareFonts(
@@ -137,10 +141,10 @@ export async function fetchAllFontshareFonts(
limit,
});
allFonts.push(...response.items);
allFonts.push(...response.fonts);
// Check if we've fetched all items
if (response.items.length < limit) {
if (response.fonts.length < limit) {
break;
}
@@ -152,6 +156,6 @@ export async function fetchAllFontshareFonts(
return {
...firstResponse,
items: allFonts,
fonts: allFonts,
};
}