Skip to content

Commit

Permalink
Filters implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JF committed Jun 21, 2018
1 parent 0c999c9 commit 9df485a
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 229 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-prettier": "^2.6.0",
"flow-bin": "^0.42.0",
"jest": "^20.0.0",
"jest": "^23.0.0",
"jest-fetch-mock": "^1.0.8",
"prettier": "^1.12.1"
},
Expand Down
18 changes: 18 additions & 0 deletions src/Filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @flow

/**
* @property {string} variable - The variable of this field
*/
export default class Filter {
property: string;
variable: string;

/**
* @param {string} property
* @param {string} variable
*/
constructor(property: string, variable: string) {
this.property = property;
this.variable = variable;
}
}
2 changes: 2 additions & 0 deletions src/Resource.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import Field from "./Field";
import Filter from "./Filter";
import Operation from "./Operation";

type ResourceOptions = {
Expand All @@ -9,6 +10,7 @@ type ResourceOptions = {
deprecated?: boolean,
readableFields?: Field[],
writableFields?: Field[],
filters?: Filter[],
operations?: Operation[]
};

Expand Down
38 changes: 38 additions & 0 deletions src/hydra/addFilters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Filter from "../Filter";
import fetchResource from "./fetchResource";

export default api => {
const promises = [];

for (const resource of api.resources) {
let promise = fetchResource(resource.url).then(response => {
if (!response.filters) {
return [];
}

const resourceFilters = [];

for (const filter of response.filters) {
let property = filter.property;

if (property === null) {
continue;
}

resourceFilters.push(new Filter(property, filter.variable));
}

return resourceFilters;
});

promises.push(promise);
}

return Promise.all(promises).then(values => {
api.resources.map((resource, index) => {
resource.filters = values[index];
});

return api;
});
};
13 changes: 13 additions & 0 deletions src/hydra/fetchResource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fetchJsonLd from "./fetchJsonLd";
import get from "lodash.get";

export default async entrypointUrl => {
return await fetchJsonLd(entrypointUrl, { itemsPerPage: 0 }).then(
d => ({
filters: get(d, "body.hydra:search.hydra:mapping")
}),
() => {
throw new Error("Unreachable resource");
}
);
};

0 comments on commit 9df485a

Please sign in to comment.