@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
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export { theme, useTheme } from './client'
2
+ export { usePaths } from './composables/usePaths'
3
+ export { useSidebar } from './composables/useSidebar'
4
+ export { OpenApi } from './lib/OpenApi'
5
+
6
+ export const httpVerbs = ['get', 'post', 'put', 'delete', 'patch', 'options', 'head']
7
+
8
+ export const literalTypes = ['string', 'number', 'integer', 'boolean', 'null']
package/src/json.css ADDED
@@ -0,0 +1,251 @@
1
+ :root {
2
+ --vjs-caret-color: #6b7280;
3
+ --vjs-border-color: #9ca3af;
4
+ --vjs-key-color: #998418;
5
+ --vjs-value-null-color: #ab5959;
6
+ --vjs-value-undefined-color: #ab5959;
7
+ --vjs-value-number-color: #2f798a;
8
+ --vjs-value-boolean-color: #1e754f;
9
+ --vjs-value-string-color: #b56959;
10
+ --vjs-hover-background-color: #eaebee;
11
+ }
12
+
13
+ .dark {
14
+ --vjs-caret-color: #9ca3af;
15
+ --vjs-border-color: #374151;
16
+ --vjs-key-color: #b8a965;
17
+ --vjs-value-null-color: #cb7676;
18
+ --vjs-value-undefined-color: #cb7676;
19
+ --vjs-value-number-color: #4c9a91;
20
+ --vjs-value-boolean-color: #4d9375;
21
+ --vjs-value-string-color: #c98a7d;
22
+ --vjs-hover-background-color: #24272c;
23
+ }
24
+
25
+ /**
26
+ JSON Viewer
27
+ */
28
+ .vjs-carets {
29
+ @apply text-gray-500 dark:text-gray-400 !important;
30
+ }
31
+ .vjs-tree-node .vjs-indent-unit.has-line {
32
+ border-color: var(--vp-c-divider) !important;
33
+ }
34
+ .vjs-key {
35
+ color: var(--vjs-key-color) !important;
36
+ }
37
+ .dark .vjs-key {
38
+ color: var(--vjs-key-color) !important;
39
+ }
40
+ .vjs-value-null {
41
+ color: var(--vjs-value-null-color) !important;
42
+ }
43
+ .dark .vjs-value-null {
44
+ color: var(--vjs-value-null-color) !important;
45
+ }
46
+ .vjs-value-undefined {
47
+ color: var(--vjs-value-undefined-color) !important;
48
+ }
49
+ .dark .vjs-value-undefined {
50
+ color: var(--vjs-value-undefined-color) !important;
51
+ }
52
+ .vjs-value-number {
53
+ color: var(--vjs-value-number-color) !important;
54
+ }
55
+ .dark .vjs-value-number {
56
+ color: var(--vjs-value-number-color) !important;
57
+ }
58
+ .vjs-value-boolean {
59
+ color: var(--vjs-value-boolean-color) !important;
60
+ }
61
+ .dark .vjs-value-boolean {
62
+ color: var(--vjs-value-boolean-color) !important;
63
+ }
64
+ .vjs-value-string {
65
+ color: var(--vjs-value-string-color) !important;
66
+ }
67
+ .dark .vjs-value-string {
68
+ color: var(--vjs-value-string-color) !important;
69
+ }
70
+ .vjs-tree-node:hover {
71
+ background-color: var(--vjs-hover-background-color) !important;
72
+ }
73
+ .dark .vjs-tree-node:hover {
74
+ background-color: var(--vjs-hover-background-color) !important;
75
+ }
76
+
77
+ /**
78
+ JSON Editor
79
+ */
80
+ .oa-jse-theme-light {
81
+ --jse-theme: light;
82
+
83
+ /* Over all fonts, sizes, and colors */
84
+ --jse-font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell,
85
+ 'Helvetica Neue', sans-serif;
86
+ --jse-font-family-mono: consolas, menlo, monaco, 'Ubuntu Mono', 'source-code-pro', monospace;
87
+ --jse-font-size-mono: 14px;
88
+ --jse-theme-color: var(--vp-c-bg-alt);
89
+ --jse-theme-color-highlight: var(--vp-c-bg);
90
+ --jse-background-color: var(--vp-code-block-bg);
91
+ --jse-text-color: var(--vp-c-text-1);
92
+ --jse-text-color-inverse: var(--vp-c-neutral-inverse);
93
+ --jse-text-readonly: var(--vp-c-text-3);
94
+ --jse-error-color: var(--vp-c-danger-1);
95
+ --jse-warning-color: var(--vp-c-warning-1);
96
+ --jse-info-color: var(--vp-c-brand-1);
97
+
98
+ /* Main, menu, modal */
99
+ --jse-main-border: 0 solid var(--vp-c-border);
100
+ --jse-menu-color: var(--vp-c-text-3);
101
+ --jse-modal-background: var(--vp-c-bg-alt);
102
+ --jse-modal-overlay-background: rgba(0, 0, 0, 0.3);
103
+ --jse-modal-code-background: var(--vp-c-bg-soft);
104
+
105
+ /* Tooltip in text mode */
106
+ --jse-tooltip-color: var(--vp-c-text-1);
107
+ --jse-tooltip-background: var(--vp-c-bg-alt);
108
+ --jse-tooltip-border: 1px solid var(--vp-c-border);
109
+ --jse-tooltip-action-button-color: var(--vp-c-neutral-inverse);
110
+ --jse-tooltip-action-button-background: var(--vp-c-text-1);
111
+
112
+ /* Panels: navigation bar, gutter, search box */
113
+ --jse-panel-background: var(--vp-c-bg-alt);
114
+ --jse-panel-color: var(--vp-c-text-1);
115
+ --jse-panel-color-readonly: var(--vp-c-text-3);
116
+ --jse-panel-border: 1px solid var(--vp-c-border);
117
+ --jse-panel-button-color-highlight: var(--vp-c-text-1);
118
+ --jse-panel-button-background-highlight: var(--vp-c-bg-soft);
119
+
120
+ /* Navigation bar */
121
+ --jse-navigation-bar-background: var(--vp-c-bg);
122
+ --jse-navigation-bar-background-highlight: var(--vp-c-bg-alt);
123
+ --jse-navigation-bar-dropdown-color: var(--vp-c-text-1);
124
+
125
+ /* Context menu */
126
+ --jse-context-menu-background: var(--vp-c-bg-alt);
127
+ --jse-context-menu-background-highlight: var(--vp-c-bg-soft);
128
+ --jse-context-menu-color: var(--vp-c-text-1);
129
+ --jse-context-menu-separator-color: var(--vp-c-border);
130
+ --jse-context-menu-pointer-background: var(--vp-c-bg-alt);
131
+ --jse-context-menu-pointer-background-highlight: var(--vp-c-bg-soft);
132
+ --jse-context-menu-pointer-color: var(--vp-c-text-1);
133
+
134
+ /* Contents: JSON key and values */
135
+ --jse-key-color: var(--vjs-key-color);
136
+ --jse-value-color: var(--vjs-value-string-color);
137
+ --jse-value-color-number: var(--vjs-value-number-color);
138
+ --jse-value-color-boolean: var(--vjs-value-boolean-color);
139
+ --jse-value-color-null: var(--vjs-value-null-color);
140
+ --jse-value-color-string: var(--vjs-value-string-color);
141
+ --jse-value-color-url: var(--vjs-value-string-color);
142
+ --jse-delimiter-color: var(--vjs-caret-color);
143
+ --jse-edit-outline: 2px solid var(--vjs-border-color);
144
+
145
+ /* Selected or hovered */
146
+ --jse-selection-background-color: var(--vjs-hover-background-color);
147
+ --jse-hover-background-color: var(--vjs-hover-background-color);
148
+
149
+ /* Controls in modals: inputs, buttons, and `a` */
150
+ --jse-input-background: var(--vp-c-bg);
151
+ --jse-input-color: var(--vp-c-text-1);
152
+ --jse-input-border: 1px solid var(--vp-c-border);
153
+ --jse-button-background: var(--vp-c-default-3);
154
+ --jse-button-primary-background: var(--vp-c-brand-3);
155
+ --jse-button-primary-color: var(--vp-c-white);
156
+ --jse-button-secondary-background: var(--vp-c-default-3);
157
+ --jse-a-color: var(--vp-c-brand-1);
158
+ --jse-a-color-highlight: var(--vp-c-brand-2);
159
+ }
160
+
161
+ .oa-jse-theme-dark {
162
+ --jse-theme: dark;
163
+
164
+ /* Over all fonts, sizes, and colors */
165
+ --jse-font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell,
166
+ 'Helvetica Neue', sans-serif;
167
+ --jse-font-family-mono: consolas, menlo, monaco, 'Ubuntu Mono', 'source-code-pro', monospace;
168
+ --jse-font-size-mono: 14px;
169
+ --jse-theme-color: var(--vp-c-bg-alt);
170
+ --jse-theme-color-highlight: var(--vp-c-bg);
171
+ --jse-background-color: var(--vp-code-block-bg);
172
+ --jse-text-color: var(--vp-c-text-1);
173
+ --jse-text-color-inverse: var(--vp-c-neutral-inverse);
174
+ --jse-text-readonly: var(--vp-c-text-3);
175
+ --jse-error-color: var(--vp-c-danger-1);
176
+ --jse-warning-color: var(--vp-c-warning-1);
177
+ --jse-info-color: var(--vp-c-brand-1);
178
+
179
+ /* Main, menu, modal */
180
+ --jse-main-border: 0 solid var(--vp-c-border);
181
+ --jse-menu-color: var(--vp-c-text-3);
182
+ --jse-modal-background: var(--vp-c-bg-alt);
183
+ --jse-modal-overlay-background: rgba(0, 0, 0, 0.3);
184
+ --jse-modal-code-background: var(--vp-c-bg-soft);
185
+
186
+ /* Tooltip in text mode */
187
+ --jse-tooltip-color: var(--vp-c-text-1);
188
+ --jse-tooltip-background: var(--vp-c-bg-alt);
189
+ --jse-tooltip-border: 1px solid var(--vp-c-border);
190
+ --jse-tooltip-action-button-color: var(--vp-c-neutral-inverse);
191
+ --jse-tooltip-action-button-background: var(--vp-c-text-1);
192
+
193
+ /* Panels: navigation bar, gutter, search box */
194
+ --jse-panel-background: var(--vp-c-bg-alt);
195
+ --jse-panel-color: var(--vp-c-text-1);
196
+ --jse-panel-color-readonly: var(--vp-c-text-3);
197
+ --jse-panel-border: 1px solid var(--vp-c-border);
198
+ --jse-panel-button-color-highlight: var(--vp-c-text-1);
199
+ --jse-panel-button-background-highlight: var(--vp-c-bg-soft);
200
+
201
+ /* Navigation bar */
202
+ --jse-navigation-bar-background: var(--vp-c-bg);
203
+ --jse-navigation-bar-background-highlight: var(--vp-c-bg-alt);
204
+ --jse-navigation-bar-dropdown-color: var(--vp-c-text-1);
205
+
206
+ /* Context menu */
207
+ --jse-context-menu-background: var(--vp-c-bg-alt);
208
+ --jse-context-menu-background-highlight: var(--vp-c-bg-soft);
209
+ --jse-context-menu-color: var(--vp-c-text-1);
210
+ --jse-context-menu-separator-color: var(--vp-c-border);
211
+ --jse-context-menu-pointer-background: var(--vp-c-bg-alt);
212
+ --jse-context-menu-pointer-background-highlight: var(--vp-c-bg-soft);
213
+ --jse-context-menu-pointer-color: var(--vp-c-text-1);
214
+
215
+ /* Contents: JSON key and values */
216
+ --jse-key-color: var(--vjs-key-color);
217
+ --jse-value-color: var(--vjs-value-string-color);
218
+ --jse-value-color-number: var(--vjs-value-number-color);
219
+ --jse-value-color-boolean: var(--vjs-value-boolean-color);
220
+ --jse-value-color-null: var(--vjs-value-null-color);
221
+ --jse-value-color-string: var(--vjs-value-string-color);
222
+ --jse-value-color-url: var(--vjs-value-string-color);
223
+ --jse-delimiter-color: var(--vjs-caret-color);
224
+ --jse-edit-outline: 2px solid var(--vjs-border-color);
225
+
226
+ /* Selected or hovered */
227
+ --jse-selection-background-color: var(--vjs-hover-background-color);
228
+ --jse-hover-background-color: var(--vjs-hover-background-color);
229
+
230
+ /* Controls in modals: inputs, buttons, and `a` */
231
+ --jse-input-background: var(--vp-c-bg);
232
+ --jse-input-color: var(--vp-c-text-1);
233
+ --jse-input-border: 1px solid var(--vp-c-border);
234
+ --jse-button-background: var(--vp-c-default-3);
235
+ --jse-button-primary-background: var(--vp-c-brand-3);
236
+ --jse-button-primary-color: var(--vp-c-white);
237
+ --jse-button-secondary-background: var(--vp-c-default-3);
238
+ --jse-a-color: var(--vp-c-brand-1);
239
+ --jse-a-color-highlight: var(--vp-c-brand-2);
240
+ }
241
+
242
+ .oa-jse .jse-main {
243
+ @apply rounded p-2;
244
+ background-color: var(--vp-code-block-bg);
245
+ min-height: 100px !important;
246
+ }
247
+ .oa-jse .jse-modal-bg {
248
+ left: calc(var(--vp-sidebar-width) + max(32px, calc((100% - (var(--vp-layout-max-width) - 64px)) / 2))) !important;
249
+ max-width: var(--vp-layout-max-width);
250
+ top: var(--vp-nav-height) !important;
251
+ }
@@ -0,0 +1,254 @@
1
+ import type { OpenAPIV3 } from '@scalar/openapi-types'
2
+ import type { ParsedOpenAPI } from '../types'
3
+ import { httpVerbs } from '../index'
4
+ import { processOpenAPI } from './processOpenAPI'
5
+
6
+ export function OpenApi({
7
+ spec,
8
+ parsedSpec,
9
+ transformedSpec,
10
+ }: {
11
+ spec: any
12
+ transformedSpec?: any
13
+ parsedSpec?: any
14
+ } = {
15
+ spec: null,
16
+ transformedSpec: null,
17
+ parsedSpec: null,
18
+ }) {
19
+ function findOperation(paths: OpenAPIV3.PathsObject, operationId: string) {
20
+ for (const path of Object.values(paths)) {
21
+ for (const verb of httpVerbs) {
22
+ if (path && path[verb]?.operationId === operationId) {
23
+ return path[verb]
24
+ }
25
+ }
26
+ }
27
+ return null
28
+ }
29
+
30
+ function getSpec(): ParsedOpenAPI {
31
+ return parsedSpec ?? transformedSpec ?? spec ?? {}
32
+ }
33
+
34
+ function getParsedSpec() {
35
+ if (!parsedSpec) {
36
+ parsedSpec = processOpenAPI(transformedSpec ?? spec)
37
+ }
38
+
39
+ return parsedSpec
40
+ }
41
+
42
+ function getOperation(operationId: string) {
43
+ if (!getSpec().paths) {
44
+ return null
45
+ }
46
+
47
+ return findOperation(getSpec().paths, operationId)
48
+ }
49
+
50
+ function getOperationPath(operationId: string) {
51
+ if (!getSpec().paths) {
52
+ return null
53
+ }
54
+
55
+ for (const [path, methods] of Object.entries(getSpec().paths)) {
56
+ for (const verb of httpVerbs) {
57
+ if (methods[verb]?.operationId === operationId) {
58
+ return path
59
+ }
60
+ }
61
+ }
62
+
63
+ return null
64
+ }
65
+
66
+ function getOperationMethod(operationId: string) {
67
+ if (!getSpec().paths) {
68
+ return null
69
+ }
70
+
71
+ for (const path of Object.values(getSpec().paths)) {
72
+ for (const verb of httpVerbs) {
73
+ if (path[verb]?.operationId === operationId) {
74
+ return verb
75
+ }
76
+ }
77
+ }
78
+
79
+ return null
80
+ }
81
+
82
+ function getOperationParameters(operationId: string) {
83
+ const operation = getOperation(operationId)
84
+ if (!operation) {
85
+ return []
86
+ }
87
+ return operation.parameters || []
88
+ }
89
+
90
+ function getParsedOperation(operationId: string) {
91
+ if (!getParsedSpec()?.paths) {
92
+ return null
93
+ }
94
+
95
+ return findOperation(getParsedSpec().paths, operationId)
96
+ }
97
+
98
+ function getPaths(): OpenAPIV3.PathsObject {
99
+ return getSpec().paths ?? {}
100
+ }
101
+
102
+ function getPathsByVerbs() {
103
+ const paths = getPaths()
104
+
105
+ return Object.keys(paths)
106
+ .flatMap((path) => {
107
+ return httpVerbs
108
+ .filter((verb: string) => paths && paths[path] && paths[path][verb])
109
+ .map((verb: string) => {
110
+ if (!paths || !paths[path] || !paths[path][verb]) {
111
+ return null
112
+ }
113
+
114
+ const { operationId, summary, tags } = paths[path][verb]
115
+
116
+ return {
117
+ path,
118
+ verb,
119
+ operationId,
120
+ summary,
121
+ tags: tags ?? [],
122
+ }
123
+ })
124
+ })
125
+ }
126
+
127
+ function getInfo() {
128
+ return getSpec().info ?? {}
129
+ }
130
+
131
+ function getExternalDocs() {
132
+ return getSpec().externalDocs ?? {}
133
+ }
134
+
135
+ function getServers() {
136
+ return getSpec().servers ?? []
137
+ }
138
+
139
+ function getOperationServers(operationId: string) {
140
+ const operation = findOperation(getPaths(), operationId)
141
+
142
+ if (!operation) {
143
+ return []
144
+ }
145
+
146
+ const operationPath = getOperationPath(operationId)
147
+
148
+ const pathServers = getSpec().paths[(operationPath ?? '')]?.servers
149
+
150
+ return [
151
+ ...(operation?.servers ?? []),
152
+ ...(pathServers ?? []),
153
+ ...(getSpec().servers ?? []),
154
+ ]
155
+ }
156
+
157
+ function getOperationsTags(): string[] {
158
+ if (!getSpec().paths) {
159
+ return []
160
+ }
161
+
162
+ return Object.values(getSpec().paths).reduce((tags: string[], path) => {
163
+ for (const verb of httpVerbs) {
164
+ if (path[verb]?.tags) {
165
+ path[verb].tags.forEach((tag: string) => {
166
+ if (!tags.includes(tag)) {
167
+ tags.push(tag)
168
+ }
169
+ })
170
+ }
171
+ }
172
+ return tags
173
+ }, [])
174
+ }
175
+
176
+ function filterPaths(predicate: (operation: any) => boolean) {
177
+ const paths = getPaths() ?? {}
178
+ const output: OpenAPIV3.PathsObject = {}
179
+
180
+ for (const [path, methods] of Object.entries(paths)) {
181
+ if (!methods) {
182
+ continue
183
+ }
184
+
185
+ for (const verb of httpVerbs) {
186
+ const operation = methods[verb]
187
+
188
+ if (operation && predicate(operation)) {
189
+ output[path] = output[path] || {}
190
+ output[path][verb] = operation
191
+ }
192
+ }
193
+ }
194
+
195
+ return output
196
+ }
197
+
198
+ function getPathsByTags(tags: string | string[]) {
199
+ const tagList = typeof tags === 'string' ? [tags] : tags
200
+ return filterPaths(operation => operation?.tags?.some((tag: string) => tagList.includes(tag)))
201
+ }
202
+
203
+ function getPathsWithoutTags() {
204
+ return filterPaths(operation => !operation?.tags || operation.tags.length === 0)
205
+ }
206
+
207
+ function getTags() {
208
+ return (getSpec().tags ?? [])
209
+ .map((tag: OpenAPIV3.TagObject) => ({
210
+ name: tag.name ?? null,
211
+ description: tag.description ?? null,
212
+ }))
213
+ }
214
+
215
+ function getFilteredTags(): OpenAPIV3.TagObject[] {
216
+ const operationsTags = getOperationsTags()
217
+
218
+ const tags = getTags()
219
+ .filter((tag: OpenAPIV3.TagObject) => operationsTags.includes(tag.name ?? ''))
220
+
221
+ return tags
222
+ .concat([
223
+ ...operationsTags
224
+ .filter(tag => !tags.map((tag: OpenAPIV3.TagObject) => tag.name).includes(tag))
225
+ .map(tag => ({
226
+ name: tag,
227
+ description: null,
228
+ })),
229
+ ])
230
+ }
231
+
232
+ return {
233
+ spec: getSpec(),
234
+ transformedSpec,
235
+ parsedSpec,
236
+ getOperation,
237
+ getOperationPath,
238
+ getOperationMethod,
239
+ getOperationParameters,
240
+ getParsedOperation,
241
+ getParsedSpec,
242
+ getPaths,
243
+ getPathsByVerbs,
244
+ getInfo,
245
+ getExternalDocs,
246
+ getServers,
247
+ getOperationServers,
248
+ getOperationsTags,
249
+ getPathsByTags,
250
+ getPathsWithoutTags,
251
+ getTags,
252
+ getFilteredTags,
253
+ }
254
+ }
@@ -0,0 +1,149 @@
1
+ import type { OpenAPIV3 } from '@scalar/openapi-types'
2
+ import type { PlaygroundSecurityScheme } from '../../types'
3
+ import { unref } from 'vue'
4
+ import { getPropertyExample } from '../examples/getPropertyExample'
5
+ import { OARequest } from './request'
6
+
7
+ function processParameters(variables: Record<string, string | string[]>, parameters: OpenAPIV3.ParameterObject[], callback: (key: string, value: string | string[]) => void) {
8
+ const parameterNames = new Set(parameters.map(parameter => parameter.name))
9
+ for (const [key, value] of Object.entries(variables)) {
10
+ if (!parameterNames.has(key)) {
11
+ continue
12
+ }
13
+ if (value === undefined || value === '' || value.length === 0) {
14
+ continue
15
+ }
16
+ if (typeof value === 'string' && value.includes(',')) {
17
+ callback(key, value.split(',').map(v => v.trim()))
18
+ continue
19
+ }
20
+ callback(key, value)
21
+ }
22
+ }
23
+
24
+ function getPath(variables: Record<string, string>, pathParameters: OpenAPIV3.ParameterObject[], path: string) {
25
+ let resolvedPath = path
26
+ processParameters(variables, pathParameters, (key, value) => {
27
+ resolvedPath = resolvedPath.replace(`{${key}}`, value as string)
28
+ })
29
+ return resolvedPath
30
+ }
31
+
32
+ function getHeaders(variables: Record<string, string>, headerParameters: OpenAPIV3.ParameterObject[], authorizations: PlaygroundSecurityScheme | PlaygroundSecurityScheme[] | null) {
33
+ const headers = new Headers({})
34
+
35
+ processParameters(variables, headerParameters, (key: string, value: string | string[]) => {
36
+ headers.set(key, value as string)
37
+ })
38
+
39
+ if (authorizations && !Array.isArray(authorizations)) {
40
+ authorizations = [authorizations]
41
+ }
42
+
43
+ if (!authorizations?.length) {
44
+ return headers
45
+ }
46
+
47
+ Object.entries(authorizations).forEach(([_, authorization]) => {
48
+ if (!authorization?.type) {
49
+ return
50
+ }
51
+
52
+ const value = unref(authorization.playgroundValue)
53
+
54
+ if (authorization.type === 'http') {
55
+ headers.set('Authorization', `${authorization.scheme === 'basic' ? 'Basic' : 'Bearer'} ${value}`)
56
+ } else if (authorization.type === 'apiKey') {
57
+ headers.set(authorization.name ?? '', value)
58
+ } else if (authorization.type === 'openIdConnect') {
59
+ headers.set('Authorization', `Bearer ${value}`)
60
+ } else if (authorization.type === 'oauth2') {
61
+ headers.set('Authorization', `Bearer ${value}`)
62
+ } else {
63
+ console.warn('Unknown auth scheme:', authorization)
64
+ }
65
+ })
66
+
67
+ return headers
68
+ }
69
+
70
+ function getQuery(variables: Record<string, string>, queryParameters: OpenAPIV3.ParameterObject[]) {
71
+ const query: Record<string, string | string[]> = {}
72
+
73
+ processParameters(variables, queryParameters, (key: string, value: string | string[]) => {
74
+ query[key] = value
75
+ })
76
+
77
+ return query
78
+ }
79
+
80
+ function setExamplesAsVariables(parameters: OpenAPIV3.ParameterObject[], variables: Record<string, string>) {
81
+ parameters.forEach((parameter) => {
82
+ if (!parameter.name) {
83
+ return
84
+ }
85
+
86
+ if (variables[parameter.name] !== undefined) {
87
+ return
88
+ }
89
+
90
+ const example = getPropertyExample(parameter)
91
+ if (example != null) {
92
+ variables[parameter.name] = example
93
+ }
94
+ })
95
+
96
+ return variables
97
+ }
98
+
99
+ export function buildRequest({
100
+ path,
101
+ method,
102
+ baseUrl,
103
+ parameters,
104
+ authorizations,
105
+ body,
106
+ variables = {},
107
+ }: {
108
+ path: string
109
+ method: OpenAPIV3.HttpMethods
110
+ baseUrl: string
111
+ parameters: OpenAPIV3.ParameterObject[]
112
+ authorizations: PlaygroundSecurityScheme | PlaygroundSecurityScheme[] | null
113
+ body: any
114
+ variables: any
115
+ }) {
116
+ const resolvedVariables = setExamplesAsVariables(parameters, variables)
117
+
118
+ const pathParameters = parameters.filter(parameter => parameter.in === 'path')
119
+ const queryParameters = parameters.filter(parameter => parameter.in === 'query')
120
+ const headerParameters = parameters.filter(parameter => parameter.in === 'header')
121
+
122
+ if (import.meta.env.VITE_DEBUG) {
123
+ console.warn('Building request with parameters:', {
124
+ path,
125
+ method,
126
+ baseUrl,
127
+ pathParameters,
128
+ queryParameters,
129
+ headerParameters,
130
+ authorizations,
131
+ body,
132
+ resolvedVariables,
133
+ })
134
+ }
135
+
136
+ const resolvedPath = getPath(resolvedVariables, pathParameters, path)
137
+
138
+ const query = getQuery(resolvedVariables, queryParameters)
139
+
140
+ const headers = getHeaders(resolvedVariables, headerParameters, authorizations)
141
+
142
+ return new OARequest(
143
+ `${baseUrl}${resolvedPath}`,
144
+ method,
145
+ Object.fromEntries(headers),
146
+ body,
147
+ query,
148
+ )
149
+ }
@@ -0,0 +1,20 @@
1
+ import type { OARequest } from './request'
2
+ import { generateCodeSampleCurl } from './generateCodeSampleCurl'
3
+ import { generateCodeSampleJavaScript } from './generateCodeSampleJavaScript'
4
+ import { generateCodeSamplePhp } from './generateCodeSamplePhp'
5
+ import { generateCodeSamplePython } from './generateCodeSamplePython'
6
+
7
+ export function generateCodeSample(lang: string, request: OARequest): string {
8
+ switch (lang) {
9
+ case 'curl':
10
+ return generateCodeSampleCurl(request)
11
+ case 'javascript':
12
+ return generateCodeSampleJavaScript(request)
13
+ case 'php':
14
+ return generateCodeSamplePhp(request)
15
+ case 'python':
16
+ return generateCodeSamplePython(request)
17
+ default:
18
+ return ''
19
+ }
20
+ }