Skip to content

Commit

Permalink
Update Endpoints documentation with helpful details. (#4033)
Browse files Browse the repository at this point in the history
* Update Endpoints documentation:

- Describe how simple endpoints can be access directly via `/__data.json`.
- Describe how standalone endpoints can be given file extensions (`.json`).

* use more realistic endpoint, add index files, add src/routes prefix

* support tables

* combine __data.json docs with accept header

Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
brev and Rich-Harris authored Feb 22, 2022
1 parent 231d0c8 commit 64234fa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
11 changes: 10 additions & 1 deletion documentation/docs/01-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export async function post({ request }) {
</form>
```

If you request the route with an `accept: application/json` header, SvelteKit will render the endpoint data as JSON, rather than the page as HTML.
If you request the route with an `accept: application/json` header, SvelteKit will render the endpoint data as JSON, rather than the page as HTML. You can also get the raw data by appending `/__data.json` to the URL, e.g. `/items/__data.json`.

#### Body parsing

Expand Down Expand Up @@ -269,6 +269,15 @@ Most commonly, endpoints exist to provide data to the page with which they're pa

> Support for streaming request and response bodies is [coming soon](https://github.com/sveltejs/kit/issues/3419).
Standalone endpoints can be given a file extension if desired, or accessed directly if not:

| filename | endpoint |
| ----------------------------- | ---------- |
| src/routes/data/index.json.js | /data.json |
| src/routes/data.json.js | /data.json |
| src/routes/data/index.js | /data |
| src/routes/data.js | /data |

### Private modules

Files and directories with a leading `_` or `.` (other than [`.well-known`](https://en.wikipedia.org/wiki/Well-known_URI)) are private by default, meaning that they do not create routes (but can be imported by files that do). You can configure which modules are considered public or private with the [`routes`](/docs/configuration#routes) configuration.
Expand Down
1 change: 1 addition & 0 deletions sites/kit.svelte.dev/src/lib/docs/client/docs.css
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
.content table {
margin: 0;
margin-bottom: 2em;
max-width: var(--linemax);
}

.content section {
Expand Down
12 changes: 4 additions & 8 deletions sites/kit.svelte.dev/src/routes/content.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,10 @@ function plaintext(markdown) {
listitem: block,
checkbox: block,
paragraph: (text) => `${text}\n\n`,
table: () => {
throw new Error('TODO implement tables');
},
tablerow: () => {
throw new Error('TODO implement tables');
},
tablecell: () => {
throw new Error('TODO implement tables');
table: block,
tablerow: block,
tablecell: (text, opts) => {
return text + ' ';
},
strong: inline,
em: inline,
Expand Down

0 comments on commit 64234fa

Please sign in to comment.