Skip to content

Commit 1631843

Browse files
committed
refactor: remove lodash.get
1 parent 34f0c40 commit 1631843

File tree

5 files changed

+165
-161
lines changed

5 files changed

+165
-161
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252
"graphql": "^16.0.0",
5353
"inflection": "^3.0.0",
5454
"jsonld": "^8.3.2",
55-
"jsonref": "^9.0.0",
56-
"lodash.get": "^4.4.0"
55+
"jsonref": "^9.0.0"
5756
},
5857
"devDependencies": {
5958
"@types/inflection": "^1.13.0",

pnpm-lock.yaml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hydra/parseHydraDocumentation.ts

Lines changed: 96 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import jsonld from "jsonld";
2-
import get from "lodash.get";
32
import { Api } from "../Api.js";
43
import { Field } from "../Field.js";
54
import { Resource } from "../Resource.js";
@@ -45,10 +44,8 @@ function findSupportedClass(
4544
docs: ExpandedDoc[],
4645
classToFind: string,
4746
): 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"];
5249
if (!Array.isArray(supportedClasses)) {
5350
throw new TypeError(
5451
'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(
155152
api: new Api(entrypointUrl, { resources: [] }),
156153
error,
157154
response,
158-
status: get(response, "status"),
155+
status: response?.status,
159156
};
160157
}
161158
}
@@ -173,25 +170,29 @@ function findRelatedClass(
173170
property: ExpandedRdfProperty,
174171
): ExpandedClass {
175172
// 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;
188173

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);
195196
}
196197
}
197198

@@ -205,10 +206,10 @@ function findRelatedClass(
205206
continue;
206207
}
207208

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"];
212213
if (
213214
typeof returns === "string" &&
214215
returns.indexOf("http://www.w3.org/ns/hydra/core") !== 0
@@ -241,15 +242,11 @@ export default async function parseHydraDocumentation(
241242
const resources = [],
242243
fields = [],
243244
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];
253250
if (!entrypointType) {
254251
throw new Error('The API entrypoint has no "@type" key.');
255252
}
@@ -274,43 +271,42 @@ export default async function parseHydraDocumentation(
274271
writableFields = [],
275272
resourceOperations = [];
276273

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"];
281277

282-
if (!property) {
278+
if (!property || !propertyIri) {
283279
continue;
284280
}
285281

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;
289288

290289
if (!url) {
291290
console.error(
292291
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.`,
294293
),
295294
);
296295
continue;
297296
}
298297

299298
// Add fields
300299
const relatedClass = findRelatedClass(docs, property);
301-
for (const supportedProperties of relatedClass[
300+
for (const supportedProperties of relatedClass?.[
302301
"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];
308305
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;
314310

315311
const field = new Field(
316312
supportedProperties?.["http://www.w3.org/ns/hydra/core#title"]?.[0]?.[
@@ -324,59 +320,52 @@ export default async function parseHydraDocumentation(
324320
range,
325321
type: getType(id, range),
326322
reference:
327-
get(supportedProperty, '["@type"][0]') ===
323+
supportedProperty?.["@type"]?.[0] ===
328324
"http://www.w3.org/ns/hydra/core#Link"
329325
? range // Will be updated in a subsequent pass
330326
: null,
331327
embedded:
332-
get(supportedProperty, '["@type"][0]') ===
328+
supportedProperty?.["@type"]?.[0] ===
333329
"http://www.w3.org/ns/hydra/core#Link"
334330
? null
335331
: (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,
356348
},
357349
);
358350

359351
fields.push(field);
360352
resourceFields.push(field);
361353

362354
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"]
367358
) {
368359
readableFields.push(field);
369360
}
370361

371362
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"]
380369
) {
381370
writableFields.push(field);
382371
}
@@ -414,11 +403,10 @@ export default async function parseHydraDocumentation(
414403
]?.[0]?.["@id"],
415404
returns: range,
416405
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,
422410
},
423411
);
424412

@@ -464,11 +452,10 @@ export default async function parseHydraDocumentation(
464452
]?.[0]?.["@id"],
465453
returns: range,
466454
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,
472459
},
473460
);
474461

@@ -478,20 +465,18 @@ export default async function parseHydraDocumentation(
478465

479466
const resource = new Resource(guessNameFromUrl(url, entrypointUrl), url, {
480467
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+
] ?? "",
486472
fields: resourceFields,
487473
readableFields,
488474
writableFields,
489475
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,
495480
});
496481

497482
resource.parameters = [];

0 commit comments

Comments
 (0)