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

Rename getEntry to getEntryBySlug #5893

Merged
merged 6 commits into from
Jan 19, 2023
Merged

Rename getEntry to getEntryBySlug #5893

merged 6 commits into from
Jan 19, 2023

Conversation

matthewp
Copy link
Contributor

@matthewp matthewp commented Jan 18, 2023

Changes

  • Rename getEntry to getEntryBySlug.
    • The new function takes a slug as the second argument instead of an id.
    • It loops over the collection and finds a matching entry that has the provided slug.
  • The types here could maybe be better? I could figure out a way to type it so that the provided slug matches an entry with that slug. I think the fact that a slug can be any string is a blocker here. Happy to be proven otherwise.

Testing

  • Tests updated

Docs

@changeset-bot
Copy link

changeset-bot bot commented Jan 18, 2023

🦋 Changeset detected

Latest commit: aa395e4

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jan 18, 2023
@matthewp
Copy link
Contributor Author

Going to change this to return | undefined so that in SSR you don't have to try/catch.

@matthewp matthewp requested a review from a team January 18, 2023 16:51
@matthewp matthewp marked this pull request as ready for review January 18, 2023 16:51
Copy link
Contributor

@bholmesdev bholmesdev left a comment

Choose a reason for hiding this comment

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

Just some type cleanup. Some entryguing changes!

packages/astro/src/content/template/types.d.ts Outdated Show resolved Hide resolved
entryKey: E
): Promise<(typeof entryMap)[C][E] & Render>;
// Note that this has to accept a regular string too, for SSR
entrySlug: ValidEntrySlug<C> | string
Copy link
Member

@Princesseuh Princesseuh Jan 18, 2023

Choose a reason for hiding this comment

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

Make sure completions still work here. Doing literal unions | string typically break the completions because TypeScript will only see string

You have to do a work around for it to keep the completions, aka | (string & {})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok, thank you. I realized that the SSR use-case you're going to be grabbing a string from Astro.params so not allowing any strings would give bad type messages. I'll try your suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, updated here: d41cc2f

This works in my testing. If you provide a known slug value you get back just an entry. If you provide any other string you get back an entry or undefined, which is what we want.

@matthewp matthewp removed the request for review from a team January 18, 2023 19:34
@bholmesdev bholmesdev changed the title Rename getEntry to getEntryBySchema Rename getEntry to getEntryBySlug Jan 18, 2023
Copy link
Member

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

LGTM on the API! Thanks for getting this across the line in time for 2.0.

@bholmesdev I'm happy to factor these changes into my docs PR, so no action needed on your part for docs other than giving a review if you can. cc @sarah11918 so she's aware!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants