Skip to content

Commit de80e6e

Browse files
Add format script and apply prettier formatting
1 parent a545dc4 commit de80e6e

11 files changed

Lines changed: 53 additions & 26 deletions

File tree

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"editor.wordBasedSuggestions": "off"
3-
}
2+
"editor.wordBasedSuggestions": "off"
3+
}

CLAUDE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,39 @@ node --test tests/http.test.js
2828
## Architecture
2929

3030
### Multi-Version Support
31+
3132
The API uses npm workspaces to maintain multiple DiceBear library versions side-by-side:
33+
3234
- `versions/5.x/` through `versions/9.x/` - Each contains a package.json that pins specific `@dicebear/core` and `@dicebear/collection` versions
3335
- Versions are loaded dynamically via `src/utils/versions.ts` based on the `VERSIONS` env var
3436
- Routes are prefixed by version: `/:version/:style/:format` (e.g., `/9.x/avataaars/svg`)
3537

3638
### Route Structure
39+
3740
```
3841
src/routes/version.ts → Registers routes for each enabled version
3942
src/routes/collection.ts → Registers routes for each avatar style in a version
4043
src/routes/style.ts → Defines the actual endpoints (/:format, /:format/:options, /schema.json)
4144
```
4245

4346
### Request Flow
47+
4448
1. Request hits `/:version/:style/:format` or `/:version/:style/:format/:options`
4549
2. Query string parsing via `src/utils/query-string.ts` (supports both `?key=value` and path-based `/key=value`)
4650
3. Options validated against combined JSON Schema from core + style
4751
4. `src/handler/avatar.ts` creates avatar using the appropriate DiceBear version
4852
5. Format conversion (PNG, JPEG, WebP, AVIF) via `@dicebear/converter`
4953

5054
### Font System
55+
5156
- `scripts/build-fonts.ts` - Pre-build script that extracts TTF fonts from @fontsource packages
5257
- Fonts are used for rendering text in raster formats (initials style, etc.)
5358
- Font selection based on Unicode ranges in `fonts/fonts.json`
5459

5560
## Configuration
5661

5762
All configuration is via environment variables (see `src/config.ts`):
63+
5864
- `PORT`, `HOST`, `WORKERS` - Server settings
5965
- `VERSIONS` - Comma-separated list of versions to enable (default: 5,6,7,8,9)
6066
- `PNG`, `JPEG`, `WEBP`, `AVIF`, `JSON` - Enable/disable output formats (0/1)

CODE_OF_CONDUCT.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ diverse, inclusive, and healthy community.
1717
Examples of behavior that contributes to a positive environment for our
1818
community include:
1919

20-
* Demonstrating empathy and kindness toward other people
21-
* Being respectful of differing opinions, viewpoints, and experiences
22-
* Giving and gracefully accepting constructive feedback
23-
* Accepting responsibility and apologizing to those affected by our mistakes,
20+
- Demonstrating empathy and kindness toward other people
21+
- Being respectful of differing opinions, viewpoints, and experiences
22+
- Giving and gracefully accepting constructive feedback
23+
- Accepting responsibility and apologizing to those affected by our mistakes,
2424
and learning from the experience
25-
* Focusing on what is best not just for us as individuals, but for the
25+
- Focusing on what is best not just for us as individuals, but for the
2626
overall community
2727

2828
Examples of unacceptable behavior include:
2929

30-
* The use of sexualized language or imagery, and sexual attention or
30+
- The use of sexualized language or imagery, and sexual attention or
3131
advances of any kind
32-
* Trolling, insulting or derogatory comments, and personal or political attacks
33-
* Public or private harassment
34-
* Publishing others' private information, such as a physical or email
32+
- Trolling, insulting or derogatory comments, and personal or political attacks
33+
- Public or private harassment
34+
- Publishing others' private information, such as a physical or email
3535
address, without their explicit permission
36-
* Other conduct which could reasonably be considered inappropriate in a
36+
- Other conduct which could reasonably be considered inappropriate in a
3737
professional setting
3838

3939
## Enforcement Responsibilities
@@ -106,7 +106,7 @@ Violating these terms may lead to a permanent ban.
106106
### 4. Permanent Ban
107107

108108
**Community Impact**: Demonstrating a pattern of violation of community
109-
standards, including sustained inappropriate behavior, harassment of an
109+
standards, including sustained inappropriate behavior, harassment of an
110110
individual, or aggression toward or disparagement of classes of individuals.
111111

112112
**Consequence**: A permanent ban from any sort of public interaction within

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"prebuild": "tsx ./scripts/build-fonts.ts",
1111
"build": "tsc --build",
1212
"start": "node ./dist/server.js",
13-
"test": "node --test tests/*.js"
13+
"test": "node --test tests/*.js",
14+
"format": "prettier --write ."
1415
},
1516
"workspaces": [
1617
"./versions/*"

src/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export const config: Config = {
3131
};
3232

3333
// Format metadata mapping request formats to config keys and content types
34-
export const IMAGE_FORMATS: Record<string, { configKey: ImageFormat; contentType: string }> = {
34+
export const IMAGE_FORMATS: Record<
35+
string,
36+
{ configKey: ImageFormat; contentType: string }
37+
> = {
3538
png: { configKey: 'png', contentType: 'image/png' },
3639
jpg: { configKey: 'jpeg', contentType: 'image/jpeg' },
3740
jpeg: { configKey: 'jpeg', contentType: 'image/jpeg' },

src/handler/avatar.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const FORMAT_CONVERTERS = {
2323
export function avatarHandler(app: FastifyInstance, core: Core, style: any) {
2424
return async (
2525
request: FastifyRequest<AvatarRequest>,
26-
reply: FastifyReply
26+
reply: FastifyReply,
2727
) => {
2828
const options = request.query;
2929
const format = request.params.format;
@@ -35,7 +35,10 @@ export function avatarHandler(app: FastifyInstance, core: Core, style: any) {
3535
// Validate and apply size constraints for image formats
3636
if (formatConfig) {
3737
options['size'] = options['size']
38-
? Math.min(Math.max(options['size'], formatConfig.size.min), formatConfig.size.max)
38+
? Math.min(
39+
Math.max(options['size'], formatConfig.size.min),
40+
formatConfig.size.max,
41+
)
3942
: formatConfig.size.default;
4043
}
4144

@@ -64,7 +67,8 @@ export function avatarHandler(app: FastifyInstance, core: Core, style: any) {
6467
}
6568

6669
// Handle image formats (png, jpg, jpeg, webp, avif)
67-
const converter = FORMAT_CONVERTERS[format as keyof typeof FORMAT_CONVERTERS];
70+
const converter =
71+
FORMAT_CONVERTERS[format as keyof typeof FORMAT_CONVERTERS];
6872
if (converter && formatConfig) {
6973
const svgString = avatar.toString();
7074
const fonts = app.fontLookup.getRequiredFonts(svgString);

src/routes/collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Options = {
1010
export const collectionRoutes: FastifyPluginCallback<Options> = (
1111
app,
1212
{ version },
13-
done
13+
done,
1414
) => {
1515
for (const [prefix, style] of Object.entries(version.collection)) {
1616
app.register(styleRoutes, {

src/routes/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Options = {
99
export const versionRoutes: FastifyPluginCallback<Options> = (
1010
app,
1111
{ versions },
12-
done
12+
done,
1313
) => {
1414
for (const [prefix, version] of Object.entries(versions)) {
1515
app.register(collectionRoutes, {

src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (cluster.isPrimary && useCluster) {
1111

1212
cluster.on('exit', (worker, code, signal) => {
1313
console.log(
14-
`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`
14+
`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`,
1515
);
1616

1717
// Fork a new worker
@@ -34,6 +34,6 @@ if (cluster.isPrimary && useCluster) {
3434
}
3535

3636
console.info(`Server listening at http://${config.host}:${config.port}`);
37-
}
37+
},
3838
);
3939
}

src/types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ declare module 'fastify' {
1111
export type Core = {
1212
createAvatar: (
1313
style: any,
14-
options?: any
14+
options?: any,
1515
) => {
1616
toString: () => string;
1717
toJson: () => {
@@ -41,7 +41,14 @@ export type ImageFormatConfig = {
4141
};
4242

4343
export type ImageFormat = 'png' | 'jpeg' | 'webp' | 'avif';
44-
export type RequestFormat = 'svg' | 'png' | 'jpg' | 'jpeg' | 'webp' | 'avif' | 'json';
44+
export type RequestFormat =
45+
| 'svg'
46+
| 'png'
47+
| 'jpg'
48+
| 'jpeg'
49+
| 'webp'
50+
| 'avif'
51+
| 'json';
4552

4653
export type Config = {
4754
port: number;

0 commit comments

Comments
 (0)