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

Improve APIRoute Typing #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jonathantneal
Copy link

Changes

This updates the APIRoute type to support specific Props, allowing authors to type routes more succinctly.

This is only a suggested improvement for authoring convenience. Typing without this change is provided below this example for comparison.

Example: /src/pages/content/[slug].js.ts:

import type * as TS from 'astro'

export interface Props {
  title: string
  content: string
}

export function getStaticPaths() {
  return [
    {
      params: {
        slug: 'some-slug',
      },
      props: {
        title: 'Some Title',
        content: 'Some content.',
      },
    }
  ]
}

With APIRoute:

export const get: APIRoute<Props> = ({ props }) => ({
  body: `export default ${JSON.stringify(props)}`,
})

Without APIRoute:

export const get = ({ props }: TS.APIContext<Props>): TS.EndpointOutput => ({
  body: `export default ${JSON.stringify(props)}`,
})

Testing

This change was tested by overriding the type locally.

Docs

/cc @withastro/maintainers-docs for feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant