Skip to content

Commit 2a49591

Browse files
authored
Merge pull request linode#66 from Jskobos/refactor-1
Refactor some things using getOr
2 parents 4497ff7 + 9e3e9b5 commit 2a49591

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
@@ -10,18 +10,15 @@ import CharDisplay from "./charDisplay";
1010

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

19+
const requiredProperties = schema.required || [];
20+
const properties = schema.properties;
21+
2522
const sortByRequired = (a, b) => {
2623
const r1 = requiredProperties.includes(a) ? 1 : 0;
2724
const r2 = requiredProperties.includes(b) ? 1 : 0;
@@ -50,13 +47,13 @@ export const BodySchema = props => {
5047
Object.keys(properties)
5148
.filter(
5249
v =>
53-
data.requestBody.content.application_json.schema.properties[v] !==
50+
properties[v] !==
5451
null
5552
)
5653
.sort(sortByRequired)
5754
.map((p, i) => {
5855
const b =
59-
data.requestBody.content.application_json.schema.properties[p];
56+
properties[p];
6057
return (
6158
b &&
6259
b.readOnly !== true && (
@@ -67,9 +64,8 @@ export const BodySchema = props => {
6764
<b className={b.deprecated && "line-through"}>{p}</b>
6865
</div>
6966
<div className="leading-xs">
70-
{data.requestBody.content.application_json.schema
71-
.required &&
72-
data.requestBody.content.application_json.schema.required.map(
67+
{requiredProperties &&
68+
requiredProperties.map(
7369
(req, i) => {
7470
if (p === req) {
7571
return (
@@ -115,18 +111,16 @@ export const BodySchema = props => {
115111
)
116112
);
117113
})}
118-
{data.requestBody.content.application_json &&
119-
data.requestBody.content.application_json.schema &&
120-
data.requestBody.content.application_json.schema.allOf &&
121-
Object.keys(data.requestBody.content.application_json.schema.allOf).map(
114+
{schema.allOf &&
115+
Object.keys(schema.allOf).map(
122116
a => {
123-
const s = data.requestBody.content.application_json.schema.allOf[a];
117+
const s = schema.allOf[a];
124118
return (
125119
s.properties &&
126120
Object.keys(s.properties)
127121
.filter(
128122
v =>
129-
data.requestBody.content.application_json.schema.allOf[
123+
schema.allOf[
130124
v
131125
] !== null
132126
)
@@ -144,8 +138,8 @@ export const BodySchema = props => {
144138
<b>{p}</b>
145139
</div>
146140
<div className="leading-xs">
147-
{s.required &&
148-
s.required.map((req, i) => {
141+
{s.requiredProperties &&
142+
s.requiredProperties.map((req, i) => {
149143
if (p === req) {
150144
return (
151145
<span
@@ -160,9 +154,8 @@ export const BodySchema = props => {
160154
})}
161155
</div>
162156
<div className="leading-xs">
163-
{data.requestBody.content.application_json.schema
164-
.required &&
165-
data.requestBody.content.application_json.schema.required.map(
157+
{requiredProperties &&
158+
requiredProperties.map(
166159
(req, i) => {
167160
if (p === req) {
168161
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)