Skip to content

Commit 773051c

Browse files
committed
Collecting all operations
Collecting entrypoint operations and inject them into the resource's operations as well
1 parent 24aa238 commit 773051c

File tree

2 files changed

+276
-216
lines changed

2 files changed

+276
-216
lines changed

src/hydra/parseHydraDocumentation.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
145145

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

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

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

165165
const property = get(properties, '["http://www.w3.org/ns/hydra/core#property"][0]');
166166
if (!property) {
@@ -197,21 +197,45 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
197197
}
198198
}
199199

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+
returns: range,
213+
types: entrypointOperation['@type'],
214+
},
215+
);
216+
217+
resourceOperations.push(operation);
218+
operations.push(operation);
219+
}
220+
}
221+
222+
// parse resource operations (a.k.a. item operations)
200223
for (const supportedOperation of relatedClass['http://www.w3.org/ns/hydra/core#supportedOperation']) {
201-
// @todo not sure what's the best attribute to consider the operation as valid
202224
if (!supportedOperation['http://www.w3.org/ns/hydra/core#returns']) {
203225
continue;
204226
}
205227

228+
const range = supportedOperation['http://www.w3.org/ns/hydra/core#returns'][0]['@id'];
206229
const operation = new Operation(
207230
supportedOperation['http://www.w3.org/2000/01/rdf-schema#label'][0]['@value'],
208231
{
209232
method: supportedOperation['http://www.w3.org/ns/hydra/core#method'][0]['@value'],
210-
returns: supportedOperation['http://www.w3.org/ns/hydra/core#returns'][0]['@id'],
233+
returns: range,
211234
types: supportedOperation['@type'],
212235
},
213236
);
214237

238+
resourceOperations.push(operation);
215239
operations.push(operation);
216240
}
217241

@@ -229,7 +253,7 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
229253
fields: resourceFields,
230254
readableFields,
231255
writableFields,
232-
operations
256+
operations: resourceOperations
233257
}
234258
));
235259
}

0 commit comments

Comments
 (0)