Quantcast
Channel: User Hubro - Stack Overflow
Viewing all articles
Browse latest Browse all 46

How do I attach calculated properties or methods to collections in Astro?

$
0
0

I'm using Astro with strict type checking.

I have a collection called "portfolio" and I have a couple of conventions for where images are stored for my portfolio. It goes something like:

  • If portfolioEntry.type = "website" then
    • desktopScreenshot = "@content/portfolio/_img/${portfolioEntry.slug}-desktop.png"
    • tabletScreenshot = "@content/portfolio/_img/${portfolioEntry.slug}-tablet.png"
  • Etc.

Is there any way to attach logic like this to collection entries? It would be great if I was able to:

let entry = getEntry("portfolio", "foo-2023");console.log(entry.thumbnail());

Where thumbnail() is a function that I've attached to the collection, like a method on a class.

Is there currently any way to achieve this?


What I'm doing currently is:

function resolvePortfolioThumbnail(entry: any): ImageMetadata {    // ...}

Which is annoying since I'm not getting any type checking, and in spite of my best effort I couldn't find any way to refer to the portfolio collection type in my function signature.


Viewing all articles
Browse latest Browse all 46

Trending Articles