Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add /update-custom-icons request, and modify updateIconSets function #28

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/data/icon-sets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function getPrefixes(type: GetPrefixes = 'all'): string[] {
/**
* All icon sets
*/
export const iconSets = Object.create(null) as Record<string, IconSetEntry>;
export let iconSets = Object.create(null) as Record<string, IconSetEntry>;

/**
* Loaded icon sets
Expand All @@ -60,6 +60,7 @@ export function updateIconSets(): number {
const newPrefixes: Set<string> = new Set();
const newPrefixesWithInfo: Set<string> = new Set();
const newVisiblePrefixes: Set<string> = new Set();
const newIconSets = Object.create(null) as Record<string, IconSetEntry>;

importers.forEach((importer, importerIndex) => {
const data = importer.data;
Expand Down Expand Up @@ -89,14 +90,18 @@ export function updateIconSets(): number {
}
}

// Set data
iconSets[prefix] = {
// Add data to a new temporary icon set.
// If some files have deleted from icons folder, they're not be here, too.
newIconSets[prefix] = {
importer,
item,
};
});
});

// Set data
iconSets = newIconSets;

// Replace list of icon sets
if (loadedIconSets.size) {
// Got some icon sets to clean up
Expand Down
27 changes: 16 additions & 11 deletions src/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createKeywordsResponse } from './responses/keywords.js';
import { createLastModifiedResponse } from './responses/modified.js';
import { createAPIv2SearchResponse } from './responses/search.js';
import { generateSVGResponse } from './responses/svg.js';
import { generateUpdateResponse } from './responses/update.js';
import { generateUpdateCustomResponse, generateUpdateResponse } from './responses/update.js';
import { initVersionResponse, versionResponse } from './responses/version.js';
import { generateIconsStyleResponse } from './responses/css.js';
import { handleJSONResponse } from './helpers/send.js';
Expand Down Expand Up @@ -63,16 +63,13 @@ export async function startHTTPServer() {
}

// SVG: /prefix/icon.svg, /prefix:name.svg, /prefix-name.svg
server.get(
'/:prefix(' + iconNameRoutePartialRegEx + ')/:name(' + iconNameRoutePartialRegEx + ').svg',
(req, res) => {
type Params = PrefixParams & NameParams;
const name = req.params as Params;
runWhenLoaded(() => {
generateSVGResponse(name.prefix, name.name, req.query, res);
});
}
);
server.get('/:prefix(' + iconNameRoutePartialRegEx + ')/:name(' + iconNameRoutePartialRegEx + ').svg', (req, res) => {
type Params = PrefixParams & NameParams;
const name = req.params as Params;
runWhenLoaded(() => {
generateSVGResponse(name.prefix, name.name, req.query, res);
});
});

// SVG: /prefix:name.svg, /prefix-name.svg
server.get('/:name(' + iconNameRouteRegEx + ').svg', (req, res) => {
Expand Down Expand Up @@ -176,6 +173,14 @@ export async function startHTTPServer() {
generateUpdateResponse(req.query, res);
});

// Update custom icon sets only
server.get('/update-custom-icons', (req, res) => {
generateUpdateCustomResponse(req.query, res);
});
server.post('/update-custom-icons', (req, res) => {
generateUpdateCustomResponse(req.query, res);
});

// Options
server.options('/*', (req, res) => {
res.send(200);
Expand Down
11 changes: 11 additions & 0 deletions src/http/responses/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ export function generateUpdateResponse(query: FastifyRequest['query'], res: Fast
// Send same message regardless of status
res.send('ok');
}

/**
* Generate custom icons data only
*/
export function generateUpdateCustomResponse(query: FastifyRequest['query'], res: FastifyReply) {
runWhenLoaded(() => {
triggerIconSetsUpdate(1, () => {
res.send('ok');
});
});
}