Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Moving functions out of scope to appease codeclimate #65

Merged
merged 1 commit into from
Oct 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions packages/api-explorer-ui/src/lib/parameters-to-json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,69 @@ const types = {
header: 'Headers',
};

module.exports = pathOperation => {
const hasRequestBody = !!pathOperation.requestBody;
const hasParameters = !!(pathOperation.parameters && pathOperation.parameters.length !== 0);
function getBodyParam(pathOperation) {
const schema = getSchema(pathOperation);

if (!hasParameters && !hasRequestBody) return null;
if (!schema) return null;

function getBodyParam() {
const schema = getSchema(pathOperation);
const type = schema.type === 'application/x-www-form-urlencoded' ? 'formData' : 'body';

if (!schema) return null;
return {
type,
label: types[type],
schema: schema.schema,
};
}

const type = schema.type === 'application/x-www-form-urlencoded' ? 'formData' : 'body';
function getOtherParams(pathOperation) {
return Object.keys(types).map(type => {
const required = [];
const parameters = (pathOperation.parameters || []).filter(param => param.in === type);
if (parameters.length === 0) return null;

return {
type,
label: types[type],
schema: schema.schema,
};
}
const properties = parameters.reduce((prev, current) => {
const schema = { type: 'string' };

function getOtherParams() {
return Object.keys(types).map(type => {
const required = [];
const parameters = (pathOperation.parameters || []).filter(param => param.in === type);
if (parameters.length === 0) return null;
if (current.description) schema.description = current.description;

const properties = parameters.reduce((prev, current) => {
const schema = { type: 'string' };
if (current.schema) {
if (current.schema.type === 'array') {
schema.type = 'array';
schema.items = current.schema.items;
}

if (current.description) schema.description = current.description;
if (current.schema.default) schema.default = current.schema.default;
if (current.schema.enum) schema.enum = current.schema.enum;
}

if (current.schema) {
if (current.schema.type === 'array') {
schema.type = 'array';
schema.items = current.schema.items;
}
prev[current.name] = schema;

if (current.schema.default) schema.default = current.schema.default;
if (current.schema.enum) schema.enum = current.schema.enum;
}
if (current.required) {
required.push(current.name);
}

prev[current.name] = schema;
return prev;
}, {});

if (current.required) {
required.push(current.name);
}
return {
type,
label: types[type],
schema: {
type: 'object',
properties,
required,
},
};
});
}

module.exports = pathOperation => {
const hasRequestBody = !!pathOperation.requestBody;
const hasParameters = !!(pathOperation.parameters && pathOperation.parameters.length !== 0);

if (!hasParameters && !hasRequestBody) return null;

return prev;
}, {});

return {
type,
label: types[type],
schema: {
type: 'object',
properties,
required,
},
};
});
}

return [getBodyParam()].concat(...getOtherParams()).filter(Boolean);
return [getBodyParam(pathOperation)].concat(...getOtherParams(pathOperation)).filter(Boolean);
};

// Exported for use in oas-to-har for default values object
Expand Down