This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (39 loc) · 1.41 KB
/
index.js
File metadata and controls
48 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const express = require('express');
const dot = require('dot-object');
const primitives = require('@primer/primitives');
const colors = primitives.default.colors;
const colorModes = Object.keys(colors);
const getHex = (colorMode, token) => {
const tokens = colors[colorMode];
if (!tokens)
return {
error: `could not find ${colorMode} in color modes, did you mean one of ${colorModes.join(', ')}`
};
const hex = dot.pick(token, tokens);
if (!hex) {
const possibleTokens = Object.keys(dot.dot(tokens));
return {
error: `could not find ${token} in ${colorMode}, did you mean one of \n${possibleTokens.join('\n')}`
};
}
return { hex };
};
const app = express();
app.listen(process.env.PORT || 3000, () => {
console.log('listening');
});
app.get('/', async (req, res) => {
const { mode, token, size = 24 } = req.query;
const { hex, error } = getHex(mode, token);
if (error) {
res.status(404).end(error);
} else {
res.setHeader('content-type', 'image/svg+xml');
res.status(200).send(
`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="${size}" height="${size}" viewBox="0 0 24 24" style="vertical-align: middle">
<rect xmlns="http://www.w3.org/2000/svg" fill="${hex}" stroke="#0000001a" x="0" y="0" width="24" height="24" rx="4" stroke-linecap="round"/>
</svg>`
);
}
});
module.exports = app;