Skip to content

Commit 9e3e9b5

Browse files
committed
Refactor some things using getOr
1 parent 3b86f3b commit 9e3e9b5

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

src/components/2_molecules/BodySchema.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@ import CharDisplay from "./charDisplay";
99

1010
export const BodySchema = props => {
1111
const { data } = props;
12-
const requiredProperties =
13-
getOr(
14-
[],
15-
["requestBody", "content", "application_json", "schema", "required"],
16-
data
17-
) || [];
18-
const properties = getOr(
19-
null,
20-
["requestBody", "content", "application_json", "schema", "properties"],
12+
const schema = getOr(
13+
[],
14+
["requestBody", "content", "application_json", "schema"],
2115
data
2216
);
2317

18+
const requiredProperties = schema.required || [];
19+
const properties = schema.properties;
20+
2421
const sortByRequired = (a, b) => {
2522
const r1 = requiredProperties.includes(a) ? 1 : 0;
2623
const r2 = requiredProperties.includes(b) ? 1 : 0;
@@ -49,13 +46,13 @@ export const BodySchema = props => {
4946
Object.keys(properties)
5047
.filter(
5148
v =>
52-
data.requestBody.content.application_json.schema.properties[v] !==
49+
properties[v] !==
5350
null
5451
)
5552
.sort(sortByRequired)
5653
.map((p, i) => {
5754
const b =
58-
data.requestBody.content.application_json.schema.properties[p];
55+
properties[p];
5956
return (
6057
b &&
6158
b.readOnly !== true && (
@@ -66,9 +63,8 @@ export const BodySchema = props => {
6663
<b className={b.deprecated && "line-through"}>{p}</b>
6764
</div>
6865
<div className="leading-xs">
69-
{data.requestBody.content.application_json.schema
70-
.required &&
71-
data.requestBody.content.application_json.schema.required.map(
66+
{requiredProperties &&
67+
requiredProperties.map(
7268
(req, i) => {
7369
if (p === req) {
7470
return (
@@ -113,18 +109,16 @@ export const BodySchema = props => {
113109
)
114110
);
115111
})}
116-
{data.requestBody.content.application_json &&
117-
data.requestBody.content.application_json.schema &&
118-
data.requestBody.content.application_json.schema.allOf &&
119-
Object.keys(data.requestBody.content.application_json.schema.allOf).map(
112+
{schema.allOf &&
113+
Object.keys(schema.allOf).map(
120114
a => {
121-
const s = data.requestBody.content.application_json.schema.allOf[a];
115+
const s = schema.allOf[a];
122116
return (
123117
s.properties &&
124118
Object.keys(s.properties)
125119
.filter(
126120
v =>
127-
data.requestBody.content.application_json.schema.allOf[
121+
schema.allOf[
128122
v
129123
] !== null
130124
)
@@ -142,8 +136,8 @@ export const BodySchema = props => {
142136
<b>{p}</b>
143137
</div>
144138
<div className="leading-xs">
145-
{s.required &&
146-
s.required.map((req, i) => {
139+
{s.requiredProperties &&
140+
s.requiredProperties.map((req, i) => {
147141
if (p === req) {
148142
return (
149143
<span
@@ -158,9 +152,8 @@ export const BodySchema = props => {
158152
})}
159153
</div>
160154
<div className="leading-xs">
161-
{data.requestBody.content.application_json.schema
162-
.required &&
163-
data.requestBody.content.application_json.schema.required.map(
155+
{requiredProperties &&
156+
requiredProperties.map(
164157
(req, i) => {
165158
if (p === req) {
166159
return (

src/components/2_molecules/ResponseSampleBody.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1+
import { getOr } from "lodash/fp";
12
import React from "react";
23
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
34
import { atomDark } from "react-syntax-highlighter/dist/esm/styles/prism";
45

56
export const ResponseSampleBody = props => {
67
const { context, response } = props;
8+
const properties = getOr([], ['content', 'application_json', 'schema', 'properties'], context);
79

810
const sampleSource = `
911
{
10-
${context.content &&
11-
context.content.application_json &&
12-
context.content.application_json.schema &&
13-
context.content.application_json.schema.properties &&
14-
Object.keys(context.content.application_json.schema.properties)
12+
${properties &&
13+
Object.keys(properties)
1514
.filter(
16-
v => context.content.application_json.schema.properties[v] !== null
15+
v => properties[v] !== null
1716
)
1817
.map(p => {
19-
const l = context.content.application_json.schema.properties[p];
18+
const l = properties[p];
2019
return (
2120
l &&
2221
(l.type !== "array" && l.type !== "object" && p !== "errors"

0 commit comments

Comments
 (0)