Skip to content

Commit

Permalink
feat: added ability to add supplemental documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
zstix committed Aug 17, 2020
1 parent 3a47267 commit b726217
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 23 deletions.
51 changes: 51 additions & 0 deletions src/data/additionalDocs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const additionalDocs = {
nerdlet: {
methods: [
{
name: 'nerdlet.setUrlState',
description: `
Updates the current nerdlet's URL state that can be read from
\`NerdletStateContext.Consumer\`.
If you wish to update nerdlet's state without persisting its value in the
url, use React's built-in \`setState()\`.
This method behaves like React's \`setState()\`, meaning that it performs a
shallow merge between the current URL state and the provided state in the
\`urlState\` parameter.
If you wish to navigate without adding an entry to the browser history, set
\`urlStateOptions.replaceHistory\` to \`true\`.
`.trim(),
returnValue: { type: 'void' },
params: [
{
name: 'urlState',
type: 'Object',
description: 'New nerdlet URL state.',
},
{
name: 'urlStateOptions',
type: 'UrlStateOptions',
description: 'Options for the URL state.',
},
],
examples: [
{
label: 'Example 1',
sourceCode: `
nerlet.setUrlState({
foo: 'bar',
});
`.trim(),
options: {
live: false,
},
},
],
},
],
},
};

export default additionalDocs;
61 changes: 38 additions & 23 deletions src/hooks/useApiDoc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react';
import { getTypeDefs } from '../utils/typeDefs';
import navigationApi from '../data/navigationApi';
import additionalDocs from '../data/additionalDocs';

const IGNORED_METHODS = [
'prototype',
Expand Down Expand Up @@ -34,6 +35,15 @@ const useApiDoc = (name) => {
return navigationApi;
}

// The SDK may be missing method documentation. If so, we need to add in
// some additional hardcoded values.
const clientDocs = {
typeDefs: [],
constants: [],
methods: [],
...additionalDocs[name],
};

if (!api) {
const err = new Error('NR1_SDK API not found');
window.NREUM && window.NREUM.noticeError(err, { apiName: name });
Expand Down Expand Up @@ -65,30 +75,35 @@ const useApiDoc = (name) => {
return {
description: apiDocs?.text,
usage: `import { ${name} } from 'nr1'`,
typeDefs: getTypeDefs(properties),
constants: getConstants(api),
methods: Object.getOwnPropertyNames(api)
.filter(
(member) =>
!IGNORED_METHODS.includes(member) &&
typeof api[member] === 'function'
)
.filter(
(member) =>
!IGNORED_METHODS_BY_LIB[name] ||
!IGNORED_METHODS_BY_LIB[name].includes(member)
)
.map((member) => {
const methodDocs = api[member].__docs__;
typeDefs: [...clientDocs.typeDefs, ...getTypeDefs(properties)],
constants: [...clientDocs.constants, ...getConstants(api)],
methods: [
...clientDocs.methods,
...Object.getOwnPropertyNames(api)
.filter(
(member) =>
!IGNORED_METHODS.includes(member) &&
typeof api[member] === 'function'
)
.filter(
(member) =>
!IGNORED_METHODS_BY_LIB[name] ||
!IGNORED_METHODS_BY_LIB[name].includes(member)
)
.map((member) => {
const methodDocs = api[member].__docs__;

return {
name: `${name}.${member}`,
description: methodDocs?.text,
returnValue: methodDocs?.tags.return?.[0] ?? { type: 'undefined' },
params: methodDocs?.tags.param ?? [],
examples: methodDocs?.tags.examples ?? [],
};
}),
return {
name: `${name}.${member}`,
description: methodDocs?.text,
returnValue: methodDocs?.tags.return?.[0] ?? {
type: 'undefined',
},
params: methodDocs?.tags.param ?? [],
examples: methodDocs?.tags.examples ?? [],
};
}),
],
};
}, [name]);
};
Expand Down

0 comments on commit b726217

Please sign in to comment.