Skip to content

Commit 0c999c9

Browse files
arojuniordunglas
authored andcommitted
Swagger parser (#30)
* add support to parse Swagger format * eslint fixes * adjusts required by revision to swagger parser
1 parent 2181af8 commit 0c999c9

File tree

5 files changed

+1265
-0
lines changed

5 files changed

+1265
-0
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export Api from "./Api";
22
export Resource from "./Resource";
33
export Field from "./Field";
44
export * from "./hydra";
5+
export * from "./swagger";

src/swagger/handleJson.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { get, uniq } from "lodash";
2+
import Field from "../Field";
3+
import Resource from "../Resource";
4+
5+
export const removeTrailingSlash = url => {
6+
if (/\/$/.test(url)) {
7+
url = url.slice(0, -1);
8+
}
9+
return url;
10+
};
11+
12+
export default function(response, entrypointUrl) {
13+
const paths = uniq(
14+
Object.keys(response.paths).map(item => item.replace(`/{id}`, ``))
15+
);
16+
17+
const resources = paths.map(item => {
18+
const name = item.replace(`/`, ``);
19+
const url = removeTrailingSlash(entrypointUrl) + item;
20+
const firstMethod = Object.keys(response.paths[item])[0];
21+
const title = response.paths[item][firstMethod]["tags"][0];
22+
const fieldNames = Object.keys(response.definitions[title].properties);
23+
24+
const fields = fieldNames.map(
25+
fieldName =>
26+
new Field(fieldName, {
27+
id: null,
28+
range: null,
29+
reference: null,
30+
required: !!response.definitions[title].required.find(
31+
value => value === fieldName
32+
),
33+
description: get(
34+
response.definitions[title].properties[fieldName],
35+
`description`,
36+
``
37+
)
38+
})
39+
);
40+
41+
return new Resource(name, url, {
42+
id: null,
43+
title,
44+
fields,
45+
readableFields: fields,
46+
writableFields: fields
47+
});
48+
});
49+
50+
return resources;
51+
}

0 commit comments

Comments
 (0)