Skip to content

Improve APIRoute Typing#3

Open
jonathantneal wants to merge 1 commit into
mainfrom
jn.improve-api-route-typing
Open

Improve APIRoute Typing#3
jonathantneal wants to merge 1 commit into
mainfrom
jn.improve-api-route-typing

Conversation

@jonathantneal

Copy link
Copy Markdown

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