|
1 | 1 | import { Request, Response } from "express"; |
2 | | -import Card from "../cards/card"; |
3 | | -import { getThemeByName } from "../cards/themes"; |
| 2 | +import CardBuilder from "../cards/card-builder"; |
4 | 3 | import SvgGenerator from "../svg/svg-generator"; |
5 | | -import { |
6 | | - validateAlign, |
7 | | - validateBorderRadius, |
8 | | - validateFontSize, |
9 | | - validateLine, |
10 | | - validateLineCount, |
11 | | -} from "../utils/validator"; |
| 4 | +import { validateLine } from "../utils/validator"; |
12 | 5 |
|
13 | 6 | export const getCard = async (req: Request, res: Response) => { |
14 | | - const card = new Card(); |
| 7 | + const { |
| 8 | + title, |
| 9 | + lineCount, |
| 10 | + align, |
| 11 | + showBorder, |
| 12 | + borderRadius, |
| 13 | + fontWeight, |
| 14 | + fontSize, |
| 15 | + theme, |
| 16 | + } = req.query; |
15 | 17 |
|
16 | | - const title = req.query.title?.toString() || "My Tech Stack"; |
17 | | - const theme = req.query.theme?.toString() || ""; |
18 | | - const lineCount = req.query.lineCount?.toString() || "1"; |
19 | | - const align = req.query.align?.toString() || "left"; |
20 | | - const showBorder = req.query.showBorder?.toString() || "true"; |
21 | | - const borderRadius = req.query.borderRadius?.toString() || "4.5"; |
22 | | - const fontWeight = req.query.fontWeight?.toString() || "semibold"; |
23 | | - const fontSize = req.query.fontSize?.toString() || "18"; |
24 | | - |
25 | | - card.setTitle(title); |
26 | | - card.setTheme(getThemeByName(theme)); |
27 | | - card.setLineCount(validateLineCount(lineCount)); |
28 | | - card.setBadgeAlign(validateAlign(align)); |
29 | | - card.setShowBorder(showBorder !== "false"); |
30 | | - card.setBorderRadius(validateBorderRadius(borderRadius)); |
31 | | - card.setFontWeight(fontWeight); |
32 | | - card.setFontSize(validateFontSize(fontSize)); |
33 | | - |
34 | | - // run a loop card.getLineCount() times |
35 | | - for (let i = 1; i <= card.getLineCount(); i++) { |
36 | | - // get the dynamic query param (line + i) |
37 | | - const lineValue = req.query[`line${i}`]?.toString() || ""; |
38 | | - |
39 | | - [...validateLine(lineValue)].forEach((b) => card.addBadge(i, b)); |
40 | | - } |
| 18 | + const card = new CardBuilder() |
| 19 | + .title(title?.toString()) |
| 20 | + .lineCount(lineCount?.toString()) |
| 21 | + .align(align?.toString()) |
| 22 | + .border(showBorder?.toString()) |
| 23 | + .borderRadius(borderRadius?.toString()) |
| 24 | + .fontWeight(fontWeight?.toString()) |
| 25 | + .fontSize(fontSize?.toString()) |
| 26 | + .theme(theme?.toString()) |
| 27 | + .lines((line, addBadge) => { |
| 28 | + // get the line query param based on the `line` argument (example: line1) |
| 29 | + // validate the line |
| 30 | + // iterate through it, then append every badge |
| 31 | + validateLine(req.query[`line${line}`]?.toString() || "").forEach((b) => |
| 32 | + addBadge(b) |
| 33 | + ); |
| 34 | + }) |
| 35 | + .build(); |
41 | 36 |
|
42 | 37 | res.setHeader("Content-Type", "image/svg+xml"); |
43 | 38 | res.send(await new SvgGenerator(card).toString()); |
|
0 commit comments