Skip to content

Commit 22cba50

Browse files
add language client examples to YAML files
1 parent e4094f4 commit 22cba50

File tree

653 files changed

+37021
-737
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

653 files changed

+37021
-737
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ setup: ## Install dependencies for contrib target
4343
@npm install --prefix validator
4444
@npm install --prefix specification
4545
@npm install @redocly/cli
46+
@npm install --prefix docs/examples
4647

4748
clean-dep: ## Clean npm dependencies
4849
@rm -rf compiler/node_modules
@@ -52,6 +53,7 @@ transform-expand-generics: ## Create a new schema with all generics expanded
5253
@npm run transform-expand-generics --prefix compiler
5354

5455
transform-to-openapi: ## Generate the OpenAPI definition from the compiled schema
56+
@make generate-language-examples
5557
@npm run transform-to-openapi -- --schema output/schema/schema.json --flavor stack --output output/openapi/elasticsearch-openapi.json
5658
@npm run transform-to-openapi -- --schema output/schema/schema.json --flavor serverless --output output/openapi/elasticsearch-serverless-openapi.json
5759

@@ -74,6 +76,10 @@ overlay-docs: ## Apply overlays to OpenAPI documents
7476
rm output/openapi/elasticsearch-serverless-openapi.tmp*.json
7577
rm output/openapi/elasticsearch-openapi.tmp*.json
7678

79+
generate-language-examples:
80+
@node docs/examples/generate-language-examples.js
81+
@npm run format:fix-examples --prefix compiler
82+
7783
lint-docs: ## Lint the OpenAPI documents after overlays
7884
@npx @redocly/cli lint "output/openapi/elasticsearch-*.json" --config "docs/linters/redocly.yaml" --format stylish --max-problems 500
7985

compiler/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"lint:fix": "ts-standard --fix src",
99
"format:check": "prettier --config .prettierrc.json --loglevel warn --check ../specification/",
1010
"format:fix": "prettier --config .prettierrc.json --loglevel warn --write ../specification/",
11+
"format:fix-examples": "prettier --config .prettierrc.json --loglevel warn --write ../specification/**/*.yaml",
1112
"generate-schema": "ts-node src/index.ts",
1213
"transform-expand-generics": "ts-node src/transform/expand-generics.ts",
1314
"transform-to-openapi": "ts-node src/transform/schema-to-openapi.ts",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { parseDocument: yamlParseDocument } = require('yaml');
4+
const { convertRequests, loadSchema } = require('@elastic/request-converter');
5+
6+
const LANGUAGES = ['Python', 'JavaScript', 'Ruby', 'PHP', 'curl'];
7+
8+
async function generateLanguages(example) {
9+
const doc = yamlParseDocument(await fs.promises.readFile(example, 'utf8'));
10+
const data = doc.toJS();
11+
let request = data.method_request;
12+
if (data.value) {
13+
if (typeof data.value === 'string') {
14+
request += '\n' + data.value;
15+
}
16+
else {
17+
request += '\n' + JSON.stringify(data.value);
18+
}
19+
}
20+
data.alternatives = {};
21+
for (const lang of LANGUAGES) {
22+
data.alternatives[lang] = (await convertRequests(request, lang, {})).trim();
23+
}
24+
doc.delete('alternatives');
25+
doc.add(doc.createPair('alternatives', data.alternatives));
26+
await fs.promises.writeFile(example, doc.toString({lineWidth: 132}));
27+
}
28+
29+
async function* walkExamples(dir) {
30+
for await (const d of await fs.promises.opendir(dir)) {
31+
const entry = path.join(dir, d.name);
32+
if (d.isDirectory()) {
33+
yield* walkExamples(entry);
34+
}
35+
else if (d.isFile() && entry.includes('/examples/request/') && entry.endsWith('.yaml')) {
36+
yield entry;
37+
}
38+
}
39+
}
40+
41+
async function main() {
42+
let count = 0;
43+
let errors = 0;
44+
await loadSchema('output/schema/schema.json');
45+
for await (const example of walkExamples('./specification/')) {
46+
try {
47+
await generateLanguages(example);
48+
}
49+
catch (err) {
50+
console.log(`${example}: ${err}`);
51+
errors += 1;
52+
}
53+
count += 1;
54+
}
55+
console.log(`${count} examples processed, ${errors} errors.`);
56+
}
57+
58+
main();

0 commit comments

Comments
 (0)