mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
js-api: Refactor ExamplesHandler to avoid running examples in a loop.
This refactors `ExamplesHandler` to avoid running examples in a loop and add result objects to `response_data` array one by one with `generate_validation_data`.
This commit is contained in:
committed by
Tim Abbott
parent
31d04fb370
commit
302906211d
@@ -4,7 +4,8 @@ const ExamplesHandler = function () {
|
||||
apiKey: process.env.ZULIP_API_KEY,
|
||||
realm: process.env.ZULIP_REALM,
|
||||
};
|
||||
const examples = [];
|
||||
const examples = {};
|
||||
const response_data = [];
|
||||
|
||||
const make_result_object = (example, result, count = false) => {
|
||||
const name = count !== false ? `${example.name}_${count}` : example.name;
|
||||
@@ -17,69 +18,38 @@ const ExamplesHandler = function () {
|
||||
};
|
||||
};
|
||||
|
||||
const generate_validation_data = async (client) => {
|
||||
const response_data = [];
|
||||
for (const example of examples) {
|
||||
const result = await example.func(client);
|
||||
if (Array.isArray(result)) {
|
||||
// Handle special cases where some examples make
|
||||
// more than 1 API requests.
|
||||
result.forEach((r, index) => {
|
||||
response_data.push(make_result_object(example, r, index));
|
||||
});
|
||||
} else {
|
||||
response_data.push(make_result_object(example, result));
|
||||
}
|
||||
const generate_validation_data = async (client, example) => {
|
||||
const result = await example.func(client);
|
||||
if (Array.isArray(result)) {
|
||||
// Handle special cases where some examples make
|
||||
// more than 1 API requests.
|
||||
result.forEach((r, index) => {
|
||||
response_data.push(make_result_object(example, r, index));
|
||||
});
|
||||
} else {
|
||||
response_data.push(make_result_object(example, result));
|
||||
}
|
||||
return response_data;
|
||||
};
|
||||
|
||||
const format_example_code = (example) => {
|
||||
let body = example.func.toString();
|
||||
body = body.slice(body.indexOf("{") + 2, body.lastIndexOf("}") - 1);
|
||||
body = body.replace(/\n\s*\n\s*\n/g, '\n\n');
|
||||
const wrapper = [
|
||||
"Zulip(config).then(async (client) => {",
|
||||
`${body}`,
|
||||
"}).then(console.log).catch(console.err);",
|
||||
].join('\n');
|
||||
return wrapper;
|
||||
};
|
||||
|
||||
const main = async () => {
|
||||
const command = process.argv[2];
|
||||
if (command === 'generate-responses') {
|
||||
const Zulip = require('zulip-js');
|
||||
const client = await Zulip(config);
|
||||
const response_data = await generate_validation_data(client);
|
||||
console.log(JSON.stringify(response_data));
|
||||
return;
|
||||
}
|
||||
if (command === 'generate-example') {
|
||||
const endpoint = process.argv[3];
|
||||
if (!endpoint) {
|
||||
console.error("js-examples: Please specify an endpoint.");
|
||||
process.exit(1);
|
||||
}
|
||||
const example = examples.find(x => x.endpoint.toString() === endpoint);
|
||||
if (!example) {
|
||||
console.error(`js-examples: Endpoint ${endpoint} not found.`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(format_example_code(example));
|
||||
return;
|
||||
}
|
||||
console.error("js-examples: Invalid command.");
|
||||
process.exit(1);
|
||||
const Zulip = require('zulip-js');
|
||||
const client = await Zulip(config);
|
||||
|
||||
await generate_validation_data(client, examples.send_message);
|
||||
await generate_validation_data(client, examples.create_user);
|
||||
|
||||
console.log(JSON.stringify(response_data));
|
||||
return;
|
||||
};
|
||||
|
||||
const add_example = (name, endpoint, status_code, func) => {
|
||||
examples.push({
|
||||
const example = {
|
||||
name,
|
||||
endpoint,
|
||||
status_code,
|
||||
func,
|
||||
});
|
||||
};
|
||||
examples[name] = example;
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user