2017-11-14 18:46:50 +03:00
|
|
|
import * as React from 'react';
|
|
|
|
|
2017-11-22 12:02:26 +03:00
|
|
|
import { Loading } from './Loading/Loading';
|
2017-11-14 18:46:50 +03:00
|
|
|
import { StoreProvider } from './StoreProvider';
|
|
|
|
import { ErrorBoundary } from './ErrorBoundary';
|
|
|
|
import { Redoc } from './Redoc/Redoc';
|
2017-11-22 12:02:26 +03:00
|
|
|
import { RedocNormalizedOptions, RedocRawOptions } from '../services/RedocNormalizedOptions';
|
2017-11-14 18:46:50 +03:00
|
|
|
|
2017-11-19 22:27:44 +03:00
|
|
|
export interface RedocStandaloneProps {
|
2017-11-20 16:02:35 +03:00
|
|
|
spec?: object;
|
|
|
|
specUrl?: string;
|
2017-11-21 14:00:33 +03:00
|
|
|
options?: RedocRawOptions;
|
2017-11-14 18:46:50 +03:00
|
|
|
}
|
|
|
|
|
2017-11-19 22:27:44 +03:00
|
|
|
export class RedocStandalone extends React.Component<RedocStandaloneProps> {
|
2017-11-20 19:03:11 +03:00
|
|
|
static propTypes = {
|
|
|
|
spec: (props, _, componentName) => {
|
|
|
|
if (!props.spec && !props.specUrl) {
|
|
|
|
return new Error(
|
|
|
|
`One of props 'spec' or 'specUrlurl' was not specified in '${componentName}'.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
specUrl: (props, _, componentName) => {
|
|
|
|
if (!props.spec && !props.specUrl) {
|
|
|
|
return new Error(
|
|
|
|
`One of props 'spec' or 'specUrl' was not specified in '${componentName}'.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-11-14 18:46:50 +03:00
|
|
|
render() {
|
2017-11-22 12:02:26 +03:00
|
|
|
const { spec, specUrl, options = {} } = this.props;
|
|
|
|
const hideLoading = options.hideLoading !== undefined;
|
|
|
|
|
|
|
|
const normalizedOpts = new RedocNormalizedOptions(options);
|
2017-11-14 18:46:50 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ErrorBoundary>
|
2017-11-21 14:00:33 +03:00
|
|
|
<StoreProvider spec={spec} specUrl={specUrl} options={options}>
|
2017-11-22 12:02:26 +03:00
|
|
|
{({ loading, store }) =>
|
|
|
|
!loading ? (
|
|
|
|
<Redoc store={store!} />
|
|
|
|
) : hideLoading ? null : (
|
|
|
|
<Loading color={normalizedOpts.theme.colors.main} />
|
|
|
|
)
|
|
|
|
}
|
2017-11-14 18:46:50 +03:00
|
|
|
</StoreProvider>
|
|
|
|
</ErrorBoundary>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|