docs icon indicating copy to clipboard operation
docs copied to clipboard

Update `APIRoute` code snippets and clarify `props` in endpoints

Open ArmandPhilippot opened this issue 5 months ago β€’ 2 comments

πŸ“š Subject area/topic

Endpoints

πŸ“‹ Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/guides/endpoints/

πŸ“‹ Description of content that is out-of-date or incorrect

  1. Update code snippets using APIRoute

Erika was suggesting on Discord:

export const GET: APIRoute = async ({ params, request }) => {...} should use satisfies instead

I'm not entirely sure why. I tried both but I don't see any differences... Maybe type inference in some cases?

  1. Misleading wording regarding props

In params and Dynamic routing, we say:

but because the endpoint is a function and not a component, props aren’t supported.

In static mode, we can share props through the APIContext object using getStaticPaths. So, I think this is not entirely correct and could be confusing. Here's a small repro using a tweaked version of the code snippet we show above this sentence: https://stackblitz.com/edit/astro-endpoints-dynamic-routing?file=src%2Fpages%2Fapi%2F[id].json.ts,src%2Fpages%2Findex.astro&on=stackblitz

But, yeah, I guess we can't do that with on-demand rendered endpoints. So we might need to precise this only applies to on-demand mode.

πŸ–₯️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

https://stackblitz.com/edit/astro-endpoints-dynamic-routing?file=src%2Fpages%2Fapi%2F[id].json.ts,src%2Fpages%2Findex.astro&on=stackblitz

By clicking on the links, the console should log:

1 Chris
15:22:37 [200] /api/1.json 2ms
15:22:38 [200] / 27ms
2 Yan
15:22:39 [200] /api/2.json 4ms
15:22:39 [200] / 28ms
3 Elian
15:22:40 [200] /api/3.json 5ms
15:22:41 [200] / 18ms

props can be used with endpoints.

ArmandPhilippot avatar May 28 '25 13:05 ArmandPhilippot