|
| 1 | +import {Fragment} from 'react'; |
| 2 | +import {jsx, jsxs} from 'react/jsx-runtime'; |
| 3 | +import {toJsxRuntime} from 'hast-util-to-jsx-runtime'; |
| 4 | +import {Nodes} from 'hastscript/lib/create-h'; |
| 5 | +import bash from 'refractor/lang/bash.js'; |
| 6 | +import json from 'refractor/lang/json.js'; |
| 7 | +import {refractor} from 'refractor/lib/core.js'; |
| 8 | + |
| 9 | +import {getCurrentPlatformOrGuide} from 'sentry-docs/docTree'; |
| 10 | +import {serverContext} from 'sentry-docs/serverContext'; |
| 11 | +import {PlatformCategory} from 'sentry-docs/types'; |
| 12 | + |
| 13 | +import {CodeBlock} from './codeBlock'; |
| 14 | +import {CodeTabs} from './codeTabs'; |
| 15 | +import {PlatformCategorySection} from './platformCategorySection'; |
| 16 | +import {PlatformSection} from './platformSection'; |
| 17 | +import {SdkDefinition, SdkDefinitionTable, SdkDefinitionTableRow} from './sdkDefinition'; |
| 18 | + |
| 19 | +interface ParameterDef { |
| 20 | + name: string; |
| 21 | + type: string | ParameterDef[]; |
| 22 | + defaultValue?: string; |
| 23 | + description?: string; |
| 24 | + required?: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +type Props = { |
| 28 | + name: string; |
| 29 | + parameters: ParameterDef[]; |
| 30 | + signature: string; |
| 31 | + categorySupported?: PlatformCategory[]; |
| 32 | + children?: React.ReactNode; |
| 33 | +}; |
| 34 | + |
| 35 | +refractor.register(bash); |
| 36 | +refractor.register(json); |
| 37 | + |
| 38 | +const codeToJsx = (code: string, lang = 'json') => { |
| 39 | + return toJsxRuntime(refractor.highlight(code, lang) as Nodes, {Fragment, jsx, jsxs}); |
| 40 | +}; |
| 41 | + |
| 42 | +export function SdkApi({ |
| 43 | + name, |
| 44 | + children, |
| 45 | + signature, |
| 46 | + parameters = [], |
| 47 | + categorySupported = [], |
| 48 | +}: Props) { |
| 49 | + return ( |
| 50 | + <SdkDefinition name={name} categorySupported={categorySupported}> |
| 51 | + <pre className="mt-2 mb-2">{codeToJsx(signature, 'typescript')}</pre> |
| 52 | + |
| 53 | + {parameters.length ? ( |
| 54 | + <Fragment> |
| 55 | + <SdkDefinitionTable> |
| 56 | + {parameters.map(param => ( |
| 57 | + <ApiParameterDef key={param.name} {...param} /> |
| 58 | + ))} |
| 59 | + </SdkDefinitionTable> |
| 60 | + </Fragment> |
| 61 | + ) : null} |
| 62 | + |
| 63 | + {children} |
| 64 | + </SdkDefinition> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +function ApiParameterDef({name, type, description, required}: ParameterDef) { |
| 69 | + return ( |
| 70 | + <tr> |
| 71 | + <th> |
| 72 | + {name} |
| 73 | + {required ? <span className="text-red">*</span> : null} |
| 74 | + </th> |
| 75 | + <td> |
| 76 | + {typeof type === 'string' ? ( |
| 77 | + <code>{type}</code> |
| 78 | + ) : ( |
| 79 | + <RenderNestedObject objProps={type} /> |
| 80 | + )} |
| 81 | + |
| 82 | + {description ? <p className="m-0">{description}</p> : null} |
| 83 | + </td> |
| 84 | + </tr> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +function RenderNestedObject({objProps}: {objProps: ParameterDef[]}) { |
| 89 | + return ( |
| 90 | + <Fragment> |
| 91 | + <div> |
| 92 | + <code>Object:</code> |
| 93 | + </div> |
| 94 | + <SdkDefinitionTable className="mt-1"> |
| 95 | + {objProps.map(prop => ( |
| 96 | + <ApiParameterDef key={prop.name} {...prop} /> |
| 97 | + ))} |
| 98 | + </SdkDefinitionTable> |
| 99 | + </Fragment> |
| 100 | + ); |
| 101 | +} |
0 commit comments