Skip to content

Commit

Permalink
feat: WidgetMakeOptions#convertOptions and HEIF image widget support (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zero734kr authored Nov 10, 2021
1 parent a69ebca commit c314ba9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/managers/WidgetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ export class WidgetManager {
target: options.target,
type: options.type
}

const key = createHash("sha256").update(JSON.stringify(cacheKey)).digest("hex")
const cache = this.cache.get(key)

if (!fetchOptions?.force && cache) return cache
if (!fetchOptions?.force && cache) {
const cache = this.cache.get(key)
return cache
}

for (const queryOption of queryOptions) {
const value = options[queryOption]?.toString?.() || options[queryOption] as string
Expand Down Expand Up @@ -146,13 +148,19 @@ export class WidgetManager {
switch (options?.format) {
case "jpeg":
case "jpg":
converted = sh(buffer).jpeg().toBuffer()
converted = sh(buffer).jpeg(options.convertOptions).toBuffer()
break
case "png":
converted = sh(buffer).png().toBuffer()
converted = sh(buffer).png(options.convertOptions).toBuffer()
break
case "webp":
converted = sh(buffer).webp().toBuffer()
converted = sh(buffer).webp(options.convertOptions).toBuffer()
break
case "avif":
converted = sh(buffer).avif(options.convertOptions).toBuffer()
break
case "heif":
converted = sh(buffer).heif(options.convertOptions).toBuffer()
break
case "avif":
converted = sh(buffer).avif().toBuffer()
Expand Down
3 changes: 2 additions & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type Nullable<T> = T | null
export type WidgetType = "status" | "servers" | "votes"
export type WidgetTarget = "bots"
export type WidgetStyle = "classic" | "flat"
export type WidgetFormat = "webp" | "png" | "jpg" | "jpeg" | "svg" | "avif"
export type WidgetFormat = "webp" | "png" | "jpg" | "jpeg" | "svg" | "avif" | "heif"

export interface KoreanbotsClientOptions extends ClientOptions {
koreanbots: Omit<KoreanbotsOptions, "clientID"> & { clientID?: string }
Expand All @@ -33,6 +33,7 @@ export interface WidgetOptions {
scale?: number
icon?: boolean
format?: WidgetFormat
convertOptions?: Record<string, unknown>
}

export interface FetchOptions {
Expand Down

0 comments on commit c314ba9

Please sign in to comment.