feat: types for old API search requests and responses

This commit is contained in:
Vjacheslav Trushkin 2022-10-15 11:36:59 +03:00
parent 49ce3f8410
commit 5c53b0a01a
4 changed files with 146 additions and 5 deletions

View File

@ -1,6 +1,6 @@
import type { IconifyInfo } from '@iconify/types';
import type { FastifyReply, FastifyRequest } from 'fastify';
import { getPrefixes, iconSets } from '../../data/icon-sets';
import type { APIv2CollectionsResponse } from '../../types/server/v2';
import { checkJSONPQuery, sendJSONResponse } from '../helpers/json';
import { filterPrefixesByPrefix } from '../helpers/prefixes';
@ -18,7 +18,7 @@ export function generateCollectionsListResponse(query: FastifyRequest['query'],
// Filter prefixes
const prefixes = filterPrefixesByPrefix(getPrefixes('info'), q, false);
const response = Object.create(null) as Record<string, IconifyInfo>;
const response = Object.create(null) as APIv2CollectionsResponse;
for (let i = 0; i < prefixes.length; i++) {
const prefix = prefixes[i];

View File

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

View File

@ -3,6 +3,6 @@
*
* Key is prefix, value is last modification time number
*/
export interface LastModifiedAPIResponse {
export interface APIv3LastModifiedResponse {
lastModified: Record<string, number>;
}

141
src/types/server/v2.ts Normal file
View File

@ -0,0 +1,141 @@
import type { IconifyInfo, IconifyJSON } from '@iconify/types';
/**
* Common stuff
*/
interface APIv2CommonParams {
// Pretty output, default = false
pretty?: boolean;
}
/**
* Prefixes list
*
* Separated by ','
* Ends with '-' = partial prefix: 'fa-'
* Does not end with '-' = full prefix: 'mdi'
*/
type PrefixesMatches = string;
/**
* Search query string
*
* List of queries, split by whitespace. Must match all queries
* Entry starts with '-': exclude it
*/
type SearchQuery = string;
/**
* /collections
*/
export interface APIv2CollectionsParams extends APIv2CommonParams {
// Useless
version?: 1;
// Include hidden icon sets
hidden?: boolean;
// Filter icon sets by prefixes
prefixes?: PrefixesMatches;
}
export type APIv2CollectionsResponse = Record<string, IconifyInfo>;
/**
* /collection
*/
export interface APIv2CollectionParams extends APIv2CommonParams {
// Icon set prefix
prefix: string;
// Include icon set info in response
info?: boolean;
// Include aliases in response
aliases?: boolean;
// Include characters in response
chars?: boolean;
// Include hidden icons
hidden?: boolean;
}
export interface APIv2CollectionResponse {
// Icon set prefix
prefix: string;
// Number of icons (duplicate of info?.total)
total: number;
// Icon set title, if available (duplicate of info?.name)
title?: string;
// Icon set info
info?: IconifyInfo;
// List of icons without categories
uncategorized?: string[];
// List of icons, sorted by category
categories?: Record<string, string[]>;
// List of hidden icons
hidden?: string[];
// List of aliases, key = alias, value = parent icon
aliases?: Record<string, string>;
// Characters, key = character, value = icon name
chars?: Record<string, string>;
// Themes
themes?: IconifyJSON['themes'];
prefixes?: IconifyJSON['prefixes'];
suffixes?: IconifyJSON['suffixes'];
}
/**
* /search
*/
export interface APIv2SearchParams extends APIv2CommonParams {
// Search string
query: SearchQuery;
// Maximum number of items in response
limit?: number;
// Start index for results
start?: number;
// Filter icon sets by prefixes
prefix?: string; // One prefix
collection?: string; // One prefix
prefixes?: PrefixesMatches; // Multiple prefixes or partial
// Filter icon sets by category
category?: string;
// Include similar keywords (partial matches for words), default = true
similar?: boolean;
}
export interface APIv2SearchResponse {
// List of icons, including prefixes
icons: string[];
// Number of results. If same as `limit`, more results are available
total: number;
// Number of results shown
limit: number;
// Index of first result
start: number;
// Info about icon sets
collections: Record<string, IconifyInfo>;
// Copy of request
request: APIv2SearchParams;
}