From d310f2267134c406020db5fa173289b2f41ae244 Mon Sep 17 00:00:00 2001 From: dotnetCarpenter Date: Thu, 15 Aug 2019 01:28:32 +0200 Subject: [PATCH 1/2] documented pagination --- public/guide.html | 20 +++++++++++++++++++- templates/GUIDE.md | 28 ++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/public/guide.html b/public/guide.html index ca2ee02..8b76f60 100644 --- a/public/guide.html +++ b/public/guide.html @@ -82,7 +82,25 @@

List all resources

{ id: 100, title: '[...]' /* ... */ } ] -

Create a resource

+

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

+
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

fetch('https://jsonplaceholder.typicode.com/posts', {
     method: 'POST',
     body: JSON.stringify({
diff --git a/templates/GUIDE.md b/templates/GUIDE.md
index 5563566..e3b38f9 100644
--- a/templates/GUIDE.md
+++ b/templates/GUIDE.md
@@ -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
+'; rel="first", ; rel="next", ; rel="last"'
+[
+  { id: 1, title: '[...]' /* ... */ },
+  { id: 2, title: '[...]' /* ... */ }
+]
+```
+
 ### Create a resource
 
 ```js
@@ -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
 
@@ -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
 

From d0f61e26223ead9e69467b2a270e21f94e277d73 Mon Sep 17 00:00:00 2001
From: dotnetCarpenter 
Date: Sun, 3 Apr 2022 12:44:27 +0200
Subject: [PATCH 2/2] h2 headings are now h3 headings

---
 public/guide.html | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/public/guide.html b/public/guide.html
index 8b76f60..ff36d7a 100644
--- a/public/guide.html
+++ b/public/guide.html
@@ -82,7 +82,7 @@ 

List all resources

{ id: 100, title: '[...]' /* ... */ } ]
-

Paginate all resources

+

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

@@ -100,7 +100,7 @@

Paginate all resources

{ id: 2, title: '[...]' /* ... */ } ] -

Create a resource

+

Create a resource

fetch('https://jsonplaceholder.typicode.com/posts', {
     method: 'POST',
     body: JSON.stringify({