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

documented pagination #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions public/guide.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ <h3 id="listallresources">List all resources</h3>
{ id: 100, title: '[...]' /* ... */ }
]
</code></pre>
<h3 id="paginateallresources">Paginate all resources</h3>
<p>Use <code>_page</code> and optionally <code>_limit</code> to paginate returned data.</p>
<p>In the <code>link</code> header you'll get <code>"first"</code>, <code>"prev"</code>, <code>"next"</code> and <code>"last"</code> links.</p>
<p><em>Source:</em> <a href="https://github.com/typicode/json-server/blob/master/README.md#paginate">https://github.com/typicode/json-server/blob/master/README.md#paginate</a></p>
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/posts?_page=1&amp;_limit=2')
.then(async response =&gt; {
const link = response.headers.get('link')
const json = await response.json()
console.log(link, json)
})

// Output
'&lt;http://jsonplaceholder.typicode.com/posts?_page=1&amp;_limit=2&gt;; rel="first", &lt;http://jsonplaceholder.typicode.com/posts?_page=2&amp;_limit=2&gt;; rel="next", &lt;http://jsonplaceholder.typicode.com/posts?_page=50&amp;_limit=2&gt;; rel="last"'
[
{ id: 1, title: '[...]' /* ... */ },
{ id: 2, title: '[...]' /* ... */ }
]
</code></pre>
<h3 id="createaresource">Create a resource</h3>
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
Expand Down
28 changes: 26 additions & 2 deletions templates/GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ fetch('https://jsonplaceholder.typicode.com/posts')
]
```

### Paginate all resources

Use `_page` and optionally `_limit` to paginate returned data.

In the `link` header you'll get `"first"`, `"prev"`, `"next"` and `"last"` links.

_Source:_ https://github.com/typicode/json-server/blob/master/README.md#paginate

```js
fetch('https://jsonplaceholder.typicode.com/posts?_page=1&_limit=2')
.then(async response => {
const link = response.headers.get('link')
const json = await response.json()
console.log(link, json)
})

// Output
'<http://jsonplaceholder.typicode.com/posts?_page=1&_limit=2>; rel="first", <http://jsonplaceholder.typicode.com/posts?_page=2&_limit=2>; rel="next", <http://jsonplaceholder.typicode.com/posts?_page=50&_limit=2>; rel="last"'
[
{ id: 1, title: '[...]' /* ... */ },
{ id: 2, title: '[...]' /* ... */ }
]
```

### Create a resource

```js
Expand Down Expand Up @@ -121,7 +145,7 @@ fetch('https://jsonplaceholder.typicode.com/posts/1', {
}
```

Important: the resource will not be really updated on the server but it will be faked as if.
Important: the resource will not be really updated on the server but it will be faked as if.

### Delete a resource

Expand All @@ -131,7 +155,7 @@ fetch('https://jsonplaceholder.typicode.com/posts/1', {
})
```

Important: the resource will not be really deleted on the server but it will be faked as if.
Important: the resource will not be really deleted on the server but it will be faked as if.

### Filter resources

Expand Down