api-render-ui 1.0.7 → 1.0.8

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