Skip to content

Commit d310f22

Browse files
documented pagination
1 parent 7ae4432 commit d310f22

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

public/guide.html

+19-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,25 @@ <h3 id="listallresources">List all resources</h3>
8282
{ id: 100, title: '[...]' /* ... */ }
8383
]
8484
</code></pre>
85-
<h3 id="createaresource">Create a resource</h3>
85+
<h2 id="paginateallresources">Paginate all resources</h2>
86+
<p>Use <code>_page</code> and optionally <code>_limit</code> to paginate returned data.</p>
87+
<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>
88+
<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>
89+
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/posts?_page=1&amp;_limit=2')
90+
.then(async response =&gt; {
91+
const link = response.headers.get('link')
92+
const json = await response.json()
93+
console.log(link, json)
94+
})
95+
96+
// Output
97+
'&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"'
98+
[
99+
{ id: 1, title: '[...]' /* ... */ },
100+
{ id: 2, title: '[...]' /* ... */ }
101+
]
102+
</code></pre>
103+
<h2 id="createaresource">Create a resource</h2>
86104
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/posts', {
87105
method: 'POST',
88106
body: JSON.stringify({

templates/GUIDE.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ fetch('https://jsonplaceholder.typicode.com/posts')
4040
]
4141
```
4242

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

4569
```js
@@ -121,7 +145,7 @@ fetch('https://jsonplaceholder.typicode.com/posts/1', {
121145
}
122146
```
123147

124-
Important: the resource will not be really updated on the server but it will be faked as if.
148+
Important: the resource will not be really updated on the server but it will be faked as if.
125149

126150
### Delete a resource
127151

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

134-
Important: the resource will not be really deleted on the server but it will be faked as if.
158+
Important: the resource will not be really deleted on the server but it will be faked as if.
135159

136160
### Filter resources
137161

0 commit comments

Comments
 (0)