Skip to content

Commit 5fffb1a

Browse files
committed
WIP - parsing resources and fields
1 parent 62d21cc commit 5fffb1a

File tree

3 files changed

+40
-128
lines changed

3 files changed

+40
-128
lines changed

src/swagger/handleJson.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
1+
import { get } from 'lodash'
2+
import Field from '../Field'
3+
import Resource from '../Resource'
4+
15
String.prototype.capitalize = function() {
26
return this.charAt(0).toUpperCase() + this.slice(1);
3-
}
7+
}
48

5-
function removeTrailingSlash(url) {
9+
const removeTrailingSlash = url => {
610
if (/\/$/.test(url)) {
711
url = url.slice(0, -1)
812
}
9-
1013
return url
1114
}
1215

13-
function onlyUnique(value, index, self) {
14-
return self.indexOf(value) === index;
15-
}
16+
const onlyUnique = (value, index, self) => self.indexOf(value) === index;
1617

1718
export default function(response, entrypointUrl) {
18-
const paths = Object.keys(response.paths).map(item => item.replace(`/{id}`, ``)).filter(onlyUnique);
19+
const paths = Object.keys(response.paths).map(item => item.replace(`/{id}`, ``)).filter(onlyUnique);
1920

20-
let resources = paths.map(item => {
21+
const resources = paths.map(item => {
2122

22-
let firstMethod = Object.keys(response.paths[item])[0];
23-
24-
return {
25-
name: item.replace(`/`, ``),
26-
url: removeTrailingSlash(entrypointUrl) + item,
27-
id: `http://schema.org/` + item.replace(`/`, ``).capitalize(),
28-
title: response.paths[item][firstMethod]['tags'][0]
29-
}
30-
});
23+
const name = item.replace(`/`, ``);
24+
const url = removeTrailingSlash(entrypointUrl) + item;
25+
const firstMethod = Object.keys(response.paths[item])[0];
26+
const title = response.paths[item][firstMethod]['tags'][0];
27+
const fieldNames = Object.keys(response.definitions[title].properties);
28+
const fields = fieldNames.map(fieldName => new Field(fieldName,
29+
{
30+
id: `http://schema.org/${fieldName}`,
31+
range: `http://www.w3.org/2001/XMLSchema#${response.definitions[title].properties[fieldName].type}`,
32+
reference: null,
33+
required: !!response.definitions[title].required[fieldName],
34+
description: get(response.definitions[title].properties[fieldName], `description`, ``),
35+
},
36+
));
37+
38+
return new Resource(
39+
name,
40+
url,
41+
{
42+
id: `http://schema.org/` + item.replace(`/`, ``).capitalize(),
43+
title,
44+
fields,
45+
readableFields: fields,
46+
writableFields: fields,
47+
}
48+
)
49+
})
3150

3251
return resources
3352
}
34-

src/swagger/handleJson.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,8 @@ const parsed = [
11721172

11731173

11741174
test(`Parse Swagger Documentation from Json`, () => {
1175-
//expect(handleJson(swaggerApiDefinition, 'http://demo.api-platform.com')).toEqual(expect.arrayContaining(parsed));
1175+
expect(handleJson(swaggerApiDefinition, 'http://demo.api-platform.com')).toEqual(expect.arrayContaining(parsed));
11761176
})
1177+
//console.log(JSON.stringify(handleJson(swaggerApiDefinition, 'http://demo.api-platform.com')))
1178+
11771179

1178-
console.log(handleJson(swaggerApiDefinition, 'http://demo.api-platform.com'))

src/swagger/parseSwaggerDocumentation.js

Lines changed: 1 addition & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,6 @@
11
import Api from '../Api'
2-
import Field from '../Field'
3-
import Resource from '../Resource'
42
import handleJson from './handleJson'
53

6-
/**
7-
* Extracts the short name of a resource.
8-
*
9-
* @param {string} url
10-
* @param {string} entrypointUrl
11-
* @return {string}
12-
*/
13-
function guessNameFromUrl(url, entrypointUrl) {
14-
return url.substr(entrypointUrl.length + 1);
15-
}
16-
17-
/**
18-
* Finds the description of the class with the given id.
19-
*
20-
* @param {object[]} docs
21-
* @param {string} supportedClass
22-
* @return {object}
23-
*/
24-
function findSupportedClass(docs, supportedClass) {
25-
const supportedClasses = docs[0]['http://www.w3.org/ns/hydra/core#supportedClass'];
26-
27-
for (let i = 0; i < supportedClasses.length; i++) {
28-
if (supportedClasses[i]['@id'] === supportedClass) {
29-
return supportedClasses[i];
30-
}
31-
}
32-
33-
throw new Error(`The class ${supportedClass} doesn't exist.`);
34-
}
35-
364
function removeTrailingSlash(url) {
375
if (/\/$/.test(url)) {
386
url = url.slice(0, -1)
@@ -54,82 +22,7 @@ export default function parseSwaggerDocumentation(entrypointUrl) {
5422

5523
const title = response.info.title;
5624
const resources = handleJson(response, entrypointUrl);
57-
58-
// // Add resources
59-
// for (let properties of entrypointSupportedClass['http://www.w3.org/ns/hydra/core#supportedProperty']) {
60-
// const property = properties['http://www.w3.org/ns/hydra/core#property'][0];
61-
// const entrypointSupportedOperations = property['http://www.w3.org/ns/hydra/core#supportedOperation'];
62-
63-
// let readableFields = [];
64-
// let resourceFields = [];
65-
// let writableFields = [];
66-
67-
// // Add fields
68-
// for (let j = 0; j < entrypointSupportedOperations.length; j++) {
69-
// let className = entrypointSupportedOperations[j]['http://www.w3.org/ns/hydra/core#returns'];
70-
71-
// // Skip operations not having a return type
72-
// if (!className) {
73-
// continue;
74-
// }
75-
76-
// className = className[0]['@id'];
77-
78-
// if (0 === className.indexOf('http://www.w3.org/ns/hydra/core')) {
79-
// continue;
80-
// }
81-
82-
// const supportedClass = findSupportedClass(docs, className);
83-
// for (let supportedProperties of supportedClass['http://www.w3.org/ns/hydra/core#supportedProperty']) {
84-
// const supportedProperty = supportedProperties['http://www.w3.org/ns/hydra/core#property'][0];
85-
// const range = supportedProperty['http://www.w3.org/2000/01/rdf-schema#range'] ? supportedProperty['http://www.w3.org/2000/01/rdf-schema#range'][0]['@id'] : null;
86-
87-
// const field = new Field(
88-
// supportedProperty['http://www.w3.org/2000/01/rdf-schema#label'][0]['@value'],
89-
// {
90-
// id: supportedProperty['@id'],
91-
// range,
92-
// reference: 'http://www.w3.org/ns/hydra/core#Link' === property['@type'][0] ? range : null, // Will be updated in a subsequent pass
93-
// required: supportedProperties['http://www.w3.org/ns/hydra/core#required'] ? supportedProperties['http://www.w3.org/ns/hydra/core#required'][0]['@value'] : false,
94-
// description: supportedProperties['http://www.w3.org/ns/hydra/core#description'] ? supportedProperties['http://www.w3.org/ns/hydra/core#description'][0]['@value'] : ''
95-
// },
96-
// );
97-
98-
// fields.push(field);
99-
// resourceFields.push(field);
100-
101-
// if (supportedProperties['http://www.w3.org/ns/hydra/core#readable'][0]['@value']) {
102-
// readableFields.push(field);
103-
// }
104-
105-
// if (supportedProperties['http://www.w3.org/ns/hydra/core#writable'][0]['@value']) {
106-
// writableFields.push(field);
107-
// }
108-
// }
109-
110-
// resources.push(new Resource(
111-
// guessNameFromUrl(entrypoint[0][property['@id']][0]['@id'], entrypointUrl),
112-
// entrypoint[0][property['@id']][0]['@id'],
113-
// {
114-
// id: supportedClass['@id'],
115-
// title: supportedClass['http://www.w3.org/ns/hydra/core#title'][0]['@value'],
116-
// fields: resourceFields,
117-
// readableFields,
118-
// writableFields
119-
// }
120-
// ));
121-
122-
// break;
123-
// }
124-
// }
125-
126-
// // Resolve references
127-
// for (let field of fields) {
128-
// if (null !== field.reference) {
129-
// field.reference = resources.find(resource => resource.id === field.reference) || null;
130-
// }
131-
// }
132-
25+
13326
return Promise.resolve({ api: new Api(entrypointUrl, {title, resources}), response , status: response.status });
13427
},
13528
({ response }) => Promise.reject({ api: new Api(entrypointUrl, {resources: []}), response, status: response.status })

0 commit comments

Comments
 (0)