diff --git a/plugins/gatsby-source-newrelic-sdk/src/docInfo.js b/plugins/gatsby-source-newrelic-sdk/src/docInfo.js index 0298361c8..7f7c070f2 100644 --- a/plugins/gatsby-source-newrelic-sdk/src/docInfo.js +++ b/plugins/gatsby-source-newrelic-sdk/src/docInfo.js @@ -1,4 +1,5 @@ const { getExamples } = require('./exampleInfo'); +const { getTypeDefs } = require('./typeDefInfo'); exports.getComponentDoc = (name, sdk) => { const component = sdk[name]; @@ -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), }; }; @@ -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), }; }; diff --git a/plugins/gatsby-source-newrelic-sdk/src/typeDefInfo.js b/plugins/gatsby-source-newrelic-sdk/src/typeDefInfo.js new file mode 100644 index 000000000..d37d0dd2d --- /dev/null +++ b/plugins/gatsby-source-newrelic-sdk/src/typeDefInfo.js @@ -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, + })); +};