Skip to content

Commit ea2fee3

Browse files
authored
feat: add code samples for actor collection (#1318)
Here are the first two code samples for the API docs. These are for the JS client and are a tentative proposal. When we've agreed on how they should look, I'll also add the ones for the Python client. **Note**: The Palo Alto plugin currently doesn't support TypeScript and there's no straightforward way to add multiple custom code samples for the same language (I thought we could have JS and TS client samples under the JavaScript bit). I have created an issue asking about this on the plugin's repo: PaloAltoNetworks/docusaurus-openapi-docs#1035 **Note 2**: For the POST request, as mentioned by @netmilk , it would be nice to have the request body present, but that would be complicated, since the schema isn't available in the examples, and would make the testing harder. I think it's not so bad, though, since the request body is shown right under the code example, so it's quite easy to find. So, on the new docs it will look like this: ![CleanShot 2024-12-04 at 16 06 42@2x](https://github.com/user-attachments/assets/d4c00c7b-d5e3-476c-bb3c-61fa9e1ea0e3) And in the old/current docs like this: (I had to fix the background color) ![CleanShot 2024-12-04 at 16 27 02@2x](https://github.com/user-attachments/assets/25279a78-8738-433d-bf31-54aee9205d69)
2 parents 7951a66 + d164cf9 commit ea2fee3

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

.redocly.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ plugins:
2727
decorators:
2828
apify/legacy-doc-url-decorator: on
2929
apify/client-references-links-decorator: on
30+
apify/code-samples-decorator: on
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ApifyClient } from 'apify-client';
2+
3+
const apifyClient = new ApifyClient({ token: 'my-token' });
4+
const { items } = await apifyClient.actors().list();
5+
6+
console.log(items);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ApifyClient } from 'apify-client';
2+
3+
const apifyClient = new ApifyClient({ token: 'my-token' });
4+
const myActor = await apifyClient.actors().create({ name: 'my-actor' });
5+
6+
console.log(myActor);

apify-api/plugins/apify.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const ClientReferencesLinksDecorator = require('./decorators/client-references-links-decorator');
2+
const CodeSamplesDecorator = require('./decorators/code-samples-decorator');
23
const LegacyDocUrlDecorator = require('./decorators/legacy-doc-url-decorator');
34

45
module.exports = {
@@ -7,6 +8,7 @@ module.exports = {
78
oas3: {
89
'legacy-doc-url-decorator': LegacyDocUrlDecorator,
910
'client-references-links-decorator': ClientReferencesLinksDecorator,
11+
'code-samples-decorator': CodeSamplesDecorator,
1012
},
1113
},
1214
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { existsSync } = require('fs');
2+
const path = require('path');
3+
4+
const X_CODE_SAMPLES_PROPERTY = 'x-codeSamples';
5+
6+
const LANGUAGES = [
7+
{ lang: 'JavaScript', label: 'JS client' },
8+
];
9+
10+
/**
11+
* This decorator adds the x-codeSamples property to the schema object if a file with the operationId exists in the
12+
* code samples directory.
13+
* This helps us add the samples in a consistent way and find out which operations are missing a sample.
14+
*
15+
* The added sample link will look like this:
16+
* x-codeSamples:
17+
* - lang: JavaScript
18+
* label: JS client
19+
* source:
20+
* $ref: ../../code_samples/js/acts_post.js
21+
*/
22+
function CodeSamplesDecorator(target) {
23+
if (!target.operationId) return;
24+
25+
const codeSamples = [];
26+
27+
for (const { lang, label } of LANGUAGES) {
28+
const codeSamplePath = path.join(__dirname, `../../openapi/code_samples/${lang.toLowerCase()}/${target.operationId}.js`);
29+
30+
if (!existsSync(codeSamplePath)) {
31+
// Just use this console log in development to see which operations are missing a code sample.
32+
// console.log(`Missing code sample for operation ${target.operationId}`);
33+
return;
34+
}
35+
36+
codeSamples.push({
37+
lang,
38+
label,
39+
source: {
40+
$ref: codeSamplePath,
41+
},
42+
});
43+
}
44+
45+
if (codeSamples.length) {
46+
target[X_CODE_SAMPLES_PROPERTY] = codeSamples;
47+
}
48+
}
49+
50+
module.exports = () => ({
51+
// Redocly is using a visitor pattern. What the following code does is that whenever the traverser leaves a node of
52+
// type Tag or Operation, it executes CodeSamplesDecorator on it.
53+
Tag: {
54+
leave: CodeSamplesDecorator,
55+
},
56+
Operation: {
57+
leave: CodeSamplesDecorator,
58+
},
59+
});

apify-docs-theme/src/theme/custom.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ html[data-theme='dark'] {
162162
--ifm-footer-link-color: #6b6e80;
163163
--max-layout-width: 1440px;
164164

165-
--ifm-code-background: #f6f8fa;
165+
--ifm-code-background: var(--ifm-pre-background);
166166

167167
--docusaurus-highlighted-code-line-bg: rgba(0, 0, 0, 0.1);
168168

0 commit comments

Comments
 (0)