feat: add option to display verb in webhooks (#1994)

* chore: cli test run ts files
Co-authored-by: Ivana Isadora Devcic <33730345+skadinna@users.noreply.github.com>
This commit is contained in:
Alex Varchuk 2022-05-11 19:13:50 +03:00 committed by GitHub
parent 1f417d67c6
commit 311d2ce64d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 89 additions and 77 deletions

View File

@ -251,6 +251,7 @@ You can use all of the following options with the standalone version of the <red
* **summary-only**: displays a summary in the sidebar navigation item. (**default**) * **summary-only**: displays a summary in the sidebar navigation item. (**default**)
* **path-only**: displays a path in the sidebar navigation item. * **path-only**: displays a path in the sidebar navigation item.
* **id-only**: displays the operation id with a fallback to the path in the sidebar navigation item. * **id-only**: displays the operation id with a fallback to the path in the sidebar navigation item.
* `showWebhookVerb` - when set to `true`, shows the HTTP request method for webhooks in operations and in the sidebar.
### `<redoc>` theme object ### `<redoc>` theme object
* `spacing` * `spacing`

View File

@ -4,8 +4,8 @@ import { readFileSync } from 'fs';
describe('build', () => { describe('build', () => {
it('should use .redocly.yaml', () => { it('should use .redocly.yaml', () => {
const r = spawnSync( const r = spawnSync(
'node', 'ts-node',
['../../../index.js', 'build', ' ../../../../demo/openapi.yaml', '--output=redocTest.html'], ['../../../index.ts', 'build', ' ../../../../demo/openapi.yaml', '--output=redocTest.html'],
{ {
cwd: __dirname, cwd: __dirname,
shell: true, shell: true,

View File

@ -4,9 +4,9 @@ import { readFileSync } from 'fs';
describe('build with inline options', () => { describe('build with inline options', () => {
it('should use inline options and ignore .redocly.yaml', () => { it('should use inline options and ignore .redocly.yaml', () => {
const r = spawnSync( const r = spawnSync(
'node', 'ts-node',
[ [
'../../../index.js', '../../../index.ts',
'build', 'build',
' ../../../../demo/openapi.yaml', ' ../../../../demo/openapi.yaml',
'--options.disableSearch="false" ', '--options.disableSearch="false" ',

View File

@ -27,14 +27,10 @@ export interface OperationProps {
operation: OperationModel; operation: OperationModel;
} }
@observer export const Operation = observer(({ operation }: OperationProps): JSX.Element => {
export class Operation extends React.Component<OperationProps> { const { name: summary, description, deprecated, externalDocs, isWebhook, httpVerb } = operation;
render() {
const { operation } = this.props;
const { name: summary, description, deprecated, externalDocs, isWebhook } = operation;
const hasDescription = !!(description || externalDocs); const hasDescription = !!(description || externalDocs);
const { showWebhookVerb } = React.useContext(OptionsContext);
return ( return (
<OptionsContext.Consumer> <OptionsContext.Consumer>
{options => ( {options => (
@ -43,7 +39,12 @@ export class Operation extends React.Component<OperationProps> {
<H2> <H2>
<ShareLink to={operation.id} /> <ShareLink to={operation.id} />
{summary} {deprecated && <Badge type="warning"> Deprecated </Badge>} {summary} {deprecated && <Badge type="warning"> Deprecated </Badge>}
{isWebhook && <Badge type="primary"> Webhook </Badge>} {isWebhook && (
<Badge type="primary">
{' '}
Webhook {showWebhookVerb && httpVerb && '| ' + httpVerb.toUpperCase()}
</Badge>
)}
</H2> </H2>
{options.pathInMiddlePanel && !isWebhook && ( {options.pathInMiddlePanel && !isWebhook && (
<Endpoint operation={operation} inverted={true} /> <Endpoint operation={operation} inverted={true} />
@ -70,5 +71,4 @@ export class Operation extends React.Component<OperationProps> {
)} )}
</OptionsContext.Consumer> </OptionsContext.Consumer>
); );
} });
}

View File

@ -8,11 +8,13 @@ import { MenuItems } from './MenuItems';
import { MenuItemLabel, MenuItemLi, MenuItemTitle, OperationBadge } from './styled.elements'; import { MenuItemLabel, MenuItemLi, MenuItemTitle, OperationBadge } from './styled.elements';
import { l } from '../../services/Labels'; import { l } from '../../services/Labels';
import { scrollIntoViewIfNeeded } from '../../utils'; import { scrollIntoViewIfNeeded } from '../../utils';
import { OptionsContext } from '../OptionsProvider';
export interface MenuItemProps { export interface MenuItemProps {
item: IMenuItem; item: IMenuItem;
onActivate?: (item: IMenuItem) => void; onActivate?: (item: IMenuItem) => void;
withoutChildren?: boolean; withoutChildren?: boolean;
children?: React.ReactChild;
} }
@observer @observer
@ -70,37 +72,33 @@ export class MenuItem extends React.Component<MenuItemProps> {
export interface OperationMenuItemContentProps { export interface OperationMenuItemContentProps {
item: OperationModel; item: OperationModel;
children?: React.ReactChild;
} }
@observer export const OperationMenuItemContent = observer((props: OperationMenuItemContentProps) => {
export class OperationMenuItemContent extends React.Component<OperationMenuItemContentProps> { const { item } = props;
ref = React.createRef<HTMLLabelElement>(); const ref = React.createRef<HTMLLabelElement>();
const { showWebhookVerb } = React.useContext(OptionsContext);
componentDidUpdate() { React.useEffect(() => {
if (this.props.item.active && this.ref.current) { if (props.item.active && ref.current) {
scrollIntoViewIfNeeded(this.ref.current); scrollIntoViewIfNeeded(ref.current);
}
} }
}, [props.item.active, ref]);
render() {
const { item } = this.props;
return ( return (
<MenuItemLabel <MenuItemLabel depth={item.depth} active={item.active} deprecated={item.deprecated} ref={ref}>
depth={item.depth}
active={item.active}
deprecated={item.deprecated}
ref={this.ref}
>
{item.isWebhook ? ( {item.isWebhook ? (
<OperationBadge type="hook">{l('webhook')}</OperationBadge> <OperationBadge type="hook">
{showWebhookVerb ? item.httpVerb : l('webhook')}
</OperationBadge>
) : ( ) : (
<OperationBadge type={item.httpVerb}>{shortenHTTPVerb(item.httpVerb)}</OperationBadge> <OperationBadge type={item.httpVerb}>{shortenHTTPVerb(item.httpVerb)}</OperationBadge>
)} )}
<MenuItemTitle width="calc(100% - 38px)"> <MenuItemTitle width="calc(100% - 38px)">
{item.sidebarLabel} {item.sidebarLabel}
{this.props.children} {props.children}
</MenuItemTitle> </MenuItemTitle>
</MenuItemLabel> </MenuItemLabel>
); );
} });
}

View File

@ -106,6 +106,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,
@ -360,6 +361,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,
@ -589,6 +591,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,
@ -885,6 +888,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,
@ -1139,6 +1143,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,
@ -1368,6 +1373,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,
@ -1620,6 +1626,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,
@ -1913,6 +1920,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,
@ -2167,6 +2175,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,
@ -2396,6 +2405,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
"showWebhookVerb": false,
"sideNavStyle": "summary-only", "sideNavStyle": "summary-only",
"simpleOneOfTypeLabel": false, "simpleOneOfTypeLabel": false,
"sortEnumValuesAlphabetically": false, "sortEnumValuesAlphabetically": false,

View File

@ -59,6 +59,7 @@ export interface RedocRawOptions {
nonce?: string; nonce?: string;
hideFab?: boolean; hideFab?: boolean;
minCharacterLengthToInitSearch?: number; minCharacterLengthToInitSearch?: number;
showWebhookVerb?: boolean;
} }
export function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean { export function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
@ -257,6 +258,7 @@ export class RedocNormalizedOptions {
generatedPayloadSamplesMaxDepth: number; generatedPayloadSamplesMaxDepth: number;
hideFab: boolean; hideFab: boolean;
minCharacterLengthToInitSearch: number; minCharacterLengthToInitSearch: number;
showWebhookVerb: boolean;
nonce?: string; nonce?: string;
@ -334,5 +336,6 @@ export class RedocNormalizedOptions {
this.nonce = raw.nonce; this.nonce = raw.nonce;
this.hideFab = argValueToBoolean(raw.hideFab); this.hideFab = argValueToBoolean(raw.hideFab);
this.minCharacterLengthToInitSearch = argValueToNumber(raw.minCharacterLengthToInitSearch) || 3; this.minCharacterLengthToInitSearch = argValueToNumber(raw.minCharacterLengthToInitSearch) || 3;
this.showWebhookVerb = argValueToBoolean(raw.showWebhookVerb);
} }
} }