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