@dv.net/docs-vitepress-openapi 1.0.7

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 (135) hide show
  1. package/README.md +52 -0
  2. package/dist/docs-vitepress-openapi.client.cjs.js +1 -0
  3. package/dist/docs-vitepress-openapi.client.es.js +16 -0
  4. package/dist/docs-vitepress-openapi.css +1 -0
  5. package/dist/docs-vitepress-openapi.node.cjs.js +4102 -0
  6. package/dist/docs-vitepress-openapi.node.es.js +72981 -0
  7. package/dist/vanilla-picker-BAEwS4Fy.js +536 -0
  8. package/dist/vanilla-picker-m653iADl.cjs +8 -0
  9. package/package.json +80 -0
  10. package/src/client.ts +73 -0
  11. package/src/components/Common/Lazy/OALazy.vue +49 -0
  12. package/src/components/Common/Lazy/lazyBus.ts +8 -0
  13. package/src/components/Common/OACodeBlock.vue +91 -0
  14. package/src/components/Common/OACodeValue.vue +22 -0
  15. package/src/components/Common/OAHeaderBadges.vue +44 -0
  16. package/src/components/Common/OAHeading.vue +84 -0
  17. package/src/components/Common/OAInfo.vue +106 -0
  18. package/src/components/Common/OAIntroduction.vue +25 -0
  19. package/src/components/Common/OAJSONEditor.vue +41 -0
  20. package/src/components/Common/OAMarkdown.vue +28 -0
  21. package/src/components/Common/OAMethodBadge.vue +20 -0
  22. package/src/components/Common/OAServers.vue +42 -0
  23. package/src/components/Operation/OAOperation.vue +345 -0
  24. package/src/components/Operation/OAOperationTags.vue +30 -0
  25. package/src/components/Parameter/OAParameter.vue +80 -0
  26. package/src/components/Parameter/OAParameterAttribute.vue +44 -0
  27. package/src/components/Parameter/OAParameterExamples.vue +52 -0
  28. package/src/components/Parameter/OAParameters.vue +69 -0
  29. package/src/components/Path/OAPath.vue +347 -0
  30. package/src/components/Path/OAPathEndpoint.vue +99 -0
  31. package/src/components/Path/OAPaths.vue +62 -0
  32. package/src/components/Path/OAPathsByTag.vue +118 -0
  33. package/src/components/Path/OAPathsByTags.vue +99 -0
  34. package/src/components/Path/OAPathsSummary.vue +52 -0
  35. package/src/components/Playground/OAPlaygroundParameterInput.vue +97 -0
  36. package/src/components/Playground/OAPlaygroundParameters.vue +262 -0
  37. package/src/components/Playground/OAPlaygroundResponse.vue +74 -0
  38. package/src/components/Playground/OAPlaygroundSecurityInput.vue +44 -0
  39. package/src/components/Remote/OARemoteContext.vue +42 -0
  40. package/src/components/Remote/OARemoteOperation.vue +30 -0
  41. package/src/components/Remote/OARemoteRoot.vue +29 -0
  42. package/src/components/Remote/OARemoteSpec.vue +23 -0
  43. package/src/components/Request/OARequestBody.vue +24 -0
  44. package/src/components/Response/OAResponse.vue +81 -0
  45. package/src/components/Response/OAResponses.vue +119 -0
  46. package/src/components/Sample/OACodeSamples.vue +71 -0
  47. package/src/components/Schema/OASchemaPropertyAttributes.vue +44 -0
  48. package/src/components/Schema/OASchemaTabs.vue +90 -0
  49. package/src/components/Schema/OASchemaUI.vue +151 -0
  50. package/src/components/Security/OASecurity.vue +79 -0
  51. package/src/components/Security/OASecurityContent.vue +100 -0
  52. package/src/components/Spec/OASpec.vue +127 -0
  53. package/src/components/Spec/OASpecSkeleton.vue +26 -0
  54. package/src/components/Try/OATryItButton.vue +171 -0
  55. package/src/components/Try/OATryWithVariables.vue +105 -0
  56. package/src/components/index.ts +55 -0
  57. package/src/components/ui/badge/Badge.vue +18 -0
  58. package/src/components/ui/badge/index.ts +32 -0
  59. package/src/components/ui/button/Button.vue +28 -0
  60. package/src/components/ui/button/index.ts +37 -0
  61. package/src/components/ui/checkbox/Checkbox.vue +33 -0
  62. package/src/components/ui/checkbox/index.ts +1 -0
  63. package/src/components/ui/collapsible/Collapsible.vue +18 -0
  64. package/src/components/ui/collapsible/CollapsibleContent.vue +15 -0
  65. package/src/components/ui/collapsible/CollapsibleTrigger.vue +12 -0
  66. package/src/components/ui/collapsible/index.ts +3 -0
  67. package/src/components/ui/input/Input.vue +29 -0
  68. package/src/components/ui/input/index.ts +1 -0
  69. package/src/components/ui/label/Label.vue +29 -0
  70. package/src/components/ui/label/index.ts +1 -0
  71. package/src/components/ui/multiselect/Multiselect.vue +104 -0
  72. package/src/components/ui/select/Select.vue +15 -0
  73. package/src/components/ui/select/SelectContent.vue +56 -0
  74. package/src/components/ui/select/SelectGroup.vue +24 -0
  75. package/src/components/ui/select/SelectItem.vue +40 -0
  76. package/src/components/ui/select/SelectItemText.vue +12 -0
  77. package/src/components/ui/select/SelectLabel.vue +14 -0
  78. package/src/components/ui/select/SelectScrollDownButton.vue +29 -0
  79. package/src/components/ui/select/SelectScrollUpButton.vue +29 -0
  80. package/src/components/ui/select/SelectSeparator.vue +22 -0
  81. package/src/components/ui/select/SelectTrigger.vue +33 -0
  82. package/src/components/ui/select/SelectValue.vue +12 -0
  83. package/src/components/ui/select/index.ts +11 -0
  84. package/src/components/ui/tabs/Tabs.vue +15 -0
  85. package/src/components/ui/tabs/TabsContent.vue +24 -0
  86. package/src/components/ui/tabs/TabsList.vue +27 -0
  87. package/src/components/ui/tabs/TabsTrigger.vue +33 -0
  88. package/src/components/ui/tabs/index.ts +21 -0
  89. package/src/composables/useOpenapi.ts +70 -0
  90. package/src/composables/usePaths.ts +31 -0
  91. package/src/composables/usePlayground.ts +49 -0
  92. package/src/composables/useShiki.ts +46 -0
  93. package/src/composables/useSidebar.ts +301 -0
  94. package/src/composables/useTheme.ts +799 -0
  95. package/src/index.ts +8 -0
  96. package/src/json.css +251 -0
  97. package/src/lib/OpenApi.ts +254 -0
  98. package/src/lib/codeSamples/buildRequest.ts +149 -0
  99. package/src/lib/codeSamples/generateCodeSample.ts +20 -0
  100. package/src/lib/codeSamples/generateCodeSampleCurl.ts +150 -0
  101. package/src/lib/codeSamples/generateCodeSampleJavaScript.ts +29 -0
  102. package/src/lib/codeSamples/generateCodeSamplePhp.ts +50 -0
  103. package/src/lib/codeSamples/generateCodeSamplePython.ts +32 -0
  104. package/src/lib/codeSamples/request.ts +25 -0
  105. package/src/lib/constraintsParser.ts +30 -0
  106. package/src/lib/createOpenApiInstance.ts +18 -0
  107. package/src/lib/deepUnref.ts +76 -0
  108. package/src/lib/examples/getPropertyExample.ts +31 -0
  109. package/src/lib/examples/getPropertyExamples.ts +27 -0
  110. package/src/lib/examples/getSchemaExample.ts +38 -0
  111. package/src/lib/examples/getSchemaUiJson.ts +150 -0
  112. package/src/lib/examples/getSchemaUiXml.ts +9 -0
  113. package/src/lib/examples/hasExample.ts +26 -0
  114. package/src/lib/getOpenApiInstance.ts +39 -0
  115. package/src/lib/getSchemaUi.ts +280 -0
  116. package/src/lib/getSecurityUi.ts +33 -0
  117. package/src/lib/operationData.ts +24 -0
  118. package/src/lib/prepareOpenAPI/generateMissingOperationIds.ts +19 -0
  119. package/src/lib/prepareOpenAPI/generateMissingSummary.ts +20 -0
  120. package/src/lib/prepareOpenAPI/generateMissingTags.ts +36 -0
  121. package/src/lib/prepareOpenAPI/prepareOpenAPI.ts +35 -0
  122. package/src/lib/processOpenAPI.ts +131 -0
  123. package/src/lib/resolveCircularRef.ts +66 -0
  124. package/src/lib/sidebar/cleanSidebarItems.ts +19 -0
  125. package/src/lib/sidebar/ensureGroupTextSlashPrefix.ts +18 -0
  126. package/src/lib/sidebar/flatSidebarItems.ts +58 -0
  127. package/src/lib/sidebar/generateSidebarItemsByPaths.ts +132 -0
  128. package/src/lib/sidebar/mergeAdjacentGroups.ts +56 -0
  129. package/src/lib/sidebar/setItemCollapsedState.ts +15 -0
  130. package/src/lib/utils.ts +7 -0
  131. package/src/locales/en.json +50 -0
  132. package/src/locales/index.ts +4 -0
  133. package/src/style.css +180 -0
  134. package/src/types.ts +73 -0
  135. package/src/vite-env.d.ts +8 -0
@@ -0,0 +1,26 @@
1
+ import type { OpenAPI } from '@scalar/openapi-types'
2
+
3
+ export function hasExample(schema: Partial<OpenAPI.SchemaObject>): boolean {
4
+ if (!schema || typeof schema !== 'object') {
5
+ return false
6
+ }
7
+ const visited = new Set()
8
+ function containsExample(obj: any): boolean {
9
+ if (visited.has(obj)) {
10
+ return false
11
+ }
12
+ visited.add(obj)
13
+ if ('example' in obj || 'examples' in obj) {
14
+ return true
15
+ }
16
+ for (const key in obj) {
17
+ if (typeof obj[key] === 'object' && obj[key] != null) {
18
+ if (containsExample(obj[key])) {
19
+ return true
20
+ }
21
+ }
22
+ }
23
+ return false
24
+ }
25
+ return containsExample(schema)
26
+ }
@@ -0,0 +1,39 @@
1
+ import type { Schemas } from '../composables/useOpenapi'
2
+ import { DEFAULT_SCHEMA, useOpenapi } from '../composables/useOpenapi'
3
+ import { createOpenApiInstance } from './createOpenApiInstance'
4
+
5
+ export function getOpenApiInstance({
6
+ id,
7
+ custom,
8
+ injected,
9
+ }: {
10
+ id?: string
11
+ custom?: { spec: any, parsedSpec?: any }
12
+ injected?: Schemas | any
13
+ } = {}) {
14
+ if (id === undefined) {
15
+ id = DEFAULT_SCHEMA
16
+ }
17
+
18
+ if (custom?.spec) {
19
+ return createOpenApiInstance({ spec: custom.spec })
20
+ }
21
+
22
+ if (injected && injected.schemas) {
23
+ try {
24
+ return injected.schemas.get(id)
25
+ } catch {
26
+ console.warn('Deprecated usage of injected.')
27
+ return injected
28
+ }
29
+ }
30
+
31
+ const globalSpec = useOpenapi()
32
+ if (globalSpec) {
33
+ return globalSpec
34
+ }
35
+
36
+ console.error('No OpenAPI specification found.')
37
+
38
+ return null
39
+ }
@@ -0,0 +1,280 @@
1
+ import type { OpenAPI } from '@scalar/openapi-types'
2
+ import { literalTypes } from '../index'
3
+ import { getConstraints, hasConstraints } from './constraintsParser'
4
+ import { getPropertyExamples } from './examples/getPropertyExamples'
5
+ import { resolveCircularRef } from './resolveCircularRef'
6
+
7
+ type JSONSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object' | 'null'
8
+
9
+ interface Metadata {
10
+ isCircularReference?: boolean
11
+ isAdditionalProperties?: boolean
12
+ isOneOf?: boolean
13
+ isOneOfItem?: boolean
14
+ isConstant?: boolean
15
+ extra?: Record<string, unknown>
16
+ }
17
+
18
+ interface DocumentationReference {
19
+ url?: string
20
+ description?: string
21
+ }
22
+
23
+ export interface OAProperty {
24
+ name: string
25
+ types: JSONSchemaType[]
26
+ required: boolean
27
+ examples?: unknown[]
28
+ title?: string
29
+ description?: string
30
+ defaultValue?: unknown
31
+ docs?: DocumentationReference
32
+ constraints?: Record<string, unknown>
33
+ properties?: OAProperty[]
34
+ enum?: unknown[]
35
+ subtype?: JSONSchemaType
36
+ subexamples?: unknown[]
37
+ nullable?: boolean
38
+ meta?: Metadata
39
+ }
40
+
41
+ class UiPropertyFactory {
42
+ static createBaseProperty(
43
+ name: string,
44
+ property: Partial<OpenAPI.SchemaObject> = {},
45
+ required = false,
46
+ ): OAProperty {
47
+ const nodeTypes = Array.isArray(property.type)
48
+ ? property.type
49
+ : [property.type || 'string'] as JSONSchemaType[]
50
+
51
+ const examples = getPropertyExamples(property)
52
+
53
+ const baseProperty: OAProperty = {
54
+ name,
55
+ types: nodeTypes,
56
+ required,
57
+ ...(property.title && { title: property.title }),
58
+ ...(property.description && { description: property.description }),
59
+ ...(property.default !== undefined && { defaultValue: property.default }),
60
+ ...(property.externalDocs && { docs: property.externalDocs }),
61
+ ...(examples && { examples }),
62
+ ...(property.nullable && { nullable: property.nullable }),
63
+ }
64
+
65
+ if (property.const !== undefined) {
66
+ baseProperty.meta = { ...(baseProperty.meta || {}), isConstant: true }
67
+ }
68
+
69
+ if (hasConstraints(property)) {
70
+ baseProperty.constraints = getConstraints(property)
71
+ }
72
+
73
+ Object.keys(property).forEach((key) => {
74
+ if (key.startsWith('x-')) {
75
+ baseProperty.meta = baseProperty.meta || {}
76
+ baseProperty.meta.extra = baseProperty.meta.extra || {}
77
+ baseProperty.meta.extra[key] = property[key]
78
+ }
79
+ })
80
+
81
+ return baseProperty
82
+ }
83
+
84
+ static createCircularReferenceProperty(name: string, circularRef: string): OAProperty {
85
+ return {
86
+ name,
87
+ types: ['object'],
88
+ required: false,
89
+ description: `Circular reference to **${circularRef}**`,
90
+ meta: { isCircularReference: true },
91
+ }
92
+ }
93
+
94
+ static createOneOfProperty(oneOfProperties: Partial<OpenAPI.SchemaObject>[], name: string = ''): OAProperty {
95
+ return {
96
+ name,
97
+ types: ['object'],
98
+ required: false,
99
+ properties: oneOfProperties.map((prop) => {
100
+ const property = UiPropertyFactory.schemaToUiProperty('', prop)
101
+ property.meta = { ...(property.meta || {}), isOneOfItem: true }
102
+ return property
103
+ }),
104
+ meta: { isOneOf: true },
105
+ }
106
+ }
107
+
108
+ static schemaToUiProperty(
109
+ name: string,
110
+ schema: Partial<OpenAPI.SchemaObject>,
111
+ required = false,
112
+ ): OAProperty {
113
+ if (!schema || Object.keys(schema).length === 0) {
114
+ return {
115
+ name,
116
+ types: [],
117
+ required,
118
+ }
119
+ }
120
+
121
+ if (schema.circularReference) {
122
+ return UiPropertyFactory.createCircularReferenceProperty(name, schema.circularReference)
123
+ }
124
+
125
+ if (schema.oneOf) {
126
+ return UiPropertyFactory.createOneOfProperty(schema.oneOf, name)
127
+ }
128
+
129
+ if (schema.const !== undefined) {
130
+ const example = getPropertyExamples(schema) || schema.const
131
+ return {
132
+ name,
133
+ types: [schema.type as JSONSchemaType || 'string'],
134
+ required: false,
135
+ examples: [example],
136
+ meta: { isConstant: true },
137
+ }
138
+ }
139
+
140
+ if (literalTypes.includes(String(schema.type)) && schema.enum) {
141
+ return {
142
+ name,
143
+ types: [schema.type as JSONSchemaType],
144
+ required: false,
145
+ enum: schema.enum,
146
+ }
147
+ }
148
+
149
+ const property = UiPropertyFactory.createBaseProperty(name, schema, required)
150
+
151
+ switch (schema.type) {
152
+ case 'array':
153
+ if (schema.items) {
154
+ const schemaType = determineSchemaType(schema.items)
155
+
156
+ property.properties = schemaType === 'object'
157
+ ? UiPropertyFactory.extractProperties(
158
+ schema.items.properties,
159
+ schema.items.required || [],
160
+ schema.items.additionalProperties,
161
+ )
162
+ : undefined
163
+
164
+ if (schemaType !== undefined) {
165
+ property.subtype = schemaType as JSONSchemaType
166
+ }
167
+
168
+ const itemsExamples = getPropertyExamples(schema.items)
169
+ if (itemsExamples) {
170
+ property.subexamples = itemsExamples
171
+ }
172
+
173
+ if (schema.items.const !== undefined) {
174
+ property.meta = { ...(property.meta || {}), isConstant: true }
175
+ }
176
+
177
+ if (schema.items.oneOf) {
178
+ property.meta = { ...(property.meta || {}), isOneOf: true }
179
+ property.properties = schema.items.oneOf.map((prop: any) => {
180
+ const propSchema = { ...prop, type: schema.items.type }
181
+ return {
182
+ ...UiPropertyFactory.schemaToUiProperty('', propSchema),
183
+ meta: { ...(prop.meta || {}), isOneOfItem: true },
184
+ }
185
+ })
186
+ }
187
+ }
188
+ break
189
+
190
+ case 'object':
191
+ property.properties = UiPropertyFactory.extractProperties(
192
+ schema.properties,
193
+ schema.required || [],
194
+ schema.additionalProperties,
195
+ )
196
+ break
197
+
198
+ case undefined:
199
+ if (schema.properties || schema.additionalProperties) {
200
+ property.types = ['object']
201
+ property.properties = UiPropertyFactory.extractProperties(
202
+ schema.properties,
203
+ schema.required || [],
204
+ schema.additionalProperties,
205
+ )
206
+ }
207
+ break
208
+ }
209
+
210
+ return property
211
+ }
212
+
213
+ static extractProperties(
214
+ propertiesNode?: Record<string, OpenAPI.SchemaObject>,
215
+ requiredProperties: string[] = [],
216
+ additionalPropertiesNode?: OpenAPI.SchemaObject | boolean,
217
+ ): OAProperty[] {
218
+ const properties: OAProperty[] = []
219
+
220
+ if (propertiesNode) {
221
+ Object.entries(propertiesNode).forEach(([key, value]) => {
222
+ const isRequired = requiredProperties.includes(key)
223
+ properties.push(UiPropertyFactory.schemaToUiProperty(key, value, isRequired))
224
+ })
225
+ }
226
+
227
+ if (additionalPropertiesNode) {
228
+ const additionalProps = typeof additionalPropertiesNode === 'object'
229
+ ? additionalPropertiesNode
230
+ : { type: 'string' }
231
+
232
+ properties.push({
233
+ name: 'additionalProperties',
234
+ types: [additionalProps.type as JSONSchemaType],
235
+ required: false,
236
+ meta: { isAdditionalProperties: true },
237
+ })
238
+ }
239
+
240
+ return properties
241
+ }
242
+ }
243
+
244
+ export function getSchemaUi(jsonSchema: OpenAPI.SchemaObject): OAProperty | OAProperty[] {
245
+ if (!jsonSchema || Object.keys(jsonSchema).length === 0) {
246
+ return []
247
+ }
248
+
249
+ const resolvedSchema = resolveCircularRef(jsonSchema)
250
+
251
+ return UiPropertyFactory.schemaToUiProperty('', resolvedSchema)
252
+ }
253
+
254
+ function determineSchemaType(schema: OpenAPI.SchemaObject): JSONSchemaType {
255
+ if (!schema.type && schema.properties) {
256
+ return 'object'
257
+ }
258
+
259
+ if (!schema.type && schema.items) {
260
+ return 'array'
261
+ }
262
+
263
+ if (!schema.type && schema.const !== undefined) {
264
+ if (Array.isArray(schema.const)) {
265
+ return 'array'
266
+ } else if (typeof schema.const === 'object' && schema.const !== null) {
267
+ return 'object'
268
+ } else if (typeof schema.const === 'string') {
269
+ return 'string'
270
+ } else if (typeof schema.const === 'number') {
271
+ return 'number'
272
+ } else if (typeof schema.const === 'boolean') {
273
+ return 'boolean'
274
+ } else {
275
+ return 'null'
276
+ }
277
+ }
278
+
279
+ return schema.type as JSONSchemaType
280
+ }
@@ -0,0 +1,33 @@
1
+ import type { OpenAPIV3 } from '@scalar/openapi-types'
2
+ import type { SecurityUi, SecurityUiItem } from '../types'
3
+
4
+ const NO_SECURITY = 'None'
5
+
6
+ export function getSecurityUi(security: OpenAPIV3.SecuritySchemeObject[], securitySchemes: Record<string, OpenAPIV3.SecuritySchemeObject>): SecurityUi {
7
+ const securityUi: SecurityUi = []
8
+
9
+ if (!security) {
10
+ return securityUi
11
+ }
12
+
13
+ security.forEach((securityOption) => {
14
+ const andItems = {
15
+ id: Object.keys(securityOption).length ? Object.keys(securityOption).join('|') : NO_SECURITY,
16
+ schemes: {},
17
+ } as SecurityUiItem
18
+
19
+ Object.keys(securityOption).forEach((key) => {
20
+ if (securitySchemes && securitySchemes[key]) {
21
+ andItems.schemes[key] = securitySchemes[key]
22
+ }
23
+ })
24
+
25
+ securityUi.push(andItems)
26
+ })
27
+
28
+ if (securityUi.length === 1 && securityUi[0].id === NO_SECURITY && Object.keys(securityUi[0].schemes).length === 0) {
29
+ return []
30
+ }
31
+
32
+ return securityUi
33
+ }
@@ -0,0 +1,24 @@
1
+ import type { Ref } from 'vue'
2
+ import type { ParsedOperation } from '../types'
3
+ import { ref } from 'vue'
4
+ import { useTheme } from '../composables/useTheme'
5
+
6
+ export interface OperationData {
7
+ operationId: string
8
+ security: {
9
+ selectedSchemeId: Ref<string>
10
+ }
11
+ }
12
+
13
+ export function initOperationData(operation: ParsedOperation): OperationData {
14
+ const firstSecurityScheme = operation.securityUi?.[0]?.id || ''
15
+
16
+ const defaultSecurityScheme = useTheme().getSecurityDefaultScheme() || firstSecurityScheme
17
+
18
+ return {
19
+ operationId: operation.operationId,
20
+ security: {
21
+ selectedSchemeId: ref(operation.securityUi?.some(s => s.id === defaultSecurityScheme) ? defaultSecurityScheme : firstSecurityScheme),
22
+ },
23
+ }
24
+ }
@@ -0,0 +1,19 @@
1
+ import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
2
+
3
+ export function generateMissingOperationIds(spec: OpenAPI.Document): OpenAPI.Document {
4
+ spec.paths = spec.paths || {}
5
+
6
+ for (const path of Object.keys(spec.paths)) {
7
+ const pathValue = spec.paths[path] as Record<string, OpenAPIV3.OperationObject>
8
+
9
+ for (const verb of Object.keys(pathValue) as OpenAPIV3.HttpMethods[]) {
10
+ const operation = pathValue[verb]
11
+
12
+ if (!operation.operationId) {
13
+ operation.operationId = `${verb}${path.replace(/\//g, '-')}`
14
+ }
15
+ }
16
+ }
17
+
18
+ return spec
19
+ }
@@ -0,0 +1,20 @@
1
+ import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
2
+ import type { OperationObject } from '../../types'
3
+
4
+ export function generateMissingSummary(spec: OpenAPI.Document): OpenAPI.Document {
5
+ spec.paths = spec.paths || {}
6
+
7
+ for (const path of Object.keys(spec.paths)) {
8
+ const pathValue = spec.paths[path] as Record<string, OperationObject>
9
+
10
+ for (const verb of Object.keys(pathValue) as OpenAPIV3.HttpMethods[]) {
11
+ const operation = pathValue[verb] as OperationObject
12
+
13
+ if (!operation.summary) {
14
+ operation.summary = `${verb.toUpperCase()} ${path}`
15
+ }
16
+ }
17
+ }
18
+
19
+ return spec
20
+ }
@@ -0,0 +1,36 @@
1
+ import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
2
+
3
+ export function generateMissingTags({
4
+ spec,
5
+ defaultTag = 'Default',
6
+ defaultTagDescription = '',
7
+ }: {
8
+ spec: OpenAPI.Document
9
+ defaultTag?: string
10
+ defaultTagDescription?: string
11
+ }): OpenAPI.Document {
12
+ const operationTags = new Set<string>()
13
+
14
+ spec.paths = spec.paths || {}
15
+
16
+ for (const path of Object.values(spec.paths)) {
17
+ for (const verb of Object.keys(path) as OpenAPIV3.HttpMethods[]) {
18
+ const operation = path[verb]
19
+ const tags = operation.tags || [defaultTag]
20
+ operation.tags = tags
21
+ tags.forEach((tag: string) => operationTags.add(tag))
22
+ }
23
+ }
24
+
25
+ spec.tags = spec.tags || []
26
+ if (
27
+ operationTags.has(defaultTag)
28
+ && !spec.tags.find((tag: any) => tag.name === defaultTag)) {
29
+ spec.tags.push({
30
+ name: defaultTag,
31
+ description: defaultTagDescription,
32
+ })
33
+ }
34
+
35
+ return spec
36
+ }
@@ -0,0 +1,35 @@
1
+ import type { OpenAPI } from '@scalar/openapi-types'
2
+ import { generateMissingOperationIds } from './generateMissingOperationIds'
3
+ import { generateMissingSummary } from './generateMissingSummary'
4
+ import { generateMissingTags } from './generateMissingTags'
5
+
6
+ export function prepareOpenAPI({
7
+ spec,
8
+ defaultTag = undefined,
9
+ defaultTagDescription = undefined,
10
+ }: {
11
+ spec: OpenAPI.Document
12
+ defaultTag?: string
13
+ defaultTagDescription?: string
14
+ }): OpenAPI.Document {
15
+ if (import.meta.env.VITE_DEBUG) {
16
+ console.warn('Transforming OpenAPI spec:', spec)
17
+ }
18
+
19
+ if (!spec) {
20
+ return {}
21
+ }
22
+
23
+ if (!spec.openapi || !spec.openapi.startsWith('3.')) {
24
+ console.warn('Only OpenAPI 3.x is supported')
25
+ return {}
26
+ }
27
+
28
+ if (spec?.paths) {
29
+ spec = generateMissingOperationIds(spec)
30
+ spec = generateMissingSummary(spec)
31
+ spec = generateMissingTags({ spec, defaultTag, defaultTagDescription })
32
+ }
33
+
34
+ return Object.assign({}, spec)
35
+ }
@@ -0,0 +1,131 @@
1
+ import type { OpenAPI, OpenAPIV3 } from '@scalar/openapi-types'
2
+ import type { JSONSchema } from '@trojs/openapi-dereference'
3
+ import type { ParsedContent, ParsedOpenAPI, ParsedOperation } from '../types'
4
+ import { dereferenceSync } from '@trojs/openapi-dereference'
5
+ import { merge } from 'allof-merge'
6
+ import { getSchemaExample } from './examples/getSchemaExample'
7
+ import { getSchemaUi } from './getSchemaUi'
8
+ import { getSecurityUi } from './getSecurityUi'
9
+
10
+ function safelyMergeSpec(spec: OpenAPI.Document): ParsedOpenAPI {
11
+ try {
12
+ return merge(spec) as ParsedOpenAPI
13
+ } catch (error: any) {
14
+ console.warn('Failed to merge OpenAPI spec:', error.message)
15
+ return spec as ParsedOpenAPI
16
+ }
17
+ }
18
+
19
+ function safelyDereferenceSpec(spec: OpenAPI.Document): ParsedOpenAPI {
20
+ try {
21
+ return dereferenceSync(spec as JSONSchema) as ParsedOpenAPI
22
+ } catch (error: any) {
23
+ console.warn('Failed to dereference OpenAPI spec:', error.message)
24
+ return spec as ParsedOpenAPI
25
+ }
26
+ }
27
+
28
+ function safelyGenerateSchemaUi(spec: ParsedOpenAPI): ParsedOpenAPI {
29
+ try {
30
+ return generateSchemaUi(spec)
31
+ } catch (error: any) {
32
+ console.warn('Failed to generate schema UI:', error.message)
33
+ return spec
34
+ }
35
+ }
36
+
37
+ export function processOpenAPI(spec: OpenAPI.Document): ParsedOpenAPI {
38
+ if (import.meta.env.VITE_DEBUG) {
39
+ console.warn('Processing OpenAPI spec:', spec)
40
+ }
41
+
42
+ let parsedSpec = safelyMergeSpec(spec)
43
+ parsedSpec = safelyDereferenceSpec(parsedSpec)
44
+ parsedSpec = safelyGenerateSecurityUi(parsedSpec)
45
+ parsedSpec = safelyGenerateSchemaUi(parsedSpec)
46
+
47
+ return { ...parsedSpec }
48
+ }
49
+
50
+ function safelyGenerateSecurityUi(spec: ParsedOpenAPI): ParsedOpenAPI {
51
+ if (!spec?.paths) {
52
+ return spec
53
+ }
54
+
55
+ for (const path of Object.values(spec.paths)) {
56
+ for (const verb of Object.keys(path) as OpenAPIV3.HttpMethods[]) {
57
+ const operation = path[verb] as ParsedOperation
58
+
59
+ if (!operation) {
60
+ continue
61
+ }
62
+
63
+ operation.securityUi = getSecurityUi(operation.security ?? spec.security, spec.components?.securitySchemes || {})
64
+ }
65
+ }
66
+
67
+ return spec
68
+ }
69
+
70
+ function generateSchemaUi(spec: ParsedOpenAPI): ParsedOpenAPI {
71
+ if (!spec?.paths) {
72
+ return spec
73
+ }
74
+
75
+ for (const path of Object.values(spec.paths)) {
76
+ for (const verb of Object.keys(path) as OpenAPIV3.HttpMethods[]) {
77
+ const operation = path[verb] as ParsedOperation
78
+
79
+ if (!operation) {
80
+ continue
81
+ }
82
+
83
+ enhanceRequestBody(operation)
84
+ enhanceResponses(operation)
85
+ }
86
+ }
87
+
88
+ return spec
89
+ }
90
+
91
+ function enhanceRequestBody(operation: ParsedOperation): void {
92
+ if (!operation.requestBody) {
93
+ return
94
+ }
95
+
96
+ for (const [contentType, content] of Object.entries(operation.requestBody.content || {})) {
97
+ const parsedContent = content as ParsedContent
98
+
99
+ if (!parsedContent.schema) {
100
+ continue
101
+ }
102
+
103
+ parsedContent.ui = getSchemaUi(parsedContent.schema)
104
+ parsedContent.examples = {
105
+ ...(parsedContent.examples || {}),
106
+ ...getSchemaExample(contentType, parsedContent.ui, true),
107
+ }
108
+ }
109
+ }
110
+
111
+ function enhanceResponses(operation: ParsedOperation): void {
112
+ if (!operation.responses) {
113
+ return
114
+ }
115
+
116
+ for (const response of Object.values(operation.responses || {})) {
117
+ for (const [contentType, content] of Object.entries(response.content || {})) {
118
+ const parsedContent = content as ParsedContent
119
+
120
+ if (!parsedContent.schema) {
121
+ continue
122
+ }
123
+
124
+ parsedContent.ui = getSchemaUi(parsedContent.schema)
125
+ parsedContent.examples = {
126
+ ...(parsedContent.examples || {}),
127
+ ...getSchemaExample(contentType, parsedContent.ui, true),
128
+ }
129
+ }
130
+ }
131
+ }