Skip to content

Commit

Permalink
Add support for version 9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianKoerner committed Jun 15, 2024
1 parent 68908d2 commit 43a3ca1
Show file tree
Hide file tree
Showing 10 changed files with 1,095 additions and 61 deletions.
1,080 changes: 1,045 additions & 35 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
"@dicebear/api-6": "*",
"@dicebear/api-7": "*",
"@dicebear/api-8": "*",
"@dicebear/api-9": "*",
"@dicebear/converter": "^9.0.0",
"@fastify/cors": "^8.4.0",
"@resvg/resvg-js": "^2.4.1",
"change-case": "^5.0.2",
"exiftool-vendored": "^23.3.0",
"fastify": "^4.23.2",
"qs": "^6.11.2",
"sharp": "^0.32.6"
"qs": "^6.11.2"
},
"devDependencies": {
"@dicebear/core": "^7.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const config: Config = {
json: {
enabled: Boolean(Number(process.env.JSON ?? 1)),
},
versions: process.env.VERSIONS?.split(',').map(Number) ?? [5, 6, 7, 8],
versions: process.env.VERSIONS?.split(',').map(Number) ?? [5, 6, 7, 8, 9],
cacheControl: {
avatar: Number(process.env.CACHE_CONTROL_AVATARS ?? 60 * 60 * 24 * 365),
},
Expand Down
24 changes: 10 additions & 14 deletions src/handler/avatar.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Style, StyleOptions } from '@dicebear/core';
import type { FastifyReply, FastifyRequest } from 'fastify';
import type { Core } from '../types.js';
import { config } from '../config.js';
import { toJpeg, toPng } from '@dicebear/converter';

export type AvatarRequest = {
Params: {
format: 'svg' | 'png' | 'jpg' | 'jpeg' | 'json';
options?: StyleOptions<any>;
options?: Record<string, any>;
};
Querystring: StyleOptions<any>;
Querystring: Record<string, any>;
};

export function avatarHandler(core: Core, style: Style<any>) {
export function avatarHandler(core: Core, style: any) {
return async (
request: FastifyRequest<AvatarRequest>,
reply: FastifyReply
Expand Down Expand Up @@ -62,23 +62,19 @@ export function avatarHandler(core: Core, style: Style<any>) {
case 'png':
reply.header('Content-Type', 'image/png');

const png = await avatar
.png({
includeExif: config.png.exif,
})
.toArrayBuffer();
const png = await toPng(avatar.toString(), {
includeExif: config.png.exif,
}).toArrayBuffer();

return Buffer.from(png);

case 'jpg':
case 'jpeg':
reply.header('Content-Type', 'image/jpeg');

const jpeg = await avatar
.jpeg({
includeExif: config.jpeg.exif,
})
.toArrayBuffer();
const jpeg = await toJpeg(avatar.toString(), {
includeExif: config.jpeg.exif,
}).toArrayBuffer();

return Buffer.from(jpeg);

Expand Down
3 changes: 1 addition & 2 deletions src/routes/style.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { FastifyPluginCallback } from 'fastify';
import type { JSONSchema7Definition } from 'json-schema';
import type { Style } from '@dicebear/core';
import type { Core } from '../types.js';
import { schemaHandler } from '../handler/schema.js';
import { parseQueryString } from '../utils/parseQueryString.js';
Expand All @@ -9,7 +8,7 @@ import { config } from '../config.js';

type Options = {
core: Core;
style: Style<any>;
style: any;
};

const paramsSchema: Record<string, JSONSchema7Definition> = {
Expand Down
19 changes: 14 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import type { Style, Result, StyleSchema, StyleOptions } from '@dicebear/core';
import { JSONSchema7 } from 'json-schema';

export type Core = {
createAvatar: (style: Style<any>, options?: StyleOptions<any>) => Result;
schema: StyleSchema;
createAvatar: (
style: any,
options?: any
) => {
toString: () => string;
toJson: () => {
svg: string;
extra: Record<string, unknown>;
};
};
schema: JSONSchema7;
};

export type Version = {
core: Core;
collection: Record<string, Style<any>>;
collection: Record<string, any>;
};

export type Config = {
port: number;
host: string;
logger: boolean;
workers: number,
workers: number;
versions: number[];
png: {
enabled: boolean;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/getVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ export async function getVersions(): Promise<Record<string, Version>> {
versions['8.x'] = await import('@dicebear/api-8');
}

if (config.versions.includes(9)) {
versions['9.x'] = await import('@dicebear/api-9');
}

return versions;
}
2 changes: 2 additions & 0 deletions versions/9.x/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as core from '@dicebear/core';
export * as collection from '@dicebear/collection';
2 changes: 2 additions & 0 deletions versions/9.x/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as core from '@dicebear/core';
export * as collection from '@dicebear/collection';
13 changes: 13 additions & 0 deletions versions/9.x/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@dicebear/api-9",
"private": true,
"type": "module",
"exports": {
"default": "./index.js",
"types": "./index.d.ts"
},
"dependencies": {
"@dicebear/collection": "^9.0.0",
"@dicebear/core": "^9.0.0"
}
}

0 comments on commit 43a3ca1

Please sign in to comment.