rests
Version:
Easily generate API client's SDK — organize and simplify API Requests.
185 lines (184 loc) • 9.61 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const Rests = require('../index.js');
const { capitalize, mergeOptions, get, isInitializable } = require("./helpers");
/**
* Generate a simple Markdown documentation
*/
function generateDocs(schema, rests, options, docsOptions) {
return __awaiter(this, void 0, void 0, function* () {
const config = Object.assign({ request: true, responses: false, responsesSleep: 0, showOptional: true, commentOptional: true, apiName: "api", headStartLevel: 1, output: "./API.md" }, docsOptions);
let global_options = {
base: "",
headers: {},
params: {},
on_error: void 0,
on_success: void 0,
on_request: void 0,
fetch_agent: null,
};
mergeOptions(global_options, options, true);
const markdown = {
reference: "",
body: ""
};
const api = rests || Rests(schema, options);
const getParams = function (params, default_values, initialize = false) {
var _a, _b, _c, _d;
default_values = default_values || {};
var code = "";
for (var param_name in params) {
var param = params[param_name];
var value = (_d = (_c = (_b = (_a = param.example) !== null && _a !== void 0 ? _a : param.default) !== null && _b !== void 0 ? _b : default_values[param_name]) !== null && _c !== void 0 ? _c : param.type) !== null && _d !== void 0 ? _d : "<any>";
if (!param.required && !config.showOptional) {
continue;
}
if (default_values[param_name] && initialize) {
continue;
}
var commentOptional = config.commentOptional && !param.required ? "//" : "";
var helpers = [
param.required ? "required" : "optional",
param.in && Array.isArray(param.in) ? "Allowed: " + param.in.join(", ") : null,
param.min ? "Min: " + param.min : null,
param.mix ? "Max: " + param.max : null,
param.validate ? "Validate: " + (typeof param.validate === "string" ? param.validate : param.validate.toString()) : null
].filter(h => h).join(" | ");
value = typeof value === "string" ? '"' + value + '"' : value;
code += ` ${commentOptional}${param_name}: ${value}, //${helpers}\n`;
}
return code;
};
const getInitialize = function (name, call_name, item_options, options) {
var params = item_options.params || {};
var default_values = Object.assign({}, item_options.values, options.values);
var code = "```javascript\n";
code += `const ${name} = new ${call_name}({\n`;
code += getParams(params, default_values, true);
code += "})\n";
code += "```\r\n\r\n";
return code;
};
const getCall = function (call_name, params, options) {
params = params || {};
var code = "```javascript\n";
code += `${call_name}({\n`;
code += getParams(params, options.values);
code += "})\n";
code += "```\r\n\r\n";
return code;
};
const getRequest = function (item) {
var code = "<details>\n<summary>Request</summary>\r\n\r\n";
var method = (item["method"] || "get").toUpperCase();
var default_location = method === "GET" ? "query" : "body";
var params = item["params"] || {};
code += `**${method}** ${item["path"]}\n`;
if (Object.keys(params).length) {
code += "|Parameter|Location|Required|Description|\n|--|--|--|--|\n";
var parameters = Object.keys(params).map(function (name) {
return [
params[name].name || name,
params[name].location || default_location,
params[name].required ? true : false,
params[name].help
].join("|");
}).join("\n");
code += parameters + "\n";
}
code += "</details>\r\n\r\n";
return code;
};
const getResponse = function (item, current_call_name) {
return __awaiter(this, void 0, void 0, function* () {
var code = "<details>\n<summary>Response</summary>\r\n\r\n";
var body = item.example_response;
if (!body) {
if (!config.responses) {
return "";
}
var send = get(api, current_call_name);
try {
console.log(`[*] Generating response for ${item['path']}`);
//Sleep Between Requests when generating responses to avoid hitting rate limits
yield new Promise(function (res, rej) {
setTimeout(function () {
res(true);
}, config.responseSleep);
});
var res = yield send({
$sandbox: true
});
body = res.json;
}
catch (err) {
console.log(err);
return "";
}
}
code += "```json\r\n";
code += typeof body === "string" ? body : JSON.stringify(body, null, "\t");
code += "\r\n```\r\n";
code += "</details>\r\n\r\n";
return code;
});
};
const generate = function (categories, options, selector, call_name, level, true_level = 0) {
return __awaiter(this, void 0, void 0, function* () {
level += 1;
var head_level = Math.min(level, 6);
for (var category in categories) {
if (!categories[category] || typeof categories[category] !== 'object') {
continue;
}
if (category.startsWith('$')) {
continue;
}
var current_call_name = `${call_name}.${category}`;
var current_selector = `${selector}-${category}`;
var current_options = options;
var items = categories[category];
var isEndpoint = (items["path"] && typeof items["path"] === "string");
var head = `<h${head_level} id="${current_selector}">${capitalize(category)}</h${head_level}>\r\n\r\n`, link = " ".repeat(true_level) + `- [${category}](#${current_selector})\r\n`, help_string = (items['$help'] || items['help']), help = typeof help_string === "string" ? help_string + "\r\n" : "", initialize = "", endpoint = "", request = "", response = "";
if (items["$options"]) {
if (isInitializable(items)) {
initialize = getInitialize(category, current_call_name, items["$options"], options);
current_call_name = category;
}
current_options = mergeOptions(options, items["$options"]);
}
if (isEndpoint) {
endpoint = getCall(current_call_name, items["params"], current_options);
if (config.request) {
request = getRequest(items);
}
response = yield getResponse(items, current_call_name);
}
markdown.reference += link;
markdown.body += head + help + initialize + endpoint + request + response;
if (!isEndpoint) {
yield generate(items, current_options, current_selector, current_call_name, level, true_level + 1);
}
}
});
};
yield generate(schema, global_options, config.apiName, config.apiName, config.headStartLevel);
if (fs && fs.writeFileSync && config.output) {
fs.writeFileSync(config.output, markdown.reference + "\r\n\r\n" + markdown.body, {
encoding: "utf8"
});
}
return markdown;
});
}
module.exports = generateDocs;
;