Skip to content

Commit

Permalink
feat: Pull nested type defs
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 15, 2020
1 parent 289514d commit 0397182
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions plugins/gatsby-source-newrelic-sdk/src/typeDefInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,34 @@ const IGNORED_TYPE_DEFS = [
];

const flatten = (arr) => [].concat(...arr);
const uniqBy = (arr, key) => {
return [...new Set(arr.map((item) => item[key]))].map((value) =>
arr.find((item) => item[key] === value)
);
};

exports.getTypeDefs = (properties, sdk) => {
return properties
.map((property) => property && property.__docs__ && property.__docs__.tags)
.filter(Boolean)
.flatMap((tags) => flatten(Object.values(tags)))
.flatMap((tag) => [tag.type, tag.promiseType])
const getTypeDefsFromProperty = (property, sdk) => {
return [property.type, property.promiseType]
.filter(Boolean)
.filter((name) => !IGNORED_TYPE_DEFS.includes(name))
.map((name) => name.replace(/\[\]$/, '')) // TimePickerRange[] => TimePickerRange
.map((name) => sdk.__typeDefs__[name])
.filter((typeDef) => typeDef !== undefined)
.flatMap((typeDef) => [
typeDef,
...typeDef.tags.property.flatMap((property) =>
getTypeDefsFromProperty(property, sdk)
),
])
.filter(Boolean);
};

exports.getTypeDefs = (properties, sdk) => {
const typeDefs = properties
.map((property) => property && property.__docs__ && property.__docs__.tags)
.filter(Boolean)
.flatMap((tags) => flatten(Object.values(tags)))
.flatMap((property) => getTypeDefsFromProperty(property, sdk))
.map((typeDef) => ({
properties: typeDef.tags.property.map(
({ type, description, identifier }) => ({
Expand All @@ -30,4 +46,6 @@ exports.getTypeDefs = (properties, sdk) => {
),
name: typeDef.tags.typedef.find((tag) => tag.identifier).identifier.name,
}));

return uniqBy(typeDefs, 'name');
};

0 comments on commit 0397182

Please sign in to comment.