Skip to content

Commit

Permalink
feat: Add typeDef info to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 15, 2020
1 parent 12b7930 commit 33aacd6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions plugins/gatsby-source-newrelic-sdk/src/docInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { getExamples } = require('./exampleInfo');
const { getTypeDefs } = require('./typeDefInfo');

exports.getComponentDoc = (name, sdk) => {
const component = sdk[name];
Expand All @@ -7,11 +8,20 @@ exports.getComponentDoc = (name, sdk) => {
return null;
}

const properties = Object.getOwnPropertyNames(component).map(
(key) => component[key]
);

const propTypes = Object.getOwnPropertyNames(component.propTypes || {}).map(
(key) => component.propTypes[key]
);

return {
name,
usage: `import { ${name} } from 'nr1'`,
description: component.__docs__.text,
examples: getExamples(component),
typeDefs: getTypeDefs(properties.concat(propTypes), sdk),
};
};

Expand All @@ -22,10 +32,13 @@ exports.getApiDoc = (name, sdk) => {
return null;
}

const properties = Object.getOwnPropertyNames(api).map((key) => api[key]);

return {
name,
usage: `import { ${name} } from 'nr1'`,
description: api.__docs__.text,
examples: getExamples(api),
typeDefs: getTypeDefs(properties, sdk),
};
};
31 changes: 31 additions & 0 deletions plugins/gatsby-source-newrelic-sdk/src/typeDefInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const IGNORED_TYPE_DEFS = [
'Object',
'ReactNode',
'Event',
'number',
'string',
'boolean',
];

const flatten = (arr) => [].concat(...arr);

const getTypeDefNames = (tags) => {
return flatten(Object.values(tags))
.flatMap((tag) => [tag.type, tag.promiseType])
.filter(Boolean)
.filter((tag) => !IGNORED_TYPE_DEFS.includes(tag))
.map((tag) => tag.replace(/\[\]$/, '')); // TimePickerRange[] => TimePickerRange
};

exports.getTypeDefs = (properties, sdk) => {
return properties
.map((property) => property && property.__docs__ && property.__docs__.tags)
.filter(Boolean)
.flatMap(getTypeDefNames)
.map((name) => sdk.__typeDefs__[name])
.filter((typeDef) => typeDef !== undefined)
.map((typeDef) => ({
properties: typeDef.tags.property,
name: typeDef.tags.typedef.find((tag) => tag.identifier).identifier.name,
}));
};

0 comments on commit 33aacd6

Please sign in to comment.