mirror of https://github.com/iconify/api.git
feat: support /collections request
This commit is contained in:
parent
66c6b93ac4
commit
5b78c43da6
|
|
@ -16,13 +16,25 @@ export function setImporters(items: Importer[]) {
|
||||||
/**
|
/**
|
||||||
* All prefixes, sorted
|
* All prefixes, sorted
|
||||||
*/
|
*/
|
||||||
let prefixes: string[] = [];
|
let allPrefixes: string[] = [];
|
||||||
|
let prefixesWithInfo: string[] = [];
|
||||||
|
let visiblePrefixes: string[] = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all prefixes
|
* Get all prefixes
|
||||||
*/
|
*/
|
||||||
export function getPrefixes(): string[] {
|
type GetPrefixes = 'all' | 'info' | 'visible';
|
||||||
return prefixes;
|
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 newLoadedIconSets: Set<StoredIconSet> = new Set();
|
||||||
const newPrefixes: Set<string> = new Set();
|
const newPrefixes: Set<string> = new Set();
|
||||||
|
const newPrefixesWithInfo: Set<string> = new Set();
|
||||||
|
const newVisiblePrefixes: Set<string> = new Set();
|
||||||
|
|
||||||
importers.forEach((importer, importerIndex) => {
|
importers.forEach((importer, importerIndex) => {
|
||||||
const data = importer.data;
|
const data = importer.data;
|
||||||
|
|
@ -63,7 +77,16 @@ export function updateIconSets(): number {
|
||||||
|
|
||||||
// Add prefix, but delete it first to keep order
|
// Add prefix, but delete it first to keep order
|
||||||
newPrefixes.delete(prefix);
|
newPrefixes.delete(prefix);
|
||||||
|
newPrefixesWithInfo.delete(prefix);
|
||||||
|
newVisiblePrefixes.delete(prefix);
|
||||||
|
|
||||||
newPrefixes.add(prefix);
|
newPrefixes.add(prefix);
|
||||||
|
if (item.info) {
|
||||||
|
newPrefixesWithInfo.add(prefix);
|
||||||
|
if (!item.info.hidden) {
|
||||||
|
newVisiblePrefixes.add(prefix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set data
|
// Set data
|
||||||
iconSets[prefix] = {
|
iconSets[prefix] = {
|
||||||
|
|
@ -81,8 +104,10 @@ export function updateIconSets(): number {
|
||||||
loadedIconSets = newLoadedIconSets;
|
loadedIconSets = newLoadedIconSets;
|
||||||
|
|
||||||
// Update prefixes
|
// Update prefixes
|
||||||
prefixes = Array.from(newPrefixes);
|
allPrefixes = Array.from(newPrefixes);
|
||||||
return prefixes.length;
|
prefixesWithInfo = Array.from(newPrefixesWithInfo);
|
||||||
|
visiblePrefixes = Array.from(newVisiblePrefixes);
|
||||||
|
return allPrefixes.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,16 @@ interface MatchPrefixesParams {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter prefixes
|
* Filter prefixes by name
|
||||||
*
|
*
|
||||||
* returnEmpty = true -> if no filter params set, returns empty array
|
* returnEmpty = true -> if no filter params set, returns empty array
|
||||||
* returnEmpty = false -> if no filter params set, returns all filters
|
* 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;
|
const exactMatch = params.prefix;
|
||||||
if (exactMatch) {
|
if (exactMatch) {
|
||||||
// Exact match
|
// Exact match
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import fastify, { FastifyReply } from 'fastify';
|
||||||
import { appConfig } from '../config/app';
|
import { appConfig } from '../config/app';
|
||||||
import { runWhenLoaded } from '../data/loading';
|
import { runWhenLoaded } from '../data/loading';
|
||||||
import { iconNameRoutePartialRegEx, iconNameRouteRegEx, splitIconName } from '../misc/name';
|
import { iconNameRoutePartialRegEx, iconNameRouteRegEx, splitIconName } from '../misc/name';
|
||||||
|
import { generateCollectionsListResponse } from './responses/collections';
|
||||||
import { generateIconsDataResponse } from './responses/icons';
|
import { generateIconsDataResponse } from './responses/icons';
|
||||||
import { generateLastModifiedResponse } from './responses/modified';
|
import { generateLastModifiedResponse } from './responses/modified';
|
||||||
import { generateSVGResponse } from './responses/svg';
|
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
|
// Update icon sets
|
||||||
server.get('/update', (req, res) => {
|
server.get('/update', (req, res) => {
|
||||||
generateUpdateResponse(req.query, res);
|
generateUpdateResponse(req.query, res);
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,7 @@ import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||||
import { getPrefixes, iconSets } from '../../data/icon-sets';
|
import { getPrefixes, iconSets } from '../../data/icon-sets';
|
||||||
import type { LastModifiedAPIResponse } from '../../types/server/modified';
|
import type { LastModifiedAPIResponse } from '../../types/server/modified';
|
||||||
import { checkJSONPQuery, sendJSONResponse } from '../helpers/json';
|
import { checkJSONPQuery, sendJSONResponse } from '../helpers/json';
|
||||||
import { filterPrefixes } from '../helpers/prefixes';
|
import { filterPrefixesByPrefix } from '../helpers/prefixes';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate icons data
|
* Generate icons data
|
||||||
|
|
@ -17,7 +17,7 @@ export function generateLastModifiedResponse(query: FastifyRequest['query'], res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter prefixes
|
// Filter prefixes
|
||||||
const prefixes = filterPrefixes(getPrefixes(), q, false);
|
const prefixes = filterPrefixesByPrefix(getPrefixes(), q, false);
|
||||||
|
|
||||||
// Generate result
|
// Generate result
|
||||||
const lastModified = Object.create(null) as Record<string, number>;
|
const lastModified = Object.create(null) as Record<string, number>;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue