@gitbook/react-openapi 1.1.10 → 1.2.0

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 (164) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/InteractiveSection.d.ts +4 -0
  3. package/dist/InteractiveSection.jsx +11 -11
  4. package/dist/OpenAPICodeSample.d.ts +2 -1
  5. package/dist/OpenAPICodeSample.jsx +6 -5
  6. package/dist/OpenAPICodeSampleInteractive.d.ts +3 -0
  7. package/dist/OpenAPICodeSampleInteractive.jsx +19 -43
  8. package/dist/OpenAPICodeSampleSelector.d.ts +3 -4
  9. package/dist/OpenAPICodeSampleSelector.jsx +6 -11
  10. package/dist/OpenAPICopyButton.d.ts +2 -0
  11. package/dist/OpenAPICopyButton.jsx +5 -2
  12. package/dist/OpenAPIDisclosure.d.ts +4 -3
  13. package/dist/OpenAPIDisclosure.jsx +8 -11
  14. package/dist/OpenAPIDisclosureGroup.d.ts +7 -3
  15. package/dist/OpenAPIDisclosureGroup.jsx +18 -18
  16. package/dist/OpenAPIExample.d.ts +4 -22
  17. package/dist/OpenAPIExample.jsx +5 -72
  18. package/dist/OpenAPIMediaType.d.ts +21 -0
  19. package/dist/OpenAPIMediaType.jsx +61 -0
  20. package/dist/OpenAPIOperation.d.ts +3 -2
  21. package/dist/OpenAPIOperation.jsx +9 -68
  22. package/dist/OpenAPIOperationDescription.d.ts +9 -0
  23. package/dist/OpenAPIOperationDescription.jsx +22 -0
  24. package/dist/OpenAPIOperationStability.d.ts +9 -0
  25. package/dist/OpenAPIOperationStability.jsx +27 -0
  26. package/dist/OpenAPIPath.d.ts +2 -0
  27. package/dist/OpenAPIPath.jsx +3 -2
  28. package/dist/OpenAPIRequestBody.d.ts +3 -1
  29. package/dist/OpenAPIRequestBody.jsx +4 -3
  30. package/dist/OpenAPIResponse.d.ts +1 -1
  31. package/dist/OpenAPIResponse.jsx +1 -1
  32. package/dist/OpenAPIResponseExample.d.ts +3 -2
  33. package/dist/OpenAPIResponseExample.jsx +24 -63
  34. package/dist/OpenAPIResponseExampleContent.d.ts +19 -0
  35. package/dist/OpenAPIResponseExampleContent.jsx +57 -0
  36. package/dist/OpenAPIResponses.d.ts +1 -1
  37. package/dist/OpenAPIResponses.jsx +49 -36
  38. package/dist/OpenAPISchema.d.ts +1 -1
  39. package/dist/OpenAPISchema.jsx +103 -15
  40. package/dist/OpenAPISchemaName.d.ts +2 -0
  41. package/dist/OpenAPISchemaName.jsx +19 -10
  42. package/dist/OpenAPISchemaServer.d.ts +1 -1
  43. package/dist/OpenAPISecurities.d.ts +2 -1
  44. package/dist/OpenAPISecurities.jsx +11 -10
  45. package/dist/OpenAPISelect.d.ts +10 -3
  46. package/dist/OpenAPISelect.jsx +20 -9
  47. package/dist/OpenAPISpec.d.ts +3 -2
  48. package/dist/OpenAPISpec.jsx +11 -9
  49. package/dist/OpenAPIWebhook.d.ts +10 -0
  50. package/dist/OpenAPIWebhook.jsx +23 -0
  51. package/dist/OpenAPIWebhookExample.d.ts +6 -0
  52. package/dist/OpenAPIWebhookExample.jsx +41 -0
  53. package/dist/ScalarApiButton.d.ts +2 -0
  54. package/dist/ScalarApiButton.jsx +4 -3
  55. package/dist/StaticSection.d.ts +4 -1
  56. package/dist/StaticSection.jsx +13 -4
  57. package/dist/code-samples.js +57 -39
  58. package/dist/common/OpenAPIColumnSpec.d.ts +6 -0
  59. package/dist/common/OpenAPIColumnSpec.jsx +20 -0
  60. package/dist/common/OpenAPIOperationDescription.d.ts +6 -0
  61. package/dist/common/OpenAPIOperationDescription.jsx +19 -0
  62. package/dist/common/OpenAPIStability.d.ts +4 -0
  63. package/dist/common/OpenAPIStability.jsx +15 -0
  64. package/dist/common/OpenAPISummary.d.ts +6 -0
  65. package/dist/common/OpenAPISummary.jsx +30 -0
  66. package/dist/context.d.ts +23 -2
  67. package/dist/context.js +32 -0
  68. package/dist/getOrCreateStoreByKey.d.ts +1 -1
  69. package/dist/getOrCreateStoreByKey.js +0 -1
  70. package/dist/index.d.ts +5 -1
  71. package/dist/index.js +3 -0
  72. package/dist/resolveOpenAPIWebhook.d.ts +11 -0
  73. package/dist/resolveOpenAPIWebhook.js +127 -0
  74. package/dist/schemas/OpenAPISchemas.d.ts +2 -2
  75. package/dist/schemas/OpenAPISchemas.jsx +19 -23
  76. package/dist/stringifyOpenAPI.d.ts +1 -1
  77. package/dist/stringifyOpenAPI.js +6 -3
  78. package/dist/translate.d.ts +10 -0
  79. package/dist/translate.jsx +75 -0
  80. package/dist/translations/de.d.ts +37 -0
  81. package/dist/translations/de.js +37 -0
  82. package/dist/translations/en.d.ts +37 -0
  83. package/dist/translations/en.js +37 -0
  84. package/dist/translations/es.d.ts +37 -0
  85. package/dist/translations/es.js +37 -0
  86. package/dist/translations/fr.d.ts +37 -0
  87. package/dist/translations/fr.js +37 -0
  88. package/dist/translations/index.d.ts +341 -0
  89. package/dist/translations/index.js +27 -0
  90. package/dist/translations/ja.d.ts +37 -0
  91. package/dist/translations/ja.js +37 -0
  92. package/dist/translations/nl.d.ts +37 -0
  93. package/dist/translations/nl.js +37 -0
  94. package/dist/translations/no.d.ts +37 -0
  95. package/dist/translations/no.js +37 -0
  96. package/dist/translations/pt-br.d.ts +37 -0
  97. package/dist/translations/pt-br.js +37 -0
  98. package/dist/translations/types.d.ts +5 -0
  99. package/dist/translations/types.js +1 -0
  100. package/dist/translations/zh.d.ts +37 -0
  101. package/dist/translations/zh.js +37 -0
  102. package/dist/tsconfig.build.tsbuildinfo +1 -1
  103. package/dist/types.d.ts +8 -50
  104. package/dist/util/example.d.ts +35 -0
  105. package/dist/util/example.jsx +103 -0
  106. package/dist/utils.d.ts +18 -0
  107. package/dist/utils.js +57 -0
  108. package/package.json +3 -3
  109. package/src/InteractiveSection.tsx +16 -14
  110. package/src/OpenAPICodeSample.tsx +22 -4
  111. package/src/OpenAPICodeSampleInteractive.tsx +38 -58
  112. package/src/OpenAPICodeSampleSelector.tsx +19 -12
  113. package/src/OpenAPICopyButton.tsx +7 -2
  114. package/src/OpenAPIDisclosure.tsx +20 -22
  115. package/src/OpenAPIDisclosureGroup.tsx +40 -22
  116. package/src/OpenAPIExample.tsx +8 -82
  117. package/src/OpenAPIMediaType.tsx +139 -0
  118. package/src/OpenAPIOperation.tsx +11 -100
  119. package/src/OpenAPIOperationDescription.tsx +34 -0
  120. package/src/OpenAPIOperationStability.tsx +39 -0
  121. package/src/OpenAPIPath.tsx +4 -1
  122. package/src/OpenAPIRequestBody.tsx +9 -4
  123. package/src/OpenAPIResponse.tsx +2 -2
  124. package/src/OpenAPIResponseExample.tsx +39 -108
  125. package/src/OpenAPIResponseExampleContent.tsx +123 -0
  126. package/src/OpenAPIResponses.tsx +83 -62
  127. package/src/OpenAPISchema.test.ts +80 -0
  128. package/src/OpenAPISchema.tsx +123 -16
  129. package/src/OpenAPISchemaName.tsx +26 -11
  130. package/src/OpenAPISchemaServer.tsx +1 -1
  131. package/src/OpenAPISecurities.tsx +33 -12
  132. package/src/OpenAPISelect.tsx +42 -16
  133. package/src/OpenAPISpec.tsx +21 -10
  134. package/src/OpenAPIWebhook.tsx +33 -0
  135. package/src/OpenAPIWebhookExample.tsx +60 -0
  136. package/src/ScalarApiButton.tsx +6 -6
  137. package/src/StaticSection.tsx +37 -5
  138. package/src/code-samples.test.ts +3 -1
  139. package/src/code-samples.ts +67 -54
  140. package/src/common/OpenAPIColumnSpec.tsx +31 -0
  141. package/src/common/OpenAPIOperationDescription.tsx +31 -0
  142. package/src/common/OpenAPIStability.tsx +23 -0
  143. package/src/common/OpenAPISummary.tsx +45 -0
  144. package/src/context.ts +37 -2
  145. package/src/getOrCreateStoreByKey.ts +1 -3
  146. package/src/index.ts +5 -1
  147. package/src/resolveOpenAPIWebhook.ts +99 -0
  148. package/src/schemas/OpenAPISchemas.tsx +34 -34
  149. package/src/stringifyOpenAPI.ts +11 -3
  150. package/src/translate.tsx +80 -0
  151. package/src/translations/de.ts +37 -0
  152. package/src/translations/en.ts +37 -0
  153. package/src/translations/es.ts +37 -0
  154. package/src/translations/fr.ts +37 -0
  155. package/src/translations/index.ts +33 -0
  156. package/src/translations/ja.ts +37 -0
  157. package/src/translations/nl.ts +37 -0
  158. package/src/translations/no.ts +37 -0
  159. package/src/translations/pt-br.ts +37 -0
  160. package/src/translations/types.ts +7 -0
  161. package/src/translations/zh.ts +37 -0
  162. package/src/types.ts +11 -53
  163. package/src/util/example.tsx +129 -0
  164. package/src/utils.ts +67 -0
@@ -0,0 +1,37 @@
1
+ export const zh = {
2
+ required: '必填',
3
+ deprecated: '已弃用',
4
+ deprecated_and_sunset_on: '此操作已弃用,将于 ${1} 停止使用。',
5
+ stability_experimental: '实验性',
6
+ stability_alpha: 'Alpha',
7
+ stability_beta: 'Beta',
8
+ copy_to_clipboard: '复制到剪贴板',
9
+ copied: '已复制',
10
+ no_content: '无内容',
11
+ unresolved_reference: '未解析的引用',
12
+ circular_reference: '循环引用',
13
+ read_only: '只读',
14
+ write_only: '只写',
15
+ optional: '可选',
16
+ min: '最小值',
17
+ max: '最大值',
18
+ nullable: '可为 null',
19
+ body: '请求体',
20
+ payload: '有效载荷',
21
+ headers: '头部信息',
22
+ authorizations: '授权',
23
+ responses: '响应',
24
+ path_parameters: '路径参数',
25
+ query_parameters: '查询参数',
26
+ header_parameters: '头参数',
27
+ attributes: '属性',
28
+ test_it: '测试一下',
29
+ information: '信息',
30
+ success: '成功',
31
+ redirect: '重定向',
32
+ error: '错误',
33
+ show: '显示',
34
+ hide: '隐藏',
35
+ available_items: '可用项',
36
+ child_attributes: '子属性',
37
+ };
package/src/types.ts CHANGED
@@ -4,59 +4,6 @@ import type {
4
4
  OpenAPIV3,
5
5
  } from '@gitbook/openapi-parser';
6
6
 
7
- export interface OpenAPIClientContext {
8
- /**
9
- * Icons used in the block.
10
- */
11
- icons: {
12
- chevronDown: React.ReactNode;
13
- chevronRight: React.ReactNode;
14
- plus: React.ReactNode;
15
- };
16
-
17
- /**
18
- * Force all sections to be opened by default.
19
- * @default false
20
- */
21
- defaultInteractiveOpened?: boolean;
22
-
23
- /**
24
- * The key of the block
25
- */
26
- blockKey?: string;
27
-
28
- /**
29
- * Optional id attached to the heading and used as an anchor.
30
- */
31
- id?: string;
32
- }
33
-
34
- export interface OpenAPIContext extends OpenAPIClientContext {
35
- /**
36
- * Render a code block.
37
- */
38
- renderCodeBlock: (props: { code: string; syntax: string }) => React.ReactNode;
39
-
40
- /**
41
- * Render the heading of the operation.
42
- */
43
- renderHeading: (props: {
44
- deprecated?: boolean;
45
- title: string;
46
- stability?: string;
47
- }) => React.ReactNode;
48
-
49
- /**
50
- * Render the document of the operation.
51
- */
52
- renderDocument: (props: { document: object }) => React.ReactNode;
53
-
54
- /**
55
- * Specification URL.
56
- */
57
- specUrl: string;
58
- }
59
-
60
7
  export type OpenAPISecurityWithRequired = OpenAPIV3.SecuritySchemeObject & { required?: boolean };
61
8
 
62
9
  export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
@@ -72,3 +19,14 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
72
19
  /** Securities that should be used for this operation */
73
20
  securities: [string, OpenAPISecurityWithRequired][];
74
21
  }
22
+
23
+ export interface OpenAPIWebhookData extends OpenAPICustomSpecProperties {
24
+ name: string;
25
+ method: string;
26
+
27
+ /** Servers to be used for this operation */
28
+ servers: OpenAPIV3.ServerObject[];
29
+
30
+ /** Spec of the webhook */
31
+ operation: OpenAPIV3.OperationObject<OpenAPICustomOperationProperties>;
32
+ }
@@ -0,0 +1,129 @@
1
+ import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
+ import { OpenAPIExample } from '../OpenAPIExample';
3
+ import type { OpenAPIContext } from '../context';
4
+ import { generateSchemaExample } from '../generateSchemaExample';
5
+ import { tString } from '../translate';
6
+ import { checkIsReference } from '../utils';
7
+
8
+ /**
9
+ * Generate an example from a reference object.
10
+ */
11
+ export function getExampleFromReference(
12
+ ref: OpenAPIV3.ReferenceObject,
13
+ context: OpenAPIContext
14
+ ): OpenAPIV3.ExampleObject {
15
+ return {
16
+ summary: tString(context.translation, 'unresolved_reference'),
17
+ value: { $ref: ref.$ref },
18
+ };
19
+ }
20
+
21
+ /**
22
+ * Get examples from a media type object.
23
+ */
24
+ export function getExamplesFromMediaTypeObject(args: {
25
+ mediaType: string;
26
+ mediaTypeObject: OpenAPIV3.MediaTypeObject;
27
+ context: OpenAPIContext;
28
+ }): { key: string; example: OpenAPIV3.ExampleObject }[] {
29
+ const { mediaTypeObject, mediaType, context } = args;
30
+ if (mediaTypeObject.examples) {
31
+ return Object.entries(mediaTypeObject.examples).map(([key, example]) => {
32
+ return {
33
+ key,
34
+ example: checkIsReference(example)
35
+ ? getExampleFromReference(example, context)
36
+ : example,
37
+ };
38
+ });
39
+ }
40
+
41
+ if (mediaTypeObject.example) {
42
+ return [{ key: 'default', example: { value: mediaTypeObject.example } }];
43
+ }
44
+
45
+ if (mediaTypeObject.schema) {
46
+ if (mediaType === 'application/xml') {
47
+ // @TODO normally we should use the name of the schema but we don't have it
48
+ // fix it when we got the reference name
49
+ const root = mediaTypeObject.schema.xml?.name ?? 'object';
50
+ return [
51
+ {
52
+ key: 'default',
53
+ example: {
54
+ value: {
55
+ [root]: generateSchemaExample(mediaTypeObject.schema, {
56
+ xml: mediaType === 'application/xml',
57
+ mode: 'read',
58
+ }),
59
+ },
60
+ },
61
+ },
62
+ ];
63
+ }
64
+ return [
65
+ {
66
+ key: 'default',
67
+ example: {
68
+ value: generateSchemaExample(mediaTypeObject.schema, {
69
+ mode: 'read',
70
+ }),
71
+ },
72
+ },
73
+ ];
74
+ }
75
+ return [];
76
+ }
77
+
78
+ /**
79
+ * Get example from a schema object.
80
+ */
81
+ export function getExampleFromSchema(args: {
82
+ schema: OpenAPIV3.SchemaObject;
83
+ }): OpenAPIV3.ExampleObject {
84
+ const { schema } = args;
85
+
86
+ if (schema.example) {
87
+ return { value: schema.example };
88
+ }
89
+
90
+ return { value: generateSchemaExample(schema, { mode: 'read' }) };
91
+ }
92
+
93
+ /**
94
+ * Get the examples from a media type object.
95
+ */
96
+ export function getExamples(props: {
97
+ mediaTypeObject: OpenAPIV3.MediaTypeObject;
98
+ mediaType: string;
99
+ context: OpenAPIContext;
100
+ }) {
101
+ const { mediaTypeObject, mediaType, context } = props;
102
+ const examples = getExamplesFromMediaTypeObject({ mediaTypeObject, mediaType, context });
103
+ const syntax = getSyntaxFromMediaType(mediaType);
104
+
105
+ return examples.map((example) => {
106
+ return {
107
+ key: example.key,
108
+ label: example.example.summary || example.key,
109
+ body: (
110
+ <OpenAPIExample example={example.example} context={props.context} syntax={syntax} />
111
+ ),
112
+ };
113
+ });
114
+ }
115
+
116
+ /**
117
+ * Get the syntax from a media type.
118
+ */
119
+ function getSyntaxFromMediaType(mediaType: string): string {
120
+ if (mediaType.includes('json')) {
121
+ return 'json';
122
+ }
123
+
124
+ if (mediaType === 'application/xml') {
125
+ return 'xml';
126
+ }
127
+
128
+ return 'text';
129
+ }
package/src/utils.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import type { AnyObject, OpenAPIV3, OpenAPIV3_1 } from '@gitbook/openapi-parser';
2
+ import type { OpenAPIUniversalContext } from './context';
2
3
  import { stringifyOpenAPI } from './stringifyOpenAPI';
4
+ import { tString } from './translate';
3
5
 
4
6
  export function checkIsReference(input: unknown): input is OpenAPIV3.ReferenceObject {
5
7
  return typeof input === 'object' && !!input && '$ref' in input;
@@ -149,3 +151,68 @@ function shouldDisplayExample(schema: OpenAPIV3.SchemaObject): boolean {
149
151
  Object.keys(schema.example).length > 0)
150
152
  );
151
153
  }
154
+
155
+ /**
156
+ * Get the class name for a status code.
157
+ * 1xx: informational
158
+ * 2xx: success
159
+ * 3xx: redirect
160
+ * 4xx, 5xx: error
161
+ */
162
+ export function getStatusCodeClassName(statusCode: number | string): string {
163
+ const category = getStatusCodeCategory(statusCode);
164
+ switch (category) {
165
+ case 1:
166
+ return 'informational';
167
+ case 2:
168
+ return 'success';
169
+ case 3:
170
+ return 'redirect';
171
+ case 4:
172
+ case 5:
173
+ return 'error';
174
+ default:
175
+ return 'unknown';
176
+ }
177
+ }
178
+
179
+ /**
180
+ * Get a default label for a status code.
181
+ * This is used when there is no label provided in the OpenAPI spec.
182
+ * 1xx: Information
183
+ * 2xx: Success
184
+ * 3xx: Redirect
185
+ * 4xx, 5xx: Error
186
+ */
187
+ export function getStatusCodeDefaultLabel(
188
+ statusCode: number | string,
189
+ context: OpenAPIUniversalContext
190
+ ): string {
191
+ const category = getStatusCodeCategory(statusCode);
192
+ switch (category) {
193
+ case 1:
194
+ return tString(context.translation, 'information');
195
+ case 2:
196
+ return tString(context.translation, 'success');
197
+ case 3:
198
+ return tString(context.translation, 'redirect');
199
+ case 4:
200
+ case 5:
201
+ return tString(context.translation, 'error');
202
+ default:
203
+ return '';
204
+ }
205
+ }
206
+
207
+ function getStatusCodeCategory(statusCode: number | string): number | string {
208
+ const code = typeof statusCode === 'string' ? Number.parseInt(statusCode, 10) : statusCode;
209
+
210
+ if (Number.isNaN(code) || code < 100 || code >= 600) {
211
+ return 'unknown';
212
+ }
213
+
214
+ // Determine the category of the status code based on the first digit
215
+ const category = Math.floor(code / 100);
216
+
217
+ return category;
218
+ }