Skip to content

Commit

Permalink
🎨 simplify getLargestImageUrl function
Browse files Browse the repository at this point in the history
  • Loading branch information
Oculux314 committed Jul 15, 2024
1 parent 0c23d89 commit 2143bc5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
8 changes: 4 additions & 4 deletions next/schemas/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export const imageSchema = z.object({
width: z.number(),
height: z.number(),
formats: z.object({
large: imageFormatSchema.nullable(),
medium: imageFormatSchema.nullable(),
small: imageFormatSchema.nullable(),
thumbnail: imageFormatSchema.nullable(),
large: imageFormatSchema.optional(),
medium: imageFormatSchema.optional(),
small: imageFormatSchema.optional(),
thumbnail: imageFormatSchema.optional(),
}),
hash: z.string(),
ext: z.string(),
Expand Down
15 changes: 3 additions & 12 deletions next/util/image.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { ImageType } from "@/schemas/Image";

export const getLargestImageUrl = (img: ImageType) => {
try {
const formats = img.data.attributes.formats;

const largestFormat = formats.large ??
formats.medium ??
formats.small ??
formats.thumbnail ?? { url: "" };

return `${process.env.STRAPI_URL}${largestFormat.url}`;
} catch {
return process.env.STRAPI_URL;
}
const { large, medium, small, thumbnail } = img.data.attributes.formats;
const largestFormat = large ?? medium ?? small ?? thumbnail ?? { url: "" };
return `${process.env.STRAPI_URL}${largestFormat.url}`;
};

0 comments on commit 2143bc5

Please sign in to comment.