nuxt-openapi-docs-module
Version:
A module for Nuxt.js to generate pages from OpenAPI specifications
32 lines (31 loc) • 1.35 kB
JavaScript
import { traverse } from "../traverse.js";
export function sampleObject(schema, options = {}, spec, context) {
let res = {};
const depth = context && context.depth || 1;
if (schema && typeof schema.properties === "object") {
let requiredKeys = Array.isArray(schema.required) ? schema.required : [];
let requiredKeyDict = requiredKeys.reduce((dict, key) => {
dict[key] = true;
return dict;
}, {});
Object.keys(schema.properties).forEach((propertyName) => {
if (options.skipNonRequired && !requiredKeyDict.hasOwnProperty(propertyName)) {
return;
}
const sample = traverse(schema.properties[propertyName], options, spec, { propertyName, depth: depth + 1 });
if (options.skipReadOnly && sample.readOnly) {
return;
}
if (options.skipWriteOnly && sample.writeOnly) {
return;
}
res[propertyName] = sample.value;
});
}
if (schema && typeof schema.additionalProperties === "object") {
const propertyName = schema.additionalProperties["x-additionalPropertiesName"] || "property";
res[`${String(propertyName)}1`] = traverse(schema.additionalProperties, options, spec, { depth: depth + 1 }).value;
res[`${String(propertyName)}2`] = traverse(schema.additionalProperties, options, spec, { depth: depth + 1 }).value;
}
return res;
}