Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-image): Allow image helpers to take other node types #29625

Merged
merged 3 commits into from
Feb 22, 2021

Conversation

ascorbic
Copy link
Contributor

This allows the getImage and getSrc helpers to take nodes other than File nodes, as long as they include a gatsbyImageData field, for example ContentfulAssets, and allows getSrc to take imageData directly. It also adds a stack of unit tests, and a similar getSrcSet helper.

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Feb 20, 2021
@ascorbic ascorbic added topic: media Related to gatsby-plugin-image, or general image/media processing topics and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Feb 22, 2021
}

describe(`The image helper functions`, () => {
it(`getImage returns the same data if passed gatsbyImageData`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, but I might make a nested describe for getImage since that's technically the object of the next few tests.


export const isGatsbyImageData = (
node: IGatsbyImageData | any
): node is IGatsbyImageData => Boolean(node?.images?.fallback?.src) // 🦆
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this duck comment a question? Boolean is fine, an if statement might be slightly more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it's just saying that I'm duck typing here by checking for specific props. Clearly should have better comments!


type ImageDataLike = FileNode | IGatsbyImageDataParent | IGatsbyImageData
export const getImage = (node: ImageDataLike): IGatsbyImageData | undefined => {
if (isGatsbyImageData(node)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for making isGatsbyImageData and isGatsbyImageDataParent? It seems like checking for existence inside this function would be equivalent? I noticed those functions are exported, are they meant for plugins to be able to use?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making them type guard functions allows type narrowing in TS. I exported them so they could be unit tested, but in the end just tested the getImage function itself so I could remove the export.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok. Too bad we can't get the benefit without the extra functions. Would make it easier to scan.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's easier to read with function name!

@ascorbic ascorbic force-pushed the feat/better-image-helpers branch from cc9a193 to c608155 Compare February 22, 2021 13:29
@gatsbot
Copy link

gatsbot bot commented Feb 22, 2021

Danger run resulted in 1 fail and 1 markdown; to find out more, see the checks page.

Generated by 🚫 dangerJS

Copy link
Contributor

@laurieontech laurieontech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Tests are easier to scan now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: media Related to gatsby-plugin-image, or general image/media processing topics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants