Skip to content

Commit a0a2c37

Browse files
authored
Merge pull request #28 from olivermack/supported-operations
Parse supportedOperations into Resource
2 parents 3d5d9dc + 67b9676 commit a0a2c37

File tree

4 files changed

+351
-177
lines changed

4 files changed

+351
-177
lines changed

src/Operation.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @flow
2+
3+
type OperationOptions = {
4+
method?: string,
5+
returns?: string,
6+
types?: Array<string>,
7+
}
8+
9+
/**
10+
* @property {string} name - The name of this operation
11+
*/
12+
export default class Operation {
13+
name: string;
14+
15+
/**
16+
* @param {string} name
17+
* @param {?OperationOptions} options
18+
*/
19+
constructor(name: string, options: OperationOptions = {}) {
20+
this.name = name;
21+
22+
Object.keys(options).forEach((key) => {
23+
Object.defineProperty(this, key, {
24+
readable: true,
25+
writable: true,
26+
enumerable: true,
27+
value: options[key],
28+
});
29+
});
30+
}
31+
}

src/Resource.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// @flow
22

33
import Field from './Field';
4+
import Operation from './Operation';
45

56
type ResourceOptions = {
67
id?: string,
78
title?: string,
89
readableFields?: Field[],
910
writableFields?: Field[],
11+
operations?: Operation[],
1012
};
1113

1214
/**

src/hydra/parseHydraDocumentation.js

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import get from 'lodash.get';
33
import Api from '../Api'
44
import Field from '../Field'
55
import Resource from '../Resource'
6+
import Operation from '../Operation'
67
import fetchJsonLd from './fetchJsonLd';
78

89
/**
@@ -144,7 +145,7 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
144145

145146
return fetchEntrypointAndDocs(entrypointUrl, options).then(
146147
({ entrypoint, docs, response }) => {
147-
const resources = [], fields = [];
148+
const resources = [], fields = [], operations = [];
148149
const title = get(docs, '[0]["http://www.w3.org/ns/hydra/core#title"][0]["@value"]', 'API Platform');
149150

150151
const entrypointType = get(entrypoint, '[0]["@type"][0]');
@@ -159,7 +160,7 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
159160

160161
// Add resources
161162
for (const properties of entrypointClass['http://www.w3.org/ns/hydra/core#supportedProperty']) {
162-
const readableFields = [], resourceFields = [], writableFields = [];
163+
const readableFields = [], resourceFields = [], writableFields = [], resourceOperations = [];
163164

164165
const property = get(properties, '["http://www.w3.org/ns/hydra/core#property"][0]');
165166
if (!property) {
@@ -196,6 +197,50 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
196197
}
197198
}
198199

200+
// parse entrypoint's operations (a.k.a. collection operations)
201+
if (property['http://www.w3.org/ns/hydra/core#supportedOperation']) {
202+
for (const entrypointOperation of property['http://www.w3.org/ns/hydra/core#supportedOperation']) {
203+
if (!entrypointOperation['http://www.w3.org/ns/hydra/core#returns']) {
204+
continue;
205+
}
206+
207+
const range = entrypointOperation['http://www.w3.org/ns/hydra/core#returns'][0]['@id'];
208+
const operation = new Operation(
209+
entrypointOperation['http://www.w3.org/2000/01/rdf-schema#label'][0]['@value'],
210+
{
211+
method: entrypointOperation['http://www.w3.org/ns/hydra/core#method'][0]['@value'],
212+
expects: entrypointOperation['http://www.w3.org/ns/hydra/core#expects'] && entrypointOperation['http://www.w3.org/ns/hydra/core#expects'][0]['@id'],
213+
returns: range,
214+
types: entrypointOperation['@type'],
215+
},
216+
);
217+
218+
resourceOperations.push(operation);
219+
operations.push(operation);
220+
}
221+
}
222+
223+
// parse resource operations (a.k.a. item operations)
224+
for (const supportedOperation of relatedClass['http://www.w3.org/ns/hydra/core#supportedOperation']) {
225+
if (!supportedOperation['http://www.w3.org/ns/hydra/core#returns']) {
226+
continue;
227+
}
228+
229+
const range = supportedOperation['http://www.w3.org/ns/hydra/core#returns'][0]['@id'];
230+
const operation = new Operation(
231+
supportedOperation['http://www.w3.org/2000/01/rdf-schema#label'][0]['@value'],
232+
{
233+
method: supportedOperation['http://www.w3.org/ns/hydra/core#method'][0]['@value'],
234+
expects: supportedOperation['http://www.w3.org/ns/hydra/core#expects'] && supportedOperation['http://www.w3.org/ns/hydra/core#expects'][0]['@id'],
235+
returns: range,
236+
types: supportedOperation['@type'],
237+
},
238+
);
239+
240+
resourceOperations.push(operation);
241+
operations.push(operation);
242+
}
243+
199244
const url = get(entrypoint, `[0]["${property['@id']}"][0]["@id"]`);
200245
if (!url) {
201246
throw new Error(`Unable to find the URL for "${property['@id']}".`);
@@ -209,7 +254,8 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
209254
title: get(relatedClass, '["http://www.w3.org/ns/hydra/core#title"][0]["@value"]', ''),
210255
fields: resourceFields,
211256
readableFields,
212-
writableFields
257+
writableFields,
258+
operations: resourceOperations
213259
}
214260
));
215261
}

0 commit comments

Comments
 (0)