Skip to content

Commit

Permalink
Filters implementation (#33)
Browse files Browse the repository at this point in the history
* Filters implementation

* Fix CS
  • Loading branch information
jfthuillier authored and dunglas committed Oct 6, 2018
1 parent 41f9442 commit da3131b
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 202 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
29 changes: 29 additions & 0 deletions src/Parameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @flow

/**
* @property {string} variable - The variable of this field
*/
export default class Parameter {
variable: string;
range: string;
required: boolean;
description: string;

/**
* @param {string} variable
* @param {string} range
* @param {boolean} required
* @param {string} description
*/
constructor(
variable: string,
range: string,
required: boolean,
description: string
) {
this.variable = variable;
this.range = range;
this.required = required;
this.description = description;
}
}
2 changes: 2 additions & 0 deletions src/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

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

type ResourceOptions = {
id?: string,
title?: string,
deprecated?: boolean,
readableFields?: Field[],
writableFields?: Field[],
parameters?: Parameter[],
operations?: Operation[]
};

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

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

for (const resource of api.resources) {
const promise = fetchResource(resource.url).then(({ parameters = {} }) => {
const resourceParameters = [];
parameters.forEach(({ property = null, required, variable }) => {
if (null === property) {
return;
}

const { range = null } =
resource.fields.find(({ name }) => property === name) || {};

resourceParameters.push(new Parameter(variable, range, required, ""));
});

return resourceParameters;
});

promises.push(promise);
}

return Promise.all(promises).then(values => {
api.resources.map((resource, index) => {
resource.parameters = 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 resourceUrl => {
return await fetchJsonLd(resourceUrl, { itemsPerPage: 0 }).then(
d => ({
parameters: get(d, "body.hydra:search.hydra:mapping")
}),
() => {
throw new Error("Unreachable resource");
}
);
};

0 comments on commit da3131b

Please sign in to comment.