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

chore(gatsby): Add types for getServerData #34406

Merged
merged 5 commits into from
Jan 6, 2022

Conversation

imjoshin
Copy link
Contributor

@imjoshin imjoshin commented Jan 5, 2022

Description

getServerData does not currently have any types around it. This code change should allow developers to have type safety within this method for each page.

Note: this does change the type of the old IServerData's headers from a Record to a Map. There were inconsistencies in it's typing, so this syncs them up. Please mention any concerns there may be with this conversion.

Using the example from Gatsby's SSR doc, and given the following type:

type ServerDataProps = {
  message?: string,
  status?: string,
}

We can get type safety in an anonymous function using the given shorthand:

export const getServerData: GetServerData<ServerDataProps> = async (props) => {
  try {
    const res = await fetch(`https://dog.ceo/api/breeds/image/random`)
    if (!res.ok) {
      throw new Error(`Response failed`)
    }
    return {
      props: await res.json(),
    }
  } catch (error) {
    return {
      status: 500,
      headers: new Map(),
      props: {}
    }
  }
}

Or if we use a named function, we can use the Props and Return types:

export async function getServerData(props: GetServerDataProps): GetServerDataReturn<ServerDataProps> {
  try {
    const res = await fetch(`https://dog.ceo/api/breeds/image/random`)
    if (!res.ok) {
      throw new Error(`Response failed`)
    }
    return {
      props: await res.json(),
    }
  } catch (error) {
    return {
      status: 500,
      headers: new Map(),
      props: {}
    }
  } 
}

[sc-38899]

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jan 5, 2022
@pieh pieh added topic: TypeScript Issues and PRs related to TS in general, public typings or gatsby-plugin-typescript and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jan 6, 2022
packages/gatsby/index.d.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Awesome 🎉

@pieh pieh added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Jan 6, 2022
@LekoArts LekoArts changed the title Add types for getServerData chore(gatsby): Add types for getServerData Jan 6, 2022
@gatsbybot gatsbybot merged commit 183b5f1 into gatsbyjs:master Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes topic: TypeScript Issues and PRs related to TS in general, public typings or gatsby-plugin-typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants