32 lines
1.2 KiB
TypeScript
32 lines
1.2 KiB
TypeScript
import {
|
|
type FontshareParams,
|
|
fetchFontshareFonts,
|
|
} from '../../api';
|
|
import { normalizeFontshareFonts } from '../../lib';
|
|
import type { UnifiedFont } from '../types';
|
|
|
|
/**
|
|
* Query function for fetching fonts from Fontshare.
|
|
*
|
|
* @param params - The parameters for fetching fonts from Fontshare (E.g. search query, page number, etc.).
|
|
* @returns A promise that resolves with an array of UnifiedFont objects representing the fonts found in Fontshare.
|
|
*/
|
|
export async function fetchFontshareFontsQuery(params: FontshareParams): Promise<UnifiedFont[]> {
|
|
try {
|
|
const response = await fetchFontshareFonts(params);
|
|
return normalizeFontshareFonts(response.fonts);
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('Failed to fetch')) {
|
|
throw new Error(
|
|
'Unable to connect to Fontshare. Please check your internet connection.',
|
|
);
|
|
}
|
|
if (error.message.includes('404')) {
|
|
throw new Error('Font not found in Fontshare catalog.');
|
|
}
|
|
}
|
|
throw new Error('Failed to load fonts from Fontshare.');
|
|
}
|
|
}
|