feat: support /collections request

This commit is contained in:
Vjacheslav Trushkin 2022-10-15 10:23:07 +03:00
parent 66c6b93ac4
commit 5b78c43da6
5 changed files with 78 additions and 9 deletions

View File

@ -16,13 +16,25 @@ export function setImporters(items: Importer[]) {
/**
* All prefixes, sorted
*/
let prefixes: string[] = [];
let allPrefixes: string[] = [];
let prefixesWithInfo: string[] = [];
let visiblePrefixes: string[] = [];
/**
* Get all prefixes
*/
export function getPrefixes(): string[] {
return prefixes;
type GetPrefixes = 'all' | 'info' | 'visible';
export function getPrefixes(type: GetPrefixes = 'all'): string[] {
switch (type) {
case 'all':
return allPrefixes;
case 'info':
return prefixesWithInfo;
case 'visible':
return visiblePrefixes;
}
}
/**
@ -45,6 +57,8 @@ export function updateIconSets(): number {
const newLoadedIconSets: Set<StoredIconSet> = new Set();
const newPrefixes: Set<string> = new Set();
const newPrefixesWithInfo: Set<string> = new Set();
const newVisiblePrefixes: Set<string> = new Set();
importers.forEach((importer, importerIndex) => {
const data = importer.data;
@ -63,7 +77,16 @@ export function updateIconSets(): number {
// Add prefix, but delete it first to keep order
newPrefixes.delete(prefix);
newPrefixesWithInfo.delete(prefix);
newVisiblePrefixes.delete(prefix);
newPrefixes.add(prefix);
if (item.info) {
newPrefixesWithInfo.add(prefix);
if (!item.info.hidden) {
newVisiblePrefixes.add(prefix);
}
}
// Set data
iconSets[prefix] = {
@ -81,8 +104,10 @@ export function updateIconSets(): number {
loadedIconSets = newLoadedIconSets;
// Update prefixes
prefixes = Array.from(newPrefixes);
return prefixes.length;
allPrefixes = Array.from(newPrefixes);
prefixesWithInfo = Array.from(newPrefixesWithInfo);
visiblePrefixes = Array.from(newVisiblePrefixes);
return allPrefixes.length;
}
/**

View File

@ -7,12 +7,16 @@ interface MatchPrefixesParams {
}
/**
* Filter prefixes
* Filter prefixes by name
*
* returnEmpty = true -> if no filter params set, returns empty array
* returnEmpty = false -> if no filter params set, returns all filters
*/
export function filterPrefixes(prefixes: string[], params: MatchPrefixesParams, returnEmpty: boolean): string[] {
export function filterPrefixesByPrefix(
prefixes: string[],
params: MatchPrefixesParams,
returnEmpty: boolean
): string[] {
const exactMatch = params.prefix;
if (exactMatch) {
// Exact match

View File

@ -2,6 +2,7 @@ import fastify, { FastifyReply } from 'fastify';
import { appConfig } from '../config/app';
import { runWhenLoaded } from '../data/loading';
import { iconNameRoutePartialRegEx, iconNameRouteRegEx, splitIconName } from '../misc/name';
import { generateCollectionsListResponse } from './responses/collections';
import { generateIconsDataResponse } from './responses/icons';
import { generateLastModifiedResponse } from './responses/modified';
import { generateSVGResponse } from './responses/svg';
@ -106,6 +107,13 @@ export async function startHTTPServer() {
});
});
// Icon sets list
server.get('/collections', (req, res) => {
runWhenLoaded(() => {
generateCollectionsListResponse(req.query, res);
});
});
// Update icon sets
server.get('/update', (req, res) => {
generateUpdateResponse(req.query, res);

View File

@ -0,0 +1,32 @@
import type { IconifyInfo } from '@iconify/types';
import type { FastifyReply, FastifyRequest } from 'fastify';
import { getPrefixes, iconSets } from '../../data/icon-sets';
import { checkJSONPQuery, sendJSONResponse } from '../helpers/json';
import { filterPrefixesByPrefix } from '../helpers/prefixes';
/**
* Send response
*/
export function generateCollectionsListResponse(query: FastifyRequest['query'], res: FastifyReply) {
const q = (query || {}) as Record<string, string>;
const wrap = checkJSONPQuery(q);
if (!wrap) {
// Invalid JSONP callback
res.send(400);
return;
}
// Filter prefixes
const prefixes = filterPrefixesByPrefix(getPrefixes('info'), q, false);
const response = Object.create(null) as Record<string, IconifyInfo>;
for (let i = 0; i < prefixes.length; i++) {
const prefix = prefixes[i];
const info = iconSets[prefix]?.item.info;
if (info) {
response[prefix] = info;
}
}
sendJSONResponse(response, q, wrap, res);
}

View File

@ -2,7 +2,7 @@ import type { FastifyReply, FastifyRequest } from 'fastify';
import { getPrefixes, iconSets } from '../../data/icon-sets';
import type { LastModifiedAPIResponse } from '../../types/server/modified';
import { checkJSONPQuery, sendJSONResponse } from '../helpers/json';
import { filterPrefixes } from '../helpers/prefixes';
import { filterPrefixesByPrefix } from '../helpers/prefixes';
/**
* Generate icons data
@ -17,7 +17,7 @@ export function generateLastModifiedResponse(query: FastifyRequest['query'], res
}
// Filter prefixes
const prefixes = filterPrefixes(getPrefixes(), q, false);
const prefixes = filterPrefixesByPrefix(getPrefixes(), q, false);
// Generate result
const lastModified = Object.create(null) as Record<string, number>;