-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create useApiDoc hook. Update ApiDocTemplate to use api-specifi…
…c language
- Loading branch information
1 parent
cca78bf
commit 49e5f61
Showing
4 changed files
with
58 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useMemo } from 'react'; | ||
|
||
const IGNORED_METHODS = [ | ||
'prototype', | ||
'length', | ||
'name', | ||
'propTypes', | ||
'getDerivedStateFromProps', | ||
'defaultProps', | ||
]; | ||
|
||
const useApiDoc = (name) => { | ||
if (typeof window === 'undefined') global.window = {}; | ||
|
||
return useMemo(() => { | ||
const sdk = window.__NR1_SDK__?.default ?? {}; | ||
const api = sdk[name]; | ||
|
||
if (!api) { | ||
return null; | ||
} | ||
|
||
const apiDocs = api?.__docs__; | ||
|
||
return { | ||
description: apiDocs?.text, | ||
usage: `import { ${name} } from 'nr1'`, | ||
methods: Object.getOwnPropertyNames(api) | ||
.filter( | ||
(member) => | ||
!IGNORED_METHODS.includes(member) && | ||
typeof api[member] === 'function' | ||
) | ||
.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 ?? [], | ||
}; | ||
}), | ||
}; | ||
}, [name, window?.__NR1_SDK__]); | ||
}; | ||
|
||
export default useApiDoc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters