1
1
import { Request , Response } from 'express' ;
2
2
import fetchBadge from '../services/fetchBadges' ;
3
3
import iconDatabase from '../services/iconDatabase' ;
4
+ import octicons from '../services/octicons' ;
4
5
5
- async function listIconsJSON ( req : Request , res : Response ) : Promise < void > {
6
+ /**
7
+ * List all icons in the database
8
+ * @param {Request } _req The request object
9
+ * @param {Response } res The response object
10
+ */
11
+ async function listIconsJSON ( _req : Request , res : Response ) : Promise < void > {
6
12
res . status ( 200 ) . json ( {
7
13
icons : await iconDatabase . getIcons ( ) ,
8
14
} ) ;
9
15
}
10
16
17
+ /**
18
+ * Display a badge for the given parameters
19
+ * @param {Request } req The request object
20
+ * @param {Response } res The response object
21
+ */
11
22
async function getBadge ( req : Request , res : Response ) : Promise < void > {
12
23
// get logo from query as a string, use nothing if multiple or empty
13
24
const slug = typeof req . query . logo === 'string' ? req . query . logo : '' ;
14
25
// check if slug exists
15
- const item = slug ? await iconDatabase . checkSlugExists ( slug ) : null ;
26
+ const item = slug ? await octicons . getIcon ( slug ) || await iconDatabase . getIcon ( slug ) : null ;
16
27
// get badge for item
17
28
const response = await fetchBadge . fetchBadgeFromRequest ( req , item ) ;
18
29
// get content type
@@ -21,6 +32,11 @@ async function getBadge(req: Request, res: Response): Promise<void> {
21
32
res . status ( response . status ) . type ( contentType ) . send ( response . data ) ;
22
33
}
23
34
35
+ /**
36
+ * Add a new icon to the database
37
+ * @param {Request } req The request object
38
+ * @param {Response } res The response object
39
+ */
24
40
async function postIcon ( req : Request , res : Response ) : Promise < void > {
25
41
const { slug, type, data } : { slug : string , type : string , data : string } = req . body ;
26
42
@@ -34,7 +50,7 @@ async function postIcon(req: Request, res: Response): Promise<void> {
34
50
return ;
35
51
}
36
52
37
- console . log ( `Received icon for ${ slug } ` ) ;
53
+ console . info ( `Received icon for ${ slug } ` ) ;
38
54
39
55
// get the badge for item data
40
56
const logoBadgeResponse = await fetchBadge . fetchBadgeFromRequest ( req , { slug, type, data } ) ;
@@ -58,15 +74,15 @@ async function postIcon(req: Request, res: Response): Promise<void> {
58
74
}
59
75
60
76
// check for slug in the database
61
- const item = await iconDatabase . checkSlugExists ( slug ) ;
77
+ const item = await octicons . getIcon ( slug ) || await iconDatabase . getIcon ( slug ) ;
62
78
63
79
// Get default badge with the logo set to the slug
64
80
const defaultBadgeResponse = await fetchBadge . fetchDefaultBadge ( slug ) ;
65
81
66
82
// Check if the slug is reserved
67
83
// Slug is reserved if it is in the database or shields.io has an icon for it
68
84
if ( item !== null || defaultBadgeResponse . data . match ( / < i m a g e [ ^ > ] * > / ) !== null ) {
69
- console . log ( 'Slug is already in use' ) ;
85
+ console . info ( `The slug ${ slug } is already in use` ) ;
70
86
// slug already exists
71
87
res . status ( 409 ) . json ( {
72
88
type : 'error' ,
@@ -77,7 +93,7 @@ async function postIcon(req: Request, res: Response): Promise<void> {
77
93
}
78
94
79
95
// All checks passed, add the icon to the database
80
- console . log ( `Creating new icon for ${ slug } ` ) ;
96
+ console . info ( `Creating new icon for ${ slug } ` ) ;
81
97
// create item
82
98
const body = await iconDatabase . insertIcon ( slug , type , data ) ;
83
99
// return success response
0 commit comments