Skip to content

Filters implementation #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
);
};
Loading