api-render-ui 1.0.0

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.
@@ -0,0 +1,238 @@
1
+ .codigma-apioperatorlist {
2
+ align-items: flex-start;
3
+ background: white;
4
+ display: inline-flex;
5
+ flex-direction: column;
6
+ gap: 5px;
7
+ height: 100%;
8
+ justify-content: flex-start;
9
+ width: 100%
10
+ }
11
+ .codigma-apioperator {
12
+ align-items: center;
13
+ align-self: stretch;
14
+ background: #F0F1F4;
15
+ display: inline-flex;
16
+ gap: 10px;
17
+ height: 48px;
18
+ justify-content: flex-start
19
+ }
20
+ .codigma-apioperator:hover {
21
+ cursor: pointer;
22
+ }
23
+ .codigma-method {
24
+ align-items: center;
25
+ display: flex;
26
+ gap: 5px;
27
+ height: 48px;
28
+ justify-content: center;
29
+ width: 64px
30
+ }
31
+ .codigma-methodtype {
32
+ align-items: center;
33
+ display: flex;
34
+ gap: 10px;
35
+ justify-content: center
36
+ }
37
+ .codigma-get {
38
+ color: var(--Labels---Vibrant---Controls-Primary-√, #404040);
39
+ font-family: Inter;
40
+ font-size: 16px;
41
+ font-weight: 400;
42
+ word-wrap: break-word
43
+ }
44
+ .codigma-requrl {
45
+ color: black;
46
+ flex: 1 1 0;
47
+ font-family: Inter;
48
+ font-size: 16px;
49
+ font-weight: 400;
50
+ word-wrap: break-word
51
+ }
52
+ /**********************apiunit css style*************************/
53
+ .codigma-apiunit {
54
+ align-items: flex-start;
55
+ background: white;
56
+ box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
57
+ display: inline-flex;
58
+ flex-direction: column;
59
+ gap: 8px;
60
+ height: 100%;
61
+ justify-content: flex-start;
62
+ width: 100%
63
+ }
64
+ .codigma-apiunit-apioperator {
65
+ align-items: center;
66
+ align-self: stretch;
67
+ border-bottom: 1px var(--Labels---Vibrant---Controls-Primary-√, #404040) solid;
68
+ border-top: 1px var(--Labels---Vibrant---Controls-Primary-√, #404040) solid;
69
+ display: inline-flex;
70
+ gap: 10px;
71
+ height: 48px;
72
+ justify-content: flex-start
73
+ }
74
+ .codigma-apiunit-method {
75
+ align-items: center;
76
+ display: flex;
77
+ gap: 5px;
78
+ height: 48px;
79
+ justify-content: center;
80
+ width: 64px
81
+ }
82
+ .codigma-apiunit-methodtype {
83
+ align-items: center;
84
+ display: flex;
85
+ gap: 10px;
86
+ justify-content: center
87
+ }
88
+ .codigma-apiunit-post {
89
+ color: var(--Labels---Vibrant---Controls-Primary-√, #404040);
90
+ font-family: Inter;
91
+ font-size: 16px;
92
+ font-weight: 400;
93
+ word-wrap: break-word
94
+ }
95
+ .codigma-apiunit-requrl {
96
+ color: black;
97
+ flex: 1 1 0;
98
+ font-family: Inter;
99
+ font-size: 16px;
100
+ font-weight: 400;
101
+ word-wrap: break-word
102
+ }
103
+ .codigma-apiunit-send-button {
104
+ align-items: center;
105
+ display: flex;
106
+ gap: 10px;
107
+ height: 48px;
108
+ justify-content: center;
109
+ width: 88px
110
+ }
111
+ .codigma-apiunit-send {
112
+ color: black;
113
+ font-family: Inter;
114
+ font-size: 16px;
115
+ font-weight: 400;
116
+ word-wrap: break-word
117
+ }
118
+ .codigma-apiunit-reqcontent {
119
+ align-items: flex-start;
120
+ align-self: stretch;
121
+ display: flex;
122
+ flex-direction: column;
123
+ flex: 1 1 0;
124
+ gap: 5px;
125
+ justify-content: flex-start
126
+ }
127
+ .codigma-apiunit-paramters-section {
128
+ align-items: center;
129
+ align-self: stretch;
130
+ display: inline-flex;
131
+ flex: 1 1 0;
132
+ justify-content: flex-start
133
+ }
134
+ .codigma-apiunit-parameters-cnr {
135
+ align-items: center;
136
+ background: white;
137
+ display: flex;
138
+ height: 42px;
139
+ justify-content: space-between;
140
+ width: 122px
141
+ }
142
+ .codigma-apiunit-parakeyvalues {
143
+ align-items: flex-start;
144
+ align-self: stretch;
145
+ background: white;
146
+ display: inline-flex;
147
+ flex-direction: column;
148
+ flex: 1 1 0;
149
+ gap: 10px;
150
+ justify-content: flex-start;
151
+ overflow: hidden;
152
+ padding: 10px
153
+ }
154
+ .codigma-apiunit-keyvalue {
155
+ align-items: center;
156
+ display: inline-flex;
157
+ flex: 1 1 0;
158
+ gap: 10px;
159
+ justify-content: flex-start
160
+ }
161
+ .codigma-apiunit-valuetext {
162
+ align-self: stretch;
163
+ background: white;
164
+ border-bottom: 1px var(--Labels---Vibrant---Controls-Primary-√, #404040) solid;
165
+ padding: 10px;
166
+ width: 161px
167
+ }
168
+ /* .codigma-apiunit-keyvalue {
169
+ align-items: center;
170
+ display: inline-flex;
171
+ flex: 1 1 0;
172
+ gap: 10px;
173
+ justify-content: flex-start;
174
+ width: 211px
175
+ } */
176
+ .codigma-apiunit-request-body-section {
177
+ align-items: center;
178
+ align-self: stretch;
179
+ display: inline-flex;
180
+ height: 114px;
181
+ justify-content: flex-start;
182
+ padding-right: 5px
183
+ }
184
+ .codigma-apiunit-parakeyvalues {
185
+ align-items: flex-start;
186
+ align-self: stretch;
187
+ background: white;
188
+ display: flex;
189
+ flex: 1 1 0;
190
+ gap: 10px;
191
+ justify-content: flex-start;
192
+ outline-offset: -1px;
193
+ outline: 1px var(--Labels---Vibrant---Controls-Primary-√, #404040) solid;
194
+ overflow: hidden;
195
+ padding: 10px
196
+ }
197
+ .codigma-apiunit-reqresponse {
198
+ align-items: flex-start;
199
+ align-self: stretch;
200
+ background: white;
201
+ border-top: 1px var(--Labels---Vibrant---Controls-Primary-√, #404040) solid;
202
+ display: flex;
203
+ flex-direction: column;
204
+ flex: 1 1 0;
205
+ gap: 10px;
206
+ justify-content: flex-start;
207
+ padding-left: 5px;
208
+ padding-right: 5px
209
+ }
210
+ .codigma-apiunit-responsedesc {
211
+ align-items: center;
212
+ align-self: stretch;
213
+ display: inline-flex;
214
+ justify-content: space-between;
215
+ padding-right: 32px
216
+ }
217
+ .codigma-apiunit-responsetitle {
218
+ align-items: center;
219
+ border-bottom: 1px var(--Labels---Vibrant---Controls-Primary-√, #404040) solid;
220
+ display: flex;
221
+ height: 42px;
222
+ justify-content: space-between;
223
+ width: 122px
224
+ }
225
+ .codigma-apiunit-response-cnr {
226
+ align-items: center;
227
+ align-self: stretch;
228
+ background: white;
229
+ display: flex;
230
+ justify-content: space-between;
231
+ padding-top: 2px;
232
+ width: 122px
233
+ }
234
+ .codigma-apiunit-responsebody {
235
+ align-self: stretch;
236
+ flex: 1 1 0;
237
+ padding: 10px
238
+ }
@@ -0,0 +1,408 @@
1
+ // 第三方库形式封装 (codigma-api-renderer.js)
2
+ (function(global) {
3
+ function ApiRenderer(options) {
4
+ this.options = Object.assign({
5
+ mountPoint: document.body, // 默认挂载到body
6
+ className: 'Apioperatorlist codigma-apioperatorlist',
7
+ layerName: 'apioperatorlist'
8
+ }, options);
9
+
10
+ this.container = null;
11
+ }
12
+
13
+ ApiRenderer.prototype.render = function(openapiSpec) {
14
+ // 创建主容器
15
+ this.container = document.createElement('div');
16
+ this.container.setAttribute('data-layer', this.options.layerName);
17
+ this.container.className = this.options.className;
18
+ // 存储原始元素与对应新元素的映射
19
+ const elementMap = new Map();
20
+
21
+ // 获取实际挂载点
22
+ const mountElement = typeof this.options.mountPoint === 'string' ?
23
+ document.querySelector(this.options.mountPoint) :
24
+ this.options.mountPoint;
25
+
26
+ if (!mountElement) {
27
+ throw new Error('Invalid mount point specified');
28
+ }
29
+
30
+
31
+ // 解析OpenAPI规范并构建API操作列表
32
+ function parseOpenAPI(openapiSpec) {
33
+ const apiOperatorList = [];
34
+
35
+ // 遍历所有路径
36
+ Object.entries(openapiSpec.paths).forEach(([path, pathItem]) => {
37
+ // 遍历路径下的所有方法
38
+ Object.entries(pathItem).forEach(([method, operation]) => {
39
+ // 初始化当前API操作对象
40
+ const apiOperator = {
41
+ method: method.toUpperCase(),
42
+ url: path,
43
+ parametersQueryOrPath: [],
44
+ parametersBody: null
45
+ };
46
+
47
+ // 处理参数
48
+ if (operation.parameters) {
49
+ operation.parameters.forEach(param => {
50
+ // 处理path/query参数
51
+ if (param.in === 'path' || param.in === 'query') {
52
+ apiOperator.parametersQueryOrPath.push(param);
53
+ }
54
+ // 处理body参数
55
+ else if (param.in === 'body') {
56
+ apiOperator.parametersBody = param;
57
+ }
58
+ });
59
+ }
60
+
61
+ // 添加到结果列表
62
+ apiOperatorList.push(apiOperator);
63
+ });
64
+ });
65
+
66
+ return apiOperatorList;
67
+ }
68
+
69
+ // 示例使用
70
+ const apiOperatorList = parseOpenAPI(openapiSpec);
71
+ console.log('解析结果:', apiOperatorList);
72
+
73
+ // 生成内容
74
+ apiOperatorList.forEach(item => {
75
+ const reqOperator = document.createElement('div');
76
+ reqOperator.setAttribute('data-layer', 'apioperator');
77
+ reqOperator.className = 'Apioperator codigma-apioperator';
78
+
79
+ // 创建method部分
80
+ const methodContainer = document.createElement('div');
81
+ methodContainer.setAttribute('data-layer', 'method');
82
+ methodContainer.className = 'Method codigma-method';
83
+
84
+ const methodType = document.createElement('div');
85
+ methodType.setAttribute('data-layer', 'methodtype');
86
+ methodType.className = 'Methodtype codigma-methodtype';
87
+
88
+ const methodValue = document.createElement('div');
89
+ methodValue.setAttribute('data-layer', item.method.toLowerCase());
90
+ methodValue.className = `${item.method} codigma-${item.method.toLowerCase()}`;
91
+ methodValue.textContent = item.method;
92
+
93
+ methodType.appendChild(methodValue);
94
+ methodContainer.appendChild(methodType);
95
+ reqOperator.appendChild(methodContainer);
96
+
97
+ // 创建URL部分
98
+ const urlElement = document.createElement('div');
99
+ urlElement.setAttribute('data-layer', 'requrl');
100
+ urlElement.className = 'Requrl codigma-requrl';
101
+ urlElement.textContent = item.url;
102
+ reqOperator.appendChild(urlElement);
103
+
104
+ this.container.appendChild(reqOperator);
105
+
106
+ // 添加点击事件
107
+ reqOperator.addEventListener('click', renderApiUnit(item, this.container, elementMap));
108
+ });
109
+
110
+ // 清空挂载点并插入新内容
111
+ mountElement.innerHTML = '';
112
+ mountElement.appendChild(this.container);
113
+
114
+ function renderApiUnit(item, containerRef, elementMap) {
115
+ return function (evt) {
116
+ console.log('点击的API操作:');
117
+ console.log('方法:', item.method);
118
+ console.log('URL:', item.url);
119
+ console.log('------------------------');
120
+
121
+ const currentTarget = evt.currentTarget;
122
+ // 检查是否已存在对应的新元素
123
+ if (elementMap.has(currentTarget)) {
124
+ const newElement = elementMap.get(currentTarget);
125
+ newElement.remove(); // 移除新元素
126
+ elementMap.delete(currentTarget); // 清除映射
127
+ return;
128
+ }
129
+
130
+ // 示例变量赋值(根据实际需要使用)
131
+ let method = item.method;
132
+ let url = item.url;
133
+ let parametersQueryOrPath = item.parametersQueryOrPath;
134
+ let parametersBody = item.parametersBody;
135
+ let contentType = 'application/json';
136
+ let acceptType = 'application/json';
137
+ let userName = 'admin';
138
+ let password = 'secret';
139
+
140
+ // 创建根容器
141
+ const apiContainer = document.createElement('div');
142
+ apiContainer.setAttribute('data-layer', 'apiunit');
143
+ apiContainer.className = 'Requnit codigma-apiunit';
144
+ // 请求操作区
145
+ const reqOperator = document.createElement('div');
146
+ reqOperator.setAttribute('data-layer', 'apioperator');
147
+ reqOperator.className = 'Reqoperator codigma-apiunit-apioperator';
148
+ // 方法类型
149
+ const methodType = document.createElement('div');
150
+ methodType.setAttribute('data-layer', 'method');
151
+ methodType.className = 'Method codigma-apiunit-method';
152
+ const methodTypeInner = document.createElement('div');
153
+ methodTypeInner.setAttribute('data-layer', 'methodtype');
154
+ methodTypeInner.className = 'Methodtype codigma-apiunit-methodtype';
155
+ methodTypeInner.textContent = method;
156
+ methodType.appendChild(methodTypeInner);
157
+ // 方法选项图标 - 使用内联SVG
158
+ const methodOpt = document.createElement('div');
159
+ methodOpt.setAttribute('data-svg-wrapper', '');
160
+ methodOpt.setAttribute('data-layer', 'methodopt');
161
+ methodOpt.className = 'Methodopt';
162
+
163
+ // 创建内联SVG
164
+ const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
165
+ svg.setAttribute('width', '21');
166
+ svg.setAttribute('height', '22');
167
+ svg.setAttribute('viewBox', '0 0 21 22');
168
+ svg.setAttribute('fill', 'none');
169
+ svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
170
+
171
+ const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
172
+ path.setAttribute('d', 'M5.5 8.5L10.5 13.5L15.5 8.5');
173
+ path.setAttribute('stroke', 'var(--Labels---Vibrant---Controls-Primary--, #404040)');
174
+ path.setAttribute('stroke-width', '2');
175
+ path.setAttribute('stroke-linecap', 'round');
176
+ path.setAttribute('stroke-linejoin', 'round');
177
+
178
+ svg.appendChild(path);
179
+ methodOpt.appendChild(svg);
180
+ methodType.appendChild(methodOpt);
181
+ reqOperator.appendChild(methodType);
182
+ // 请求URL
183
+ const reqUrl = document.createElement('div');
184
+ reqUrl.setAttribute('data-layer', 'requrl');
185
+ reqUrl.className = 'Requrl codigma-apiunit-requrl';
186
+ reqUrl.textContent = url;
187
+ reqOperator.appendChild(reqUrl);
188
+ // 发送按钮
189
+ const sendButton = document.createElement('div');
190
+ sendButton.setAttribute('data-layer', 'send-button');
191
+ sendButton.className = 'SendButton codigma-apiunit-send-button';
192
+ const sendText = document.createElement('div');
193
+ sendText.setAttribute('data-layer', 'Send');
194
+ sendText.className = 'Send codigma-apiunit-send';
195
+ sendText.textContent = 'Send';
196
+ sendButton.appendChild(sendText);
197
+ reqOperator.appendChild(sendButton);
198
+ apiContainer.appendChild(reqOperator);
199
+ // 请求内容区
200
+ const reqContent = document.createElement('div');
201
+ reqContent.setAttribute('data-layer', 'reqcontent');
202
+ reqContent.className = 'Reqcontent codigma-apiunit-reqcontent';
203
+ // 参数部分
204
+ const paramSection = document.createElement('div');
205
+ paramSection.setAttribute('data-layer', 'paramters-section');
206
+ paramSection.className = 'ParamtersSection codigma-apiunit-paramters-section';
207
+ // 参数容器头
208
+ const paramCnr = document.createElement('div');
209
+ paramCnr.setAttribute('data-layer', 'parameters-cnr');
210
+ paramCnr.className = 'ParametersCnr codigma-apiunit-parameters-cnr';
211
+ const paramText = document.createElement('div');
212
+ paramText.setAttribute('data-layer', 'parameters');
213
+ paramText.className = 'Parameters codigma-apiunit-send';
214
+ paramText.textContent = 'Parameters';
215
+ paramCnr.appendChild(paramText);
216
+ paramSection.appendChild(paramCnr);
217
+ // 参数键值对
218
+ const paramValues = document.createElement('div');
219
+ paramValues.setAttribute('data-layer', 'paraKeyValues');
220
+ paramValues.className = 'Parakeyvalues codigma-apiunit-parakeyvalues';
221
+ const parameterRows = parametersQueryOrPath.map(parameterQueryOrPath => createRow(parameterQueryOrPath['name'], "000"))
222
+ paramValues.append(...parameterRows);
223
+ paramSection.append(paramCnr, paramValues);
224
+ // 头部部分
225
+ const headerSection = document.createElement('div');
226
+ headerSection.setAttribute('data-layer', 'headers-section');
227
+ headerSection.className = 'HeadersSection codigma-apiunit-paramters-section';
228
+ // 头部容器头
229
+ const headerCnr = document.createElement('div');
230
+ headerCnr.setAttribute('data-layer', 'headers-cnr');
231
+ headerCnr.className = 'HeadersCnr codigma-apiunit-parameters-cnr';
232
+ const headerText = document.createElement('div');
233
+ headerText.setAttribute('data-layer', 'headers');
234
+ headerText.className = 'Headers codigma-apiunit-send';
235
+ headerText.textContent = 'Headers';
236
+ headerCnr.appendChild(headerText);
237
+ headerSection.appendChild(headerCnr);
238
+ // 头部键值对
239
+ const headerValues = document.createElement('div');
240
+ headerValues.setAttribute('data-layer', 'paraKeyValues');
241
+ headerValues.className = 'Parakeyvalues codigma-apiunit-parakeyvalues';
242
+ // Content-Type
243
+ const contentTypeRow = document.createElement('div');
244
+ contentTypeRow.setAttribute('data-layer', 'keyValue1');
245
+ contentTypeRow.className = 'Keyvalue1 codigma-apiunit-keyvalue';
246
+ const contentTypeLabel = document.createElement('div');
247
+ contentTypeLabel.setAttribute('data-layer', 'Content-Type:');
248
+ contentTypeLabel.className = 'ContentType codigma-apiunit-send';
249
+ contentTypeLabel.textContent = 'Content-Type:';
250
+ const contentTypeValue = document.createElement('div');
251
+ contentTypeValue.setAttribute('data-layer', 'valueText');
252
+ contentTypeValue.className = 'Valuetext codigma-apiunit-valuetext';
253
+ contentTypeValue.textContent = contentType;
254
+ contentTypeRow.append(contentTypeLabel, contentTypeValue);
255
+ // Accept
256
+ const acceptRow = document.createElement('div');
257
+ acceptRow.setAttribute('data-layer', 'keyValue2');
258
+ acceptRow.className = 'Keyvalue2 codigma-apiunit-keyvalue';
259
+ const acceptLabel = document.createElement('div');
260
+ acceptLabel.setAttribute('data-layer', 'Accept:');
261
+ acceptLabel.className = 'Accept codigma-apiunit-send';
262
+ acceptLabel.textContent = 'Accept:';
263
+ const acceptValue = document.createElement('div');
264
+ acceptValue.setAttribute('data-layer', 'valueText');
265
+ acceptValue.className = 'Valuetext codigma-apiunit-valuetext';
266
+ acceptValue.textContent = acceptType;
267
+ acceptRow.append(acceptLabel, acceptValue);
268
+ headerValues.append(contentTypeRow, acceptRow);
269
+ headerSection.append(headerCnr, headerValues);
270
+ // 授权部分
271
+ const authSection = document.createElement('div');
272
+ authSection.setAttribute('data-layer', 'authorization-section');
273
+ authSection.className = 'AuthorizationSection codigma-apiunit-paramters-section';
274
+ // 授权容器头
275
+ const authCnr = document.createElement('div');
276
+ authCnr.setAttribute('data-layer', 'authorization-cnr');
277
+ authCnr.className = 'AuthorizationCnr codigma-apiunit-parameters-cnr';
278
+ const authText = document.createElement('div');
279
+ authText.setAttribute('data-layer', 'authorization');
280
+ authText.className = 'Authorization codigma-apiunit-send';
281
+ authText.textContent = 'Authorization';
282
+ authCnr.appendChild(authText);
283
+ authSection.appendChild(authCnr);
284
+ // 授权键值对
285
+ const authValues = document.createElement('div');
286
+ authValues.setAttribute('data-layer', 'paraKeyValues');
287
+ authValues.className = 'Parakeyvalues codigma-apiunit-parakeyvalues';
288
+ // 用户名
289
+ const userRow = document.createElement('div');
290
+ userRow.setAttribute('data-layer', 'keyValue1');
291
+ userRow.className = 'Keyvalue1 codigma-apiunit-keyvalue';
292
+ const userLabel = document.createElement('div');
293
+ userLabel.setAttribute('data-layer', 'Username:');
294
+ userLabel.className = 'Username codigma-apiunit-send';
295
+ userLabel.textContent = 'Username:';
296
+ const userValue = document.createElement('div');
297
+ userValue.setAttribute('data-layer', 'valueText');
298
+ userValue.className = 'Valuetext codigma-apiunit-valuetext';
299
+ userValue.textContent = userName;
300
+ userRow.append(userLabel, userValue);
301
+ // 密码
302
+ const passRow = document.createElement('div');
303
+ passRow.setAttribute('data-layer', 'keyValue2');
304
+ passRow.className = 'Keyvalue2 codigma-apiunit-keyvalue';
305
+ const passLabel = document.createElement('div');
306
+ passLabel.setAttribute('data-layer', 'Password:');
307
+ passLabel.className = 'Password codigma-apiunit-send';
308
+ passLabel.textContent = 'Password:';
309
+ const passValue = document.createElement('div');
310
+ passValue.setAttribute('data-layer', 'valueText');
311
+ passValue.className = 'Valuetext codigma-apiunit-valuetext';
312
+ passValue.textContent = password;
313
+ passRow.append(passLabel, passValue);
314
+ authValues.append(userRow, passRow);
315
+ authSection.append(authCnr, authValues);
316
+ // 请求体部分
317
+ const bodySection = document.createElement('div');
318
+ bodySection.setAttribute('data-layer', 'request-body-section');
319
+ bodySection.className = 'RequestBodySection codigma-apiunit-request-body-section';
320
+ // 请求体容器头
321
+ const bodyCnr = document.createElement('div');
322
+ bodyCnr.setAttribute('data-layer', 'body-cnr');
323
+ bodyCnr.className = 'BodyCnr codigma-apiunit-parameters-cnr';
324
+ const bodyText = document.createElement('div');
325
+ bodyText.setAttribute('data-layer', 'body');
326
+ bodyText.className = 'Body codigma-apiunit-send';
327
+ bodyText.textContent = 'Body';
328
+ bodyCnr.appendChild(bodyText);
329
+ bodySection.appendChild(bodyCnr);
330
+ // 请求体内容
331
+ const bodyValue = document.createElement('div');
332
+ bodyValue.setAttribute('data-layer', 'bodyTextValue');
333
+ bodyValue.className = 'Id0CategoryId0NameNamePhotourlsTagsId0NameStatusAvailable codigma-apiunit-requrl';
334
+ bodyValue.textContent = JSON.stringify(parametersBody);
335
+ bodySection.appendChild(bodyValue);
336
+
337
+ reqContent.append(paramSection, headerSection, authSection, bodySection);
338
+ apiContainer.appendChild(reqContent);
339
+ // 响应部分
340
+ const responseSection = document.createElement('div');
341
+ responseSection.setAttribute('data-layer', 'reqresponse');
342
+ responseSection.className = 'Reqresponse codigma-apiunit-reqresponse';
343
+ // 响应描述
344
+ const responseDesc = document.createElement('div');
345
+ responseDesc.setAttribute('data-layer', 'responsedesc');
346
+ responseDesc.className = 'Responsedesc codigma-apiunit-responsedesc';
347
+ // 响应标题
348
+ const responseTitle = document.createElement('div');
349
+ responseTitle.setAttribute('data-layer', 'responsetitle');
350
+ responseTitle.className = 'Responsetitle codigma-apiunit-responsetitle';
351
+ const responseCnr = document.createElement('div');
352
+ responseCnr.setAttribute('data-layer', 'response-cnr');
353
+ responseCnr.className = 'ResponseCnr codigma-apiunit-response-cnr';
354
+ const responseText = document.createElement('div');
355
+ responseText.setAttribute('data-layer', 'response');
356
+ responseText.className = 'Response codigma-apiunit-send';
357
+ responseText.textContent = 'Response';
358
+ responseCnr.appendChild(responseText);
359
+ responseTitle.appendChild(responseCnr);
360
+ const timeStatus = document.createElement('div');
361
+ timeStatus.setAttribute('data-layer', 'Time Status:0');
362
+ timeStatus.className = 'TimeStatus0 codigma-apiunit-send';
363
+ timeStatus.textContent = 'Time Status:0';
364
+ responseDesc.append(responseTitle, timeStatus);
365
+ // 响应体
366
+ const responseBody = document.createElement('div');
367
+ responseBody.setAttribute('data-layer', 'responsebody');
368
+ responseBody.className = 'Responsebody codigma-apiunit-responsebody';
369
+ responseSection.append(responseDesc, responseBody);
370
+ apiContainer.appendChild(responseSection);
371
+
372
+ // 添加到文档
373
+ // 插入到当前元素后面
374
+ if (currentTarget.nextSibling) {
375
+ containerRef.insertBefore(apiContainer, currentTarget.nextSibling);
376
+ } else {
377
+ containerRef.appendChild(apiContainer);
378
+ }
379
+
380
+ // 存储映射关系
381
+ elementMap.set(currentTarget, apiContainer);
382
+
383
+ function createRow(parameterName, parameter) {
384
+ const petIdRow = document.createElement('div');
385
+ petIdRow.setAttribute('data-layer', 'keyValue');
386
+ petIdRow.className = 'Keyvalue codigma-apiunit-keyvalue';
387
+ const petIdLabel = document.createElement('div');
388
+ petIdLabel.setAttribute('data-layer', parameterName);
389
+ petIdLabel.className = parameterName + ' codigma-apiunit-send';
390
+ petIdLabel.textContent = parameterName + ':';
391
+ const petIdValue = document.createElement('div');
392
+ petIdValue.setAttribute('data-layer', 'valueText');
393
+ petIdValue.className = 'Valuetext codigma-apiunit-valuetext';
394
+ petIdValue.textContent = parameter;
395
+ petIdRow.append(petIdLabel, petIdValue);
396
+ return petIdRow;
397
+ }
398
+ }
399
+ }
400
+ };
401
+
402
+ // 导出到全局或模块系统
403
+ if (typeof module !== 'undefined' && module.exports) {
404
+ module.exports = ApiRenderer;
405
+ } else {
406
+ global.ApiRenderer = ApiRenderer;
407
+ }
408
+ })(window);
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "api-render-ui",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "api-render-ui.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC"
12
+ }
package/test.html ADDED
@@ -0,0 +1,262 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+
4
+ <head>
5
+ <title>API Renderer Demo</title>
6
+ <link rel="stylesheet" href="./api-render-ui.css">
7
+ </head>
8
+
9
+ <body>
10
+ <!-- 挂载点容器 -->
11
+ <div id="apiContainer"></div>
12
+
13
+ <!-- 引入库代码(假设库文件为api-render-ui.js) -->
14
+ <script src="./api-render-ui.js"></script>
15
+
16
+ <script type="module">
17
+ // 初始化渲染器(ES6模块方式)
18
+ // import ApiRenderer from './api-render-ui.js';
19
+
20
+ // 创建实例并配置挂载点
21
+ const apiRenderer = new ApiRenderer({
22
+ mountPoint: '#apiContainer', // 可以是选择器字符串
23
+ });
24
+
25
+ // 示例数据
26
+ const openapiSpec = {
27
+ "openapi": "3.1.0",
28
+ "info": {
29
+ "title": "ChimichangApp",
30
+ "summary": "Deadpool's favorite app. Nuff said.",
31
+ "description": "\nChimichangApp API helps you do awesome stuff. 🚀\n\n## Items\n\nYou can **read items**.\n\n## Users\n\nYou will be able to:\n\n* **Create users** (_not implemented_).\n* **Read users** (_not implemented_).\n",
32
+ "termsOfService": "http://example.com/terms/",
33
+ "contact": {
34
+ "name": "Deadpoolio the Amazing",
35
+ "url": "http://x-force.example.com/contact/",
36
+ "email": "dp@x-force.example.com"
37
+ },
38
+ "license": {
39
+ "name": "Apache 2.0",
40
+ "url": "https://www.apache.org/licenses/LICENSE-2.0.html"
41
+ },
42
+ "version": "0.0.1"
43
+ },
44
+ "paths": {
45
+ "/items/": {
46
+ "get": {
47
+ "summary": "Read Items",
48
+ "operationId": "read_items_items__get",
49
+ "responses": {
50
+ "200": {
51
+ "description": "Successful Response",
52
+ "content": {
53
+ "application/json": {
54
+ "schema": {}
55
+ }
56
+ }
57
+ }
58
+ }
59
+ },
60
+ "post": {
61
+ "summary": "Create Item",
62
+ "operationId": "create_item_items__post",
63
+ "requestBody": {
64
+ "content": {
65
+ "application/json": {
66
+ "schema": {
67
+ "$ref": "#/components/schemas/Item"
68
+ }
69
+ }
70
+ },
71
+ "required": true
72
+ },
73
+ "responses": {
74
+ "200": {
75
+ "description": "Successful Response",
76
+ "content": {
77
+ "application/json": {
78
+ "schema": {}
79
+ }
80
+ }
81
+ },
82
+ "422": {
83
+ "description": "Validation Error",
84
+ "content": {
85
+ "application/json": {
86
+ "schema": {
87
+ "$ref": "#/components/schemas/HTTPValidationError"
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+ }
94
+ },
95
+ "/items/{item_id}": {
96
+ "get": {
97
+ "summary": "Read Root",
98
+ "operationId": "read_root_items__item_id__get",
99
+ "parameters": [
100
+ {
101
+ "name": "item_id",
102
+ "in": "path",
103
+ "required": true,
104
+ "schema": {
105
+ "type": "string",
106
+ "title": "Item Id"
107
+ }
108
+ }
109
+ ],
110
+ "responses": {
111
+ "200": {
112
+ "description": "Successful Response",
113
+ "content": {
114
+ "application/json": {
115
+ "schema": {}
116
+ }
117
+ }
118
+ },
119
+ "422": {
120
+ "description": "Validation Error",
121
+ "content": {
122
+ "application/json": {
123
+ "schema": {
124
+ "$ref": "#/components/schemas/HTTPValidationError"
125
+ }
126
+ }
127
+ }
128
+ }
129
+ }
130
+ }
131
+ },
132
+ "/": {
133
+ "get": {
134
+ "summary": "Root",
135
+ "operationId": "root__get",
136
+ "responses": {
137
+ "200": {
138
+ "description": "Successful Response",
139
+ "content": {
140
+ "application/json": {
141
+ "schema": {}
142
+ }
143
+ }
144
+ }
145
+ }
146
+ }
147
+ },
148
+ "/legacy/": {
149
+ "get": {
150
+ "summary": "Get Legacy Data",
151
+ "operationId": "get_legacy_data_legacy__get",
152
+ "responses": {
153
+ "200": {
154
+ "description": "Successful Response",
155
+ "content": {
156
+ "application/json": {
157
+ "schema": {}
158
+ }
159
+ }
160
+ }
161
+ }
162
+ }
163
+ }
164
+ },
165
+ "components": {
166
+ "schemas": {
167
+ "HTTPValidationError": {
168
+ "properties": {
169
+ "detail": {
170
+ "items": {
171
+ "$ref": "#/components/schemas/ValidationError"
172
+ },
173
+ "type": "array",
174
+ "title": "Detail"
175
+ }
176
+ },
177
+ "type": "object",
178
+ "title": "HTTPValidationError"
179
+ },
180
+ "Item": {
181
+ "properties": {
182
+ "name": {
183
+ "type": "string",
184
+ "title": "Name"
185
+ },
186
+ "price": {
187
+ "type": "number",
188
+ "title": "Price"
189
+ },
190
+ "description": {
191
+ "anyOf": [
192
+ {
193
+ "type": "string"
194
+ },
195
+ {
196
+ "type": "null"
197
+ }
198
+ ],
199
+ "title": "Description"
200
+ },
201
+ "tax": {
202
+ "anyOf": [
203
+ {
204
+ "type": "number"
205
+ },
206
+ {
207
+ "type": "null"
208
+ }
209
+ ],
210
+ "title": "Tax"
211
+ }
212
+ },
213
+ "type": "object",
214
+ "required": [
215
+ "name",
216
+ "price"
217
+ ],
218
+ "title": "Item"
219
+ },
220
+ "ValidationError": {
221
+ "properties": {
222
+ "loc": {
223
+ "items": {
224
+ "anyOf": [
225
+ {
226
+ "type": "string"
227
+ },
228
+ {
229
+ "type": "integer"
230
+ }
231
+ ]
232
+ },
233
+ "type": "array",
234
+ "title": "Location"
235
+ },
236
+ "msg": {
237
+ "type": "string",
238
+ "title": "Message"
239
+ },
240
+ "type": {
241
+ "type": "string",
242
+ "title": "Error Type"
243
+ }
244
+ },
245
+ "type": "object",
246
+ "required": [
247
+ "loc",
248
+ "msg",
249
+ "type"
250
+ ],
251
+ "title": "ValidationError"
252
+ }
253
+ }
254
+ }
255
+ };
256
+
257
+ // 执行渲染
258
+ apiRenderer.render(openapiSpec);
259
+ </script>
260
+ </body>
261
+
262
+ </html>