@gitbook/react-openapi 1.4.3 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (263) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/InteractiveSection.js +59 -0
  3. package/dist/Markdown.js +10 -0
  4. package/dist/OpenAPICodeSample.js +219 -0
  5. package/dist/OpenAPICodeSampleInteractive.js +66 -0
  6. package/dist/OpenAPICodeSampleSelector.js +45 -0
  7. package/dist/OpenAPICopyButton.js +39 -0
  8. package/dist/OpenAPIDisclosure.js +30 -0
  9. package/dist/OpenAPIDisclosureGroup.js +75 -0
  10. package/dist/OpenAPIExample.js +41 -0
  11. package/dist/OpenAPIMediaType.js +58 -0
  12. package/dist/OpenAPIOperation.d.ts +12 -7
  13. package/dist/OpenAPIOperation.js +30 -0
  14. package/dist/OpenAPIOperationContext.d.ts +10 -6
  15. package/dist/OpenAPIOperationContext.js +30 -0
  16. package/dist/OpenAPIPath.js +51 -0
  17. package/dist/OpenAPIPrefillContextProvider.d.ts +11 -7
  18. package/dist/OpenAPIPrefillContextProvider.js +25 -0
  19. package/dist/OpenAPIRequestBody.js +28 -0
  20. package/dist/OpenAPIRequestBodyHeaderType.js +23 -0
  21. package/dist/OpenAPIResponse.js +39 -0
  22. package/dist/OpenAPIResponseExample.js +75 -0
  23. package/dist/OpenAPIResponseExampleContent.js +61 -0
  24. package/dist/OpenAPIResponses.js +61 -0
  25. package/dist/OpenAPISchema.js +373 -0
  26. package/dist/OpenAPISchemaName.js +45 -0
  27. package/dist/OpenAPISchemaServer.js +13 -0
  28. package/dist/OpenAPISecurities.js +124 -0
  29. package/dist/OpenAPISelect.js +45 -0
  30. package/dist/OpenAPISpec.js +73 -0
  31. package/dist/OpenAPIWebhook.d.ts +12 -7
  32. package/dist/OpenAPIWebhook.js +28 -0
  33. package/dist/OpenAPIWebhookExample.js +40 -0
  34. package/dist/ScalarApiButton.js +87 -0
  35. package/dist/StaticSection.js +37 -0
  36. package/dist/code-samples.js +267 -419
  37. package/dist/common/OpenAPIColumnSpec.js +23 -0
  38. package/dist/common/OpenAPIOperationDescription.js +18 -0
  39. package/dist/common/OpenAPIStability.js +17 -0
  40. package/dist/common/OpenAPISummary.js +27 -0
  41. package/dist/contentTypeChecks.js +24 -20
  42. package/dist/context.d.ts +68 -72
  43. package/dist/context.js +25 -39
  44. package/dist/decycle.js +39 -68
  45. package/dist/dereference.js +20 -64
  46. package/dist/generateSchemaExample.js +188 -332
  47. package/dist/getDisclosureLabel.js +15 -16
  48. package/dist/getOrCreateStoreByKey.js +20 -17
  49. package/dist/index.d.ts +12 -10
  50. package/dist/index.js +11 -8
  51. package/dist/json2xml.js +10 -5
  52. package/dist/resolveOpenAPIOperation.d.ts +11 -7
  53. package/dist/resolveOpenAPIOperation.js +88 -159
  54. package/dist/resolveOpenAPIWebhook.d.ts +11 -7
  55. package/dist/resolveOpenAPIWebhook.js +41 -116
  56. package/dist/schemas/OpenAPISchemaItem.js +26 -0
  57. package/dist/schemas/OpenAPISchemas.d.ts +16 -11
  58. package/dist/schemas/OpenAPISchemas.js +57 -0
  59. package/dist/schemas/resolveOpenAPISchemas.d.ts +9 -4
  60. package/dist/schemas/resolveOpenAPISchemas.js +15 -59
  61. package/dist/stringifyOpenAPI.js +12 -13
  62. package/dist/translate.js +43 -0
  63. package/dist/translations/de.js +47 -42
  64. package/dist/translations/en.d.ts +46 -42
  65. package/dist/translations/en.js +47 -42
  66. package/dist/translations/es.js +47 -42
  67. package/dist/translations/fr.js +47 -42
  68. package/dist/translations/index.d.ts +404 -391
  69. package/dist/translations/index.js +28 -24
  70. package/dist/translations/ja.js +47 -42
  71. package/dist/translations/nl.js +47 -42
  72. package/dist/translations/no.js +47 -42
  73. package/dist/translations/pt-br.js +47 -42
  74. package/dist/translations/types.d.ts +7 -5
  75. package/dist/translations/zh.js +47 -42
  76. package/dist/types.d.ts +30 -24
  77. package/dist/util/example.js +84 -0
  78. package/dist/util/server.js +32 -38
  79. package/dist/util/tryit-prefill.js +135 -121
  80. package/dist/utils.js +135 -196
  81. package/package.json +18 -11
  82. package/dist/InteractiveSection.d.ts +0 -33
  83. package/dist/InteractiveSection.jsx +0 -61
  84. package/dist/Markdown.d.ts +0 -4
  85. package/dist/Markdown.jsx +0 -5
  86. package/dist/OpenAPICodeSample.d.ts +0 -19
  87. package/dist/OpenAPICodeSample.jsx +0 -230
  88. package/dist/OpenAPICodeSampleInteractive.d.ts +0 -14
  89. package/dist/OpenAPICodeSampleInteractive.jsx +0 -73
  90. package/dist/OpenAPICodeSampleSelector.d.ts +0 -14
  91. package/dist/OpenAPICodeSampleSelector.jsx +0 -44
  92. package/dist/OpenAPICopyButton.d.ts +0 -13
  93. package/dist/OpenAPICopyButton.jsx +0 -35
  94. package/dist/OpenAPIDisclosure.d.ts +0 -11
  95. package/dist/OpenAPIDisclosure.jsx +0 -30
  96. package/dist/OpenAPIDisclosureGroup.d.ts +0 -23
  97. package/dist/OpenAPIDisclosureGroup.jsx +0 -83
  98. package/dist/OpenAPIExample.d.ts +0 -16
  99. package/dist/OpenAPIExample.jsx +0 -36
  100. package/dist/OpenAPIMediaType.d.ts +0 -21
  101. package/dist/OpenAPIMediaType.jsx +0 -61
  102. package/dist/OpenAPIOperation.jsx +0 -25
  103. package/dist/OpenAPIOperationContext.jsx +0 -26
  104. package/dist/OpenAPIOperationDescription.d.ts +0 -9
  105. package/dist/OpenAPIOperationDescription.jsx +0 -22
  106. package/dist/OpenAPIOperationStability.d.ts +0 -9
  107. package/dist/OpenAPIOperationStability.jsx +0 -27
  108. package/dist/OpenAPIPath.d.ts +0 -18
  109. package/dist/OpenAPIPath.jsx +0 -55
  110. package/dist/OpenAPIPrefillContextProvider.jsx +0 -19
  111. package/dist/OpenAPIRequestBody.d.ts +0 -11
  112. package/dist/OpenAPIRequestBody.jsx +0 -28
  113. package/dist/OpenAPIRequestBodyHeaderType.d.ts +0 -8
  114. package/dist/OpenAPIRequestBodyHeaderType.jsx +0 -25
  115. package/dist/OpenAPIResponse.d.ts +0 -10
  116. package/dist/OpenAPIResponse.jsx +0 -57
  117. package/dist/OpenAPIResponseExample.d.ts +0 -9
  118. package/dist/OpenAPIResponseExample.jsx +0 -105
  119. package/dist/OpenAPIResponseExampleContent.d.ts +0 -22
  120. package/dist/OpenAPIResponseExampleContent.jsx +0 -60
  121. package/dist/OpenAPIResponses.d.ts +0 -9
  122. package/dist/OpenAPIResponses.jsx +0 -77
  123. package/dist/OpenAPISchema.d.ts +0 -27
  124. package/dist/OpenAPISchema.jsx +0 -400
  125. package/dist/OpenAPISchemaName.d.ts +0 -16
  126. package/dist/OpenAPISchemaName.jsx +0 -43
  127. package/dist/OpenAPISchemaServer.d.ts +0 -12
  128. package/dist/OpenAPISchemaServer.jsx +0 -8
  129. package/dist/OpenAPISecurities.d.ts +0 -9
  130. package/dist/OpenAPISecurities.jsx +0 -114
  131. package/dist/OpenAPISelect.d.ts +0 -22
  132. package/dist/OpenAPISelect.jsx +0 -44
  133. package/dist/OpenAPISpec.d.ts +0 -6
  134. package/dist/OpenAPISpec.jsx +0 -80
  135. package/dist/OpenAPITabs.d.ts +0 -26
  136. package/dist/OpenAPITabs.jsx +0 -109
  137. package/dist/OpenAPIWebhook.jsx +0 -23
  138. package/dist/OpenAPIWebhookExample.d.ts +0 -6
  139. package/dist/OpenAPIWebhookExample.jsx +0 -41
  140. package/dist/ScalarApiButton.d.ts +0 -14
  141. package/dist/ScalarApiButton.jsx +0 -81
  142. package/dist/StaticSection.d.ts +0 -13
  143. package/dist/StaticSection.jsx +0 -32
  144. package/dist/code-samples.d.ts +0 -17
  145. package/dist/common/OpenAPIColumnSpec.d.ts +0 -6
  146. package/dist/common/OpenAPIColumnSpec.jsx +0 -20
  147. package/dist/common/OpenAPIOperationDescription.d.ts +0 -6
  148. package/dist/common/OpenAPIOperationDescription.jsx +0 -19
  149. package/dist/common/OpenAPIStability.d.ts +0 -4
  150. package/dist/common/OpenAPIStability.jsx +0 -15
  151. package/dist/common/OpenAPISummary.d.ts +0 -6
  152. package/dist/common/OpenAPISummary.jsx +0 -30
  153. package/dist/contentTypeChecks.d.ts +0 -10
  154. package/dist/decycle.d.ts +0 -2
  155. package/dist/dereference.d.ts +0 -5
  156. package/dist/generateSchemaExample.d.ts +0 -45
  157. package/dist/getDisclosureLabel.d.ts +0 -7
  158. package/dist/getOrCreateStoreByKey.d.ts +0 -10
  159. package/dist/json2xml.d.ts +0 -4
  160. package/dist/schemas/OpenAPISchemaItem.d.ts +0 -7
  161. package/dist/schemas/OpenAPISchemaItem.jsx +0 -16
  162. package/dist/schemas/OpenAPISchemas.jsx +0 -59
  163. package/dist/schemas/index.d.ts +0 -2
  164. package/dist/schemas/index.js +0 -2
  165. package/dist/stringifyOpenAPI.d.ts +0 -4
  166. package/dist/translate.d.ts +0 -10
  167. package/dist/translate.jsx +0 -75
  168. package/dist/translations/de.d.ts +0 -43
  169. package/dist/translations/es.d.ts +0 -43
  170. package/dist/translations/fr.d.ts +0 -43
  171. package/dist/translations/ja.d.ts +0 -43
  172. package/dist/translations/nl.d.ts +0 -43
  173. package/dist/translations/no.d.ts +0 -43
  174. package/dist/translations/pt-br.d.ts +0 -43
  175. package/dist/translations/types.js +0 -1
  176. package/dist/translations/zh.d.ts +0 -43
  177. package/dist/tsconfig.build.tsbuildinfo +0 -1
  178. package/dist/types.js +0 -1
  179. package/dist/util/example.d.ts +0 -35
  180. package/dist/util/example.jsx +0 -103
  181. package/dist/util/server.d.ts +0 -9
  182. package/dist/util/tryit-prefill.d.ts +0 -20
  183. package/dist/utils.d.ts +0 -50
  184. package/src/InteractiveSection.tsx +0 -147
  185. package/src/Markdown.tsx +0 -12
  186. package/src/OpenAPICodeSample.tsx +0 -330
  187. package/src/OpenAPICodeSampleInteractive.tsx +0 -136
  188. package/src/OpenAPICodeSampleSelector.tsx +0 -94
  189. package/src/OpenAPICopyButton.tsx +0 -72
  190. package/src/OpenAPIDisclosure.tsx +0 -46
  191. package/src/OpenAPIDisclosureGroup.tsx +0 -158
  192. package/src/OpenAPIExample.tsx +0 -55
  193. package/src/OpenAPIMediaType.tsx +0 -139
  194. package/src/OpenAPIOperation.tsx +0 -35
  195. package/src/OpenAPIOperationContext.tsx +0 -45
  196. package/src/OpenAPIOperationDescription.tsx +0 -34
  197. package/src/OpenAPIOperationStability.tsx +0 -39
  198. package/src/OpenAPIPath.tsx +0 -90
  199. package/src/OpenAPIPrefillContextProvider.tsx +0 -40
  200. package/src/OpenAPIRequestBody.tsx +0 -54
  201. package/src/OpenAPIRequestBodyHeaderType.tsx +0 -36
  202. package/src/OpenAPIResponse.tsx +0 -82
  203. package/src/OpenAPIResponseExample.tsx +0 -151
  204. package/src/OpenAPIResponseExampleContent.tsx +0 -125
  205. package/src/OpenAPIResponses.tsx +0 -125
  206. package/src/OpenAPISchema.test.ts +0 -172
  207. package/src/OpenAPISchema.tsx +0 -654
  208. package/src/OpenAPISchemaName.tsx +0 -80
  209. package/src/OpenAPISchemaServer.tsx +0 -34
  210. package/src/OpenAPISecurities.tsx +0 -231
  211. package/src/OpenAPISelect.tsx +0 -96
  212. package/src/OpenAPISpec.tsx +0 -138
  213. package/src/OpenAPITabs.tsx +0 -147
  214. package/src/OpenAPIWebhook.tsx +0 -33
  215. package/src/OpenAPIWebhookExample.tsx +0 -60
  216. package/src/ScalarApiButton.tsx +0 -132
  217. package/src/StaticSection.tsx +0 -91
  218. package/src/__snapshots__/json2xml.test.ts.snap +0 -18
  219. package/src/code-samples.test.ts +0 -714
  220. package/src/code-samples.ts +0 -448
  221. package/src/common/OpenAPIColumnSpec.tsx +0 -31
  222. package/src/common/OpenAPIOperationDescription.tsx +0 -31
  223. package/src/common/OpenAPIStability.tsx +0 -23
  224. package/src/common/OpenAPISummary.tsx +0 -45
  225. package/src/contentTypeChecks.ts +0 -39
  226. package/src/context.ts +0 -99
  227. package/src/decycle.ts +0 -68
  228. package/src/dereference.ts +0 -29
  229. package/src/generateSchemaExample.test.ts +0 -1040
  230. package/src/generateSchemaExample.ts +0 -530
  231. package/src/getDisclosureLabel.ts +0 -25
  232. package/src/getOrCreateStoreByKey.ts +0 -33
  233. package/src/index.ts +0 -10
  234. package/src/json2xml.test.ts +0 -46
  235. package/src/json2xml.ts +0 -8
  236. package/src/resolveOpenAPIOperation.test.ts +0 -177
  237. package/src/resolveOpenAPIOperation.ts +0 -151
  238. package/src/resolveOpenAPIWebhook.ts +0 -99
  239. package/src/schemas/OpenAPISchemaItem.tsx +0 -34
  240. package/src/schemas/OpenAPISchemas.tsx +0 -98
  241. package/src/schemas/index.ts +0 -2
  242. package/src/schemas/resolveOpenAPISchemas.test.ts +0 -174
  243. package/src/schemas/resolveOpenAPISchemas.ts +0 -28
  244. package/src/stringifyOpenAPI.ts +0 -25
  245. package/src/translate.tsx +0 -80
  246. package/src/translations/de.ts +0 -43
  247. package/src/translations/en.ts +0 -43
  248. package/src/translations/es.ts +0 -43
  249. package/src/translations/fr.ts +0 -43
  250. package/src/translations/index.ts +0 -33
  251. package/src/translations/ja.ts +0 -43
  252. package/src/translations/nl.ts +0 -43
  253. package/src/translations/no.ts +0 -43
  254. package/src/translations/pt-br.ts +0 -43
  255. package/src/translations/types.ts +0 -7
  256. package/src/translations/zh.ts +0 -43
  257. package/src/types.ts +0 -46
  258. package/src/util/example.tsx +0 -129
  259. package/src/util/server.test.ts +0 -58
  260. package/src/util/server.ts +0 -47
  261. package/src/util/tryit-prefill.test.ts +0 -311
  262. package/src/util/tryit-prefill.ts +0 -160
  263. package/src/utils.ts +0 -255
@@ -1,80 +0,0 @@
1
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
- import type React from 'react';
3
- import type { OpenAPIClientContext } from './context';
4
- import { t, tString } from './translate';
5
-
6
- interface OpenAPISchemaNameProps {
7
- schema?: OpenAPIV3.SchemaObject;
8
- propertyName?: string | React.JSX.Element;
9
- required?: boolean | null;
10
- type?: string;
11
- context: OpenAPIClientContext;
12
- }
13
-
14
- /**
15
- * Display the schema name row.
16
- * It includes the property name, type, required and deprecated status.
17
- */
18
- export function OpenAPISchemaName(props: OpenAPISchemaNameProps) {
19
- const { schema, type, propertyName, required, context } = props;
20
-
21
- const additionalItems = schema && getAdditionalItems(schema, context);
22
-
23
- return (
24
- <span className="openapi-schema-name">
25
- {propertyName ? (
26
- <span data-deprecated={schema?.deprecated} className="openapi-schema-propertyname">
27
- {propertyName}
28
- </span>
29
- ) : null}
30
- {type || additionalItems ? (
31
- <span>
32
- {type ? <span className="openapi-schema-type">{type}</span> : null}
33
- {additionalItems ? (
34
- <span className="openapi-schema-type">{additionalItems}</span>
35
- ) : null}
36
- </span>
37
- ) : null}
38
- {schema?.readOnly ? (
39
- <span className="openapi-schema-readonly">
40
- {t(context.translation, 'read_only')}
41
- </span>
42
- ) : null}
43
- {schema?.writeOnly ? (
44
- <span className="openapi-schema-writeonly">
45
- {t(context.translation, 'write_only')}
46
- </span>
47
- ) : null}
48
- {required === null ? null : required ? (
49
- <span className="openapi-schema-required">
50
- {t(context.translation, 'required')}
51
- </span>
52
- ) : (
53
- <span className="openapi-schema-optional">
54
- {t(context.translation, 'optional')}
55
- </span>
56
- )}
57
- {schema?.deprecated ? (
58
- <span className="openapi-deprecated">{t(context.translation, 'deprecated')}</span>
59
- ) : null}
60
- </span>
61
- );
62
- }
63
-
64
- function getAdditionalItems(schema: OpenAPIV3.SchemaObject, context: OpenAPIClientContext): string {
65
- let additionalItems = '';
66
-
67
- if (schema.minimum || schema.minLength || schema.minItems) {
68
- additionalItems += ` · ${tString(context.translation, 'min').toLowerCase()}: ${schema.minimum || schema.minLength || schema.minItems}`;
69
- }
70
-
71
- if (schema.maximum || schema.maxLength || schema.maxItems) {
72
- additionalItems += ` · ${tString(context.translation, 'max').toLowerCase()}: ${schema.maximum || schema.maxLength || schema.maxItems}`;
73
- }
74
-
75
- if (schema.nullable) {
76
- additionalItems = ` | ${tString(context.translation, 'nullable').toLowerCase()}`;
77
- }
78
-
79
- return additionalItems;
80
- }
@@ -1,34 +0,0 @@
1
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
- import {
3
- OpenAPIRootSchemaFromServer,
4
- OpenAPISchemaPropertiesFromServer,
5
- type OpenAPISchemaPropertyEntry,
6
- } from './OpenAPISchema';
7
- import type { OpenAPIClientContext } from './context';
8
- import { decycle } from './decycle';
9
-
10
- export function OpenAPISchemaProperties(props: {
11
- id?: string;
12
- properties: OpenAPISchemaPropertyEntry[];
13
- context: OpenAPIClientContext;
14
- }) {
15
- return (
16
- <OpenAPISchemaPropertiesFromServer
17
- id={props.id}
18
- properties={JSON.stringify(props.properties, decycle())}
19
- context={props.context}
20
- />
21
- );
22
- }
23
-
24
- export function OpenAPIRootSchema(props: {
25
- schema: OpenAPIV3.SchemaObject;
26
- context: OpenAPIClientContext;
27
- }) {
28
- return (
29
- <OpenAPIRootSchemaFromServer
30
- schema={JSON.stringify(props.schema, decycle())}
31
- context={props.context}
32
- />
33
- );
34
- }
@@ -1,231 +0,0 @@
1
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
- import { InteractiveSection } from './InteractiveSection';
3
- import { Markdown } from './Markdown';
4
- import { OpenAPICopyButton } from './OpenAPICopyButton';
5
- import { OpenAPISchemaName } from './OpenAPISchemaName';
6
- import type { OpenAPIClientContext } from './context';
7
- import { t } from './translate';
8
- import type { OpenAPIOperationData, OpenAPISecurityWithRequired } from './types';
9
- import { createStateKey, resolveDescription } from './utils';
10
-
11
- /**
12
- * Present securities authorization that can be used for this operation.
13
- */
14
- export function OpenAPISecurities(props: {
15
- securities: OpenAPIOperationData['securities'];
16
- context: OpenAPIClientContext;
17
- }) {
18
- const { securities, context } = props;
19
-
20
- if (securities.length === 0) {
21
- return null;
22
- }
23
-
24
- return (
25
- <InteractiveSection
26
- header={t(context.translation, 'authorizations')}
27
- stateKey={createStateKey('securities', context.blockKey)}
28
- toggeable
29
- defaultOpened={false}
30
- toggleIcon={context.icons.chevronRight}
31
- selectIcon={context.icons.chevronDown}
32
- className="openapi-securities"
33
- tabs={securities.map(([key, security]) => {
34
- const description = resolveDescription(security);
35
- return {
36
- key: key,
37
- label: key,
38
- body: (
39
- <div className="openapi-schema">
40
- <div className="openapi-schema-presentation">
41
- {getLabelForType(security, context)}
42
-
43
- {description ? (
44
- <Markdown
45
- source={description}
46
- className="openapi-securities-description"
47
- />
48
- ) : null}
49
- </div>
50
- </div>
51
- ),
52
- };
53
- })}
54
- />
55
- );
56
- }
57
-
58
- function getLabelForType(security: OpenAPISecurityWithRequired, context: OpenAPIClientContext) {
59
- switch (security.type) {
60
- case 'apiKey':
61
- return (
62
- <OpenAPISchemaName
63
- context={context}
64
- propertyName={security.name ?? 'apiKey'}
65
- type="string"
66
- required={security.required}
67
- />
68
- );
69
- case 'http':
70
- if (security.scheme === 'basic') {
71
- return (
72
- <OpenAPISchemaName
73
- context={context}
74
- propertyName="Authorization"
75
- type="string"
76
- required={security.required}
77
- />
78
- );
79
- }
80
-
81
- if (security.scheme === 'bearer') {
82
- const description = resolveDescription(security);
83
- return (
84
- <>
85
- <OpenAPISchemaName
86
- context={context}
87
- propertyName="Authorization"
88
- type="string"
89
- required={security.required}
90
- />
91
- {/** Show a default description if none is provided */}
92
- {!description ? (
93
- <Markdown
94
- source={`Bearer authentication header of the form Bearer ${'&lt;token&gt;'}.`}
95
- className="openapi-securities-description"
96
- />
97
- ) : null}
98
- </>
99
- );
100
- }
101
-
102
- return (
103
- <OpenAPISchemaName
104
- context={context}
105
- propertyName="HTTP"
106
- required={security.required}
107
- />
108
- );
109
- case 'oauth2':
110
- return <OpenAPISchemaOAuth2Flows context={context} security={security} />;
111
- case 'openIdConnect':
112
- return (
113
- <OpenAPISchemaName
114
- context={context}
115
- propertyName="OpenID Connect"
116
- required={security.required}
117
- />
118
- );
119
- default:
120
- // @ts-ignore
121
- return security.type;
122
- }
123
- }
124
-
125
- function OpenAPISchemaOAuth2Flows(props: {
126
- context: OpenAPIClientContext;
127
- security: OpenAPIV3.OAuth2SecurityScheme & { required?: boolean };
128
- }) {
129
- const { context, security } = props;
130
-
131
- const flows = Object.entries(security.flows ?? {});
132
-
133
- return (
134
- <div className="openapi-securities-oauth-flows">
135
- {flows.map(([name, flow], index) => (
136
- <OpenAPISchemaOAuth2Item
137
- key={index}
138
- flow={flow}
139
- name={name}
140
- context={context}
141
- security={security}
142
- />
143
- ))}
144
- </div>
145
- );
146
- }
147
-
148
- function OpenAPISchemaOAuth2Item(props: {
149
- flow: NonNullable<OpenAPIV3.OAuth2SecurityScheme['flows']>[keyof NonNullable<
150
- OpenAPIV3.OAuth2SecurityScheme['flows']
151
- >];
152
- name: string;
153
- context: OpenAPIClientContext;
154
- security: OpenAPIV3.OAuth2SecurityScheme & { required?: boolean };
155
- }) {
156
- const { flow, context, security, name } = props;
157
-
158
- if (!flow) {
159
- return null;
160
- }
161
-
162
- const scopes = Object.entries(flow?.scopes ?? {});
163
-
164
- return (
165
- <div>
166
- <OpenAPISchemaName
167
- context={context}
168
- propertyName="OAuth2"
169
- type={name}
170
- required={security.required}
171
- />
172
- <div className="openapi-securities-oauth-content openapi-markdown">
173
- {security.description ? <Markdown source={security.description} /> : null}
174
- {'authorizationUrl' in flow && flow.authorizationUrl ? (
175
- <span>
176
- Authorization URL:{' '}
177
- <OpenAPICopyButton
178
- value={flow.authorizationUrl}
179
- context={context}
180
- className="openapi-securities-url"
181
- withTooltip
182
- >
183
- {flow.authorizationUrl}
184
- </OpenAPICopyButton>
185
- </span>
186
- ) : null}
187
- {'tokenUrl' in flow && flow.tokenUrl ? (
188
- <span>
189
- Token URL:{' '}
190
- <OpenAPICopyButton
191
- value={flow.tokenUrl}
192
- context={context}
193
- className="openapi-securities-url"
194
- withTooltip
195
- >
196
- {flow.tokenUrl}
197
- </OpenAPICopyButton>
198
- </span>
199
- ) : null}
200
- {'refreshUrl' in flow && flow.refreshUrl ? (
201
- <span>
202
- Refresh URL:{' '}
203
- <OpenAPICopyButton
204
- value={flow.refreshUrl}
205
- context={context}
206
- className="openapi-securities-url"
207
- withTooltip
208
- >
209
- {flow.refreshUrl}
210
- </OpenAPICopyButton>
211
- </span>
212
- ) : null}
213
- {scopes.length ? (
214
- <div>
215
- {t(context.translation, 'available_scopes')}:{' '}
216
- <ul>
217
- {scopes.map(([key, value]) => (
218
- <li key={key}>
219
- <OpenAPICopyButton value={key} context={context} withTooltip>
220
- <code>{key}</code>
221
- </OpenAPICopyButton>
222
- : {value}
223
- </li>
224
- ))}
225
- </ul>
226
- </div>
227
- ) : null}
228
- </div>
229
- </div>
230
- );
231
- }
@@ -1,96 +0,0 @@
1
- 'use client';
2
-
3
- import clsx from 'clsx';
4
- import { useCallback } from 'react';
5
- import {
6
- Button,
7
- type Key,
8
- ListBox,
9
- ListBoxItem,
10
- type ListBoxItemProps,
11
- Popover,
12
- type PopoverProps,
13
- Select,
14
- type SelectProps,
15
- SelectValue,
16
- } from 'react-aria-components';
17
- import { useStore } from 'zustand';
18
- import { getOrCreateStoreByKey } from './getOrCreateStoreByKey';
19
-
20
- export type OpenAPISelectItem = {
21
- key: Key;
22
- label: string | React.ReactNode;
23
- };
24
-
25
- interface OpenAPISelectProps<T extends OpenAPISelectItem> extends Omit<SelectProps<T>, 'children'> {
26
- items: T[];
27
- children: React.ReactNode | ((item: T) => React.ReactNode);
28
- placement?: PopoverProps['placement'];
29
- stateKey?: string;
30
- /**
31
- * Icon to display in the select button.
32
- */
33
- icon?: React.ReactNode;
34
- }
35
-
36
- export function useSelectState(stateKey = 'select-state', initialKey: Key = 'default') {
37
- const store = useStore(getOrCreateStoreByKey(stateKey, initialKey));
38
- return {
39
- key: store.key,
40
- setKey: useCallback((key: Key | null) => store.setKey(key), [store.setKey]),
41
- };
42
- }
43
-
44
- export function OpenAPISelect<T extends OpenAPISelectItem>(props: OpenAPISelectProps<T>) {
45
- const {
46
- icon = '▼',
47
- items,
48
- children,
49
- className,
50
- placement,
51
- stateKey,
52
- selectedKey,
53
- onSelectionChange,
54
- } = props;
55
-
56
- const state = useSelectState(stateKey, items[0]?.key);
57
-
58
- const selected = items.find((item) => item.key === state.key) || items[0];
59
-
60
- return (
61
- <Select
62
- aria-label="OpenAPI Select"
63
- {...props}
64
- selectedKey={selectedKey || selected?.key}
65
- onSelectionChange={(key) => {
66
- onSelectionChange?.(key);
67
- state.setKey(key);
68
- }}
69
- className={clsx('openapi-select', className)}
70
- >
71
- <Button>
72
- <SelectValue />
73
- {icon}
74
- </Button>
75
- <Popover placement={placement} className="openapi-select-popover">
76
- <ListBox className="openapi-select-listbox" items={items}>
77
- {children}
78
- </ListBox>
79
- </Popover>
80
- </Select>
81
- );
82
- }
83
-
84
- export function OpenAPISelectItem(props: ListBoxItemProps) {
85
- return (
86
- <ListBoxItem
87
- {...props}
88
- className={({ isFocused, isSelected }) =>
89
- clsx('openapi-select-item', {
90
- 'openapi-select-item-focused': isFocused,
91
- 'openapi-select-item-selected': isSelected,
92
- })
93
- }
94
- />
95
- );
96
- }
@@ -1,138 +0,0 @@
1
- import type { OpenAPI } from '@gitbook/openapi-parser';
2
-
3
- import { OpenAPIRequestBody } from './OpenAPIRequestBody';
4
- import { OpenAPIResponses } from './OpenAPIResponses';
5
- import { OpenAPISchemaProperties } from './OpenAPISchemaServer';
6
- import { OpenAPISecurities } from './OpenAPISecurities';
7
- import { StaticSection } from './StaticSection';
8
- import type { OpenAPIClientContext } from './context';
9
- import { tString } from './translate';
10
- import type { OpenAPIOperationData, OpenAPIWebhookData } from './types';
11
- import { parameterToProperty } from './utils';
12
-
13
- export function OpenAPISpec(props: {
14
- data: OpenAPIOperationData | OpenAPIWebhookData;
15
- context: OpenAPIClientContext;
16
- }) {
17
- const { data, context } = props;
18
-
19
- const { operation } = data;
20
-
21
- const parameters = deduplicateParameters(operation.parameters ?? []);
22
- const parameterGroups = groupParameters(parameters, context);
23
-
24
- const securities = 'securities' in data ? data.securities : [];
25
-
26
- return (
27
- <>
28
- {securities.length > 0 ? (
29
- <OpenAPISecurities key="securities" securities={securities} context={context} />
30
- ) : null}
31
-
32
- {parameterGroups.map((group) => {
33
- return (
34
- <StaticSection
35
- key={`parameter-${group.key}`}
36
- className="openapi-parameters"
37
- header={group.label}
38
- >
39
- <OpenAPISchemaProperties
40
- properties={group.parameters.map(parameterToProperty)}
41
- context={context}
42
- />
43
- </StaticSection>
44
- );
45
- })}
46
-
47
- {operation.requestBody ? (
48
- <OpenAPIRequestBody
49
- key="body"
50
- requestBody={operation.requestBody}
51
- context={context}
52
- data={data}
53
- />
54
- ) : null}
55
- {operation.responses ? (
56
- <OpenAPIResponses
57
- key="responses"
58
- responses={operation.responses}
59
- context={context}
60
- />
61
- ) : null}
62
- </>
63
- );
64
- }
65
-
66
- function groupParameters(
67
- parameters: OpenAPI.Parameters,
68
- context: OpenAPIClientContext
69
- ): Array<{
70
- key: string;
71
- label: string;
72
- parameters: OpenAPI.Parameters;
73
- }> {
74
- const sorted = ['path', 'query', 'header'];
75
-
76
- const groups: Array<{
77
- key: string;
78
- label: string;
79
- parameters: OpenAPI.Parameters;
80
- }> = [];
81
-
82
- parameters
83
- .filter((parameter) => parameter.in)
84
- .forEach((parameter) => {
85
- const key = parameter.in;
86
- const label = getParameterGroupName(parameter.in, context);
87
- const group = groups.find((group) => group.key === key);
88
- if (group) {
89
- group.parameters.push(parameter);
90
- } else {
91
- groups.push({
92
- key,
93
- label,
94
- parameters: [parameter],
95
- });
96
- }
97
- });
98
-
99
- groups.sort((a, b) => sorted.indexOf(a.key) - sorted.indexOf(b.key));
100
-
101
- return groups;
102
- }
103
-
104
- function getParameterGroupName(paramIn: string, context: OpenAPIClientContext): string {
105
- switch (paramIn) {
106
- case 'path':
107
- return tString(context.translation, 'path_parameters');
108
- case 'query':
109
- return tString(context.translation, 'query_parameters');
110
- case 'header':
111
- return tString(context.translation, 'header_parameters');
112
- default:
113
- return paramIn;
114
- }
115
- }
116
-
117
- /** Deduplicate parameters by name and in.
118
- * Some specs have both parameters define at path and operation level.
119
- * We only want to display one of them.
120
- * Parameters can have the wrong type (object instead of array) sometimes, we just return an empty array in that case.
121
- */
122
- function deduplicateParameters(parameters: OpenAPI.Parameters): OpenAPI.Parameters {
123
- const seen = new Set();
124
-
125
- return Array.isArray(parameters)
126
- ? parameters.filter((param) => {
127
- const key = `${param.name}:${param.in}`;
128
-
129
- if (seen.has(key)) {
130
- return false;
131
- }
132
-
133
- seen.add(key);
134
-
135
- return true;
136
- })
137
- : [];
138
- }