Skip to content

Commit ae474aa

Browse files
documented pagination
1 parent 7759489 commit ae474aa

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

public/guide.html

+18
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ <h2 id="listallresources">List all resources</h2>
8080
{ id: 100, title: '[...]' /* ... */ }
8181
]
8282
</code></pre>
83+
<h2 id="paginateallresources">Paginate all resources</h2>
84+
<p>Use <code>_page</code> and optionally <code>_limit</code> to paginate returned data.</p>
85+
<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>
86+
<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>
87+
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/posts?_page=1&amp;_limit=2')
88+
.then(async response =&gt; {
89+
const link = response.headers.get('link')
90+
const json = await response.json()
91+
console.log(link, json)
92+
})
93+
94+
// Output
95+
'&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"'
96+
[
97+
{ id: 1, title: '[...]' /* ... */ },
98+
{ id: 2, title: '[...]' /* ... */ }
99+
]
100+
</code></pre>
83101
<h2 id="createaresource">Create a resource</h2>
84102
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/posts', {
85103
method: 'POST',

templates/GUIDE.md

+24
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ fetch('https://jsonplaceholder.typicode.com/posts')
3838
]
3939
```
4040

41+
## Paginate all resources
42+
43+
Use `_page` and optionally `_limit` to paginate returned data.
44+
45+
In the `link` header you'll get `"first"`, `"prev"`, `"next"` and `"last"` links.
46+
47+
_Source:_ https://github.com/typicode/json-server/blob/master/README.md#paginate
48+
49+
```js
50+
fetch('https://jsonplaceholder.typicode.com/posts?_page=1&_limit=2')
51+
.then(async response => {
52+
const link = response.headers.get('link')
53+
const json = await response.json()
54+
console.log(link, json)
55+
})
56+
57+
// Output
58+
'<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"'
59+
[
60+
{ id: 1, title: '[...]' /* ... */ },
61+
{ id: 2, title: '[...]' /* ... */ }
62+
]
63+
```
64+
4165
## Create a resource
4266

4367
```js

0 commit comments

Comments
 (0)