Skip to content

Endpoints Guide#1638

Merged
Jutanium merged 11 commits intomainfrom
endpoints-guide
Sep 29, 2022
Merged

Endpoints Guide#1638
Jutanium merged 11 commits intomainfrom
endpoints-guide

Conversation

@Jutanium
Copy link
Copy Markdown
Contributor

@Jutanium Jutanium commented Sep 28, 2022

What kind of changes does this PR include?

  • New or updated content

Description

  • Closes New: Add "endpoints" guide #760 and Document all, del, and other possible API route functions #1618
  • Moves the File Routes content from Astro Pages and the API Routes content from SSR into one page
  • Renames "file routes" to "static file endpoints" and "api routes" to "server endpoints"
  • Documents that you can match any HTTP method, that you must use del to match DELETE, and that you can use all to as a catch-all
  • Fleshes out the buffer PNG example so that it works if you copy-paste it
  • Show how you can use params and getStaticPaths for static file endpoints (Document getStaticPaths in non-HTML pages #768), explain that props don't do anything here, and that you can access the dynamic parameter without getStaticPaths in SSR mode
  • Labels the CAPTCHA example as an example, and change the order of the files so that the endpoint is shown before the code that calls it

@netlify
Copy link
Copy Markdown

netlify bot commented Sep 28, 2022

Deploy Preview for astro-docs-2 ready!

Name Link
🔨 Latest commit db28e3d
🔍 Latest deploy log https://app.netlify.com/sites/astro-docs-2/deploys/6335e70ce80a210008579d55
😎 Deploy Preview https://deploy-preview-1638--astro-docs-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@@ -0,0 +1,275 @@
---
layout: ~/layouts/MainLayout.astro
title: Endpoints
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm totally fine with calling these Endpoints, but just for reference I've also seen them called API Routes. I'm assuming the docs team discussed it and decided on Endpoints, but just in case not, there's another name we've used.

}
```

This will generate three JSON endpoints at build time: `/api/1.json`, `/api/2.json`, `/api/3.json`. Dynamic routing with endpoints works the same as it does with pages, but because the endpoint is a function and not a component, [props](/en/reference/api-reference/#data-passing-with-props) aren't supported.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
This will generate three JSON endpoints at build time: `/api/1.json`, `/api/2.json`, `/api/3.json`. Dynamic routing with endpoints works the same as it does with pages, but because the endpoint is a function and not a component, [props](/en/reference/api-reference/#data-passing-with-props) aren't supported.
This will generate three JSON files at build time: `/api/1.json`, `/api/2.json`, `/api/3.json`. Dynamic routing with endpoints works the same as it does with pages, but because the endpoint is a function and not a component, [props](/en/reference/api-reference/#data-passing-with-props) aren't supported.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, for SSG mode we probably could support props, at least I don't see a reason why not. I don't think we excluded that on purpose.

This will generate three JSON endpoints at build time: `/api/1.json`, `/api/2.json`, `/api/3.json`. Dynamic routing with endpoints works the same as it does with pages, but because the endpoint is a function and not a component, [props](/en/reference/api-reference/#data-passing-with-props) aren't supported.

### `request`
All endpoints receive a `request` property, but only `request.url` works in static mode. This returns the full URL of the current endpoint, using your [`site`](/en/reference/configuration-reference/#site) config option as the base.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this true in dev mode? I would expect that the url is the localhost url during dev.

I would not expect a difference here between endpoints and pages, if there is one it's probably a bug.

Everything in the previous section can be used in SSR mode, but the endpoints will be built when they are requested. This unlocks new features that are unavailable at build time, and allows you to build API routes that listen for requests and securely execute code on the server at runtime.

:::note
Be sure to [enable SSR](http://localhost:3001/en/guides/server-side-rendering/#enabling-ssr-in-your-project) before trying these examples.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is pointing to localhost


```ts title="src/pages/test-post.json.ts"
export const post: APIRoute = async ({ request }) => {
if (request.headers.get("Content-Type") === "application/json") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

good example

```

### Redirects
Since `Astro.redirect` is not available in API Routes you can use [`Response.redirect`](https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect) to redirect:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a bug and something we'll probably be changing, should we document it anyways since it's useful info for now and just remove later?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, let's document it for now. Is it just Astro.redirect that should be available, or other functions on the Astro global?

@delucis delucis linked an issue Sep 28, 2022 that may be closed by this pull request
Copy link
Copy Markdown
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Love this page @Jutanium! Made some suggestions for your consideration! ❤️

---
Astro lets you create custom endpoints to serve any kind of data. You can use this to generate images, expose an RSS document, or build a full API for your site.

If your project is in [static](/en/reference/configuration-reference/#output) mode, custom endpoints are called at build time to produce static files. In [SSR](/en/guides/server-side-rendering/) mode, custom endpoints turn into live server endpoints that are called on request. Static and SSR endpoints are defined similarly, but SSR endpoints support additional features.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
If your project is in [static](/en/reference/configuration-reference/#output) mode, custom endpoints are called at build time to produce static files. In [SSR](/en/guides/server-side-rendering/) mode, custom endpoints turn into live server endpoints that are called on request. Static and SSR endpoints are defined similarly, but SSR endpoints support additional features.
If your project is in [static](/en/reference/configuration-reference/#output) mode

Do you think this maybe sounds a little "jargony" right off the bat for the regular, default SSG site most people will have? I get that it's the natural counterpart to "SSR/server mode" but I'm not sure we really refer to the "mode" so much when we're talking about our static sites? It's normally only as a way to distinguish from SSR.

Maybe a "friendlier" description here, then "static mode" throughout is probably fine.

In statically-generated sites...

As a default statically-generated site...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I like statically-generated sites, and I rephrased the second sentence to make it clear that static is the default:

In statically-generated sites, your custom endpoints are called at build time to produce static files. If you opt in to [SSR](/en/guides/server-side-rendering/) mode...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, I like that!

description: Learn how to create endpoints that serve any kind of data
i18nReady: true
---
Astro lets you create custom endpoints to serve any kind of data. You can use this to generate images, expose an RSS document, or build a full API for your site.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Astro lets you create custom endpoints to serve any kind of data. You can use this to generate images, expose an RSS document, or build a full API for your site.
Astro lets you create custom endpoints (API routes) to serve any kind of data. You can use this to generate images, expose an RSS document, or build a full API for your site.

Not sure whether even literally just throwing that in there in parenthesis is helpful, but given that a lot of people might be searching for/expecting to see the term API routes, maybe can't hurt to have it somewhere near the very top of the page? Esp. for search results.

Copy link
Copy Markdown
Contributor Author

@Jutanium Jutanium Sep 29, 2022

Choose a reason for hiding this comment

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

Do you think we should do it here, making "API Routes" a synonym for all custom endpoints, and remove the (API Routes) next to Server Endpoints? I was thinking that it was specifically the server version that was previously called API Routes, and the static version we called non-HTML pages (and then file routes).

Or maybe something like

Astro lets you create custom endpoints to serve any kind of data. You can use this to generate images, expose an RSS document, or use them as API Routes to build a full API for your site.

What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like that!


The return object can also have an `encoding` property. It can be any valid [`BufferEncoding`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/bdd02508ddb5eebcf701fdb8ffd6e84eabf47885/types/node/buffer.d.ts#L169) accepted by node.js' `fs.writeFile` method. For example, to produce a binary png image:

```ts title="src/pages/astro-logo.png.ts" {7}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Small highlighting issue, should be {6}
CleanShot 2022-09-28 at 18 52 14@2x

Dan Jutan and others added 3 commits September 29, 2022 10:21
@Jutanium
Copy link
Copy Markdown
Contributor Author

Thanks for the reviews, I've made changes to address the comments!

@sarah11918
Copy link
Copy Markdown
Member

LGTM, Dan! (I'll update branch to make my co-author cred easy. 😉 )

nokazn pushed a commit to nokazn/docs.astro.build that referenced this pull request Oct 2, 2022
* endpoints draft

* fix links

* link to endpoints guide from SSR page

* request methods -> http methods

* api routes -> server endpoints on SSR page

* use canonical url

* move endpoints in nav to after static assets

* Update src/pages/en/core-concepts/endpoints.md

Co-authored-by: Matthew Phillips <matthew@skypack.dev>

* revision: quick changes

* addressed comments

Co-authored-by: Matthew Phillips <matthew@skypack.dev>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
kevinzunigacuellar added a commit that referenced this pull request Oct 3, 2022
* i18n(es): add endpoints string for nav.ts

* i18n(es): update astro-pages to match #1638

* i18n(es): update broken links

Co-authored-by: Kevin Zuniga Cuellar <46791833+kevinzunigacuellar@users.noreply.github.com>
kevinzunigacuellar added a commit that referenced this pull request Oct 6, 2022
* 🌐 Updated text from #1638

* Err typo link elem a MDN

* 🐛 **Markdown** not worked in md example

* ✅ Updated links

* ✅ Updated links on `markdown-content.md`

* 📝 Readed `Pages non-HTML` part

* ✅ Canonical URL

* Update src/pages/fr/core-concepts/astro-pages.md

Co-authored-by: Happydev <81974850+MoustaphaDev@users.noreply.github.com>

* Update src/pages/fr/core-concepts/astro-pages.md

Co-authored-by: Happydev <81974850+MoustaphaDev@users.noreply.github.com>

* 📝 Edited example of `src/pages/index.astro` in Pages Astro section

* 📝 Composant Layouts

* 📝 Composants Layout l:45

* 📝 Composants Layout l:63

Co-authored-by: Happydev <81974850+MoustaphaDev@users.noreply.github.com>
Co-authored-by: Kevin Zuniga Cuellar <46791833+kevinzunigacuellar@users.noreply.github.com>
trueberryless added a commit to trueberryless/withastro-docs that referenced this pull request Oct 22, 2024
yanthomasdev added a commit that referenced this pull request Nov 4, 2024
* update translation #1442

* update translation #1434

* update translation #1510

* update translation #1538

* Update src/content/docs/de/basics/astro-pages.mdx

* Update src/content/docs/de/basics/astro-pages.mdx

* Update src/content/docs/de/basics/astro-pages.mdx

* Update src/content/docs/de/basics/astro-pages.mdx

* Update src/content/docs/de/basics/astro-pages.mdx

* update translation #1638

* update translation #2091

* update translation #2133

* update translation #2409

* update translation #2371

* update translation #4610

* update translation #5128

* update translation #5205

* update translation #5240

* update translation #5364

* update translation #5765

* update translation #6267

* remove paragraph where I cant find when it was deleted in original version

* update translation #6620

* update translation #8495

* update translation #8573

* update translation #9336

* update translation #9336 2/2

didnt save file locally, so git didnt add

* fix all visual differences by comparing manually against original english version

* fix broken links in `astro-pages.mdx`

* Breaking changes to other files! fixing links which link to the �stro-pages.mdx file

* but now...

* Update src/content/docs/de/basics/astro-pages.mdx

Co-authored-by: Max <51922004+Maxframe@users.noreply.github.com>

* Update src/content/docs/de/basics/astro-pages.mdx

Co-authored-by: Max <51922004+Maxframe@users.noreply.github.com>

* Update src/content/docs/de/basics/astro-pages.mdx

Co-authored-by: Max <51922004+Maxframe@users.noreply.github.com>

* revert Update markdown-content.mdx

@lunaria-track:src/content/docs/de/basics/astro-pages.mdx

---------

Co-authored-by: Max <51922004+Maxframe@users.noreply.github.com>
Co-authored-by: Yan <61414485+yanthomasdev@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

add new content Document something that is not in docs. May require testing, confirmation, or affect other pages.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document all, del, and other possible API route functions New: Add "endpoints" guide

4 participants