mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-08 14:07:54 +08:00
* Add(route): add Google Fonts and change to V2 * Fix(route): added throw error when API key is underfined * Fix(docs): add warning container * Fix(docs): Update docs/design.md Co-authored-by: Tony <TonyRL@users.noreply.github.com> * Fix(route): sort routes * Fix(route): Update lib/v2/google/fonts.js Co-authored-by: Tony <TonyRL@users.noreply.github.com> Co-authored-by: Tony <TonyRL@users.noreply.github.com>
44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
const got = require('@/utils/got');
|
|
const config = require('@/config').value;
|
|
const { art } = require('@/utils/render');
|
|
const path = require('path');
|
|
const { parseDate } = require('@/utils/parse-date');
|
|
|
|
const titleMap = {
|
|
date: 'Newest',
|
|
popularity: 'Most Popular',
|
|
trending: 'Trending',
|
|
alpha: 'Name',
|
|
style: 'Number of styles',
|
|
};
|
|
|
|
module.exports = async (ctx) => {
|
|
const sort = ctx.params.sort ?? 'date';
|
|
const limit = ctx.params.limit ?? 25;
|
|
|
|
const API_KEY = config.google.fontsApiKey;
|
|
if (!API_KEY) {
|
|
throw new Error('Google Fonts API key is required.');
|
|
}
|
|
|
|
const googleFontsAPI = `https://www.googleapis.com/webfonts/v1/webfonts?sort=${sort}&key=${API_KEY}`;
|
|
|
|
const response = await got.get(googleFontsAPI);
|
|
const data = response.data.items.slice(0, limit);
|
|
|
|
ctx.state.data = {
|
|
title: `Google Fonts - ${titleMap[sort]}`,
|
|
link: 'https://fonts.google.com',
|
|
item:
|
|
data &&
|
|
data.map((item) => ({
|
|
title: item.family,
|
|
description: art(path.join(__dirname, './templates/fonts.art'), {
|
|
item,
|
|
}),
|
|
link: `https://fonts.google.com/specimen/${item.family.replace(/\s/g, '+')}`,
|
|
pubDate: parseDate(item.lastModified, 'YYYY-MM-DD'),
|
|
})),
|
|
};
|
|
};
|