Skip to content

Commit c879e87

Browse files
JFdunglas
JF
authored andcommitted
Filters implementation
1 parent 41f9442 commit c879e87

File tree

7 files changed

+145
-15
lines changed

7 files changed

+145
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"eslint-plugin-import": "^2.2.0",
3030
"eslint-plugin-prettier": "^2.6.0",
3131
"flow-bin": "^0.42.0",
32-
"jest": "^20.0.0",
32+
"jest": "^23.0.0",
3333
"jest-fetch-mock": "^1.0.8",
3434
"prettier": "^1.12.1"
3535
},

src/Parameter.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @flow
2+
3+
/**
4+
* @property {string} variable - The variable of this field
5+
*/
6+
export default class Parameter {
7+
variable: string;
8+
range: string;
9+
required: boolean;
10+
description: string;
11+
12+
/**
13+
* @param {string} variable
14+
* @param {string} range
15+
* @param {boolean} required
16+
* @param {string} description
17+
*/
18+
constructor(
19+
variable: string,
20+
range: string,
21+
required: boolean,
22+
description: string
23+
) {
24+
this.variable = variable;
25+
this.range = range;
26+
this.required = required;
27+
this.description = description;
28+
}
29+
}

src/Resource.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import Field from "./Field";
44
import Operation from "./Operation";
5+
import Parameter from "./Parameter";
56

67
type ResourceOptions = {
78
id?: string,
89
title?: string,
910
deprecated?: boolean,
1011
readableFields?: Field[],
1112
writableFields?: Field[],
13+
parameters?: Parameter[],
1214
operations?: Operation[]
1315
};
1416

src/hydra/addParameters.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Parameter from "../Parameter";
2+
import fetchResource from "./fetchResource";
3+
4+
export default api => {
5+
const promises = [];
6+
7+
for (const resource of api.resources) {
8+
const promise = fetchResource(resource.url).then(({ parameters = {} }) => {
9+
const resourceParameters = [];
10+
parameters.forEach(({ property = null, required, variable }) => {
11+
if (null === property) {
12+
return;
13+
}
14+
15+
const { range = null } =
16+
resource.fields.find(({ name }) => property === name) || {};
17+
18+
resourceParameters.push(new Parameter(variable, range, required, ""));
19+
});
20+
21+
return resourceParameters;
22+
});
23+
24+
promises.push(promise);
25+
}
26+
27+
return Promise.all(promises).then(values => {
28+
api.resources.map((resource, index) => {
29+
resource.parameters = values[index];
30+
});
31+
32+
return api;
33+
});
34+
};

src/hydra/fetchResource.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fetchJsonLd from "./fetchJsonLd";
2+
import get from "lodash.get";
3+
4+
export default async resourceUrl => {
5+
return await fetchJsonLd(resourceUrl, { itemsPerPage: 0 }).then(
6+
d => ({
7+
parameters: get(d, "body.hydra:search.hydra:mapping")
8+
}),
9+
() => {
10+
throw new Error("Unreachable resource");
11+
}
12+
);
13+
};

src/hydra/parseHydraDocumentation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Field from "../Field";
55
import Resource from "../Resource";
66
import Operation from "../Operation";
77
import fetchJsonLd from "./fetchJsonLd";
8+
import addParameters from "./addParameters";
89

910
/**
1011
* Extracts the short name of a resource.
@@ -413,7 +414,8 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
413414
relatedClass,
414415
'["http://www.w3.org/2002/07/owl#deprecated"][0]["@value"]',
415416
false
416-
)
417+
),
418+
parameters: []
417419
})
418420
);
419421
}
@@ -438,5 +440,8 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
438440
response,
439441
status: get(response, "status")
440442
})
443+
)
444+
.then(({ api, response, status }) =>
445+
addParameters(api).then(api => ({ api, response, status }))
441446
);
442447
}

src/hydra/parseHydraDocumentation.test.js

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,25 @@ const docs = `{
552552
]
553553
}`;
554554

555+
const resourceCollection = `{
556+
"hydra:search": {
557+
"hydra:mapping": []
558+
}
559+
}`;
560+
561+
const resourceCollectionWithParameters = `{
562+
"hydra:search": {
563+
"hydra:mapping": [
564+
{
565+
"property": "isbn",
566+
"variable": "isbn",
567+
"range": "http://www.w3.org/2001/XMLSchema#string",
568+
"required": false
569+
}
570+
]
571+
}
572+
}`;
573+
555574
const book = {
556575
name: "books",
557576
url: "http://localhost/books",
@@ -743,7 +762,15 @@ const book = {
743762
deprecated: false
744763
}
745764
],
746-
deprecated: false
765+
deprecated: false,
766+
parameters: [
767+
{
768+
variable: "isbn",
769+
range: "http://www.w3.org/2001/XMLSchema#string",
770+
required: false,
771+
description: ""
772+
}
773+
]
747774
};
748775

749776
const review = {
@@ -886,7 +913,8 @@ const review = {
886913
deprecated: false
887914
}
888915
],
889-
deprecated: false
916+
deprecated: false,
917+
parameters: []
890918
};
891919

892920
const customResource = {
@@ -1012,7 +1040,8 @@ const customResource = {
10121040
deprecated: false
10131041
}
10141042
],
1015-
deprecated: false
1043+
deprecated: false,
1044+
parameters: []
10161045
};
10171046

10181047
const deprecatedResource = {
@@ -1072,13 +1101,16 @@ const deprecatedResource = {
10721101
deprecated: true
10731102
}
10741103
],
1075-
deprecated: true
1104+
deprecated: true,
1105+
parameters: []
10761106
};
10771107

1108+
const resources = [book, review, customResource, deprecatedResource];
1109+
10781110
const expectedApi = {
10791111
entrypoint: "http://localhost",
10801112
title: "API Platform's demo",
1081-
resources: [book, review, customResource, deprecatedResource]
1113+
resources: resources
10821114
};
10831115

10841116
const init = {
@@ -1091,30 +1123,45 @@ const init = {
10911123
})
10921124
};
10931125

1094-
test("parse a Hydra documentation", () => {
1095-
fetch.mockResponses([entrypoint, init], [docs, init]);
1126+
test("parse a Hydra documentation", async () => {
1127+
fetch.mockResponses(
1128+
[entrypoint, init],
1129+
[docs, init],
1130+
[resourceCollectionWithParameters, init],
1131+
[resourceCollection, init],
1132+
[resourceCollection, init],
1133+
[resourceCollection, init]
1134+
);
10961135

10971136
const options = { headers: new Headers({ CustomHeader: "customValue" }) };
10981137

1099-
return parseHydraDocumentation("http://localhost", options).then(data => {
1138+
await parseHydraDocumentation("http://localhost", options).then(data => {
11001139
expect(JSON.stringify(data.api, null, 2)).toBe(
11011140
JSON.stringify(expectedApi, null, 2)
11021141
);
11031142
expect(data.response).toBeDefined();
11041143
expect(data.status).toBe(200);
11051144

1106-
expect(fetch).toHaveBeenCalledTimes(2);
1107-
expect(fetch).toHaveBeenLastCalledWith(
1145+
expect(fetch).toHaveBeenCalledTimes(2 + resources.length);
1146+
expect(fetch).toHaveBeenNthCalledWith(
1147+
2,
11081148
"http://localhost/docs.jsonld",
11091149
options
11101150
);
11111151
});
11121152
});
11131153

1114-
test("parse a Hydra documentation (http://localhost/)", () => {
1115-
fetch.mockResponses([entrypoint, init], [docs, init]);
1154+
test("parse a Hydra documentation (http://localhost/)", async () => {
1155+
fetch.mockResponses(
1156+
[entrypoint, init],
1157+
[docs, init],
1158+
[resourceCollectionWithParameters, init],
1159+
[resourceCollection, init],
1160+
[resourceCollection, init],
1161+
[resourceCollection, init]
1162+
);
11161163

1117-
return parseHydraDocumentation("http://localhost/").then(data => {
1164+
await parseHydraDocumentation("http://localhost/").then(data => {
11181165
expect(JSON.stringify(data.api, null, 2)).toBe(
11191166
JSON.stringify(expectedApi, null, 2)
11201167
);

0 commit comments

Comments
 (0)