@salesforcedevs/docs-components 1.29.0-alpha1 → 1.29.0-llm-alpha
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.
- package/lwc.config.json +3 -1
- package/package.json +3 -3
- package/src/modules/doc/aiToolbar/aiToolbar.css +40 -0
- package/src/modules/doc/aiToolbar/aiToolbar.html +53 -0
- package/src/modules/doc/aiToolbar/aiToolbar.ts +83 -0
- package/src/modules/doc/aiToolbar/aiToolbarMocks.ts +48 -0
- package/src/modules/doc/amfReference/amfReference.ts +52 -10
- package/src/modules/doc/amfReference/types.ts +5 -0
- package/src/modules/doc/banner/banner.css +88 -0
- package/src/modules/doc/banner/banner.html +47 -0
- package/src/modules/doc/banner/banner.ts +73 -0
- package/src/modules/doc/contentLayout/contentLayout.html +1 -1
- package/src/modules/doc/contentLayout/contentLayout.ts +106 -2
- package/src/modules/doc/header/header.html +0 -1
- package/src/modules/doc/localeBanner/localeBanner.css +3 -0
- package/src/modules/doc/localeBanner/localeBanner.html +9 -0
- package/src/modules/doc/localeBanner/localeBanner.ts +195 -0
- package/src/modules/doc/lwcContentLayout/lwcContentLayout.html +5 -2
- package/src/modules/doc/redocReference/redocReference.ts +157 -121
- package/src/modules/doc/xmlContent/xmlContent.html +1 -1
- package/src/modules/doc/xmlContent/xmlContent.ts +28 -1
- package/src/modules/doc/apiPlayground/apiPlayground.css +0 -186
- package/src/modules/doc/apiPlayground/apiPlayground.html +0 -136
- package/src/modules/doc/apiPlayground/apiPlayground.ts +0 -240
- package/src/modules/docUtils/apiRequestExecutor/apiRequestExecutor.ts +0 -96
- package/src/modules/docUtils/openApiParser/openApiParser.ts +0 -187
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<div class="playground-container">
|
|
3
|
-
<div class="playground-header">
|
|
4
|
-
<span class={methodBadgeClass}>{operation.method}</span>
|
|
5
|
-
<span class="operation-path">{operation.path}</span>
|
|
6
|
-
</div>
|
|
7
|
-
|
|
8
|
-
<template lwc:if={showServerSelector}>
|
|
9
|
-
<div class="field-group">
|
|
10
|
-
<label class="field-label" for="server-select">Server</label>
|
|
11
|
-
<select id="server-select" class="server-select" aria-label="Server" onchange={handleServerChange}>
|
|
12
|
-
<template for:each={serverOptions} for:item="server">
|
|
13
|
-
<option key={server.value} value={server.value}>
|
|
14
|
-
{server.label}
|
|
15
|
-
</option>
|
|
16
|
-
</template>
|
|
17
|
-
</select>
|
|
18
|
-
</div>
|
|
19
|
-
</template>
|
|
20
|
-
|
|
21
|
-
<template lwc:if={hasPathParams}>
|
|
22
|
-
<div class="params-section">
|
|
23
|
-
<h4 class="section-title">Path Parameters</h4>
|
|
24
|
-
<template for:each={pathParams} for:item="param">
|
|
25
|
-
<div class="field-group" key={param.name}>
|
|
26
|
-
<label class="field-label">
|
|
27
|
-
{param.name}
|
|
28
|
-
<template lwc:if={param.required}>
|
|
29
|
-
<span class="required-marker">*</span>
|
|
30
|
-
</template>
|
|
31
|
-
</label>
|
|
32
|
-
<input
|
|
33
|
-
type="text"
|
|
34
|
-
class="field-input"
|
|
35
|
-
data-param={param.name}
|
|
36
|
-
value={param.value}
|
|
37
|
-
placeholder={param.description}
|
|
38
|
-
onchange={handleParamChange}
|
|
39
|
-
/>
|
|
40
|
-
</div>
|
|
41
|
-
</template>
|
|
42
|
-
</div>
|
|
43
|
-
</template>
|
|
44
|
-
|
|
45
|
-
<template lwc:if={hasQueryParams}>
|
|
46
|
-
<div class="params-section">
|
|
47
|
-
<h4 class="section-title">Query Parameters</h4>
|
|
48
|
-
<template for:each={queryParams} for:item="param">
|
|
49
|
-
<div class="field-group" key={param.name}>
|
|
50
|
-
<label class="field-label">
|
|
51
|
-
{param.name}
|
|
52
|
-
<template lwc:if={param.required}>
|
|
53
|
-
<span class="required-marker">*</span>
|
|
54
|
-
</template>
|
|
55
|
-
</label>
|
|
56
|
-
<input
|
|
57
|
-
type="text"
|
|
58
|
-
class="field-input"
|
|
59
|
-
data-param={param.name}
|
|
60
|
-
value={param.value}
|
|
61
|
-
placeholder={param.description}
|
|
62
|
-
onchange={handleParamChange}
|
|
63
|
-
/>
|
|
64
|
-
</div>
|
|
65
|
-
</template>
|
|
66
|
-
</div>
|
|
67
|
-
</template>
|
|
68
|
-
|
|
69
|
-
<template lwc:if={hasHeaderParams}>
|
|
70
|
-
<div class="params-section">
|
|
71
|
-
<h4 class="section-title">Headers</h4>
|
|
72
|
-
<template for:each={headerParams} for:item="param">
|
|
73
|
-
<div class="field-group" key={param.name}>
|
|
74
|
-
<label class="field-label">
|
|
75
|
-
{param.name}
|
|
76
|
-
<template lwc:if={param.required}>
|
|
77
|
-
<span class="required-marker">*</span>
|
|
78
|
-
</template>
|
|
79
|
-
</label>
|
|
80
|
-
<input
|
|
81
|
-
type="text"
|
|
82
|
-
class="field-input"
|
|
83
|
-
data-param={param.name}
|
|
84
|
-
value={param.value}
|
|
85
|
-
placeholder={param.description}
|
|
86
|
-
onchange={handleParamChange}
|
|
87
|
-
/>
|
|
88
|
-
</div>
|
|
89
|
-
</template>
|
|
90
|
-
</div>
|
|
91
|
-
</template>
|
|
92
|
-
|
|
93
|
-
<template lwc:if={hasRequestBody}>
|
|
94
|
-
<div class="params-section">
|
|
95
|
-
<h4 class="section-title">Request Body</h4>
|
|
96
|
-
<textarea
|
|
97
|
-
class="body-textarea"
|
|
98
|
-
value={requestBody}
|
|
99
|
-
onchange={handleBodyChange}
|
|
100
|
-
rows="8"
|
|
101
|
-
></textarea>
|
|
102
|
-
</div>
|
|
103
|
-
</template>
|
|
104
|
-
|
|
105
|
-
<div class="actions">
|
|
106
|
-
<dx-button
|
|
107
|
-
variant="brand"
|
|
108
|
-
onclick={handleSendRequest}
|
|
109
|
-
disabled={loading}
|
|
110
|
-
>
|
|
111
|
-
<template lwc:if={loading}>Sending...</template>
|
|
112
|
-
<template lwc:else>Send Request</template>
|
|
113
|
-
</dx-button>
|
|
114
|
-
</div>
|
|
115
|
-
|
|
116
|
-
<template lwc:if={hasError}>
|
|
117
|
-
<div class="error-display">
|
|
118
|
-
<p class="error-message">{error}</p>
|
|
119
|
-
</div>
|
|
120
|
-
</template>
|
|
121
|
-
|
|
122
|
-
<template lwc:if={hasResponse}>
|
|
123
|
-
<div class="response-section">
|
|
124
|
-
<div class="response-header">
|
|
125
|
-
<span class={responseStatusClass}>
|
|
126
|
-
{response.status} {response.statusText}
|
|
127
|
-
</span>
|
|
128
|
-
<span class="response-duration">{response.duration}ms</span>
|
|
129
|
-
</div>
|
|
130
|
-
<dx-code-block
|
|
131
|
-
language={responseLanguage}
|
|
132
|
-
>{formattedResponseBody}</dx-code-block>
|
|
133
|
-
</div>
|
|
134
|
-
</template>
|
|
135
|
-
</div>
|
|
136
|
-
</template>
|
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import { LightningElement, api, track } from "lwc";
|
|
2
|
-
import type {
|
|
3
|
-
OperationDefinition,
|
|
4
|
-
ParameterDefinition
|
|
5
|
-
} from "docUtils/openApiParser";
|
|
6
|
-
import {
|
|
7
|
-
generateSampleFromSchema,
|
|
8
|
-
resolveRef
|
|
9
|
-
} from "docUtils/openApiParser";
|
|
10
|
-
import {
|
|
11
|
-
buildRequestUrl,
|
|
12
|
-
executeRequest
|
|
13
|
-
} from "docUtils/apiRequestExecutor";
|
|
14
|
-
import type { ApiResponse } from "docUtils/apiRequestExecutor";
|
|
15
|
-
|
|
16
|
-
type ParamState = {
|
|
17
|
-
name: string;
|
|
18
|
-
in: string;
|
|
19
|
-
value: string;
|
|
20
|
-
required: boolean;
|
|
21
|
-
description: string;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export default class ApiPlayground extends LightningElement {
|
|
25
|
-
@api operation!: OperationDefinition;
|
|
26
|
-
@api spec: any;
|
|
27
|
-
|
|
28
|
-
@track paramStates: ParamState[] = [];
|
|
29
|
-
@track requestBody = "";
|
|
30
|
-
@track selectedServer = "";
|
|
31
|
-
@track response: ApiResponse | null = null;
|
|
32
|
-
@track error: string | null = null;
|
|
33
|
-
@track loading = false;
|
|
34
|
-
|
|
35
|
-
private initialized = false;
|
|
36
|
-
|
|
37
|
-
renderedCallback(): void {
|
|
38
|
-
if (!this.initialized && this.operation) {
|
|
39
|
-
this.initialized = true;
|
|
40
|
-
this.initializeState();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
private initializeState(): void {
|
|
45
|
-
this.paramStates = (this.operation.parameters ?? []).map(
|
|
46
|
-
(param: ParameterDefinition) => ({
|
|
47
|
-
name: param.name,
|
|
48
|
-
in: param.in,
|
|
49
|
-
value: param.example !== undefined ? String(param.example) : "",
|
|
50
|
-
required: param.required,
|
|
51
|
-
description: param.description
|
|
52
|
-
})
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
if (this.operation.requestBody) {
|
|
56
|
-
const content = this.operation.requestBody.content;
|
|
57
|
-
const jsonContent = content?.["application/json"];
|
|
58
|
-
if (jsonContent?.schema) {
|
|
59
|
-
const sample = generateSampleFromSchema(
|
|
60
|
-
jsonContent.schema,
|
|
61
|
-
this.spec
|
|
62
|
-
);
|
|
63
|
-
if (sample !== undefined) {
|
|
64
|
-
this.requestBody = JSON.stringify(sample, null, 2);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (this.servers.length > 0) {
|
|
70
|
-
this.selectedServer = this.servers[0].url;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
get servers(): Array<{ url: string; description: string }> {
|
|
75
|
-
return (this.spec?.servers ?? []).map((s: any) => ({
|
|
76
|
-
url: s.url ?? "",
|
|
77
|
-
description: s.description ?? s.url ?? ""
|
|
78
|
-
}));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
get showServerSelector(): boolean {
|
|
82
|
-
return this.servers.length > 1;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
get serverOptions(): Array<{ label: string; value: string }> {
|
|
86
|
-
return this.servers.map((s) => ({
|
|
87
|
-
label: s.description || s.url,
|
|
88
|
-
value: s.url
|
|
89
|
-
}));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
get pathParams(): ParamState[] {
|
|
93
|
-
return this.paramStates.filter((p) => p.in === "path");
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
get queryParams(): ParamState[] {
|
|
97
|
-
return this.paramStates.filter((p) => p.in === "query");
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
get headerParams(): ParamState[] {
|
|
101
|
-
return this.paramStates.filter((p) => p.in === "header");
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
get hasPathParams(): boolean {
|
|
105
|
-
return this.pathParams.length > 0;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
get hasQueryParams(): boolean {
|
|
109
|
-
return this.queryParams.length > 0;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
get hasHeaderParams(): boolean {
|
|
113
|
-
return this.headerParams.length > 0;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
get hasRequestBody(): boolean {
|
|
117
|
-
return this.operation?.requestBody !== null && this.operation?.requestBody !== undefined;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
get methodBadgeClass(): string {
|
|
121
|
-
const method = this.operation?.method?.toLowerCase() ?? "get";
|
|
122
|
-
return `method-badge method-${method}`;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
get responseStatusClass(): string {
|
|
126
|
-
if (!this.response) {
|
|
127
|
-
return "status-code";
|
|
128
|
-
}
|
|
129
|
-
const status = this.response.status;
|
|
130
|
-
if (status >= 200 && status < 300) {
|
|
131
|
-
return "status-code status-success";
|
|
132
|
-
}
|
|
133
|
-
if (status >= 400 && status < 500) {
|
|
134
|
-
return "status-code status-client-error";
|
|
135
|
-
}
|
|
136
|
-
if (status >= 500) {
|
|
137
|
-
return "status-code status-server-error";
|
|
138
|
-
}
|
|
139
|
-
return "status-code";
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
get hasResponse(): boolean {
|
|
143
|
-
return this.response !== null;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
get hasError(): boolean {
|
|
147
|
-
return this.error !== null;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
get formattedResponseBody(): string {
|
|
151
|
-
if (!this.response?.body) {
|
|
152
|
-
return "";
|
|
153
|
-
}
|
|
154
|
-
try {
|
|
155
|
-
return JSON.stringify(JSON.parse(this.response.body), null, 2);
|
|
156
|
-
} catch {
|
|
157
|
-
return this.response.body;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
get responseLanguage(): string {
|
|
162
|
-
if (!this.response?.body) {
|
|
163
|
-
return "text";
|
|
164
|
-
}
|
|
165
|
-
try {
|
|
166
|
-
JSON.parse(this.response.body);
|
|
167
|
-
return "json";
|
|
168
|
-
} catch {
|
|
169
|
-
return "text";
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
handleParamChange(event: Event): void {
|
|
174
|
-
const target = event.target as HTMLInputElement;
|
|
175
|
-
const paramName = target.dataset.param;
|
|
176
|
-
this.paramStates = this.paramStates.map((p) =>
|
|
177
|
-
p.name === paramName ? { ...p, value: target.value } : p
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
handleBodyChange(event: Event): void {
|
|
182
|
-
const target = event.target as HTMLTextAreaElement;
|
|
183
|
-
this.requestBody = target.value;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
handleServerChange(event: Event): void {
|
|
187
|
-
const target = event.target as HTMLSelectElement;
|
|
188
|
-
this.selectedServer = target.value;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async handleSendRequest(): Promise<void> {
|
|
192
|
-
this.loading = true;
|
|
193
|
-
this.response = null;
|
|
194
|
-
this.error = null;
|
|
195
|
-
|
|
196
|
-
try {
|
|
197
|
-
const pathParams: Record<string, string> = {};
|
|
198
|
-
const queryParams: Record<string, string> = {};
|
|
199
|
-
const headers: Record<string, string> = {};
|
|
200
|
-
|
|
201
|
-
for (const param of this.paramStates) {
|
|
202
|
-
if (param.in === "path") {
|
|
203
|
-
pathParams[param.name] = param.value;
|
|
204
|
-
} else if (param.in === "query") {
|
|
205
|
-
queryParams[param.name] = param.value;
|
|
206
|
-
} else if (param.in === "header") {
|
|
207
|
-
headers[param.name] = param.value;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const serverUrl =
|
|
212
|
-
this.selectedServer || this.servers[0]?.url || "";
|
|
213
|
-
const url = buildRequestUrl(
|
|
214
|
-
serverUrl,
|
|
215
|
-
this.operation.path,
|
|
216
|
-
pathParams,
|
|
217
|
-
queryParams
|
|
218
|
-
);
|
|
219
|
-
|
|
220
|
-
if (this.hasRequestBody && this.requestBody) {
|
|
221
|
-
headers["Content-Type"] =
|
|
222
|
-
headers["Content-Type"] || "application/json";
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
this.response = await executeRequest({
|
|
226
|
-
url,
|
|
227
|
-
method: this.operation.method,
|
|
228
|
-
headers,
|
|
229
|
-
body: this.hasRequestBody ? this.requestBody : undefined
|
|
230
|
-
});
|
|
231
|
-
} catch (err: any) {
|
|
232
|
-
this.error =
|
|
233
|
-
err?.message?.includes("fetch")
|
|
234
|
-
? "Network error: The request failed. This may be due to CORS restrictions or a network issue."
|
|
235
|
-
: err?.message || "An unexpected error occurred.";
|
|
236
|
-
} finally {
|
|
237
|
-
this.loading = false;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
export type ApiRequest = {
|
|
2
|
-
url: string;
|
|
3
|
-
method: string;
|
|
4
|
-
headers: Record<string, string>;
|
|
5
|
-
body?: string;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export type ApiResponse = {
|
|
9
|
-
status: number;
|
|
10
|
-
statusText: string;
|
|
11
|
-
headers: Record<string, string>;
|
|
12
|
-
body: string;
|
|
13
|
-
duration: number;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export type RequestExecutor = (request: ApiRequest) => Promise<ApiResponse>;
|
|
17
|
-
|
|
18
|
-
export function buildRequestUrl(
|
|
19
|
-
serverUrl: string,
|
|
20
|
-
pathTemplate: string,
|
|
21
|
-
pathParams: Record<string, string>,
|
|
22
|
-
queryParams: Record<string, string>
|
|
23
|
-
): string {
|
|
24
|
-
let url = pathTemplate;
|
|
25
|
-
for (const [key, value] of Object.entries(pathParams)) {
|
|
26
|
-
url = url.replace(`{${key}}`, encodeURIComponent(value));
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const fullUrl = `${serverUrl.replace(/\/$/, "")}${url}`;
|
|
30
|
-
|
|
31
|
-
const queryEntries = Object.entries(queryParams).filter(
|
|
32
|
-
([, v]) => v !== ""
|
|
33
|
-
);
|
|
34
|
-
if (queryEntries.length === 0) {
|
|
35
|
-
return fullUrl;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const searchParams = new URLSearchParams();
|
|
39
|
-
for (const [key, value] of queryEntries) {
|
|
40
|
-
searchParams.append(key, value);
|
|
41
|
-
}
|
|
42
|
-
return `${fullUrl}?${searchParams.toString()}`;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const directFetchExecutor: RequestExecutor = async (
|
|
46
|
-
request: ApiRequest
|
|
47
|
-
): Promise<ApiResponse> => {
|
|
48
|
-
const start = performance.now();
|
|
49
|
-
|
|
50
|
-
const fetchOptions: RequestInit = {
|
|
51
|
-
method: request.method,
|
|
52
|
-
headers: request.headers,
|
|
53
|
-
mode: "cors"
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
request.body &&
|
|
58
|
-
request.method !== "GET" &&
|
|
59
|
-
request.method !== "HEAD"
|
|
60
|
-
) {
|
|
61
|
-
fetchOptions.body = request.body;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const response = await fetch(request.url, fetchOptions);
|
|
65
|
-
const duration = Math.round(performance.now() - start);
|
|
66
|
-
|
|
67
|
-
const responseHeaders: Record<string, string> = {};
|
|
68
|
-
response.headers.forEach((value, key) => {
|
|
69
|
-
responseHeaders[key] = value;
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
let body: string;
|
|
73
|
-
try {
|
|
74
|
-
body = await response.text();
|
|
75
|
-
} catch {
|
|
76
|
-
body = "";
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
status: response.status,
|
|
81
|
-
statusText: response.statusText,
|
|
82
|
-
headers: responseHeaders,
|
|
83
|
-
body,
|
|
84
|
-
duration
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
let currentExecutor: RequestExecutor = directFetchExecutor;
|
|
89
|
-
|
|
90
|
-
export function setRequestExecutor(executor: RequestExecutor): void {
|
|
91
|
-
currentExecutor = executor;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function executeRequest(request: ApiRequest): Promise<ApiResponse> {
|
|
95
|
-
return currentExecutor(request);
|
|
96
|
-
}
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
export type ParameterDefinition = {
|
|
2
|
-
name: string;
|
|
3
|
-
in: "path" | "query" | "header" | "cookie";
|
|
4
|
-
required: boolean;
|
|
5
|
-
description: string;
|
|
6
|
-
schema: any;
|
|
7
|
-
example?: any;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export type RequestBodyDefinition = {
|
|
11
|
-
description: string;
|
|
12
|
-
required: boolean;
|
|
13
|
-
content: Record<string, { schema: any }>;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export type OperationDefinition = {
|
|
17
|
-
operationId: string;
|
|
18
|
-
method: string;
|
|
19
|
-
path: string;
|
|
20
|
-
summary: string;
|
|
21
|
-
description: string;
|
|
22
|
-
parameters: ParameterDefinition[];
|
|
23
|
-
requestBody: RequestBodyDefinition | null;
|
|
24
|
-
responses: Record<string, any>;
|
|
25
|
-
tags: string[];
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export function resolveRef(ref: string, spec: any): any {
|
|
29
|
-
if (!ref || !ref.startsWith("#/")) {
|
|
30
|
-
return ref;
|
|
31
|
-
}
|
|
32
|
-
const parts = ref.replace("#/", "").split("/");
|
|
33
|
-
let current = spec;
|
|
34
|
-
for (const part of parts) {
|
|
35
|
-
current = current?.[part];
|
|
36
|
-
if (current === undefined) {
|
|
37
|
-
return undefined;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return current;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function generateSampleFromSchema(schema: any, spec: any): any {
|
|
44
|
-
if (!schema) {
|
|
45
|
-
return undefined;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (schema.$ref) {
|
|
49
|
-
return generateSampleFromSchema(resolveRef(schema.$ref, spec), spec);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (schema.example !== undefined) {
|
|
53
|
-
return schema.example;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (schema.default !== undefined) {
|
|
57
|
-
return schema.default;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (schema.enum && schema.enum.length > 0) {
|
|
61
|
-
return schema.enum[0];
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
switch (schema.type) {
|
|
65
|
-
case "string":
|
|
66
|
-
return schema.format === "date"
|
|
67
|
-
? "2024-01-01"
|
|
68
|
-
: schema.format === "date-time"
|
|
69
|
-
? "2024-01-01T00:00:00Z"
|
|
70
|
-
: schema.format === "email"
|
|
71
|
-
? "user@example.com"
|
|
72
|
-
: schema.format === "uri"
|
|
73
|
-
? "https://example.com"
|
|
74
|
-
: "string";
|
|
75
|
-
case "integer":
|
|
76
|
-
return schema.minimum ?? 0;
|
|
77
|
-
case "number":
|
|
78
|
-
return schema.minimum ?? 0.0;
|
|
79
|
-
case "boolean":
|
|
80
|
-
return false;
|
|
81
|
-
case "array":
|
|
82
|
-
if (schema.items) {
|
|
83
|
-
return [generateSampleFromSchema(schema.items, spec)];
|
|
84
|
-
}
|
|
85
|
-
return [];
|
|
86
|
-
case "object": {
|
|
87
|
-
const result: Record<string, any> = {};
|
|
88
|
-
if (schema.properties) {
|
|
89
|
-
for (const [key, propSchema] of Object.entries(
|
|
90
|
-
schema.properties
|
|
91
|
-
)) {
|
|
92
|
-
result[key] = generateSampleFromSchema(
|
|
93
|
-
propSchema as any,
|
|
94
|
-
spec
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return result;
|
|
99
|
-
}
|
|
100
|
-
default:
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function resolveParameter(param: any, spec: any): ParameterDefinition {
|
|
106
|
-
const resolved = param.$ref ? resolveRef(param.$ref, spec) : param;
|
|
107
|
-
return {
|
|
108
|
-
name: resolved.name ?? "",
|
|
109
|
-
in: resolved.in ?? "query",
|
|
110
|
-
required: resolved.required ?? false,
|
|
111
|
-
description: resolved.description ?? "",
|
|
112
|
-
schema: resolved.schema ?? {},
|
|
113
|
-
example: resolved.example
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export function parseOpenApiSpec(spec: any): OperationDefinition[] {
|
|
118
|
-
if (!spec?.paths) {
|
|
119
|
-
return [];
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const operations: OperationDefinition[] = [];
|
|
123
|
-
const httpMethods = [
|
|
124
|
-
"get",
|
|
125
|
-
"post",
|
|
126
|
-
"put",
|
|
127
|
-
"delete",
|
|
128
|
-
"patch",
|
|
129
|
-
"options",
|
|
130
|
-
"head"
|
|
131
|
-
];
|
|
132
|
-
|
|
133
|
-
for (const [path, pathItem] of Object.entries(spec.paths)) {
|
|
134
|
-
const pathObj = pathItem as any;
|
|
135
|
-
const pathLevelParams: any[] = pathObj.parameters ?? [];
|
|
136
|
-
|
|
137
|
-
for (const method of httpMethods) {
|
|
138
|
-
const operation = pathObj[method];
|
|
139
|
-
if (!operation) {
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const operationParams = (operation.parameters ?? []).map(
|
|
144
|
-
(p: any) => resolveParameter(p, spec)
|
|
145
|
-
);
|
|
146
|
-
const pathParams = pathLevelParams
|
|
147
|
-
.map((p: any) => resolveParameter(p, spec))
|
|
148
|
-
.filter(
|
|
149
|
-
(pp) =>
|
|
150
|
-
!operationParams.some(
|
|
151
|
-
(op: ParameterDefinition) => op.name === pp.name
|
|
152
|
-
)
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
const mergedParams = [...pathParams, ...operationParams];
|
|
156
|
-
|
|
157
|
-
let requestBody: RequestBodyDefinition | null = null;
|
|
158
|
-
if (operation.requestBody) {
|
|
159
|
-
const rb = operation.requestBody.$ref
|
|
160
|
-
? resolveRef(operation.requestBody.$ref, spec)
|
|
161
|
-
: operation.requestBody;
|
|
162
|
-
if (rb) {
|
|
163
|
-
requestBody = {
|
|
164
|
-
description: rb.description ?? "",
|
|
165
|
-
required: rb.required ?? false,
|
|
166
|
-
content: rb.content ?? {}
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
operations.push({
|
|
172
|
-
operationId:
|
|
173
|
-
operation.operationId ?? `${method}_${path.replace(/\//g, "_")}`,
|
|
174
|
-
method: method.toUpperCase(),
|
|
175
|
-
path,
|
|
176
|
-
summary: operation.summary ?? "",
|
|
177
|
-
description: operation.description ?? "",
|
|
178
|
-
parameters: mergedParams,
|
|
179
|
-
requestBody,
|
|
180
|
-
responses: operation.responses ?? {},
|
|
181
|
-
tags: operation.tags ?? []
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return operations;
|
|
187
|
-
}
|