Skip to content

Commit 7e20fb3

Browse files
committed
Update API examples to use async/await and ES modules instead of axios
1 parent 0166f4c commit 7e20fb3

File tree

5 files changed

+243
-162
lines changed

5 files changed

+243
-162
lines changed

public/js/components/api-keys/api-delete-key.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,44 @@ export class ApiDeleteKey extends ApiEndpointBase {
2020
this._codeExamples = {
2121
curl: `curl -X DELETE "https://api.example.com/api/1/api-keys/YOUR_API_KEY_ID" \\
2222
-H "Authorization: Bearer YOUR_JWT_TOKEN"`,
23-
fetch: `fetch('https://api.example.com/api/1/api-keys/YOUR_API_KEY_ID', {
24-
method: 'DELETE',
25-
headers: {
26-
'Authorization': 'Bearer YOUR_JWT_TOKEN'
23+
fetch: `// Using async/await with ES modules
24+
const deleteApiKey = async () => {
25+
try {
26+
const response = await fetch('https://api.example.com/api/1/api-keys/YOUR_API_KEY_ID', {
27+
method: 'DELETE',
28+
headers: {
29+
'Authorization': 'Bearer YOUR_JWT_TOKEN'
30+
}
31+
});
32+
33+
const data = await response.json();
34+
console.log(data);
35+
} catch (error) {
36+
console.error('Error:', error);
2737
}
28-
})
29-
.then(response => response.json())
30-
.then(data => console.log(data))
31-
.catch(error => console.error('Error:', error));`,
32-
nodejs: `const axios = require('axios');
38+
};
3339
34-
axios.delete('https://api.example.com/api/1/api-keys/YOUR_API_KEY_ID', {
35-
headers: {
36-
'Authorization': 'Bearer YOUR_JWT_TOKEN'
40+
deleteApiKey();`,
41+
nodejs: `// Using Node.js built-in fetch with ES modules
42+
import { fetch } from 'node:fetch';
43+
44+
const deleteApiKey = async () => {
45+
try {
46+
const response = await fetch('https://api.example.com/api/1/api-keys/YOUR_API_KEY_ID', {
47+
method: 'DELETE',
48+
headers: {
49+
'Authorization': 'Bearer YOUR_JWT_TOKEN'
50+
}
51+
});
52+
53+
const data = await response.json();
54+
console.log(data);
55+
} catch (error) {
56+
console.error('Error:', error);
3757
}
38-
})
39-
.then(response => {
40-
console.log(response.data);
41-
})
42-
.catch(error => {
43-
console.error('Error:', error);
44-
});`,
58+
};
59+
60+
deleteApiKey();`,
4561
python: `import requests
4662
4763
headers = {

public/js/components/api-keys/api-get-keys.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,46 @@ export class ApiGetKeys extends ApiEndpointBase {
3636
curl: `curl -X GET "https://api.example.com/api/1/api-keys" \\
3737
-H "Authorization: Bearer YOUR_JWT_TOKEN" \\
3838
-H "Accept: application/json"`,
39-
fetch: `fetch('https://api.example.com/api/1/api-keys', {
40-
method: 'GET',
41-
headers: {
42-
'Authorization': 'Bearer YOUR_JWT_TOKEN',
43-
'Accept': 'application/json'
39+
fetch: `// Using async/await with ES modules
40+
const fetchApiKeys = async () => {
41+
try {
42+
const response = await fetch('https://api.example.com/api/1/api-keys', {
43+
method: 'GET',
44+
headers: {
45+
'Authorization': 'Bearer YOUR_JWT_TOKEN',
46+
'Accept': 'application/json'
47+
}
48+
});
49+
50+
const data = await response.json();
51+
console.log(data);
52+
} catch (error) {
53+
console.error('Error:', error);
4454
}
45-
})
46-
.then(response => response.json())
47-
.then(data => console.log(data))
48-
.catch(error => console.error('Error:', error));`,
49-
nodejs: `const axios = require('axios');
55+
};
5056
51-
axios.get('https://api.example.com/api/1/api-keys', {
52-
headers: {
53-
'Authorization': 'Bearer YOUR_JWT_TOKEN',
54-
'Accept': 'application/json'
57+
fetchApiKeys();`,
58+
nodejs: `// Using Node.js built-in fetch with ES modules
59+
import { fetch } from 'node:fetch';
60+
61+
const fetchApiKeys = async () => {
62+
try {
63+
const response = await fetch('https://api.example.com/api/1/api-keys', {
64+
method: 'GET',
65+
headers: {
66+
'Authorization': 'Bearer YOUR_JWT_TOKEN',
67+
'Accept': 'application/json'
68+
}
69+
});
70+
71+
const data = await response.json();
72+
console.log(data);
73+
} catch (error) {
74+
console.error('Error:', error);
5575
}
56-
})
57-
.then(response => {
58-
console.log(response.data);
59-
})
60-
.catch(error => {
61-
console.error('Error:', error);
62-
});`,
76+
};
77+
78+
fetchApiKeys();`,
6379
python: `import requests
6480
6581
headers = {

public/js/components/api-keys/api-html-to-pdf.js

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -73,71 +73,86 @@ export class ApiHtmlToPdf extends ApiEndpointBase {
7373
"filename": "document.pdf",
7474
"store": false
7575
}'`,
76-
fetch: `fetch('https://api.example.com/api/1/html-to-pdf', {
77-
method: 'POST',
78-
headers: {
79-
'Authorization': 'Bearer YOUR_JWT_TOKEN',
80-
'Content-Type': 'application/json'
81-
},
82-
body: JSON.stringify({
83-
html: '<html><body><h1>Hello, World!</h1></body></html>',
84-
options: {
85-
format: 'A4',
86-
printBackground: true,
87-
margin: {
88-
top: '1cm',
89-
right: '1cm',
90-
bottom: '1cm',
91-
left: '1cm'
92-
}
93-
},
94-
filename: 'document.pdf',
95-
store: false
96-
})
97-
})
98-
.then(response => response.blob())
99-
.then(blob => {
100-
// Create a link to download the PDF
101-
const url = window.URL.createObjectURL(blob);
102-
const a = document.createElement('a');
103-
a.href = url;
104-
a.download = 'document.pdf';
105-
document.body.appendChild(a);
106-
a.click();
107-
window.URL.revokeObjectURL(url);
108-
})
109-
.catch(error => console.error('Error:', error));`,
110-
nodejs: `const axios = require('axios');
111-
const fs = require('fs');
76+
fetch: `// Using async/await with ES modules
77+
const generatePdf = async () => {
78+
try {
79+
const response = await fetch('https://api.example.com/api/1/html-to-pdf', {
80+
method: 'POST',
81+
headers: {
82+
'Authorization': 'Bearer YOUR_JWT_TOKEN',
83+
'Content-Type': 'application/json'
84+
},
85+
body: JSON.stringify({
86+
html: '<html><body><h1>Hello, World!</h1></body></html>',
87+
options: {
88+
format: 'A4',
89+
printBackground: true,
90+
margin: {
91+
top: '1cm',
92+
right: '1cm',
93+
bottom: '1cm',
94+
left: '1cm'
95+
}
96+
},
97+
filename: 'document.pdf',
98+
store: false
99+
})
100+
});
101+
102+
const blob = await response.blob();
103+
104+
// Create a link to download the PDF
105+
const url = window.URL.createObjectURL(blob);
106+
const a = document.createElement('a');
107+
a.href = url;
108+
a.download = 'document.pdf';
109+
document.body.appendChild(a);
110+
a.click();
111+
window.URL.revokeObjectURL(url);
112+
} catch (error) {
113+
console.error('Error:', error);
114+
}
115+
};
112116
113-
axios.post('https://api.example.com/api/1/html-to-pdf', {
114-
html: '<html><body><h1>Hello, World!</h1></body></html>',
115-
options: {
116-
format: 'A4',
117-
printBackground: true,
118-
margin: {
119-
top: '1cm',
120-
right: '1cm',
121-
bottom: '1cm',
122-
left: '1cm'
123-
}
124-
},
125-
filename: 'document.pdf',
126-
store: false
127-
}, {
128-
headers: {
129-
'Authorization': 'Bearer YOUR_JWT_TOKEN',
130-
'Content-Type': 'application/json'
131-
},
132-
responseType: 'arraybuffer'
133-
})
134-
.then(response => {
135-
fs.writeFileSync('document.pdf', response.data);
136-
console.log('PDF saved to document.pdf');
137-
})
138-
.catch(error => {
139-
console.error('Error:', error);
140-
});`,
117+
generatePdf();`,
118+
nodejs: `// Using Node.js built-in fetch with ES modules
119+
import { fetch } from 'node:fetch';
120+
import { writeFile } from 'node:fs/promises';
121+
122+
const generatePdf = async () => {
123+
try {
124+
const response = await fetch('https://api.example.com/api/1/html-to-pdf', {
125+
method: 'POST',
126+
headers: {
127+
'Authorization': 'Bearer YOUR_JWT_TOKEN',
128+
'Content-Type': 'application/json'
129+
},
130+
body: JSON.stringify({
131+
html: '<html><body><h1>Hello, World!</h1></body></html>',
132+
options: {
133+
format: 'A4',
134+
printBackground: true,
135+
margin: {
136+
top: '1cm',
137+
right: '1cm',
138+
bottom: '1cm',
139+
left: '1cm'
140+
}
141+
},
142+
filename: 'document.pdf',
143+
store: false
144+
})
145+
});
146+
147+
const arrayBuffer = await response.arrayBuffer();
148+
await writeFile('document.pdf', Buffer.from(arrayBuffer));
149+
console.log('PDF saved to document.pdf');
150+
} catch (error) {
151+
console.error('Error:', error);
152+
}
153+
};
154+
155+
generatePdf();`,
141156
python: `import requests
142157
import json
143158

public/js/components/api-keys/api-post-key.js

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,52 @@ export class ApiPostKey extends ApiEndpointBase {
3232
-H "Authorization: Bearer YOUR_JWT_TOKEN" \\
3333
-H "Content-Type: application/json" \\
3434
-d '{"name": "Production"}'`,
35-
fetch: `fetch('https://api.example.com/api/1/api-keys', {
36-
method: 'POST',
37-
headers: {
38-
'Authorization': 'Bearer YOUR_JWT_TOKEN',
39-
'Content-Type': 'application/json'
40-
},
41-
body: JSON.stringify({
42-
name: 'Production'
43-
})
44-
})
45-
.then(response => response.json())
46-
.then(data => console.log(data))
47-
.catch(error => console.error('Error:', error));`,
48-
nodejs: `const axios = require('axios');
49-
50-
axios.post('https://api.example.com/api/1/api-keys', {
51-
name: 'Production'
52-
}, {
53-
headers: {
54-
'Authorization': 'Bearer YOUR_JWT_TOKEN',
55-
'Content-Type': 'application/json'
35+
fetch: `// Using async/await with ES modules
36+
const createApiKey = async () => {
37+
try {
38+
const response = await fetch('https://api.example.com/api/1/api-keys', {
39+
method: 'POST',
40+
headers: {
41+
'Authorization': 'Bearer YOUR_JWT_TOKEN',
42+
'Content-Type': 'application/json'
43+
},
44+
body: JSON.stringify({
45+
name: 'Production'
46+
})
47+
});
48+
49+
const data = await response.json();
50+
console.log(data);
51+
} catch (error) {
52+
console.error('Error:', error);
5653
}
57-
})
58-
.then(response => {
59-
console.log(response.data);
60-
})
61-
.catch(error => {
62-
console.error('Error:', error);
63-
});`,
54+
};
55+
56+
createApiKey();`,
57+
nodejs: `// Using Node.js built-in fetch with ES modules
58+
import { fetch } from 'node:fetch';
59+
60+
const createApiKey = async () => {
61+
try {
62+
const response = await fetch('https://api.example.com/api/1/api-keys', {
63+
method: 'POST',
64+
headers: {
65+
'Authorization': 'Bearer YOUR_JWT_TOKEN',
66+
'Content-Type': 'application/json'
67+
},
68+
body: JSON.stringify({
69+
name: 'Production'
70+
})
71+
});
72+
73+
const data = await response.json();
74+
console.log(data);
75+
} catch (error) {
76+
console.error('Error:', error);
77+
}
78+
};
79+
80+
createApiKey();`,
6481
python: `import requests
6582
import json
6683

0 commit comments

Comments
 (0)