You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApiParameters.tsx 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { Accordion, Badge, SubHeading, SubTitle, TextMuted } from 'design-system';
  21. import { groupBy } from 'lodash';
  22. import { OpenAPIV3 } from 'openapi-types';
  23. import React from 'react';
  24. import { FormattedMessage } from 'react-intl';
  25. import { translate } from '../../../helpers/l10n';
  26. import { ExcludeReferences } from '../types';
  27. import { mapOpenAPISchema } from '../utils';
  28. import ApiRequestBodyParameters from './ApiRequestParameters';
  29. import ApiResponseSchema from './ApiResponseSchema';
  30. interface Props {
  31. data: ExcludeReferences<OpenAPIV3.OperationObject>;
  32. }
  33. export default function ApiParameters({ data }: Props) {
  34. const [openParameters, setOpenParameters] = React.useState<string[]>([]);
  35. const toggleParameter = (name: string) => {
  36. if (openParameters.includes(name)) {
  37. setOpenParameters(openParameters.filter((n) => n !== name));
  38. } else {
  39. setOpenParameters([...openParameters, name]);
  40. }
  41. };
  42. const getSchemaType = (schema: ExcludeReferences<OpenAPIV3.SchemaObject>) => {
  43. const mappedSchema = mapOpenAPISchema(schema);
  44. return typeof mappedSchema === 'object' ? JSON.stringify(mappedSchema) : mappedSchema;
  45. };
  46. const requestBody = data.requestBody?.content;
  47. return (
  48. <>
  49. <SubTitle>{translate('api_documentation.v2.parameter_header')}</SubTitle>
  50. {Object.entries(groupBy(data.parameters, (p) => p.in)).map(([group, parameters]) => (
  51. <div key={group}>
  52. <SubHeading id={`api-parameters-${group}`}>
  53. {translate(`api_documentation.v2.request_subheader.${group}`)}
  54. </SubHeading>
  55. <ul aria-labelledby={`api-parameters-${group}`}>
  56. {parameters.map((parameter) => {
  57. return (
  58. <Accordion
  59. className="sw-mt-2 sw-mb-4"
  60. key={parameter.name}
  61. header={
  62. <div>
  63. {parameter.name}{' '}
  64. {parameter.schema && (
  65. <TextMuted
  66. className="sw-inline sw-ml-2"
  67. text={getSchemaType(parameter.schema)}
  68. />
  69. )}
  70. {parameter.required && (
  71. <Badge className="sw-ml-2">{translate('required')}</Badge>
  72. )}
  73. {parameter.deprecated && (
  74. <Badge variant="deleted" className="sw-ml-2">
  75. {translate('deprecated')}
  76. </Badge>
  77. )}
  78. </div>
  79. }
  80. data={parameter.name}
  81. onClick={toggleParameter}
  82. open={openParameters.includes(parameter.name)}
  83. >
  84. <div>{parameter.description}</div>
  85. {parameter.schema?.enum && (
  86. <div className="sw-mt-2">
  87. <FormattedMessage
  88. id="api_documentation.v2.enum_description"
  89. values={{
  90. values: (
  91. <div className="sw-body-sm-highlight">
  92. {parameter.schema.enum.join(', ')}
  93. </div>
  94. ),
  95. }}
  96. />
  97. </div>
  98. )}
  99. {parameter.schema?.maximum && (
  100. <TextMuted
  101. className="sw-mt-2 sw-block"
  102. text={`${translate('max')}: ${parameter.schema?.maximum}`}
  103. />
  104. )}
  105. {typeof parameter.schema?.minimum === 'number' && (
  106. <TextMuted
  107. className="sw-mt-2 sw-block"
  108. text={`${translate('min')}: ${parameter.schema?.minimum}`}
  109. />
  110. )}
  111. {parameter.example !== undefined && (
  112. <TextMuted
  113. className="sw-mt-2 sw-block"
  114. text={`${translate('example')}: ${parameter.example}`}
  115. />
  116. )}
  117. {parameter.schema?.default !== undefined && (
  118. <TextMuted
  119. className="sw-mt-2 sw-block"
  120. text={`${translate('default')}: ${parameter.schema?.default}`}
  121. />
  122. )}
  123. </Accordion>
  124. );
  125. })}
  126. </ul>
  127. </div>
  128. ))}
  129. {!requestBody && !data.parameters?.length && <TextMuted text={translate('no_data')} />}
  130. {requestBody && (
  131. <div>
  132. <SubHeading id="api_documentation.v2.request_subheader.request_body">
  133. {translate('api_documentation.v2.request_subheader.request_body')}
  134. </SubHeading>
  135. <ApiResponseSchema content={requestBody} />
  136. <ApiRequestBodyParameters content={requestBody} />
  137. </div>
  138. )}
  139. </>
  140. );
  141. }