Skip to content

Commit 9df485a

Browse files
author
JF
committed
Filters implementation
1 parent 0c999c9 commit 9df485a

File tree

7 files changed

+388
-229
lines changed

7 files changed

+388
-229
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/Filter.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @flow
2+
3+
/**
4+
* @property {string} variable - The variable of this field
5+
*/
6+
export default class Filter {
7+
property: string;
8+
variable: string;
9+
10+
/**
11+
* @param {string} property
12+
* @param {string} variable
13+
*/
14+
constructor(property: string, variable: string) {
15+
this.property = property;
16+
this.variable = variable;
17+
}
18+
}

src/Resource.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

33
import Field from "./Field";
4+
import Filter from "./Filter";
45
import Operation from "./Operation";
56

67
type ResourceOptions = {
@@ -9,6 +10,7 @@ type ResourceOptions = {
910
deprecated?: boolean,
1011
readableFields?: Field[],
1112
writableFields?: Field[],
13+
filters?: Filter[],
1214
operations?: Operation[]
1315
};
1416

src/hydra/addFilters.js

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

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 entrypointUrl => {
5+
return await fetchJsonLd(entrypointUrl, { itemsPerPage: 0 }).then(
6+
d => ({
7+
filters: get(d, "body.hydra:search.hydra:mapping")
8+
}),
9+
() => {
10+
throw new Error("Unreachable resource");
11+
}
12+
);
13+
};

0 commit comments

Comments
 (0)