From 3000b6763724b473dd09556bc4e75497c27d930b Mon Sep 17 00:00:00 2001 From: Ingo Claro Date: Mon, 23 Jul 2018 10:13:35 -0700 Subject: [PATCH] refactor Extensions, move Redoc extension list to utils file --- src/components/Fields/Extensions.tsx | 29 +++++++++++----------------- src/utils/openapi.ts | 17 ++++++++++++++++ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/components/Fields/Extensions.tsx b/src/components/Fields/Extensions.tsx index 0aae28a9..e730d983 100644 --- a/src/components/Fields/Extensions.tsx +++ b/src/components/Fields/Extensions.tsx @@ -1,5 +1,6 @@ import * as React from 'react'; +import { isRedocExtension } from '../../utils/openapi'; import { OptionsContext } from '../OptionsProvider'; import { SchemaModel } from '../../services/models'; @@ -10,33 +11,25 @@ export interface ExtensionsProps { } export class Extensions extends React.PureComponent { - redocExtensions = [ - 'x-circular-ref', - 'x-code-samples', - 'x-displayName', - 'x-examples', - 'x-ignoredHeaderParameters', - 'x-logo', - 'x-nullable', - 'x-servers', - 'x-tagGroups', - 'x-traitTag', - ]; + constructor(props) { + super(props); + this.getExtensions = this.getExtensions.bind(this); + } - render() { + getExtensions() { const { schema } = this.props; const fullSchema = schema.schema; - const extensionList = Object.keys(fullSchema).filter( - key => key.startsWith('x-') && !this.redocExtensions.includes(key), - ); + return Object.keys(fullSchema).filter(key => key.startsWith('x-') && !isRedocExtension(key)); + } + render() { return ( {options => ( <> {options.showExtensions && - extensionList.map(key => ( - + this.getExtensions().map(key => ( + ))} )} diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index 3d91ac12..190d3b2e 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -286,3 +286,20 @@ export const shortenHTTPVerb = verb => delete: 'del', options: 'opts', }[verb] || verb); + +export function isRedocExtension(key: string): boolean { + const redocExtensions = { + 'x-circular-ref': true, + 'x-code-samples': true, + 'x-displayName': true, + 'x-examples': true, + 'x-ignoredHeaderParameters': true, + 'x-logo': true, + 'x-nullable': true, + 'x-servers': true, + 'x-tagGroups': true, + 'x-traitTag': true, + }; + + return key in redocExtensions; +}