feat(apidocs): add resolvers prop to AsyncApiDefinitionWidget

Signed-off-by: Simon Stamm <simon.stamm@tui.com>
This commit is contained in:
Simon Stamm
2024-06-19 17:53:00 +02:00
parent 78621334a3
commit ebfeb40305
7 changed files with 155 additions and 8 deletions
@@ -19,6 +19,7 @@ import '@asyncapi/react-component/styles/default.css';
import { makeStyles, alpha, darken } from '@material-ui/core/styles';
import React from 'react';
import { useTheme } from '@material-ui/core/styles';
import { AsyncApiResolver } from './AsyncApiDefinitionWidget';
const useStyles = makeStyles(theme => ({
root: {
@@ -143,7 +144,7 @@ const useStyles = makeStyles(theme => ({
},
}));
const httpsFetchResolver = {
const httpsFetchResolver: AsyncApiResolver = {
schema: 'https',
order: 1,
canRead: true,
@@ -153,7 +154,7 @@ const httpsFetchResolver = {
},
};
const httpFetchResolver = {
const httpFetchResolver: AsyncApiResolver = {
schema: 'http',
order: 1,
canRead: true,
@@ -175,15 +176,24 @@ const config = {
type Props = {
definition: string;
resolvers?: AsyncApiResolver[];
};
export const AsyncApiDefinition = ({ definition }: Props): JSX.Element => {
export const AsyncApiDefinition = ({
definition,
resolvers,
}: Props): JSX.Element => {
const classes = useStyles();
const theme = useTheme();
const classNames = `${classes.root} ${
theme.palette.type === 'dark' ? classes.dark : ''
}`;
// Overwrite default resolvers if custom ones are set
if (resolvers) {
config.parserOptions.__unstable.resolver.resolvers = resolvers;
}
return (
<div className={classNames}>
<AsyncApi schema={definition} config={config} />
@@ -25,9 +25,18 @@ const LazyAsyncApiDefinition = React.lazy(() =>
})),
);
/** @public */
export type AsyncApiResolver = {
schema: string;
order: number;
canRead: boolean;
read(uri: any): Promise<string>;
};
/** @public */
export type AsyncApiDefinitionWidgetProps = {
definition: string;
resolvers?: AsyncApiResolver[];
};
/** @public */
@@ -15,4 +15,7 @@
*/
export { AsyncApiDefinitionWidget } from './AsyncApiDefinitionWidget';
export type { AsyncApiDefinitionWidgetProps } from './AsyncApiDefinitionWidget';
export type {
AsyncApiDefinitionWidgetProps,
AsyncApiResolver,
} from './AsyncApiDefinitionWidget';