Skip to content

Commit

Permalink
Filters implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JF committed Jun 27, 2018
1 parent 8e90ea1 commit 2a0bf2a
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 15 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");
}
);
};
7 changes: 6 additions & 1 deletion src/hydra/parseHydraDocumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Field from "../Field";
import Resource from "../Resource";
import Operation from "../Operation";
import fetchJsonLd from "./fetchJsonLd";
import addParameters from "./addParameters";

/**
* Extracts the short name of a resource.
Expand Down Expand Up @@ -413,7 +414,8 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
relatedClass,
'["http://www.w3.org/2002/07/owl#deprecated"][0]["@value"]',
false
)
),
parameters: []
})
);
}
Expand All @@ -438,5 +440,8 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
response,
status: get(response, "status")
})
)
.then(({ api, response, status }) =>
addParameters(api).then(api => ({ api, response, status }))
);
}
73 changes: 60 additions & 13 deletions src/hydra/parseHydraDocumentation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,25 @@ const docs = `{
]
}`;

const resourceCollection = `{
"hydra:search": {
"hydra:mapping": []
}
}`;

const resourceCollectionWithParameters = `{
"hydra:search": {
"hydra:mapping": [
{
"property": "isbn",
"variable": "isbn",
"range": "http://www.w3.org/2001/XMLSchema#string",
"required": false
}
]
}
}`;

const book = {
name: "books",
url: "http://localhost/books",
Expand Down Expand Up @@ -743,7 +762,15 @@ const book = {
deprecated: false
}
],
deprecated: false
deprecated: false,
parameters: [
{
variable: "isbn",
range: "http://www.w3.org/2001/XMLSchema#string",
required: false,
description: ""
}
]
};

const review = {
Expand Down Expand Up @@ -886,7 +913,8 @@ const review = {
deprecated: false
}
],
deprecated: false
deprecated: false,
parameters: []
};

const customResource = {
Expand Down Expand Up @@ -1012,7 +1040,8 @@ const customResource = {
deprecated: false
}
],
deprecated: false
deprecated: false,
parameters: []
};

const deprecatedResource = {
Expand Down Expand Up @@ -1072,13 +1101,16 @@ const deprecatedResource = {
deprecated: true
}
],
deprecated: true
deprecated: true,
parameters: []
};

const resources = [book, review, customResource, deprecatedResource];

const expectedApi = {
entrypoint: "http://localhost",
title: "API Platform's demo",
resources: [book, review, customResource, deprecatedResource]
resources: resources
};

const init = {
Expand All @@ -1091,30 +1123,45 @@ const init = {
})
};

test("parse a Hydra documentation", () => {
fetch.mockResponses([entrypoint, init], [docs, init]);
test("parse a Hydra documentation", async () => {
fetch.mockResponses(
[entrypoint, init],
[docs, init],
[resourceCollectionWithParameters, init],
[resourceCollection, init],
[resourceCollection, init],
[resourceCollection, init]
);

const options = { headers: new Headers({ CustomHeader: "customValue" }) };

return parseHydraDocumentation("http://localhost", options).then(data => {
await parseHydraDocumentation("http://localhost", options).then(data => {
expect(JSON.stringify(data.api, null, 2)).toBe(
JSON.stringify(expectedApi, null, 2)
);
expect(data.response).toBeDefined();
expect(data.status).toBe(200);

expect(fetch).toHaveBeenCalledTimes(2);
expect(fetch).toHaveBeenLastCalledWith(
expect(fetch).toHaveBeenCalledTimes(2 + resources.length);
expect(fetch).toHaveBeenNthCalledWith(
2,
"http://localhost/docs.jsonld",
options
);
});
});

test("parse a Hydra documentation (http://localhost/)", () => {
fetch.mockResponses([entrypoint, init], [docs, init]);
test("parse a Hydra documentation (http://localhost/)", async () => {
fetch.mockResponses(
[entrypoint, init],
[docs, init],
[resourceCollectionWithParameters, init],
[resourceCollection, init],
[resourceCollection, init],
[resourceCollection, init]
);

return parseHydraDocumentation("http://localhost/").then(data => {
await parseHydraDocumentation("http://localhost/").then(data => {
expect(JSON.stringify(data.api, null, 2)).toBe(
JSON.stringify(expectedApi, null, 2)
);
Expand Down

0 comments on commit 2a0bf2a

Please sign in to comment.