Skip to content

Commit 095610c

Browse files
add language client examples to YAML files
1 parent 291ffd3 commit 095610c

File tree

653 files changed

+40308
-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

+40308
-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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
const fs = require('fs');
21+
const path = require('path');
22+
const { parseDocument: yamlParseDocument } = require('yaml');
23+
const { convertRequests, loadSchema } = require('@elastic/request-converter');
24+
25+
const LANGUAGES = ['Python', 'JavaScript', 'Ruby', 'PHP', 'curl'];
26+
27+
async function generateLanguages(example) {
28+
const doc = yamlParseDocument(await fs.promises.readFile(example, 'utf8'));
29+
const data = doc.toJS();
30+
let request = data.method_request;
31+
if (data.value) {
32+
if (typeof data.value === 'string') {
33+
request += '\n' + data.value;
34+
}
35+
else {
36+
request += '\n' + JSON.stringify(data.value);
37+
}
38+
}
39+
data.alternatives = [];
40+
for (const lang of LANGUAGES) {
41+
data.alternatives.push({
42+
language: lang,
43+
code: (await convertRequests(request, lang, {})).trim(),
44+
});
45+
}
46+
doc.delete('alternatives');
47+
doc.add(doc.createPair('alternatives', data.alternatives));
48+
await fs.promises.writeFile(example, doc.toString({lineWidth: 132}));
49+
}
50+
51+
async function* walkExamples(dir) {
52+
for await (const d of await fs.promises.opendir(dir)) {
53+
const entry = path.join(dir, d.name);
54+
if (d.isDirectory()) {
55+
yield* walkExamples(entry);
56+
}
57+
else if (d.isFile() && entry.includes('/examples/request/') && entry.endsWith('.yaml')) {
58+
yield entry;
59+
}
60+
}
61+
}
62+
63+
async function main() {
64+
let count = 0;
65+
let errors = 0;
66+
await loadSchema('output/schema/schema.json');
67+
for await (const example of walkExamples('./specification/')) {
68+
try {
69+
await generateLanguages(example);
70+
}
71+
catch (err) {
72+
console.log(`${example}: ${err}`);
73+
errors += 1;
74+
}
75+
count += 1;
76+
}
77+
console.log(`${count} examples processed, ${errors} errors.`);
78+
}
79+
80+
main();

0 commit comments

Comments
 (0)