Skip to content

Commit

Permalink
filters : working state
Browse files Browse the repository at this point in the history
  • Loading branch information
JF committed Jun 26, 2018
1 parent 8e90ea1 commit 3101b8a
Show file tree
Hide file tree
Showing 8 changed files with 405 additions and 201 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} name - The name of this field
*/
export default class Filter {
property: string;
filter: string;

/**
* @param {string} property
* @param {string} filter
*/
constructor(property: string, filter: string) {
this.property = property;
this.filter = filter;
}
}
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;
type: string;
required: boolean;
description: string;

/**
* @param {string} variable
* @param {string} type
* @param {boolean} required
* @param {string} description
*/
constructor(
variable: string,
type: string,
required: boolean,
description: string
) {
this.variable = variable;
this.type = type;
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
40 changes: 40 additions & 0 deletions src/hydra/addParameters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Parameter from "../Parameter";
import fetchResource from "./fetchResource";

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

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

const resourceParameters = [];

response.parameters.forEach(parameter => {
const property = parameter.property;

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

resourceParameters.push(
new Parameter(parameter.variable, "", parameter.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 3101b8a

Please sign in to comment.