@@ -3,6 +3,7 @@ import get from 'lodash.get';
33import Api from '../Api'
44import Field from '../Field'
55import Resource from '../Resource'
6+ import Operation from '../Operation'
67import 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