api-render-ui 1.0.6 → 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.
package/dist/index.js DELETED
@@ -1,1024 +0,0 @@
1
- // src/inlined-styles.ts
2
- var GLOBAL_STYLES = `.codigma-apioperatorlist {
3
- align-items: flex-start;
4
- background: white;
5
- display: inline-flex;
6
- flex-direction: column;
7
- gap: 5px;
8
- height: 100%;
9
- justify-content: flex-start;
10
- width: 100%
11
- }
12
- .codigma-apioperator {
13
- align-items: center;
14
- align-self: stretch;
15
- background: #F0F1F4;
16
- display: inline-flex;
17
- gap: 10px;
18
- height: 48px;
19
- justify-content: flex-start
20
- }
21
- .codigma-apioperator:hover {
22
- cursor: pointer;
23
- }
24
- .codigma-method {
25
- align-items: center;
26
- display: flex;
27
- gap: 5px;
28
- height: 48px;
29
- justify-content: center;
30
- width: 64px
31
- }
32
- .codigma-methodtype {
33
- align-items: center;
34
- display: flex;
35
- gap: 10px;
36
- justify-content: center
37
- }
38
- .codigma-get {
39
- color: var(--Labels---Vibrant---Controls-Primary-\u221A, #404040);
40
- font-family: Inter;
41
- font-size: 16px;
42
- font-weight: 400;
43
- word-wrap: break-word
44
- }
45
- .codigma-requrl {
46
- color: black;
47
- flex: 1 1 0;
48
- font-family: Inter;
49
- font-size: 16px;
50
- font-weight: 400;
51
- word-wrap: break-word
52
- }
53
- /**********************apiunit css style*************************/
54
- .codigma-apiunit {
55
- align-items: flex-start;
56
- background: white;
57
- box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
58
- display: inline-flex;
59
- flex-direction: column;
60
- gap: 8px;
61
- height: 100%;
62
- justify-content: flex-start;
63
- width: 100%
64
- }
65
- .codigma-apiunit-apioperator {
66
- align-items: center;
67
- align-self: stretch;
68
- border-bottom: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
69
- border-top: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
70
- display: inline-flex;
71
- gap: 10px;
72
- height: 48px;
73
- justify-content: flex-start
74
- }
75
- .codigma-apiunit-method {
76
- align-items: center;
77
- display: flex;
78
- gap: 5px;
79
- height: 48px;
80
- justify-content: center;
81
- width: 100px
82
- }
83
- .codigma-apiunit-methodtype {
84
- align-items: center;
85
- display: flex;
86
- gap: 10px;
87
- justify-content: center
88
- }
89
- .codigma-apiunit-post {
90
- color: var(--Labels---Vibrant---Controls-Primary-\u221A, #404040);
91
- font-family: Inter;
92
- font-size: 16px;
93
- font-weight: 400;
94
- word-wrap: break-word
95
- }
96
- .codigma-apiunit-requrl {
97
- color: black;
98
- flex: 1 1 0;
99
- font-family: Inter;
100
- font-size: 16px;
101
- font-weight: 400;
102
- word-wrap: break-word;
103
- border-left: 0;
104
- border-right: 0;
105
- border-top: 0;
106
- border-bottom: 1px solid black;
107
- }
108
- .codigma-apiunit-send-button {
109
- align-items: center;
110
- display: flex;
111
- gap: 10px;
112
- height: 32px;
113
- justify-content: center;
114
- width: 88px
115
- }
116
- .codigma-apiunit-send {
117
- color: black;
118
- font-family: Inter;
119
- font-size: 16px;
120
- font-weight: 400;
121
- word-wrap: break-word
122
- }
123
- .codigma-apiunit-send-button:hover {
124
- cursor: pointer;
125
- }
126
- .codigma-apiunit-send-button:active {
127
- background-color: gray;
128
- }
129
-
130
- .codigma-apiunit-reqcontent {
131
- align-items: flex-start;
132
- align-self: stretch;
133
- display: flex;
134
- flex-direction: column;
135
- flex: 1 1 0;
136
- gap: 5px;
137
- justify-content: flex-start;
138
- padding-left: 5px;
139
- }
140
- .codigma-apiunit-paramters-section {
141
- align-items: center;
142
- align-self: stretch;
143
- display: inline-flex;
144
- flex: 1 1 0;
145
- justify-content: flex-start
146
- }
147
- .codigma-apiunit-parameters-cnr {
148
- align-items: center;
149
- background: white;
150
- display: flex;
151
- height: 42px;
152
- justify-content: space-between;
153
- width: 122px
154
- }
155
- .codigma-apiunit-parakeyvalues {
156
- align-items: flex-start;
157
- align-self: stretch;
158
- background: white;
159
- display: inline-flex;
160
- flex-direction: column;
161
- flex: 1 1 0;
162
- gap: 10px;
163
- justify-content: flex-start;
164
- overflow: hidden;
165
- padding: 10px
166
- }
167
- .codigma-apiunit-keyvalue {
168
- align-items: center;
169
- display: inline-flex;
170
- flex: 1 1 0;
171
- gap: 10px;
172
- justify-content: flex-start
173
- }
174
- .codigma-apiunit-valuetext {
175
- align-self: stretch;
176
- background: white;
177
- border-left: 0;
178
- border-right: 0;
179
- border-top: 0;
180
- border-bottom: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
181
- padding: 10px;
182
- width: 161px
183
- }
184
- /* .codigma-apiunit-keyvalue {
185
- align-items: center;
186
- display: inline-flex;
187
- flex: 1 1 0;
188
- gap: 10px;
189
- justify-content: flex-start;
190
- width: 211px
191
- } */
192
- .codigma-apiunit-request-body-section {
193
- align-items: center;
194
- align-self: stretch;
195
- display: inline-flex;
196
- height: 114px;
197
- justify-content: flex-start;
198
- padding-right: 5px
199
- }
200
- .codigma-apiunit-parakeyvalues {
201
- align-items: flex-start;
202
- align-self: stretch;
203
- background: white;
204
- display: flex;
205
- flex: 1 1 0;
206
- gap: 10px;
207
- justify-content: flex-start;
208
- outline-offset: -1px;
209
- outline: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
210
- overflow: hidden;
211
- padding: 10px
212
- }
213
- .codigma-apiunit-reqresponse {
214
- align-items: flex-start;
215
- align-self: stretch;
216
- background: white;
217
- border-top: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
218
- display: flex;
219
- flex-direction: column;
220
- flex: 1 1 0;
221
- gap: 10px;
222
- justify-content: flex-start;
223
- padding-left: 5px;
224
- padding-right: 5px
225
- }
226
- .codigma-apiunit-responsedesc {
227
- align-items: center;
228
- align-self: stretch;
229
- display: inline-flex;
230
- justify-content: space-between;
231
- padding-right: 32px
232
- }
233
- .codigma-apiunit-responsetitle {
234
- align-items: center;
235
- border-bottom: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
236
- display: flex;
237
- height: 42px;
238
- justify-content: space-between;
239
- width: 122px
240
- }
241
- .codigma-apiunit-response-cnr {
242
- align-items: center;
243
- align-self: stretch;
244
- background: white;
245
- display: flex;
246
- justify-content: space-between;
247
- padding-top: 2px;
248
- width: 122px
249
- }
250
- .codigma-apiunit-responsebody {
251
- position: relative;
252
- align-self: stretch;
253
- flex: 1 1 0;
254
- min-height: 5rem;
255
- /* padding: 2px */
256
- }`;
257
-
258
- // src/api-render-ui.ts
259
- var ApiRenderer = class {
260
- options;
261
- container;
262
- constructor(options) {
263
- this.options = Object.assign({
264
- mountPoint: document?.body,
265
- // 默认挂载到body
266
- className: "Apioperatorlist codigma-apioperatorlist",
267
- layerName: "apioperatorlist"
268
- }, options);
269
- this.container = null;
270
- }
271
- render(apiSpec, renderUnit = false) {
272
- const appendInlineStyle = (text) => {
273
- const styleEl = document.createElement("style");
274
- styleEl.textContent = text;
275
- document.head.appendChild(styleEl);
276
- };
277
- appendInlineStyle(GLOBAL_STYLES);
278
- const elementMap = /* @__PURE__ */ new Map();
279
- const mountElement = typeof this.options.mountPoint === "string" ? document.querySelector(this.options.mountPoint) : this.options.mountPoint;
280
- if (!mountElement) {
281
- throw new Error("Invalid mount point specified");
282
- }
283
- if (apiSpec["openapi"] && apiSpec["openapi"].startsWith("3.") || apiSpec["swagger"] && apiSpec["swagger"].startsWith("2.")) {
284
- const countApi = countOpenAPI(apiSpec);
285
- const apiOperatorList = parseOpenAPI(apiSpec);
286
- console.log("\u89E3\u6790\u7ED3\u679C:", apiOperatorList);
287
- if (countApi == 1 && renderUnit) {
288
- this.container = createApiUnit(apiOperatorList[0]);
289
- } else {
290
- this.container = document.createElement("div");
291
- this.container.setAttribute("data-layer", this.options.layerName);
292
- this.container.className = this.options.className;
293
- apiOperatorList.forEach((apiOperator) => {
294
- const reqOperator = document.createElement("div");
295
- reqOperator.setAttribute("data-layer", "apioperator");
296
- reqOperator.className = "Apioperator codigma-apioperator";
297
- const methodContainer = document.createElement("div");
298
- methodContainer.setAttribute("data-layer", "method");
299
- methodContainer.className = "Method codigma-method";
300
- const methodType = document.createElement("div");
301
- methodType.setAttribute("data-layer", "methodtype");
302
- methodType.className = "Methodtype codigma-methodtype";
303
- const methodValue = document.createElement("div");
304
- methodValue.setAttribute("data-layer", apiOperator.method.toLowerCase());
305
- methodValue.className = `${apiOperator.method} codigma-${apiOperator.method.toLowerCase()}`;
306
- methodValue.textContent = apiOperator.method;
307
- methodType.appendChild(methodValue);
308
- methodContainer.appendChild(methodType);
309
- reqOperator.appendChild(methodContainer);
310
- const urlElement = document.createElement("div");
311
- urlElement.setAttribute("data-layer", "requrl");
312
- urlElement.className = "Requrl codigma-requrl";
313
- urlElement.textContent = apiOperator.url;
314
- reqOperator.appendChild(urlElement);
315
- this.container.appendChild(reqOperator);
316
- reqOperator.addEventListener("click", renderApiUnit(apiOperator, this.container, elementMap));
317
- });
318
- }
319
- } else {
320
- const apiOperator = {
321
- method: "get",
322
- url: "/full/info",
323
- rawApiInfo: null,
324
- requestBody: null,
325
- response: {},
326
- auths: [
327
- {
328
- type: "No Auth"
329
- },
330
- {
331
- type: "Basic Auth",
332
- parameters: [
333
- {
334
- name: "userName",
335
- value: ""
336
- },
337
- {
338
- name: "password",
339
- value: ""
340
- }
341
- ]
342
- }
343
- ]
344
- };
345
- this.container = createApiUnit(apiSpec);
346
- }
347
- mountElement.innerHTML = "";
348
- mountElement.appendChild(this.container);
349
- }
350
- };
351
- function renderApiUnit(apiOperator, containerRef, elementMap) {
352
- return function(evt) {
353
- console.log("\u70B9\u51FB\u7684API\u64CD\u4F5C\uFF1A");
354
- console.log("\u65B9\u6CD5:", apiOperator.method);
355
- console.log("URL:", apiOperator.url);
356
- console.log("------------------------");
357
- const currentTarget = evt.currentTarget;
358
- if (elementMap.has(currentTarget)) {
359
- const newElement = elementMap.get(currentTarget);
360
- newElement.remove();
361
- elementMap.delete(currentTarget);
362
- return;
363
- }
364
- const apiContainer = createApiUnit(apiOperator);
365
- if (currentTarget.nextSibling) {
366
- containerRef.insertBefore(apiContainer, currentTarget.nextSibling);
367
- } else {
368
- containerRef.appendChild(apiContainer);
369
- }
370
- elementMap.set(currentTarget, apiContainer);
371
- };
372
- }
373
- function createApiUnit(apiOperator) {
374
- const apiContainer = document.createElement("div");
375
- apiContainer.setAttribute("data-layer", "apiunit");
376
- apiContainer.className = "Requnit codigma-apiunit";
377
- const responseSection = createSectionResponse(apiOperator);
378
- apiOperator._responseSectionRef = responseSection;
379
- const reqOperator = createReqOperator(apiOperator);
380
- apiContainer.appendChild(reqOperator);
381
- const reqContent = document.createElement("div");
382
- reqContent.setAttribute("data-layer", "reqcontent");
383
- reqContent.className = "Reqcontent codigma-apiunit-reqcontent";
384
- const paramSection = createParamSection(apiOperator);
385
- const headerSection = createSectionHeader(apiOperator);
386
- const authSection = createSectionAuth(apiOperator);
387
- const bodySection = createSectionRequestBody(apiOperator.requestBody);
388
- reqContent.append(paramSection, headerSection, authSection, bodySection);
389
- apiContainer.appendChild(reqContent);
390
- apiContainer.appendChild(responseSection);
391
- return apiContainer;
392
- }
393
- function createReqOperator(apiOperator) {
394
- const reqOperator = document.createElement("div");
395
- reqOperator.setAttribute("data-layer", "apioperator");
396
- reqOperator.className = "Reqoperator codigma-apiunit-apioperator";
397
- const methodType = document.createElement("div");
398
- methodType.setAttribute("data-layer", "method");
399
- methodType.className = "Method codigma-apiunit-method";
400
- const methodTypeInner = document.createElement("div");
401
- methodTypeInner.setAttribute("data-layer", "methodtype");
402
- methodTypeInner.className = "Methodtype codigma-apiunit-methodtype";
403
- methodTypeInner.textContent = apiOperator.method.toUpperCase();
404
- methodType.appendChild(methodTypeInner);
405
- const methodOpt = document.createElement("div");
406
- methodOpt.setAttribute("data-svg-wrapper", "");
407
- methodOpt.setAttribute("data-layer", "methodopt");
408
- methodOpt.className = "Methodopt";
409
- const svg = createSvg();
410
- methodOpt.appendChild(svg);
411
- methodType.appendChild(methodOpt);
412
- reqOperator.appendChild(methodType);
413
- const reqUrl = createInputElement();
414
- reqUrl.setAttribute("data-layer", "requrl");
415
- reqUrl.className = "Requrl codigma-apiunit-requrl";
416
- reqUrl.value = apiOperator.url;
417
- reqUrl.addEventListener("input", (e) => {
418
- console.log("\u5F53\u524D\u503C:", e.target.value);
419
- apiOperator.url = e.target.value;
420
- });
421
- reqOperator.appendChild(reqUrl);
422
- const sendButton = document.createElement("div");
423
- sendButton.setAttribute("data-layer", "send-button");
424
- sendButton.className = "SendButton codigma-apiunit-send-button";
425
- const sendText = document.createElement("div");
426
- sendText.setAttribute("data-layer", "Send");
427
- sendText.className = "Send codigma-apiunit-send";
428
- sendText.textContent = "Send";
429
- sendButton.appendChild(sendText);
430
- sendButton.addEventListener("click", (e) => {
431
- console.log("\u5F53\u524D\u503C:", e.target.value);
432
- const respRef = apiOperator._responseSectionRef || null;
433
- sendRequest(apiOperator, respRef);
434
- });
435
- reqOperator.appendChild(sendButton);
436
- return reqOperator;
437
- }
438
- function createParamSection(apiOperator) {
439
- const paramSection = document.createElement("div");
440
- paramSection.setAttribute("data-layer", "paramters-section");
441
- paramSection.className = "codigma-apiunit-paramters-section";
442
- const paramCnr = document.createElement("div");
443
- paramCnr.setAttribute("data-layer", "parameters-cnr");
444
- paramCnr.className = "codigma-apiunit-parameters-cnr";
445
- const paramText = document.createElement("div");
446
- paramText.setAttribute("data-layer", "parameters");
447
- paramText.className = "codigma-apiunit-send";
448
- paramText.textContent = "Parameters";
449
- paramCnr.appendChild(paramText);
450
- paramSection.appendChild(paramCnr);
451
- const paramValues = document.createElement("div");
452
- paramValues.setAttribute("data-layer", "paraKeyValues");
453
- paramValues.className = "Parakeyvalues codigma-apiunit-parakeyvalues";
454
- let parameters = apiOperator.rawApiInfo.parameters || [];
455
- const parameterRows = parameters.filter((parameter) => isPathOrQueryParam(parameter)).map((parameter) => createRow(parameter));
456
- paramValues.append(...parameterRows);
457
- paramSection.append(paramCnr, paramValues);
458
- return paramSection;
459
- }
460
- function createSectionHeader(apiOperator) {
461
- const headerSection = document.createElement("div");
462
- headerSection.setAttribute("data-layer", "headers-section");
463
- headerSection.className = "codigma-apiunit-paramters-section";
464
- const headerCnr = document.createElement("div");
465
- headerCnr.setAttribute("data-layer", "headers-cnr");
466
- headerCnr.className = "codigma-apiunit-parameters-cnr";
467
- const headerText = document.createElement("div");
468
- headerText.setAttribute("data-layer", "headers");
469
- headerText.className = "codigma-apiunit-send";
470
- headerText.textContent = "Headers";
471
- headerCnr.appendChild(headerText);
472
- headerSection.appendChild(headerCnr);
473
- const headerValues = document.createElement("div");
474
- headerValues.setAttribute("data-layer", "paraKeyValues");
475
- headerValues.className = "Parakeyvalues codigma-apiunit-parakeyvalues";
476
- let parameters = apiOperator.rawApiInfo.parameters || [];
477
- const headerRows = parameters.filter((parameter) => isHeaderParam(parameter)).map((parameter) => createRow(parameter));
478
- headerValues.append(...headerRows);
479
- headerSection.append(headerCnr, headerValues);
480
- return headerSection;
481
- }
482
- function createSectionAuth(apiOperator) {
483
- const authSection = document.createElement("div");
484
- authSection.setAttribute("data-layer", "authorization-section");
485
- authSection.className = "AuthorizationSection codigma-apiunit-paramters-section";
486
- const authCnr = document.createElement("div");
487
- authCnr.setAttribute("data-layer", "authorization-cnr");
488
- authCnr.className = "AuthorizationCnr codigma-apiunit-parameters-cnr";
489
- const authText = document.createElement("div");
490
- authText.setAttribute("data-layer", "authorization");
491
- authText.className = "Authorization codigma-apiunit-send";
492
- authText.textContent = "Authorization";
493
- authCnr.appendChild(authText);
494
- authSection.appendChild(authCnr);
495
- const authValues = document.createElement("div");
496
- authValues.setAttribute("data-layer", "paraKeyValues");
497
- authValues.className = "Parakeyvalues codigma-apiunit-parakeyvalues";
498
- let auths = apiOperator.auths || [];
499
- const authTypeRow = createSelectRow(auths, authValues);
500
- if (auths.length > 0) {
501
- let parameters = apiOperator.auths[0].parameters || [];
502
- const authRows = parameters.map((parameter) => createRow(parameter));
503
- authValues.append(authTypeRow, ...authRows);
504
- }
505
- authSection.append(authCnr, authValues);
506
- return authSection;
507
- }
508
- function createSectionRequestBody(requestBody) {
509
- const bodySection = document.createElement("div");
510
- bodySection.setAttribute("data-layer", "request-body-section");
511
- bodySection.className = "RequestBodySection codigma-apiunit-request-body-section";
512
- const bodyCnr = document.createElement("div");
513
- bodyCnr.setAttribute("data-layer", "body-cnr");
514
- bodyCnr.className = "BodyCnr codigma-apiunit-parameters-cnr";
515
- const bodyText = document.createElement("div");
516
- bodyText.setAttribute("data-layer", "body");
517
- bodyText.className = "Body codigma-apiunit-send";
518
- bodyText.textContent = "Body";
519
- bodyCnr.appendChild(bodyText);
520
- bodySection.appendChild(bodyCnr);
521
- const bodyValue = document.createElement("textarea");
522
- bodyValue.setAttribute("data-layer", "bodyTextValue");
523
- bodyValue.className = "Id0CategoryId0NameNamePhotourlsTagsId0NameStatusAvailable codigma-apiunit-parakeyvalues";
524
- bodyValue.value = JSON.stringify(requestBody);
525
- bodySection.appendChild(bodyValue);
526
- return bodySection;
527
- }
528
- function createSectionResponse(apiOperator) {
529
- const responseSection = document.createElement("div");
530
- responseSection.setAttribute("data-layer", "reqresponse");
531
- responseSection.className = "Reqresponse codigma-apiunit-reqresponse";
532
- const responseDesc = document.createElement("div");
533
- responseDesc.setAttribute("data-layer", "responsedesc");
534
- responseDesc.className = "Responsedesc codigma-apiunit-responsedesc";
535
- const responseTitle = document.createElement("div");
536
- responseTitle.setAttribute("data-layer", "responsetitle");
537
- responseTitle.className = "Responsetitle codigma-apiunit-responsetitle";
538
- const responseCnr = document.createElement("div");
539
- responseCnr.setAttribute("data-layer", "response-cnr");
540
- responseCnr.className = "ResponseCnr codigma-apiunit-response-cnr";
541
- const responseText = document.createElement("div");
542
- responseText.setAttribute("data-layer", "response");
543
- responseText.className = "Response codigma-apiunit-send";
544
- responseText.textContent = "Response";
545
- responseCnr.appendChild(responseText);
546
- responseTitle.appendChild(responseCnr);
547
- const timeStatusElement = createTimeStatusElement(apiOperator);
548
- responseDesc.append(responseTitle, timeStatusElement);
549
- const responseBody = document.createElement("div");
550
- responseBody.setAttribute("data-layer", "responsebody");
551
- responseBody.className = "Responsebody codigma-apiunit-responsebody";
552
- responseSection.append(responseDesc, responseBody);
553
- return responseSection;
554
- }
555
- function createTimeStatusElement(apiOperator) {
556
- const timeStatus = document.createElement("div");
557
- timeStatus.setAttribute("data-layer", "TimeStatus");
558
- timeStatus.className = "TimeStatus0 codigma-apiunit-send";
559
- updateTimeStatus(timeStatus, apiOperator);
560
- return timeStatus;
561
- }
562
- function createRow(parameter) {
563
- const petIdRow = document.createElement("div");
564
- petIdRow.setAttribute("data-layer", "keyValue");
565
- petIdRow.className = "Keyvalue codigma-apiunit-keyvalue";
566
- const petIdLabel = document.createElement("div");
567
- petIdLabel.setAttribute("data-layer", parameter["name"]);
568
- petIdLabel.className = parameter["name"] + " codigma-apiunit-send";
569
- petIdLabel.textContent = parameter["name"] + ":";
570
- const petIdValue = createInputElement();
571
- petIdValue.setAttribute("data-layer", "valueText");
572
- petIdValue.className = "Valuetext codigma-apiunit-valuetext";
573
- petIdValue.value = parameter["value"] || "";
574
- petIdValue.addEventListener("input", (e) => {
575
- console.log("\u5F53\u524D\u503C:", e.target.value);
576
- parameter["value"] = e.target.value;
577
- });
578
- petIdRow.append(petIdLabel, petIdValue);
579
- return petIdRow;
580
- }
581
- function createInputElement() {
582
- const inputText = document.createElement("input");
583
- inputText.setAttribute("type", "text");
584
- inputText.setAttribute("name", "text-input");
585
- inputText.setAttribute("label", "text-input");
586
- inputText.setAttribute("autocomplete", "off");
587
- return inputText;
588
- }
589
- function createSelectRow(auths, authValues) {
590
- const container = document.createElement("div");
591
- container.setAttribute("data-layer", "keyValue");
592
- container.className = "Keyvalue codigma-apiunit-keyvalue";
593
- const typeLabel = document.createElement("div");
594
- typeLabel.setAttribute("data-layer", "type");
595
- typeLabel.className = "type codigma-apiunit-send";
596
- typeLabel.textContent = "Type:";
597
- const selectElement = document.createElement("select");
598
- selectElement.name = "text-select";
599
- selectElement.label = "text-select";
600
- selectElement.setAttribute("data-layer", "valueText");
601
- auths.forEach((auth) => {
602
- const option1 = document.createElement("option");
603
- option1.value = auth["type"];
604
- option1.textContent = auth["type"];
605
- selectElement.appendChild(option1);
606
- });
607
- selectElement.addEventListener("change", function(event) {
608
- authValues && Array.from(authValues.children).slice(1).forEach((el) => el.remove());
609
- const auth = auths[event.target.selectedIndex];
610
- let parameters = auth.parameters || [];
611
- const authRows = parameters.map((parameter) => createRow(parameter));
612
- authValues.append(...authRows);
613
- });
614
- container.appendChild(typeLabel);
615
- container.appendChild(selectElement);
616
- return container;
617
- }
618
- function isPathOrQueryParam(param) {
619
- return param.in === "path" || param.in === "query";
620
- }
621
- function isHeaderParam(param) {
622
- return param.in === "header";
623
- }
624
- function sendRequest(apiOperator, responseSectionRef) {
625
- if (!responseSectionRef) {
626
- console.error("sendRequest: missing responseSectionRef. Aborting request to avoid runtime errors.");
627
- return;
628
- }
629
- let reuqestUrl = getRequestUrl(apiOperator);
630
- const result = checkIfParameter(apiOperator);
631
- let header = result.hasRequestBody ? "application/json" : result.hasRequestFormData ? "application/x-www-form-urlencoded" : "application/json";
632
- let headers = {
633
- "Content-Type": header
634
- };
635
- if (apiOperator.custom) {
636
- for (let index = 0; index < apiOperator.customHeaderparameters.length; index++) {
637
- const paras = apiOperator.customHeaderparameters[index];
638
- if (paras.name != "" && paras.value != "" && paras.name != null && paras.value != null) {
639
- headers[paras.name] = paras.value;
640
- }
641
- }
642
- }
643
- let body = result.hasRequestBody ? apiOperator.requestBody : result.hasRequestFormData ? getRequestFormData(apiOperator.rawApiInfo) : null;
644
- if (apiOperator.custom) {
645
- if (apiOperator.method.toUpperCase() == "POST" || apiOperator.method.toUpperCase() == "PUT") {
646
- body = apiOperator.requestBody;
647
- }
648
- }
649
- apiOperator.ifSendingRequest = true;
650
- const startTime = Date.now();
651
- apiOperator.controller = new AbortController();
652
- const signal = apiOperator.controller.signal;
653
- const overlayLayerContainer = createRequestOverlayLayer(apiOperator, responseSectionRef);
654
- fetch(reuqestUrl, {
655
- method: apiOperator.method.toUpperCase(),
656
- headers,
657
- body: body != null ? JSON.stringify(body) : null,
658
- signal
659
- }).then((response) => {
660
- if (!response.ok) {
661
- apiOperator.response = {
662
- status: response.status,
663
- statusText: response.statusText
664
- };
665
- const endTime2 = Date.now();
666
- apiOperator.requestDuration = formatDuration(endTime2 - startTime);
667
- apiOperator.ifSendingRequest = false;
668
- throw new Error("Network response was not ok.");
669
- }
670
- const endTime = Date.now();
671
- apiOperator.requestDuration = formatDuration(endTime - startTime);
672
- const responsebodyElement = responseSectionRef.querySelector('[data-layer="responsebody"]');
673
- responsebodyElement.removeChild(overlayLayerContainer);
674
- apiOperator.ifSendingRequest = false;
675
- apiOperator.response = {
676
- status: response.status,
677
- statusText: response.statusText
678
- };
679
- const timeStatusElement = responseSectionRef.querySelector('[data-layer="TimeStatus"]');
680
- if (timeStatusElement) {
681
- updateTimeStatus(timeStatusElement, apiOperator);
682
- } else {
683
- console.log("\u5728reqresponse\u8303\u56F4\u5185\u672A\u627E\u5230TimeStatus\u5143\u7D20");
684
- }
685
- let responseClone = response.clone();
686
- return response.json().catch((error) => {
687
- return responseClone.text();
688
- });
689
- }).then((data) => {
690
- apiOperator.gotResponse = true;
691
- const responsebodyElement = responseSectionRef.querySelector('[data-layer="responsebody"]');
692
- if (typeof data === "object") {
693
- console.log("Received JSON:", data);
694
- responsebodyElement.textContent = JSON.stringify(data, null, 4);
695
- } else {
696
- console.log("Received text:", data);
697
- responsebodyElement.textContent = data;
698
- }
699
- }).catch((error) => {
700
- console.error("There has been a problem with your fetch operation:", error);
701
- });
702
- }
703
- function updateTimeStatus(timeStatus, apiOperator) {
704
- timeStatus.textContent = `Status: ${apiOperator.response.status || ""} ${apiOperator.response.statusText || ""} Time: ${apiOperator.requestDuration || ""}`;
705
- }
706
- function createRequestOverlayLayer(apiOperator, responseSectionRef) {
707
- const container = document.createElement("div");
708
- Object.assign(container.style, {
709
- position: "absolute",
710
- top: 0,
711
- width: "100%",
712
- height: "100%",
713
- display: "flex",
714
- justifyContent: "center",
715
- alignItems: "center",
716
- backgroundColor: "rgb(255, 255, 255)",
717
- opacity: 0.8
718
- });
719
- const contentDiv = document.createElement("div");
720
- Object.assign(contentDiv.style, {
721
- display: "flex",
722
- gap: "5px",
723
- alignItems: "center"
724
- });
725
- const textDiv = document.createElement("div");
726
- textDiv.textContent = "Sending request...";
727
- const cancelBtn = document.createElement("button");
728
- cancelBtn.className = "request-cancel-btn";
729
- Object.assign(cancelBtn.style, {
730
- border: "0px"
731
- });
732
- cancelBtn.textContent = "Cancel";
733
- const responsebodyElement = responseSectionRef.querySelector('[data-layer="responsebody"]');
734
- contentDiv.appendChild(textDiv);
735
- contentDiv.appendChild(cancelBtn);
736
- container.appendChild(contentDiv);
737
- responsebodyElement.appendChild(container);
738
- cancelBtn.addEventListener("click", (e) => {
739
- if (apiOperator.controller) {
740
- apiOperator.controller.abort();
741
- apiOperator.ifSendingRequest = false;
742
- container.style.display = "none";
743
- responsebodyElement.removeChild(container);
744
- }
745
- });
746
- return container;
747
- }
748
- function checkIfParameter(apiOperator) {
749
- let hasRequestBody = false;
750
- let hasRequestFormData = false;
751
- const parameters = apiOperator.rawApiInfo.parameters;
752
- if (parameters) {
753
- for (let index = 0; index < parameters.length; index++) {
754
- const parameter = parameters[index];
755
- if (parameter.in == "query" || parameter.in == "path") {
756
- } else if (parameter.in == "body") {
757
- hasRequestBody = true;
758
- parameter.name = parameter.name.charAt(0).toUpperCase() + parameter.name.slice(1);
759
- } else if (parameter.in == "formData") {
760
- hasRequestFormData = true;
761
- }
762
- }
763
- }
764
- const requestBody = apiOperator.rawApiInfo.requestBody;
765
- if (requestBody) {
766
- hasRequestBody = true;
767
- }
768
- return { hasRequestBody, hasRequestFormData };
769
- }
770
- function formatDuration(milliseconds) {
771
- let totalSeconds = Math.floor(milliseconds / 1e3);
772
- let seconds = totalSeconds % 60;
773
- let minutes = Math.floor(totalSeconds / 60) % 60;
774
- let hours = Math.floor(totalSeconds / (60 * 60));
775
- let millisecondsPart = Math.floor(milliseconds % 1e3);
776
- millisecondsPart = millisecondsPart.toString().padStart(3, "0");
777
- hours = hours.toString().padStart(2, "0");
778
- minutes = minutes.toString().padStart(2, "0");
779
- seconds = seconds.toString().padStart(2, "0");
780
- return `${hours}h${minutes}m${seconds}s${millisecondsPart}ms`;
781
- }
782
- function getRequestUrl(apiOperator) {
783
- let reuqestUrl = apiOperator.url;
784
- const requestParameters = apiOperator.rawApiInfo.parameters || [];
785
- if (requestParameters == null) {
786
- return reuqestUrl;
787
- }
788
- for (const element of requestParameters) {
789
- if (element.in == "path") {
790
- reuqestUrl = reuqestUrl.replace("{" + element.name + "}", element.value);
791
- }
792
- }
793
- let queryParams = getQueryParams(apiOperator, requestParameters);
794
- reuqestUrl = queryParams.length > 0 ? reuqestUrl + "?" + queryParams.join("&") : reuqestUrl;
795
- return reuqestUrl;
796
- }
797
- function getQueryParams(apiOperator, requestParameters) {
798
- let queryParams = [];
799
- for (const element of requestParameters) {
800
- if (element.in == "query") {
801
- if (element.type == "array" || element.schema != null && element.schema.type == "array") {
802
- if (element.value != null) {
803
- for (let index = 0; index < element.value.length; index++) {
804
- queryParams.push(element.name + "=" + element.value[index].value);
805
- }
806
- }
807
- } else {
808
- queryParams.push(element.name + "=" + element.value);
809
- }
810
- }
811
- }
812
- if (apiOperator.custom) {
813
- for (let index = 0; index < apiOperator.customQueryparameters.length; index++) {
814
- const paras = apiOperator.customQueryparameters[index];
815
- if (paras.name != "" && paras.value != "" && paras.name != null && paras.value != null) {
816
- queryParams.push(paras.name + "=" + paras.value);
817
- }
818
- }
819
- }
820
- return queryParams;
821
- }
822
- function getRequestFormData(rawApiInfo) {
823
- let formData = "";
824
- const requestParameters = rawApiInfo.parameters;
825
- if (requestParameters == null) {
826
- return "";
827
- }
828
- let first = 0;
829
- for (const element of requestParameters) {
830
- if (element.in == "formData") {
831
- if (first == 0) {
832
- formData = element.name + "=" + element.value;
833
- } else {
834
- formData = formData + "&" + element.name + "=" + element.value;
835
- }
836
- first++;
837
- }
838
- }
839
- return formData;
840
- }
841
- function createSvg() {
842
- const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
843
- svg.setAttribute("width", "21");
844
- svg.setAttribute("height", "22");
845
- svg.setAttribute("viewBox", "0 0 21 22");
846
- svg.setAttribute("fill", "none");
847
- svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
848
- const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
849
- path.setAttribute("d", "M5.5 8.5L10.5 13.5L15.5 8.5");
850
- path.setAttribute("stroke", "var(--Labels---Vibrant---Controls-Primary--, #404040)");
851
- path.setAttribute("stroke-width", "2");
852
- path.setAttribute("stroke-linecap", "round");
853
- path.setAttribute("stroke-linejoin", "round");
854
- svg.appendChild(path);
855
- return svg;
856
- }
857
- function parseParaModel(parameterObj, dataDef) {
858
- let bodyModel;
859
- if (parameterObj && parameterObj.schema) {
860
- if (parameterObj.schema["$ref"]) {
861
- bodyModel = parseModel(parameterObj.schema["$ref"], dataDef);
862
- } else if (parameterObj.schema["type"] == "array") {
863
- const itemObj = parameterObj.schema["items"];
864
- if (itemObj["$ref"]) {
865
- bodyModel = parseModel(itemObj["$ref"], dataDef);
866
- } else if (itemObj["type"]) {
867
- bodyModel = parseElement(itemObj);
868
- }
869
- bodyModel = [bodyModel];
870
- }
871
- }
872
- return bodyModel;
873
- }
874
- function parseModel(modelDef, apiDef) {
875
- const model = {};
876
- const bodyName = modelDef.substring(modelDef.lastIndexOf("/") + 1);
877
- const def = apiDef[bodyName];
878
- const props = def["properties"];
879
- if (props) {
880
- for (const key in props) {
881
- if (Object.prototype.hasOwnProperty.call(props, key)) {
882
- const element = props[key];
883
- if (element.hasOwnProperty("items") && element["type"] == "array") {
884
- if (element["items"]["$ref"]) {
885
- model[key] = [parseModel(element["items"]["$ref"], apiDef)];
886
- } else if (element["items"]["type"]) {
887
- model[key] = [parseElement(element["items"])];
888
- }
889
- } else if (element.hasOwnProperty("$ref")) {
890
- model[key] = parseModel(element["$ref"], apiDef);
891
- } else {
892
- if (element["type"]) {
893
- model[key] = parseElement(element);
894
- }
895
- }
896
- }
897
- }
898
- }
899
- return model;
900
- }
901
- function parseElement(element) {
902
- let elementValue;
903
- if (element["type"].includes("integer")) {
904
- elementValue = 0;
905
- } else if (element["type"].includes("boolean")) {
906
- elementValue = false;
907
- } else if (element["type"].includes("string")) {
908
- if (element["enum"]) {
909
- elementValue = element["enum"][0];
910
- } else {
911
- elementValue = "";
912
- }
913
- }
914
- if (element["example"]) {
915
- elementValue = element["example"];
916
- }
917
- return elementValue;
918
- }
919
- var HTTP_METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
920
- function isHttpMethod(method) {
921
- return HTTP_METHODS.includes(method);
922
- }
923
- function parseOpenAPI(openapiSpec) {
924
- const apiOperatorList = [];
925
- const pathEntries = [];
926
- for (const path in openapiSpec.paths) {
927
- if (Object.prototype.hasOwnProperty.call(openapiSpec.paths, path)) {
928
- pathEntries.push([path, openapiSpec.paths[path]]);
929
- }
930
- }
931
- pathEntries.forEach(([path, pathItem]) => {
932
- const pathItemEntries = [];
933
- for (const method in pathItem) {
934
- if (Object.prototype.hasOwnProperty.call(pathItem, method)) {
935
- if (isHttpMethod(method)) {
936
- const operation = pathItem[method];
937
- if (operation != null && typeof operation === "object" && !("$ref" in operation)) {
938
- pathItemEntries.push([method, operation]);
939
- }
940
- }
941
- }
942
- }
943
- pathItemEntries.forEach(([method, operation]) => {
944
- const apiOperator = {
945
- method: method.toUpperCase(),
946
- url: path,
947
- rawApiInfo: null,
948
- requestBody: null,
949
- response: {},
950
- auths: [
951
- {
952
- type: "No Auth"
953
- },
954
- {
955
- type: "Basic Auth",
956
- parameters: [
957
- {
958
- name: "userName",
959
- value: ""
960
- },
961
- {
962
- name: "password",
963
- value: ""
964
- }
965
- ]
966
- }
967
- ]
968
- };
969
- if (operation) {
970
- if (operation.parameters) {
971
- const parameter = operation.parameters.filter((val) => val.in == "body");
972
- if (parameter && parameter.length > 0) {
973
- }
974
- }
975
- if (operation.requestBody) {
976
- const requestBodyObject = operation.requestBody;
977
- const content = requestBodyObject.content;
978
- for (const key in content) {
979
- if (Object.prototype.hasOwnProperty.call(content, key)) {
980
- const element = content[key];
981
- if (element) {
982
- let requestBody = parseParaModel(element, openapiSpec["components"]["schemas"]);
983
- apiOperator.requestBody = requestBody;
984
- }
985
- }
986
- }
987
- }
988
- apiOperator.rawApiInfo = operation;
989
- apiOperatorList.push(apiOperator);
990
- }
991
- });
992
- });
993
- return apiOperatorList;
994
- }
995
- function countOpenAPI(openapiSpec) {
996
- let apiCount = 0;
997
- const pathEntries = [];
998
- for (const path in openapiSpec.paths) {
999
- if (Object.prototype.hasOwnProperty.call(openapiSpec.paths, path)) {
1000
- pathEntries.push([path, openapiSpec.paths[path]]);
1001
- }
1002
- }
1003
- pathEntries.forEach(([path, pathItem]) => {
1004
- const pathItemEntries = [];
1005
- for (const method in pathItem) {
1006
- if (Object.prototype.hasOwnProperty.call(pathItem, method)) {
1007
- if (isHttpMethod(method)) {
1008
- const operation = pathItem[method];
1009
- if (operation != null && typeof operation === "object" && !("$ref" in operation)) {
1010
- pathItemEntries.push([method, operation]);
1011
- }
1012
- }
1013
- }
1014
- }
1015
- pathItemEntries.forEach(([method, operation]) => {
1016
- apiCount = apiCount + 1;
1017
- });
1018
- });
1019
- return apiCount;
1020
- }
1021
- export {
1022
- ApiRenderer
1023
- };
1024
- //# sourceMappingURL=index.js.map