Skip to content

Commit f5ad858

Browse files
committed
feat: adding custom http client
1 parent 1ff2b79 commit f5ad858

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

docs/examples/custom-http-client.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
id: custom-http-client
3+
title: Custom HTTP client
4+
keywords:
5+
- javascript
6+
- table
7+
- javascript table
8+
- gridjs
9+
- grid js
10+
- datasource
11+
- http client
12+
---
13+
14+
import { LiveExample } from "../../lib/liveExample.js";
15+
16+
You can customize `ServerStorage` class and use your own HTTP client (or SDK) to send the HTTP calls. Simply use `data` property
17+
and return a Promise object that conforms to `StorageResponse` format (https://github.com/grid-js/gridjs/blob/master/src/storage/storage.ts#L21-L24).
18+
19+
In this example, we are implementing our own HTTP client (instead of default `fetch` API that ServerStorage provides):
20+
21+
<LiveExample children={
22+
`
23+
const grid = new Grid({
24+
columns: ['Name', 'Language', 'Released At', 'Artist'],
25+
server: {
26+
url: 'https://api.scryfall.com/cards/search?q=Inspiring',
27+
data: (opts) => {
28+
return new Promise((resolve, reject) => {
29+
// let's implement our own HTTP client
30+
const xhttp = new XMLHttpRequest();
31+
xhttp.onreadystatechange = function() {
32+
if (this.readyState === 4) {
33+
if (this.status === 200) {
34+
const resp = JSON.parse(this.response);
35+
36+
// make sure the output conforms to StorageResponse format:
37+
// https://github.com/grid-js/gridjs/blob/master/src/storage/storage.ts#L21-L24
38+
resolve({
39+
data: resp.data.map(card => [card.name, card.lang, card.released_at, card.artist]),
40+
total: resp.total_cards,
41+
});
42+
} else {
43+
reject();
44+
}
45+
}
46+
};
47+
xhttp.open("GET", opts.url, true);
48+
xhttp.send();
49+
});
50+
}
51+
},
52+
pagination: {
53+
limit: 5
54+
}
55+
});
56+
`
57+
} />

sidebars.js

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module.exports = {
6464
'examples/server-side-search',
6565
'examples/server-side-pagination',
6666
'examples/server-side-sort',
67+
'examples/custom-http-client',
6768
]
6869
}, {
6970
type: 'category',

0 commit comments

Comments
 (0)