@gitbook/react-openapi 1.4.3 → 1.5.1

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 (225) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/index.d.ts +663 -10
  3. package/dist/index.js +3873 -8
  4. package/package.json +11 -7
  5. package/dist/InteractiveSection.d.ts +0 -33
  6. package/dist/InteractiveSection.jsx +0 -61
  7. package/dist/Markdown.d.ts +0 -4
  8. package/dist/Markdown.jsx +0 -5
  9. package/dist/OpenAPICodeSample.d.ts +0 -19
  10. package/dist/OpenAPICodeSample.jsx +0 -230
  11. package/dist/OpenAPICodeSampleInteractive.d.ts +0 -14
  12. package/dist/OpenAPICodeSampleInteractive.jsx +0 -73
  13. package/dist/OpenAPICodeSampleSelector.d.ts +0 -14
  14. package/dist/OpenAPICodeSampleSelector.jsx +0 -44
  15. package/dist/OpenAPICopyButton.d.ts +0 -13
  16. package/dist/OpenAPICopyButton.jsx +0 -35
  17. package/dist/OpenAPIDisclosure.d.ts +0 -11
  18. package/dist/OpenAPIDisclosure.jsx +0 -30
  19. package/dist/OpenAPIDisclosureGroup.d.ts +0 -23
  20. package/dist/OpenAPIDisclosureGroup.jsx +0 -83
  21. package/dist/OpenAPIExample.d.ts +0 -16
  22. package/dist/OpenAPIExample.jsx +0 -36
  23. package/dist/OpenAPIMediaType.d.ts +0 -21
  24. package/dist/OpenAPIMediaType.jsx +0 -61
  25. package/dist/OpenAPIOperation.d.ts +0 -10
  26. package/dist/OpenAPIOperation.jsx +0 -25
  27. package/dist/OpenAPIOperationContext.d.ts +0 -16
  28. package/dist/OpenAPIOperationContext.jsx +0 -26
  29. package/dist/OpenAPIOperationDescription.d.ts +0 -9
  30. package/dist/OpenAPIOperationDescription.jsx +0 -22
  31. package/dist/OpenAPIOperationStability.d.ts +0 -9
  32. package/dist/OpenAPIOperationStability.jsx +0 -27
  33. package/dist/OpenAPIPath.d.ts +0 -18
  34. package/dist/OpenAPIPath.jsx +0 -55
  35. package/dist/OpenAPIPrefillContextProvider.d.ts +0 -22
  36. package/dist/OpenAPIPrefillContextProvider.jsx +0 -19
  37. package/dist/OpenAPIRequestBody.d.ts +0 -11
  38. package/dist/OpenAPIRequestBody.jsx +0 -28
  39. package/dist/OpenAPIRequestBodyHeaderType.d.ts +0 -8
  40. package/dist/OpenAPIRequestBodyHeaderType.jsx +0 -25
  41. package/dist/OpenAPIResponse.d.ts +0 -10
  42. package/dist/OpenAPIResponse.jsx +0 -57
  43. package/dist/OpenAPIResponseExample.d.ts +0 -9
  44. package/dist/OpenAPIResponseExample.jsx +0 -105
  45. package/dist/OpenAPIResponseExampleContent.d.ts +0 -22
  46. package/dist/OpenAPIResponseExampleContent.jsx +0 -60
  47. package/dist/OpenAPIResponses.d.ts +0 -9
  48. package/dist/OpenAPIResponses.jsx +0 -77
  49. package/dist/OpenAPISchema.d.ts +0 -27
  50. package/dist/OpenAPISchema.jsx +0 -400
  51. package/dist/OpenAPISchemaName.d.ts +0 -16
  52. package/dist/OpenAPISchemaName.jsx +0 -43
  53. package/dist/OpenAPISchemaServer.d.ts +0 -12
  54. package/dist/OpenAPISchemaServer.jsx +0 -8
  55. package/dist/OpenAPISecurities.d.ts +0 -9
  56. package/dist/OpenAPISecurities.jsx +0 -114
  57. package/dist/OpenAPISelect.d.ts +0 -22
  58. package/dist/OpenAPISelect.jsx +0 -44
  59. package/dist/OpenAPISpec.d.ts +0 -6
  60. package/dist/OpenAPISpec.jsx +0 -80
  61. package/dist/OpenAPITabs.d.ts +0 -26
  62. package/dist/OpenAPITabs.jsx +0 -109
  63. package/dist/OpenAPIWebhook.d.ts +0 -10
  64. package/dist/OpenAPIWebhook.jsx +0 -23
  65. package/dist/OpenAPIWebhookExample.d.ts +0 -6
  66. package/dist/OpenAPIWebhookExample.jsx +0 -41
  67. package/dist/ScalarApiButton.d.ts +0 -14
  68. package/dist/ScalarApiButton.jsx +0 -81
  69. package/dist/StaticSection.d.ts +0 -13
  70. package/dist/StaticSection.jsx +0 -32
  71. package/dist/code-samples.d.ts +0 -17
  72. package/dist/code-samples.js +0 -427
  73. package/dist/common/OpenAPIColumnSpec.d.ts +0 -6
  74. package/dist/common/OpenAPIColumnSpec.jsx +0 -20
  75. package/dist/common/OpenAPIOperationDescription.d.ts +0 -6
  76. package/dist/common/OpenAPIOperationDescription.jsx +0 -19
  77. package/dist/common/OpenAPIStability.d.ts +0 -4
  78. package/dist/common/OpenAPIStability.jsx +0 -15
  79. package/dist/common/OpenAPISummary.d.ts +0 -6
  80. package/dist/common/OpenAPISummary.jsx +0 -30
  81. package/dist/contentTypeChecks.d.ts +0 -10
  82. package/dist/contentTypeChecks.js +0 -30
  83. package/dist/context.d.ts +0 -75
  84. package/dist/context.js +0 -43
  85. package/dist/decycle.d.ts +0 -2
  86. package/dist/decycle.js +0 -70
  87. package/dist/dereference.d.ts +0 -5
  88. package/dist/dereference.js +0 -68
  89. package/dist/generateSchemaExample.d.ts +0 -45
  90. package/dist/generateSchemaExample.js +0 -342
  91. package/dist/getDisclosureLabel.d.ts +0 -7
  92. package/dist/getDisclosureLabel.js +0 -18
  93. package/dist/getOrCreateStoreByKey.d.ts +0 -10
  94. package/dist/getOrCreateStoreByKey.js +0 -19
  95. package/dist/json2xml.d.ts +0 -4
  96. package/dist/json2xml.js +0 -7
  97. package/dist/resolveOpenAPIOperation.d.ts +0 -11
  98. package/dist/resolveOpenAPIOperation.js +0 -173
  99. package/dist/resolveOpenAPIWebhook.d.ts +0 -11
  100. package/dist/resolveOpenAPIWebhook.js +0 -127
  101. package/dist/schemas/OpenAPISchemaItem.d.ts +0 -7
  102. package/dist/schemas/OpenAPISchemaItem.jsx +0 -16
  103. package/dist/schemas/OpenAPISchemas.d.ts +0 -14
  104. package/dist/schemas/OpenAPISchemas.jsx +0 -59
  105. package/dist/schemas/index.d.ts +0 -2
  106. package/dist/schemas/index.js +0 -2
  107. package/dist/schemas/resolveOpenAPISchemas.d.ts +0 -10
  108. package/dist/schemas/resolveOpenAPISchemas.js +0 -61
  109. package/dist/stringifyOpenAPI.d.ts +0 -4
  110. package/dist/stringifyOpenAPI.js +0 -15
  111. package/dist/translate.d.ts +0 -10
  112. package/dist/translate.jsx +0 -75
  113. package/dist/translations/de.d.ts +0 -43
  114. package/dist/translations/de.js +0 -43
  115. package/dist/translations/en.d.ts +0 -43
  116. package/dist/translations/en.js +0 -43
  117. package/dist/translations/es.d.ts +0 -43
  118. package/dist/translations/es.js +0 -43
  119. package/dist/translations/fr.d.ts +0 -43
  120. package/dist/translations/fr.js +0 -43
  121. package/dist/translations/index.d.ts +0 -395
  122. package/dist/translations/index.js +0 -27
  123. package/dist/translations/ja.d.ts +0 -43
  124. package/dist/translations/ja.js +0 -43
  125. package/dist/translations/nl.d.ts +0 -43
  126. package/dist/translations/nl.js +0 -43
  127. package/dist/translations/no.d.ts +0 -43
  128. package/dist/translations/no.js +0 -43
  129. package/dist/translations/pt-br.d.ts +0 -43
  130. package/dist/translations/pt-br.js +0 -43
  131. package/dist/translations/types.d.ts +0 -5
  132. package/dist/translations/types.js +0 -1
  133. package/dist/translations/zh.d.ts +0 -43
  134. package/dist/translations/zh.js +0 -43
  135. package/dist/tsconfig.build.tsbuildinfo +0 -1
  136. package/dist/types.d.ts +0 -31
  137. package/dist/types.js +0 -1
  138. package/dist/util/example.d.ts +0 -35
  139. package/dist/util/example.jsx +0 -103
  140. package/dist/util/server.d.ts +0 -9
  141. package/dist/util/server.js +0 -44
  142. package/dist/util/tryit-prefill.d.ts +0 -20
  143. package/dist/util/tryit-prefill.js +0 -129
  144. package/dist/utils.d.ts +0 -50
  145. package/dist/utils.js +0 -224
  146. package/src/InteractiveSection.tsx +0 -147
  147. package/src/Markdown.tsx +0 -12
  148. package/src/OpenAPICodeSample.tsx +0 -330
  149. package/src/OpenAPICodeSampleInteractive.tsx +0 -136
  150. package/src/OpenAPICodeSampleSelector.tsx +0 -94
  151. package/src/OpenAPICopyButton.tsx +0 -72
  152. package/src/OpenAPIDisclosure.tsx +0 -46
  153. package/src/OpenAPIDisclosureGroup.tsx +0 -158
  154. package/src/OpenAPIExample.tsx +0 -55
  155. package/src/OpenAPIMediaType.tsx +0 -139
  156. package/src/OpenAPIOperation.tsx +0 -35
  157. package/src/OpenAPIOperationContext.tsx +0 -45
  158. package/src/OpenAPIOperationDescription.tsx +0 -34
  159. package/src/OpenAPIOperationStability.tsx +0 -39
  160. package/src/OpenAPIPath.tsx +0 -90
  161. package/src/OpenAPIPrefillContextProvider.tsx +0 -40
  162. package/src/OpenAPIRequestBody.tsx +0 -54
  163. package/src/OpenAPIRequestBodyHeaderType.tsx +0 -36
  164. package/src/OpenAPIResponse.tsx +0 -82
  165. package/src/OpenAPIResponseExample.tsx +0 -151
  166. package/src/OpenAPIResponseExampleContent.tsx +0 -125
  167. package/src/OpenAPIResponses.tsx +0 -125
  168. package/src/OpenAPISchema.test.ts +0 -172
  169. package/src/OpenAPISchema.tsx +0 -654
  170. package/src/OpenAPISchemaName.tsx +0 -80
  171. package/src/OpenAPISchemaServer.tsx +0 -34
  172. package/src/OpenAPISecurities.tsx +0 -231
  173. package/src/OpenAPISelect.tsx +0 -96
  174. package/src/OpenAPISpec.tsx +0 -138
  175. package/src/OpenAPITabs.tsx +0 -147
  176. package/src/OpenAPIWebhook.tsx +0 -33
  177. package/src/OpenAPIWebhookExample.tsx +0 -60
  178. package/src/ScalarApiButton.tsx +0 -132
  179. package/src/StaticSection.tsx +0 -91
  180. package/src/__snapshots__/json2xml.test.ts.snap +0 -18
  181. package/src/code-samples.test.ts +0 -714
  182. package/src/code-samples.ts +0 -448
  183. package/src/common/OpenAPIColumnSpec.tsx +0 -31
  184. package/src/common/OpenAPIOperationDescription.tsx +0 -31
  185. package/src/common/OpenAPIStability.tsx +0 -23
  186. package/src/common/OpenAPISummary.tsx +0 -45
  187. package/src/contentTypeChecks.ts +0 -39
  188. package/src/context.ts +0 -99
  189. package/src/decycle.ts +0 -68
  190. package/src/dereference.ts +0 -29
  191. package/src/generateSchemaExample.test.ts +0 -1040
  192. package/src/generateSchemaExample.ts +0 -530
  193. package/src/getDisclosureLabel.ts +0 -25
  194. package/src/getOrCreateStoreByKey.ts +0 -33
  195. package/src/index.ts +0 -10
  196. package/src/json2xml.test.ts +0 -46
  197. package/src/json2xml.ts +0 -8
  198. package/src/resolveOpenAPIOperation.test.ts +0 -177
  199. package/src/resolveOpenAPIOperation.ts +0 -151
  200. package/src/resolveOpenAPIWebhook.ts +0 -99
  201. package/src/schemas/OpenAPISchemaItem.tsx +0 -34
  202. package/src/schemas/OpenAPISchemas.tsx +0 -98
  203. package/src/schemas/index.ts +0 -2
  204. package/src/schemas/resolveOpenAPISchemas.test.ts +0 -174
  205. package/src/schemas/resolveOpenAPISchemas.ts +0 -28
  206. package/src/stringifyOpenAPI.ts +0 -25
  207. package/src/translate.tsx +0 -80
  208. package/src/translations/de.ts +0 -43
  209. package/src/translations/en.ts +0 -43
  210. package/src/translations/es.ts +0 -43
  211. package/src/translations/fr.ts +0 -43
  212. package/src/translations/index.ts +0 -33
  213. package/src/translations/ja.ts +0 -43
  214. package/src/translations/nl.ts +0 -43
  215. package/src/translations/no.ts +0 -43
  216. package/src/translations/pt-br.ts +0 -43
  217. package/src/translations/types.ts +0 -7
  218. package/src/translations/zh.ts +0 -43
  219. package/src/types.ts +0 -46
  220. package/src/util/example.tsx +0 -129
  221. package/src/util/server.test.ts +0 -58
  222. package/src/util/server.ts +0 -47
  223. package/src/util/tryit-prefill.test.ts +0 -311
  224. package/src/util/tryit-prefill.ts +0 -160
  225. package/src/utils.ts +0 -255
@@ -1,160 +0,0 @@
1
- import { ExpressionRuntime, parseTemplate } from '@gitbook/expr';
2
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
3
- import type { ApiClientConfiguration } from '@scalar/types';
4
- import type { PrefillInputContextData } from '../OpenAPIPrefillContextProvider';
5
- import type { OpenAPIOperationData } from '../types';
6
-
7
- export interface TryItPrefillConfiguration {
8
- authentication?: ApiClientConfiguration['authentication'];
9
- servers?: ApiClientConfiguration['servers'];
10
- }
11
-
12
- /**
13
- * Resolve the Scalar API client prefill configuration for a given OpenAPI operation.
14
- */
15
- export function resolveTryItPrefillForOperation(args: {
16
- /**
17
- * The parsed OpenAPI operation.
18
- */
19
- operation: Pick<OpenAPIOperationData, 'securities' | 'servers'>;
20
- /**
21
- * Prefill input context data.
22
- */
23
- prefillInputContext: PrefillInputContextData | null;
24
- }): TryItPrefillConfiguration {
25
- const {
26
- operation: { securities, servers },
27
- prefillInputContext,
28
- } = args;
29
-
30
- if (!prefillInputContext) {
31
- return {};
32
- }
33
-
34
- const runtime = new ExpressionRuntime();
35
- const resolveTryItPrefillExpression = (expr: string) => {
36
- const parts = parseTemplate(expr);
37
- if (!parts.length) {
38
- return undefined;
39
- }
40
- return runtime.evaluateTemplate(expr, prefillInputContext);
41
- };
42
-
43
- const prefillAuth = securities
44
- ? resolveTryItPrefillAuthForOperationSecurities({
45
- securities,
46
- resolveTryItPrefillExpression,
47
- })
48
- : undefined;
49
-
50
- const prefillServers = servers
51
- ? resolveTryItPrefillServersForOperationServers({ servers, resolveTryItPrefillExpression })
52
- : [];
53
-
54
- return {
55
- ...(prefillAuth ? { authentication: prefillAuth } : {}),
56
- ...(prefillServers ? { servers: prefillServers } : {}),
57
- };
58
- }
59
-
60
- /**
61
- * Resolve prefill authentication configuration for the security schemes defined for an operation.
62
- */
63
- function resolveTryItPrefillAuthForOperationSecurities(args: {
64
- securities: OpenAPIOperationData['securities'];
65
- resolveTryItPrefillExpression: (expr: string) => string | undefined;
66
- }): ApiClientConfiguration['authentication'] | undefined {
67
- const { securities, resolveTryItPrefillExpression } = args;
68
- const prefillAuthConfig: ApiClientConfiguration['authentication']['securitySchemes'] = {};
69
-
70
- for (const [schemeName, security] of Object.values(securities)) {
71
- const tryitPrefillAuthValue = security['x-gitbook-prefill']
72
- ? resolveTryItPrefillExpression(security['x-gitbook-prefill'])
73
- : undefined;
74
-
75
- if (!tryitPrefillAuthValue) {
76
- continue;
77
- }
78
-
79
- switch (security.type) {
80
- case 'http': {
81
- if (security.scheme?.includes('bearer')) {
82
- prefillAuthConfig[schemeName] = { token: tryitPrefillAuthValue };
83
- } else if (
84
- security.scheme?.includes('basic') &&
85
- tryitPrefillAuthValue.includes(':')
86
- ) {
87
- const [username, password] = tryitPrefillAuthValue.split(':', 2);
88
- prefillAuthConfig[schemeName] = { username, password };
89
- }
90
- break;
91
- }
92
- case 'apiKey': {
93
- prefillAuthConfig[schemeName] = {
94
- name: security.name,
95
- in: security.in,
96
- value: tryitPrefillAuthValue,
97
- };
98
- break;
99
- }
100
- case 'oauth2':
101
- case 'openIdConnect': {
102
- break;
103
- }
104
- }
105
- }
106
-
107
- return Object.keys(prefillAuthConfig).length > 0
108
- ? { securitySchemes: prefillAuthConfig }
109
- : undefined;
110
- }
111
-
112
- /**
113
- * Resolve prefill server configuration for the servers defined for an operation.
114
- */
115
- function resolveTryItPrefillServersForOperationServers(args: {
116
- servers: OpenAPIOperationData['servers'];
117
- resolveTryItPrefillExpression: (expr: string) => string | undefined;
118
- }): ApiClientConfiguration['servers'] | undefined {
119
- const { servers, resolveTryItPrefillExpression } = args;
120
- const resolvedServers: ApiClientConfiguration['servers'] = [];
121
-
122
- for (const server of servers) {
123
- // Url-level prefill
124
- const tryItPrefillServerUrlExpr = server['x-gitbook-prefill'];
125
- const tryItPrefillServerUrlValue = tryItPrefillServerUrlExpr
126
- ? resolveTryItPrefillExpression(tryItPrefillServerUrlExpr)
127
- : undefined;
128
-
129
- const variables: { [variable: string]: OpenAPIV3.ServerVariableObject } = server.variables
130
- ? { ...server.variables }
131
- : {};
132
-
133
- // Variable-level prefill
134
- if (server.variables) {
135
- for (const [varName, variable] of Object.entries(server.variables)) {
136
- const { 'x-gitbook-prefill': tryItPrefillVarExpr, ...variableProps } = variable;
137
-
138
- const tryItPrefillVarValue = tryItPrefillVarExpr
139
- ? resolveTryItPrefillExpression(tryItPrefillVarExpr)
140
- : undefined;
141
- variables[varName] = {
142
- ...variableProps,
143
- ...(tryItPrefillVarValue ? { default: String(tryItPrefillVarValue) } : {}),
144
- };
145
- }
146
- }
147
-
148
- const hasServerVariables = Object.keys(variables).length > 0;
149
- if (server.url && (tryItPrefillServerUrlValue || hasServerVariables)) {
150
- const resolvedServer: OpenAPIV3.ServerObject = {
151
- url: tryItPrefillServerUrlValue ?? server.url,
152
- ...(server.description ? { description: server.description } : {}),
153
- ...(hasServerVariables ? { variables } : {}),
154
- };
155
- resolvedServers.push(resolvedServer);
156
- }
157
- }
158
-
159
- return resolvedServers.length > 0 ? resolvedServers : undefined;
160
- }
package/src/utils.ts DELETED
@@ -1,255 +0,0 @@
1
- import type { AnyObject, OpenAPIV3, OpenAPIV3_1 } from '@gitbook/openapi-parser';
2
- import type { OpenAPIUniversalContext } from './context';
3
- import { stringifyOpenAPI } from './stringifyOpenAPI';
4
- import { tString } from './translate';
5
-
6
- export function checkIsReference(input: unknown): input is OpenAPIV3.ReferenceObject {
7
- return typeof input === 'object' && !!input && '$ref' in input;
8
- }
9
-
10
- export function createStateKey(key: string, scope?: string) {
11
- return scope ? `${scope}_${key}` : key;
12
- }
13
-
14
- /**
15
- * Check if an object has a description. Either at the root level or in items.
16
- */
17
- function hasDescription(object: AnyObject) {
18
- return 'description' in object || 'x-gitbook-description-html' in object;
19
- }
20
-
21
- /**
22
- * Resolve the description of an object.
23
- */
24
- export function resolveDescription(object: OpenAPIV3.SchemaObject | AnyObject) {
25
- // Resolve description from the object first
26
- if (hasDescription(object)) {
27
- return 'x-gitbook-description-html' in object &&
28
- typeof object['x-gitbook-description-html'] === 'string'
29
- ? object['x-gitbook-description-html'].trim()
30
- : typeof object.description === 'string'
31
- ? object.description.trim()
32
- : undefined;
33
- }
34
-
35
- // If the object has no description, try to resolve it from the items
36
- if ('items' in object && typeof object.items === 'object' && hasDescription(object.items)) {
37
- return resolveDescription(object.items);
38
- }
39
-
40
- return undefined;
41
- }
42
-
43
- /**
44
- * Extract descriptions from an object.
45
- */
46
- export function extractDescriptions(object: AnyObject) {
47
- return {
48
- description: object.description,
49
- 'x-gitbook-description-html':
50
- 'x-gitbook-description-html' in object
51
- ? object['x-gitbook-description-html']
52
- : undefined,
53
- };
54
- }
55
-
56
- /**
57
- * Resolve the first example from an object.
58
- */
59
- export function resolveFirstExample(object: AnyObject): string | undefined {
60
- if ('examples' in object && typeof object.examples === 'object' && object.examples) {
61
- const keys = Object.keys(object.examples);
62
- const firstKey = keys[0];
63
- if (firstKey && object.examples[firstKey]) {
64
- return formatExample(object.examples[firstKey]);
65
- }
66
- }
67
-
68
- // Resolve top level example first
69
- if (shouldDisplayExample(object)) {
70
- return formatExample(object.example);
71
- }
72
-
73
- // Resolve example from items if it exists
74
- if (object.items && typeof object.items === 'object') {
75
- return formatExample(object.items.example);
76
- }
77
-
78
- return undefined;
79
- }
80
-
81
- /**
82
- * Resolve the schema of a parameter.
83
- * Extract the description, example and deprecated from parameter.
84
- */
85
- export function resolveParameterSchema(
86
- parameter: OpenAPIV3.ParameterBaseObject
87
- ): OpenAPIV3.SchemaObject {
88
- const schema = checkIsReference(parameter.schema) ? undefined : parameter.schema;
89
- return {
90
- // Description of the parameter is defined at the parameter level
91
- // we use display it if the schema doesn't override it
92
- ...extractDescriptions(parameter),
93
- example: resolveFirstExample(parameter),
94
- // Deprecated can be defined at the parameter level
95
- deprecated: parameter.deprecated,
96
- ...schema,
97
- };
98
- }
99
-
100
- /**
101
- * Transform a parameter object to a property object.
102
- */
103
- export function parameterToProperty(
104
- parameter: OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject
105
- ): {
106
- propertyName: string | undefined;
107
- schema: OpenAPIV3.SchemaObject;
108
- required: boolean | undefined;
109
- } {
110
- if (checkIsReference(parameter)) {
111
- return {
112
- propertyName: parameter.$ref ?? 'Unknown ref',
113
- schema: {},
114
- required: undefined,
115
- };
116
- }
117
- return {
118
- propertyName: parameter.name,
119
- schema: resolveParameterSchema(parameter),
120
- required: parameter.required,
121
- };
122
- }
123
-
124
- /**
125
- * Format the example of a schema.
126
- */
127
- function formatExample(example: unknown): string {
128
- if (typeof example === 'string') {
129
- return example
130
- .replace(/\n/g, ' ') // Replace newlines with spaces
131
- .replace(/\s+/g, ' ') // Collapse multiple spaces/newlines into a single space
132
- .replace(/([\{\}:,])\s+/g, '$1 ') // Ensure a space after {, }, :, and ,
133
- .replace(/\s+([\{\}:,])/g, ' $1') // Ensure a space before {, }, :, and ,
134
- .trim();
135
- }
136
-
137
- return stringifyOpenAPI(example);
138
- }
139
-
140
- /**
141
- * Check if an example should be displayed.
142
- */
143
- function shouldDisplayExample(schema: OpenAPIV3.SchemaObject): boolean {
144
- return (
145
- (typeof schema.example === 'string' && !!schema.example) ||
146
- typeof schema.example === 'number' ||
147
- typeof schema.example === 'boolean' ||
148
- (Array.isArray(schema.example) && schema.example.length > 0) ||
149
- (typeof schema.example === 'object' &&
150
- schema.example !== null &&
151
- Object.keys(schema.example).length > 0)
152
- );
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
- }
219
-
220
- export function getSchemaTitle(schema: OpenAPIV3.SchemaObject): string {
221
- // Otherwise try to infer a nice title
222
- let type = 'any';
223
-
224
- if (schema.enum || schema['x-enumDescriptions'] || schema['x-gitbook-enum']) {
225
- type = `${schema.type} · enum`;
226
- // check array AND schema.items as this is sometimes null despite what the type indicates
227
- } else if (schema.type === 'array' && !!schema.items) {
228
- type = `${getSchemaTitle(schema.items)}[]`;
229
- } else if (Array.isArray(schema.type)) {
230
- type = schema.type.join(' | ');
231
- } else if (schema.type || schema.properties) {
232
- type = schema.type ?? 'object';
233
-
234
- if (schema.format) {
235
- type += ` · ${schema.format}`;
236
- }
237
-
238
- // Only add the title if it's an object (no need for the title of a string, number, etc.)
239
- if (type === 'object' && schema.title) {
240
- type += ` · ${schema.title.replaceAll(' ', '')}`;
241
- }
242
- }
243
-
244
- if ('anyOf' in schema) {
245
- type = 'any of';
246
- } else if ('oneOf' in schema) {
247
- type = 'one of';
248
- } else if ('allOf' in schema) {
249
- type = 'all of';
250
- } else if ('not' in schema) {
251
- type = 'not';
252
- }
253
-
254
- return type;
255
- }