Skip to content

Commit c739b9d

Browse files
committed
refactor: remove lodash.get
1 parent 7a1e724 commit c739b9d

File tree

5 files changed

+166
-161
lines changed

5 files changed

+166
-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: 97 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,30 @@ 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+
[]) {
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);
195197
}
196198
}
197199

@@ -205,10 +207,10 @@ function findRelatedClass(
205207
continue;
206208
}
207209

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"];
212214
if (
213215
typeof returns === "string" &&
214216
returns.indexOf("http://www.w3.org/ns/hydra/core") !== 0
@@ -241,15 +243,11 @@ export default async function parseHydraDocumentation(
241243
const resources = [],
242244
fields = [],
243245
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];
253251
if (!entrypointType) {
254252
throw new Error('The API entrypoint has no "@type" key.');
255253
}
@@ -274,43 +272,42 @@ export default async function parseHydraDocumentation(
274272
writableFields = [],
275273
resourceOperations = [];
276274

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

282-
if (!property) {
279+
if (!property || !propertyIri) {
283280
continue;
284281
}
285282

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

290290
if (!url) {
291291
console.error(
292292
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.`,
294294
),
295295
);
296296
continue;
297297
}
298298

299299
// Add fields
300300
const relatedClass = findRelatedClass(docs, property);
301-
for (const supportedProperties of relatedClass[
301+
for (const supportedProperties of relatedClass?.[
302302
"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];
308306
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;
314311

315312
const field = new Field(
316313
supportedProperties?.["http://www.w3.org/ns/hydra/core#title"]?.[0]?.[
@@ -324,59 +321,52 @@ export default async function parseHydraDocumentation(
324321
range,
325322
type: getType(id, range),
326323
reference:
327-
get(supportedProperty, '["@type"][0]') ===
324+
supportedProperty?.["@type"]?.[0] ===
328325
"http://www.w3.org/ns/hydra/core#Link"
329326
? range // Will be updated in a subsequent pass
330327
: null,
331328
embedded:
332-
get(supportedProperty, '["@type"][0]') ===
329+
supportedProperty?.["@type"]?.[0] ===
333330
"http://www.w3.org/ns/hydra/core#Link"
334331
? null
335332
: (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,
356349
},
357350
);
358351

359352
fields.push(field);
360353
resourceFields.push(field);
361354

362355
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"]
367359
) {
368360
readableFields.push(field);
369361
}
370362

371363
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"]
380370
) {
381371
writableFields.push(field);
382372
}
@@ -414,11 +404,10 @@ export default async function parseHydraDocumentation(
414404
]?.[0]?.["@id"],
415405
returns: range,
416406
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,
422411
},
423412
);
424413

@@ -464,11 +453,10 @@ export default async function parseHydraDocumentation(
464453
]?.[0]?.["@id"],
465454
returns: range,
466455
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,
472460
},
473461
);
474462

@@ -478,20 +466,18 @@ export default async function parseHydraDocumentation(
478466

479467
const resource = new Resource(guessNameFromUrl(url, entrypointUrl), url, {
480468
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+
] ?? "",
486473
fields: resourceFields,
487474
readableFields,
488475
writableFields,
489476
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,
495481
});
496482

497483
resource.parameters = [];

0 commit comments

Comments
 (0)