@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,150 @@
1
+ import type { OARequest } from './request'
2
+
3
+ export function getCustomQuery(query: Record<string, string | string[]>) {
4
+ let preparedQuery = ''
5
+
6
+ for (const [key, value] of Object.entries(query ?? {})) {
7
+ if (typeof value === 'string') {
8
+ preparedQuery += `&${key}=${encodeURIComponent(value)}`
9
+ } else if (Array.isArray(value)) {
10
+ value.forEach((value) => {
11
+ preparedQuery += `&${key}[]=${encodeURIComponent(value)}`
12
+ })
13
+ }
14
+ }
15
+
16
+ return preparedQuery
17
+ }
18
+
19
+ export function generateCodeSampleCurl(request: OARequest) {
20
+ const headersArgument = generateHeadersArgument(request.headers)
21
+
22
+ let output = `curl ${generateMethodArgument(request.method)}'${request.url}${Object.keys(request.query ?? {}).length ? `?${getCustomQuery(request.query!)}` : ''}'`
23
+
24
+ if (headersArgument.params) {
25
+ output += ` \\\n${headersArgument.params}`
26
+ }
27
+
28
+ if (request.body) {
29
+ output += ` \\\n${generateBody(JSON.stringify(request.body, null, 2))}`
30
+ }
31
+
32
+ if (headersArgument.isEncode) {
33
+ output += ` \\\n${generateCompress(headersArgument.isEncode)}`
34
+ }
35
+
36
+ return output.trim()
37
+ }
38
+
39
+ /**
40
+ * Based on [fetch-to-curl](https://github.com/leoek/fetch-to-curl]
41
+ * @author [leoek](https://github.com/leoek)
42
+ * @author [enzonotario](https://github.com/enzonotario)
43
+ */
44
+
45
+ interface HeaderParams {
46
+ isEncode: boolean
47
+ params: string
48
+ }
49
+
50
+ function removeLastBackslash(str: string): string {
51
+ return str.replace(/ \\$/, '')
52
+ }
53
+
54
+ function generateMethodArgument(method: string): string {
55
+ if (!method) {
56
+ return ''
57
+ }
58
+
59
+ const type: { [key: string]: string } = {
60
+ GET: '-X GET \\\n',
61
+ POST: '-X POST \\\n',
62
+ PUT: '-X PUT \\\n',
63
+ PATCH: '-X PATCH \\\n',
64
+ DELETE: '-X DELETE \\\n',
65
+ HEAD: '-X HEAD \\\n',
66
+ OPTIONS: '-X OPTIONS\\\n ',
67
+ }
68
+
69
+ return type[method.toUpperCase()] || ''
70
+ }
71
+
72
+ function isInstanceOfHeaders(val: any): boolean {
73
+ if (typeof Headers !== 'function') {
74
+ /**
75
+ * Environment does not support the Headers constructor
76
+ * old internet explorer?
77
+ */
78
+ return false
79
+ }
80
+ return val instanceof Headers
81
+ }
82
+
83
+ function getHeaderString(name: string, val: any): string {
84
+ return ` -H "${name}: ${`${val}`.replace(/(\\|")/g, '\\$1')}"`
85
+ }
86
+
87
+ function generateHeadersArgument(headers?: any): HeaderParams {
88
+ if (!headers || Object.keys(headers).length === 0) {
89
+ return {
90
+ params: '',
91
+ isEncode: false,
92
+ }
93
+ }
94
+
95
+ let isEncode = false
96
+ let headerParam = ''
97
+ if (isInstanceOfHeaders(headers)) {
98
+ headers.forEach((val: any, name: string) => {
99
+ if (name.toLocaleLowerCase() !== 'content-length') {
100
+ headerParam += `${getHeaderString(name, val)} \\\n`
101
+ }
102
+ if (name.toLocaleLowerCase() === 'accept-encoding') {
103
+ isEncode = true
104
+ }
105
+ })
106
+ } else if (headers) {
107
+ Object.keys(headers).forEach((name) => {
108
+ if (name.toLocaleLowerCase() !== 'content-length') {
109
+ headerParam += `${getHeaderString(name, headers[name])} \\\n`
110
+ }
111
+ if (name.toLocaleLowerCase() === 'accept-encoding') {
112
+ isEncode = true
113
+ }
114
+ })
115
+ }
116
+
117
+ headerParam = headerParam.trim()
118
+
119
+ headerParam = removeLastBackslash(headerParam)
120
+
121
+ headerParam = ` ${headerParam}`
122
+
123
+ return {
124
+ params: headerParam,
125
+ isEncode,
126
+ }
127
+ }
128
+
129
+ function escapeBody(body: any): string {
130
+ if (typeof body !== 'string') {
131
+ return body
132
+ }
133
+ return body.replace(/'/g, `'\\''`)
134
+ }
135
+
136
+ function generateBody(body: any): string {
137
+ if (!body) {
138
+ return ''
139
+ }
140
+
141
+ if (typeof body === 'object') {
142
+ return ` --data '${escapeBody(JSON.stringify(body))}'`
143
+ }
144
+
145
+ return ` --data '${escapeBody(body)}'`
146
+ }
147
+
148
+ function generateCompress(isEncode: boolean): string {
149
+ return isEncode ? ' --compressed' : ''
150
+ }
@@ -0,0 +1,29 @@
1
+ import type { OARequest } from './request'
2
+ import { getCustomQuery } from './generateCodeSampleCurl'
3
+
4
+ export function generateCodeSampleJavaScript(request: OARequest) {
5
+ const queryString = Object.keys(request.query ?? {}).length ? `?${getCustomQuery(request.query!)}` : ''
6
+
7
+ const jsFetchOptions = Object.fromEntries(
8
+ Object.entries({
9
+ method: request.method !== 'GET' ? request.method : undefined,
10
+ headers: Object.keys(request.headers ?? {}).length ? 'REPLACE_HEADERS' : undefined,
11
+ body: request.body ? 'REPLACE_BODY' : undefined,
12
+ })
13
+ .filter(([_, v]) => v !== undefined), // Filter undefined values.
14
+ )
15
+
16
+ const optionsString = JSON.stringify(jsFetchOptions)
17
+ .replace(/"([^"]+)":/g, '$1:') // Remove quotes from keys.
18
+ .replace(/"/g, '\'') // Replace double quotes with single quotes.
19
+ .replace(/REPLACE_BODY/, JSON.stringify(request.body))
20
+ .replace(
21
+ /'REPLACE_HEADERS'/,
22
+ JSON.stringify(request.headers ?? {})
23
+ .replace(/"/g, '\''),
24
+ )
25
+
26
+ return `fetch('${request.url}${queryString}'${Object.keys(jsFetchOptions).length ? `, ${optionsString}` : ''})
27
+ .then(response => response.json())
28
+ .then(data => console.log(data));`
29
+ }
@@ -0,0 +1,50 @@
1
+ import type { OARequest } from './request'
2
+
3
+ export function generateCodeSamplePhp({ url, method, headers, body, query }: OARequest): string {
4
+ let phpCode = `<?php\n`
5
+
6
+ phpCode += `$url = '${url}';\n`
7
+
8
+ phpCode += `$method = '${method}';\n`
9
+
10
+ if (headers && Object.keys(headers).length > 0) {
11
+ phpCode += `$headers = [\n`
12
+ for (const [key, value] of Object.entries(headers)) {
13
+ phpCode += ` '${key}' => '${value}',\n`
14
+ }
15
+ phpCode += `];\n`
16
+ }
17
+
18
+ if (query && Object.keys(query).length > 0) {
19
+ phpCode += `$query = http_build_query([\n`
20
+ for (const [key, value] of Object.entries(query)) {
21
+ phpCode += ` '${key}' => '${value}',\n`
22
+ }
23
+ phpCode += `]);\n`
24
+ }
25
+
26
+ if (body) {
27
+ phpCode += `$body = json_encode(${JSON.stringify(body)});\n`
28
+ }
29
+
30
+ phpCode += `\n$ch = curl_init();\n`
31
+ phpCode += `curl_setopt($ch, CURLOPT_URL, $url${Object.keys(query ?? {}).length ? ` . '?' . $query` : ''});\n`
32
+ phpCode += `curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n`
33
+ phpCode += `curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);\n`
34
+
35
+ if (headers && Object.keys(headers).length > 0) {
36
+ phpCode += `curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\n`
37
+ }
38
+
39
+ if (body) {
40
+ phpCode += `curl_setopt($ch, CURLOPT_POSTFIELDS, $body);\n`
41
+ }
42
+
43
+ phpCode += `\n$response = curl_exec($ch);\n`
44
+ phpCode += `curl_close($ch);\n`
45
+ phpCode += `\necho $response;\n`
46
+
47
+ phpCode += `?>`
48
+
49
+ return phpCode
50
+ }
@@ -0,0 +1,32 @@
1
+ import type { OARequest } from './request'
2
+
3
+ export function generateCodeSamplePython(request: OARequest): string {
4
+ const { url, method, headers, body, query } = request
5
+
6
+ let queryString
7
+ if (Object.keys(query ?? {}).length) {
8
+ queryString = `params = ${JSON.stringify(query, null, 4).replace(/"([^"]+)":/g, '\'$1\':').replace(/"/g, '\'')}`
9
+ }
10
+
11
+ let headersString
12
+ if (Object.keys(headers ?? {}).length) {
13
+ headersString = `headers = ${JSON.stringify(headers, null, 4).replace(/"([^"]+)":/g, '\'$1\':').replace(/"/g, '\'')}`
14
+ }
15
+
16
+ let bodyString
17
+ if (body) {
18
+ bodyString = `data = ${JSON.stringify(body, null, 4).replace(/"([^"]+)":/g, '\'$1\':').replace(/"/g, '\'')}`
19
+ }
20
+
21
+ return (`import requests
22
+
23
+ url = '${url}'
24
+ ${queryString || ''}
25
+ ${headersString || ''}
26
+ ${bodyString || ''}
27
+
28
+ response = requests.${method.toLowerCase()}(url${queryString ? ', params=params' : ''}${headersString ? ', headers=headers' : ''}${bodyString ? ', json=data' : ''})
29
+ print(response.json())
30
+ `)
31
+ .replace(/\n{2,}/g, '\n\n') // Remove extra newlines.
32
+ }
@@ -0,0 +1,25 @@
1
+ export interface IOARequest {
2
+ url: string
3
+ method: string
4
+ headers: Record<string, string>
5
+ body: any
6
+ query: Record<string, string>
7
+ }
8
+
9
+ export class OARequest {
10
+ constructor(
11
+ public url: string = '',
12
+ public method: string = 'GET',
13
+ public headers?: Record<string, string>,
14
+ public body?: any,
15
+ public query?: Record<string, string | string[]>,
16
+ ) {
17
+ if (!headers) {
18
+ this.headers = {}
19
+ }
20
+
21
+ if (!query) {
22
+ this.query = {}
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,30 @@
1
+ const VALIDATION_KEYWORDS: ReadonlyArray<string> = [
2
+ 'exclusiveMaximum',
3
+ 'exclusiveMinimum',
4
+ 'format',
5
+ 'maximum',
6
+ 'maxItems',
7
+ 'maxLength',
8
+ 'maxProperties',
9
+ 'minimum',
10
+ 'minItems',
11
+ 'minLength',
12
+ 'minProperties',
13
+ 'multipleOf',
14
+ 'pattern',
15
+ 'uniqueItems',
16
+ 'default',
17
+ ]
18
+
19
+ export function hasConstraints(property: Record<string, unknown>): boolean {
20
+ return Object.keys(property).some(key => VALIDATION_KEYWORDS.includes(key))
21
+ }
22
+
23
+ export function getConstraints(property: Record<string, unknown>): Record<string, unknown> {
24
+ return Object.entries(property)
25
+ .filter(([key]) => VALIDATION_KEYWORDS.includes(key))
26
+ .reduce((constraints: Record<string, unknown>, [key, value]) => {
27
+ constraints[key] = value
28
+ return constraints
29
+ }, {})
30
+ }
@@ -0,0 +1,18 @@
1
+ import type { OpenAPI } from '@scalar/openapi-types'
2
+ import { OpenApi } from './OpenApi'
3
+ import { prepareOpenAPI } from './prepareOpenAPI/prepareOpenAPI'
4
+ import { processOpenAPI } from './processOpenAPI'
5
+
6
+ export function createOpenApiInstance({
7
+ spec,
8
+ defaultTag = undefined,
9
+ defaultTagDescription = undefined,
10
+ }: {
11
+ spec: OpenAPI.Document
12
+ defaultTag?: string
13
+ defaultTagDescription?: string
14
+ }) {
15
+ const transformedSpec = prepareOpenAPI({ spec, defaultTag, defaultTagDescription })
16
+ const parsedSpec = processOpenAPI(transformedSpec)
17
+ return OpenApi({ spec, transformedSpec, parsedSpec })
18
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Inspired by https://github.com/DanHulton/vue-deepunref and converted to TypeScript
3
+ */
4
+
5
+ import type { Ref } from 'vue'
6
+ import { isRef, unref } from 'vue'
7
+
8
+ type DeepUnrefArray<T> = Array<DeepUnref<T>>
9
+
10
+ type DeepUnrefObject<T> = {
11
+ [K in keyof T]: DeepUnref<T[K]>;
12
+ }
13
+
14
+ export type DeepUnref<T> = T extends Ref<infer V>
15
+ ? DeepUnref<V>
16
+ : T extends Array<any>
17
+ ? DeepUnrefArray<T[number]>
18
+ : T extends object
19
+ ? DeepUnrefObject<T>
20
+ : T
21
+
22
+ const isObject = (val: unknown): val is Record<string, unknown> =>
23
+ val !== null && typeof val === 'object'
24
+
25
+ const isArray = Array.isArray
26
+
27
+ /**
28
+ * Deeply unref a value, recursing into objects and arrays.
29
+ */
30
+ export function deepUnref<T>(val: T): DeepUnref<T> {
31
+ const checkedVal = isRef(val) ? unref(val) : val
32
+
33
+ if (!isObject(checkedVal)) {
34
+ return checkedVal as DeepUnref<T>
35
+ }
36
+
37
+ if (isArray(checkedVal)) {
38
+ return unrefArray(checkedVal) as DeepUnref<T>
39
+ }
40
+
41
+ return unrefObject(checkedVal) as DeepUnref<T>
42
+ }
43
+
44
+ /**
45
+ * Unref a value, recursing into it if it's an object.
46
+ */
47
+ function smartUnref<T>(val: T): DeepUnref<T> {
48
+ // Non-ref object? Go deeper!
49
+ if (val !== null && !isRef(val) && typeof val === 'object') {
50
+ return deepUnref(val)
51
+ }
52
+
53
+ return unref(val as any) as DeepUnref<T>
54
+ }
55
+
56
+ /**
57
+ * Unref an array, recursively.
58
+ */
59
+ function unrefArray<T>(arr: T[]): DeepUnrefArray<T> {
60
+ return arr.map(item => smartUnref(item)) as DeepUnrefArray<T>
61
+ }
62
+
63
+ /**
64
+ * Unref an object, recursively.
65
+ */
66
+ function unrefObject<T extends Record<string, unknown>>(obj: T): DeepUnrefObject<T> {
67
+ const unreffed = {} as DeepUnrefObject<T>
68
+
69
+ // Object? un-ref it!
70
+ Object.keys(obj).forEach((key) => {
71
+ // @ts-expect-error: TS doesn't know that `key` is a key of `T`
72
+ unreffed[key as keyof T] = smartUnref(obj[key])
73
+ })
74
+
75
+ return unreffed
76
+ }
@@ -0,0 +1,31 @@
1
+ export function getPropertyExample(property: any): any {
2
+ if (property?.example !== undefined) {
3
+ return property.example
4
+ }
5
+
6
+ if (property?.examples && property?.examples?.length > 0) {
7
+ return property.examples[0]
8
+ }
9
+
10
+ if (property?.schema?.example !== undefined) {
11
+ return property.schema.example
12
+ }
13
+
14
+ if (property?.schema?.examples && property?.schema?.examples?.length > 0) {
15
+ const firstExample = property.schema.examples[0]
16
+
17
+ if (firstExample) {
18
+ return firstExample
19
+ }
20
+ }
21
+
22
+ if (property?.subexample !== undefined) {
23
+ return property.subexample
24
+ }
25
+
26
+ if (property?.subexamples && property?.subexamples?.length > 0) {
27
+ return property.subexamples[0]
28
+ }
29
+
30
+ return null
31
+ }
@@ -0,0 +1,27 @@
1
+ export function getPropertyExamples(property: any): any {
2
+ if (property?.examples !== undefined) {
3
+ return property.examples
4
+ }
5
+
6
+ if (property?.schema?.examples !== undefined) {
7
+ return property.schema.examples
8
+ }
9
+
10
+ if (property?.example !== undefined) {
11
+ return [property.example]
12
+ }
13
+
14
+ if (property?.subexamples !== undefined) {
15
+ return property.subexamples
16
+ }
17
+
18
+ if (property?.schema?.example !== undefined) {
19
+ return [property.schema.example]
20
+ }
21
+
22
+ if (property?.subexample !== undefined) {
23
+ return [property.subexample]
24
+ }
25
+
26
+ return null
27
+ }
@@ -0,0 +1,38 @@
1
+ import type { OAExampleObject } from '../../types'
2
+ import type { OAProperty } from '../getSchemaUi'
3
+ import { getSchemaUiJson } from './getSchemaUiJson'
4
+ import { getSchemaUiXml } from './getSchemaUiXml'
5
+
6
+ export function getSchemaExample(contentType: string, uiProperties: OAProperty[] | OAProperty, useExample = false): Record<string, OAExampleObject> {
7
+ if (isXml(contentType)) {
8
+ return {
9
+ XML: getSchemaExampleValue(contentType, uiProperties, useExample),
10
+ }
11
+ }
12
+
13
+ return {
14
+ JSON: getSchemaExampleValue(contentType, uiProperties, useExample),
15
+ }
16
+ }
17
+
18
+ function getSchemaExampleValue(contentType: string, uiProperties: OAProperty[] | OAProperty, useExample = false): OAExampleObject {
19
+ if (isXml(contentType)) {
20
+ return {
21
+ summary: 'XML',
22
+ description: '',
23
+ value: getSchemaUiXml(uiProperties, useExample),
24
+ isAutogenerated: true,
25
+ } as OAExampleObject
26
+ }
27
+
28
+ return {
29
+ summary: 'JSON',
30
+ description: '',
31
+ value: getSchemaUiJson(uiProperties, useExample),
32
+ isAutogenerated: true,
33
+ } as OAExampleObject
34
+ }
35
+
36
+ function isXml(contentType: string): boolean {
37
+ return contentType.toLowerCase().match(/^(text|application)\/.*xml($|;|\\+)/) !== null
38
+ }
@@ -0,0 +1,150 @@
1
+ import type { OAProperty } from '../getSchemaUi'
2
+ import { literalTypes } from '../../index'
3
+ import { getPropertyExample } from './getPropertyExample'
4
+
5
+ export function getSchemaUiJson(uiProperties: OAProperty[] | OAProperty, useExample = false): any {
6
+ return schemaUiToJson(uiProperties, useExample)
7
+ }
8
+
9
+ function schemaUiToJson(uiProperties: OAProperty[] | OAProperty, useExample: boolean): any {
10
+ if (Array.isArray(uiProperties)) {
11
+ return uiProperties.map(property => uiPropertyToJson(property, useExample))
12
+ }
13
+ return uiPropertyToJson(uiProperties, useExample)
14
+ }
15
+
16
+ function uiPropertyToJson(property: OAProperty, useExample: boolean): any {
17
+ if (property.meta?.isCircularReference) {
18
+ return '[Circular Reference]'
19
+ }
20
+
21
+ if (property.meta?.isConstant) {
22
+ return uiPropertyConstantToJson(property)
23
+ }
24
+
25
+ if (isSingleType(property, 'array')) {
26
+ return uiPropertyArrayToJson(property, useExample)
27
+ }
28
+
29
+ if (isLiteralType(property)) {
30
+ return uiPropertyLiteralToJson(property, useExample)
31
+ }
32
+
33
+ if (hasAllLiteralTypes(property)) {
34
+ // Return the first literal type, prioritizing a non-null type.
35
+ return uiPropertyLiteralToJson({
36
+ ...property,
37
+ types: [property.types.find(type => type !== 'null') ?? 'null'],
38
+ }, useExample)
39
+ }
40
+
41
+ if (isSingleType(property, 'object')) {
42
+ if (isOneOfProperty(property)) {
43
+ return resolveOneOfProperty(property, useExample)
44
+ }
45
+ return uiPropertyObjectToJson(property.properties || [], useExample)
46
+ }
47
+
48
+ return {}
49
+ }
50
+
51
+ function uiPropertyArrayToJson(property: OAProperty, useExample: boolean): any {
52
+ if (isOneOfProperty(property)) {
53
+ return [resolveOneOfProperty(property, useExample)]
54
+ }
55
+
56
+ if (property.properties && Array.isArray(property.properties)) {
57
+ return [uiPropertyObjectToJson(property.properties, useExample)]
58
+ }
59
+
60
+ if (property.subtype) {
61
+ if (property.subtype === 'object' && property.properties) {
62
+ return [uiPropertyObjectToJson(property.properties as OAProperty[], useExample)]
63
+ }
64
+
65
+ return useExample ? [getPropertyExample(property) ?? getDefaultValueForType(property.subtype)] : [getDefaultValueForType(property.subtype)]
66
+ }
67
+
68
+ return []
69
+ }
70
+
71
+ function uiPropertyObjectToJson(properties: OAProperty[], useExample: boolean): Record<string, any> {
72
+ return properties.reduce((result: { [key: string]: any }, property) => {
73
+ if (isSingleType(property, 'array')) {
74
+ result[property.name] = uiPropertyArrayToJson(property, useExample)
75
+ } else {
76
+ result[property.name] = uiPropertyToJson(property, useExample)
77
+ }
78
+ return result
79
+ }, {})
80
+ }
81
+
82
+ function uiPropertyLiteralToJson(property: OAProperty, useExample: boolean): any {
83
+ if (useExample) {
84
+ const example = getPropertyExample(property)
85
+ if (example != null) {
86
+ return example
87
+ }
88
+ }
89
+
90
+ return getDefaultValueForType(property.types[0] ?? 'string')
91
+ }
92
+
93
+ /**
94
+ * Constants always have an example (the constant), so `useExample` does not apply here.
95
+ */
96
+ function uiPropertyConstantToJson(property: OAProperty): any {
97
+ const example = getPropertyExample(property)
98
+
99
+ if (example != null) {
100
+ return example
101
+ }
102
+
103
+ return getDefaultValueForType(property.types[0] ?? 'string')
104
+ }
105
+
106
+ function getDefaultValueForType(type: string): any {
107
+ switch (type) {
108
+ case 'string':
109
+ return 'string'
110
+ case 'number':
111
+ case 'integer':
112
+ return 0
113
+ case 'float':
114
+ return 0.0
115
+ case 'boolean':
116
+ return true
117
+ case 'null':
118
+ return null
119
+ case 'array':
120
+ return []
121
+ case 'object':
122
+ return {}
123
+ default:
124
+ return undefined
125
+ }
126
+ }
127
+
128
+ function isSingleType(property: OAProperty, type: string): boolean {
129
+ return property.types?.length === 1 && property.types[0] === type
130
+ }
131
+
132
+ function isLiteralType(property: OAProperty): boolean {
133
+ return property.types?.length === 1 && property.types[0] != null && literalTypes.includes(property.types[0])
134
+ }
135
+
136
+ function hasAllLiteralTypes(property: OAProperty): boolean {
137
+ return property.types?.every(type => type && literalTypes.includes(type))
138
+ }
139
+
140
+ function isOneOfProperty(property: OAProperty): boolean {
141
+ return !!property.meta?.isOneOf && Array.isArray(property.properties)
142
+ }
143
+
144
+ function resolveOneOfProperty(property: OAProperty, useExample: boolean): any {
145
+ if (property.properties && property.properties.length > 0) {
146
+ return uiPropertyToJson(property.properties[0], useExample)
147
+ } else {
148
+ return useExample ? getPropertyExample(property) : getDefaultValueForType(property.types[0] ?? 'string')
149
+ }
150
+ }
@@ -0,0 +1,9 @@
1
+ import type { OAProperty } from '../getSchemaUi'
2
+ import { jsXml } from 'json-xml-parse'
3
+ import { getSchemaUiJson } from './getSchemaUiJson'
4
+
5
+ export function getSchemaUiXml(uiProperties: OAProperty[] | OAProperty, useExample = false): any {
6
+ const uiJson = getSchemaUiJson(uiProperties, useExample)
7
+
8
+ return jsXml.toXmlString(uiJson)
9
+ }