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. * Handles API requests to Fontshare API for fetching font metadata.
* Provides error handling, pagination support, and type-safe responses. * 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 * @see https://fontshare.com
*/ */
@@ -38,7 +42,7 @@ export interface FontshareParams extends QueryParams {
/** /**
* Search query to filter fonts * Search query to filter fonts
*/ */
search?: string; q?: string;
} }
/** /**
@@ -77,7 +81,7 @@ export async function fetchFontshareFonts(
params: FontshareParams = {}, params: FontshareParams = {},
): Promise<FontshareResponse> { ): Promise<FontshareResponse> {
const queryString = buildQueryString(params); const queryString = buildQueryString(params);
const url = `https://api.fontshare.com/v2${queryString}`; const url = `https://api.fontshare.com/v2/fonts${queryString}`;
try { try {
const response = await api.get<FontshareResponse>(url); const response = await api.get<FontshareResponse>(url);
@@ -107,7 +111,7 @@ export async function fetchFontshareFontBySlug(
slug: string, slug: string,
): Promise<FontshareFont | undefined> { ): Promise<FontshareFont | undefined> {
const response = await fetchFontshareFonts(); 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 * @example
* ```ts * ```ts
* const allFonts = await fetchAllFontshareFonts(); * const allFonts = await fetchAllFontshareFonts();
* console.log(`Found ${allFonts.items.length} fonts`); * console.log(`Found ${allFonts.fonts.length} fonts`);
* ``` * ```
*/ */
export async function fetchAllFontshareFonts( export async function fetchAllFontshareFonts(
@@ -137,10 +141,10 @@ export async function fetchAllFontshareFonts(
limit, limit,
}); });
allFonts.push(...response.items); allFonts.push(...response.fonts);
// Check if we've fetched all items // Check if we've fetched all items
if (response.items.length < limit) { if (response.fonts.length < limit) {
break; break;
} }
@@ -152,6 +156,6 @@ export async function fetchAllFontshareFonts(
return { return {
...firstResponse, ...firstResponse,
items: allFonts, fonts: allFonts,
}; };
} }

View File

@@ -4,6 +4,10 @@
* Handles API requests to Google Fonts API for fetching font metadata. * Handles API requests to Google Fonts API for fetching font metadata.
* Provides error handling, retry logic, and type-safe responses. * Provides error handling, retry logic, and type-safe responses.
* *
* Pagination: The Google Fonts API does NOT support pagination parameters.
* All fonts matching the query are returned in a single response.
* Use category, subset, or sort filters to reduce the result set if needed.
*
* @see https://developers.google.com/fonts/docs/developer_api * @see https://developers.google.com/fonts/docs/developer_api
*/ */
@@ -20,7 +24,7 @@ import type {
*/ */
export interface GoogleFontsParams extends QueryParams { export interface GoogleFontsParams extends QueryParams {
/** /**
* Google Fonts API key (optional for public endpoints) * Google Fonts API key (required for Google Fonts API v1)
*/ */
key?: string; key?: string;
/** /**
@@ -38,11 +42,11 @@ export interface GoogleFontsParams extends QueryParams {
/** /**
* Sort order for results * Sort order for results
*/ */
sort?: 'popularity' | 'alpha' | 'date' | 'style'; sort?: 'alpha' | 'date' | 'popularity' | 'style' | 'trending';
/** /**
* Cap the number of fonts returned * Cap the number of fonts returned
*/ */
capability?: string; capability?: 'VF' | 'WOFF2';
} }
/** /**
@@ -59,8 +63,10 @@ export type GoogleFontItem = FontItem;
/** /**
* Google Fonts API base URL * Google Fonts API base URL
* Note: Google Fonts API v1 requires an API key. For development/testing without a key,
* fonts may not load properly.
*/ */
const GOOGLE_FONTS_API_URL = 'https://fonts.googleapis.com/v2/fonts' as const; const GOOGLE_FONTS_API_URL = 'https://www.googleapis.com/webfonts/v1/webfonts' as const;
/** /**
* Fetch fonts from Google Fonts API * Fetch fonts from Google Fonts API