@@ -3,6 +3,7 @@ import get from 'lodash.get';
3
3
import Api from '../Api'
4
4
import Field from '../Field'
5
5
import Resource from '../Resource'
6
+ import Operation from '../Operation'
6
7
import fetchJsonLd from './fetchJsonLd' ;
7
8
8
9
/**
@@ -144,7 +145,7 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
144
145
145
146
return fetchEntrypointAndDocs ( entrypointUrl , options ) . then (
146
147
( { entrypoint, docs, response } ) => {
147
- const resources = [ ] , fields = [ ] ;
148
+ const resources = [ ] , fields = [ ] , operations = [ ] ;
148
149
const title = get ( docs , '[0]["http://www.w3.org/ns/hydra/core#title"][0]["@value"]' , 'API Platform' ) ;
149
150
150
151
const entrypointType = get ( entrypoint , '[0]["@type"][0]' ) ;
@@ -159,7 +160,7 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
159
160
160
161
// Add resources
161
162
for ( const properties of entrypointClass [ 'http://www.w3.org/ns/hydra/core#supportedProperty' ] ) {
162
- const readableFields = [ ] , resourceFields = [ ] , writableFields = [ ] ;
163
+ const readableFields = [ ] , resourceFields = [ ] , writableFields = [ ] , resourceOperations = [ ] ;
163
164
164
165
const property = get ( properties , '["http://www.w3.org/ns/hydra/core#property"][0]' ) ;
165
166
if ( ! property ) {
@@ -196,6 +197,50 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
196
197
}
197
198
}
198
199
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
+
199
244
const url = get ( entrypoint , `[0]["${ property [ '@id' ] } "][0]["@id"]` ) ;
200
245
if ( ! url ) {
201
246
throw new Error ( `Unable to find the URL for "${ property [ '@id' ] } ".` ) ;
@@ -209,7 +254,8 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
209
254
title : get ( relatedClass , '["http://www.w3.org/ns/hydra/core#title"][0]["@value"]' , '' ) ,
210
255
fields : resourceFields ,
211
256
readableFields,
212
- writableFields
257
+ writableFields,
258
+ operations : resourceOperations
213
259
}
214
260
) ) ;
215
261
}
0 commit comments