1
1
import jsonld from "jsonld" ;
2
- import get from "lodash.get" ;
3
2
import { Api } from "../Api.js" ;
4
3
import { Field } from "../Field.js" ;
5
4
import { Resource } from "../Resource.js" ;
@@ -45,10 +44,8 @@ function findSupportedClass(
45
44
docs : ExpandedDoc [ ] ,
46
45
classToFind : string ,
47
46
) : ExpandedClass {
48
- const supportedClasses = get (
49
- docs ,
50
- '[0]["http://www.w3.org/ns/hydra/core#supportedClass"]' ,
51
- ) as ExpandedClass [ ] | undefined ;
47
+ const supportedClasses =
48
+ docs ?. [ 0 ] ?. [ "http://www.w3.org/ns/hydra/core#supportedClass" ] ;
52
49
if ( ! Array . isArray ( supportedClasses ) ) {
53
50
throw new TypeError (
54
51
'The API documentation has no "http://www.w3.org/ns/hydra/core#supportedClass" key or its value is not an array.' ,
@@ -155,7 +152,7 @@ async function fetchEntrypointAndDocs(
155
152
api : new Api ( entrypointUrl , { resources : [ ] } ) ,
156
153
error,
157
154
response,
158
- status : get ( response , " status" ) ,
155
+ status : response ?. status ,
159
156
} ;
160
157
}
161
158
}
@@ -173,25 +170,29 @@ function findRelatedClass(
173
170
property : ExpandedRdfProperty ,
174
171
) : ExpandedClass {
175
172
// Use the entrypoint property's owl:equivalentClass if available
176
- if ( Array . isArray ( property [ "http://www.w3.org/2000/01/rdf-schema#range" ] ) ) {
177
- for ( const range of property [
178
- "http://www.w3.org/2000/01/rdf-schema#range"
179
- ] ) {
180
- const onProperty = get (
181
- range ,
182
- '["http://www.w3.org/2002/07/owl#equivalentClass"][0]["http://www.w3.org/2002/07/owl#onProperty"][0]["@id"]' ,
183
- ) as unknown as string ;
184
- const allValuesFrom = get (
185
- range ,
186
- '["http://www.w3.org/2002/07/owl#equivalentClass"][0]["http://www.w3.org/2002/07/owl#allValuesFrom"][0]["@id"]' ,
187
- ) as unknown as string ;
188
173
189
- if (
190
- allValuesFrom &&
191
- onProperty === "http://www.w3.org/ns/hydra/core#member"
192
- ) {
193
- return findSupportedClass ( docs , allValuesFrom ) ;
194
- }
174
+ for ( const range of property [ "http://www.w3.org/2000/01/rdf-schema#range" ] ) {
175
+ const equivalentClass =
176
+ "http://www.w3.org/2002/07/owl#equivalentClass" in range
177
+ ? range ?. [ "http://www.w3.org/2002/07/owl#equivalentClass" ] ?. [ 0 ]
178
+ : undefined ;
179
+
180
+ if ( ! equivalentClass ) {
181
+ continue ;
182
+ }
183
+
184
+ const onProperty =
185
+ equivalentClass [ "http://www.w3.org/2002/07/owl#onProperty" ] ?. [ 0 ] ?. [ "@id" ] ;
186
+ const allValuesFrom =
187
+ equivalentClass [ "http://www.w3.org/2002/07/owl#allValuesFrom" ] ?. [ 0 ] ?. [
188
+ "@id"
189
+ ] ;
190
+
191
+ if (
192
+ allValuesFrom &&
193
+ onProperty === "http://www.w3.org/ns/hydra/core#member"
194
+ ) {
195
+ return findSupportedClass ( docs , allValuesFrom ) ;
195
196
}
196
197
}
197
198
@@ -205,10 +206,10 @@ function findRelatedClass(
205
206
continue ;
206
207
}
207
208
208
- const returns = get (
209
- entrypointSupportedOperation ,
210
- '[ "http://www.w3.org/ns/hydra/core#returns"][0]["@id"]' ,
211
- ) as string | undefined ;
209
+ const returns =
210
+ entrypointSupportedOperation ?. [
211
+ "http://www.w3.org/ns/hydra/core#returns"
212
+ ] ?. [ 0 ] ?. [ "@id" ] ;
212
213
if (
213
214
typeof returns === "string" &&
214
215
returns . indexOf ( "http://www.w3.org/ns/hydra/core" ) !== 0
@@ -241,15 +242,11 @@ export default async function parseHydraDocumentation(
241
242
const resources = [ ] ,
242
243
fields = [ ] ,
243
244
operations = [ ] ;
244
- const title = get (
245
- docs ,
246
- '[0]["http://www.w3.org/ns/hydra/core#title"][0]["@value"]' ,
247
- "API Platform" ,
248
- ) as string ;
249
-
250
- const entrypointType = get ( entrypoint , '[0]["@type"][0]' ) as
251
- | string
252
- | undefined ;
245
+ const title =
246
+ docs ?. [ 0 ] ?. [ "http://www.w3.org/ns/hydra/core#title" ] ?. [ 0 ] ?. [ "@value" ] ??
247
+ "API Platform" ;
248
+
249
+ const entrypointType = entrypoint ?. [ 0 ] ?. [ "@type" ] ?. [ 0 ] ;
253
250
if ( ! entrypointType ) {
254
251
throw new Error ( 'The API entrypoint has no "@type" key.' ) ;
255
252
}
@@ -274,43 +271,42 @@ export default async function parseHydraDocumentation(
274
271
writableFields = [ ] ,
275
272
resourceOperations = [ ] ;
276
273
277
- const property = get (
278
- properties ,
279
- '["http://www.w3.org/ns/hydra/core#property"][0]' ,
280
- ) as ExpandedRdfProperty | undefined ;
274
+ const property =
275
+ properties ?. [ "http://www.w3.org/ns/hydra/core#property" ] ?. [ 0 ] ;
276
+ const propertyIri = property ?. [ "@id" ] ;
281
277
282
- if ( ! property ) {
278
+ if ( ! property || ! propertyIri ) {
283
279
continue ;
284
280
}
285
281
286
- const url = get ( entrypoint , `[0]["${ property [ "@id" ] } "][0]["@id"]` ) as
287
- | string
288
- | undefined ;
282
+ const resourceProperty = entrypoint ?. [ 0 ] ?. [ propertyIri ] ?. [ 0 ] ;
283
+
284
+ const url =
285
+ typeof resourceProperty === "object" && "@id" in resourceProperty
286
+ ? resourceProperty [ "@id" ]
287
+ : undefined ;
289
288
290
289
if ( ! url ) {
291
290
console . error (
292
291
new Error (
293
- `Unable to find the URL for "${ property [ "@id" ] } " in the entrypoint, make sure your API resource has at least one GET collection operation declared.` ,
292
+ `Unable to find the URL for "${ propertyIri } " in the entrypoint, make sure your API resource has at least one GET collection operation declared.` ,
294
293
) ,
295
294
) ;
296
295
continue ;
297
296
}
298
297
299
298
// Add fields
300
299
const relatedClass = findRelatedClass ( docs , property ) ;
301
- for ( const supportedProperties of relatedClass [
300
+ for ( const supportedProperties of relatedClass ?. [
302
301
"http://www.w3.org/ns/hydra/core#supportedProperty"
303
- ] ) {
304
- const supportedProperty = get (
305
- supportedProperties ,
306
- '["http://www.w3.org/ns/hydra/core#property"][0]' ,
307
- ) as unknown as ExpandedRdfProperty ;
302
+ ] ?? [ ] ) {
303
+ const supportedProperty =
304
+ supportedProperties ?. [ "http://www.w3.org/ns/hydra/core#property" ] ?. [ 0 ] ;
308
305
const id = supportedProperty ?. [ "@id" ] ;
309
- const range = get (
310
- supportedProperty ,
311
- '["http://www.w3.org/2000/01/rdf-schema#range"][0]["@id"]' ,
312
- null ,
313
- ) as unknown as string ;
306
+ const range =
307
+ supportedProperty ?. [
308
+ "http://www.w3.org/2000/01/rdf-schema#range"
309
+ ] ?. [ 0 ] ?. [ "@id" ] ?? null ;
314
310
315
311
const field = new Field (
316
312
supportedProperties ?. [ "http://www.w3.org/ns/hydra/core#title" ] ?. [ 0 ] ?. [
@@ -324,59 +320,52 @@ export default async function parseHydraDocumentation(
324
320
range,
325
321
type : getType ( id , range ) ,
326
322
reference :
327
- get ( supportedProperty , ' ["@type"][0]' ) ===
323
+ supportedProperty ?. [ "@type" ] ?. [ 0 ] ===
328
324
"http://www.w3.org/ns/hydra/core#Link"
329
325
? range // Will be updated in a subsequent pass
330
326
: null ,
331
327
embedded :
332
- get ( supportedProperty , ' ["@type"][0]' ) ===
328
+ supportedProperty ?. [ "@type" ] ?. [ 0 ] ===
333
329
"http://www.w3.org/ns/hydra/core#Link"
334
330
? null
335
331
: ( range as unknown as Resource ) , // Will be updated in a subsequent pass
336
- required : get (
337
- supportedProperties ,
338
- '["http://www.w3.org/ns/hydra/core#required"][0]["@value"]' ,
339
- false ,
340
- ) as boolean ,
341
- description : get (
342
- supportedProperties ,
343
- '["http://www.w3.org/ns/hydra/core#description"][0]["@value"]' ,
344
- "" ,
345
- ) as string ,
346
- maxCardinality : get (
347
- supportedProperty ,
348
- '["http://www.w3.org/2002/07/owl#maxCardinality"][0]["@value"]' ,
349
- null ,
350
- ) as number | null ,
351
- deprecated : get (
352
- supportedProperties ,
353
- '["http://www.w3.org/2002/07/owl#deprecated"][0]["@value"]' ,
354
- false ,
355
- ) as boolean ,
332
+ required :
333
+ supportedProperties ?. [
334
+ "http://www.w3.org/ns/hydra/core#required"
335
+ ] ?. [ 0 ] ?. [ "@value" ] ?? false ,
336
+ description :
337
+ supportedProperties ?. [
338
+ "http://www.w3.org/ns/hydra/core#description"
339
+ ] ?. [ 0 ] ?. [ "@value" ] ?? "" ,
340
+ maxCardinality :
341
+ supportedProperty ?. [
342
+ "http://www.w3.org/2002/07/owl#maxCardinality"
343
+ ] ?. [ 0 ] ?. [ "@value" ] ?? null ,
344
+ deprecated :
345
+ supportedProperties ?. [
346
+ "http://www.w3.org/2002/07/owl#deprecated"
347
+ ] ?. [ 0 ] ?. [ "@value" ] ?? false ,
356
348
} ,
357
349
) ;
358
350
359
351
fields . push ( field ) ;
360
352
resourceFields . push ( field ) ;
361
353
362
354
if (
363
- get (
364
- supportedProperties ,
365
- '["http://www.w3.org/ns/hydra/core#readable"][0]["@value"]' ,
366
- )
355
+ supportedProperties ?. [
356
+ "http://www.w3.org/ns/hydra/core#readable"
357
+ ] ?. [ 0 ] ?. [ "@value" ]
367
358
) {
368
359
readableFields . push ( field ) ;
369
360
}
370
361
371
362
if (
372
- get (
373
- supportedProperties ,
374
- '["http://www.w3.org/ns/hydra/core#writeable"][0]["@value"]' ,
375
- ) ||
376
- get (
377
- supportedProperties ,
378
- '["http://www.w3.org/ns/hydra/core#writable"][0]["@value"]' ,
379
- )
363
+ supportedProperties ?. [
364
+ "http://www.w3.org/ns/hydra/core#writeable"
365
+ ] ?. [ 0 ] ?. [ "@value" ] ||
366
+ supportedProperties ?. [
367
+ "http://www.w3.org/ns/hydra/core#writable"
368
+ ] ?. [ 0 ] ?. [ "@value" ]
380
369
) {
381
370
writableFields . push ( field ) ;
382
371
}
@@ -414,11 +403,10 @@ export default async function parseHydraDocumentation(
414
403
] ?. [ 0 ] ?. [ "@id" ] ,
415
404
returns : range ,
416
405
types : entrypointOperation [ "@type" ] ,
417
- deprecated : get (
418
- entrypointOperation ,
419
- '["http://www.w3.org/2002/07/owl#deprecated"][0]["@value"]' ,
420
- false ,
421
- ) as boolean ,
406
+ deprecated :
407
+ entrypointOperation ?. [
408
+ "http://www.w3.org/2002/07/owl#deprecated"
409
+ ] ?. [ 0 ] ?. [ "@value" ] ?? false ,
422
410
} ,
423
411
) ;
424
412
@@ -464,11 +452,10 @@ export default async function parseHydraDocumentation(
464
452
] ?. [ 0 ] ?. [ "@id" ] ,
465
453
returns : range ,
466
454
types : supportedOperation [ "@type" ] ,
467
- deprecated : get (
468
- supportedOperation ,
469
- '["http://www.w3.org/2002/07/owl#deprecated"][0]["@value"]' ,
470
- false ,
471
- ) as boolean ,
455
+ deprecated :
456
+ supportedOperation ?. [
457
+ "http://www.w3.org/2002/07/owl#deprecated"
458
+ ] ?. [ 0 ] ?. [ "@value" ] ?? false ,
472
459
} ,
473
460
) ;
474
461
@@ -478,20 +465,18 @@ export default async function parseHydraDocumentation(
478
465
479
466
const resource = new Resource ( guessNameFromUrl ( url , entrypointUrl ) , url , {
480
467
id : relatedClass [ "@id" ] ,
481
- title : get (
482
- relatedClass ,
483
- '["http://www.w3.org/ns/hydra/core#title"][0]["@value"]' ,
484
- "" ,
485
- ) as string ,
468
+ title :
469
+ relatedClass ?. [ "http://www.w3.org/ns/hydra/core#title" ] ?. [ 0 ] ?. [
470
+ "@value"
471
+ ] ?? "" ,
486
472
fields : resourceFields ,
487
473
readableFields,
488
474
writableFields,
489
475
operations : resourceOperations ,
490
- deprecated : get (
491
- relatedClass ,
492
- '["http://www.w3.org/2002/07/owl#deprecated"][0]["@value"]' ,
493
- false ,
494
- ) as boolean ,
476
+ deprecated :
477
+ relatedClass ?. [ "http://www.w3.org/2002/07/owl#deprecated" ] ?. [ 0 ] ?. [
478
+ "@value"
479
+ ] ?? false ,
495
480
} ) ;
496
481
497
482
resource . parameters = [ ] ;
0 commit comments