Spaces:
Running
Running
Update flare-ui/src/app/dialogs/api-edit-dialog/api-edit-dialog.component.ts
Browse files
flare-ui/src/app/dialogs/api-edit-dialog/api-edit-dialog.component.ts
CHANGED
@@ -14,8 +14,9 @@ import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
|
14 |
import { MatDividerModule } from '@angular/material/divider';
|
15 |
import { MatExpansionModule } from '@angular/material/expansion';
|
16 |
import { MatChipsModule } from '@angular/material/chips';
|
17 |
-
import { ApiService } from '../../services/api.service';
|
18 |
import { MatMenuModule } from '@angular/material/menu';
|
|
|
|
|
19 |
|
20 |
@Component({
|
21 |
selector: 'app-api-edit-dialog',
|
@@ -36,7 +37,8 @@ import { MatMenuModule } from '@angular/material/menu';
|
|
36 |
MatDividerModule,
|
37 |
MatExpansionModule,
|
38 |
MatChipsModule,
|
39 |
-
MatMenuModule
|
|
|
40 |
],
|
41 |
templateUrl: './api-edit-dialog.component.html',
|
42 |
styleUrls: ['./api-edit-dialog.component.scss']
|
@@ -51,17 +53,9 @@ export default class ApiEditDialogComponent implements OnInit {
|
|
51 |
responseMappingVariables: string[] = [];
|
52 |
activeTabIndex = 0;
|
53 |
|
54 |
-
variablePanelsExpanded: { [key: string]: boolean } = {
|
55 |
-
'body_template': false,
|
56 |
-
'auth.token_request_body': false,
|
57 |
-
'auth.token_refresh_body': false
|
58 |
-
};
|
59 |
-
|
60 |
httpMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
|
61 |
retryStrategies = ['static', 'exponential'];
|
62 |
variableTypes = ['str', 'int', 'float', 'bool', 'date'];
|
63 |
-
|
64 |
-
private cursorPositions: { [key: string]: number } = {};
|
65 |
|
66 |
constructor(
|
67 |
private fb: FormBuilder,
|
@@ -71,19 +65,15 @@ export default class ApiEditDialogComponent implements OnInit {
|
|
71 |
@Inject(MAT_DIALOG_DATA) public data: any
|
72 |
) {}
|
73 |
|
74 |
-
toggleVariablePanel(field: string): void {
|
75 |
-
this.variablePanelsExpanded[field] = !this.variablePanelsExpanded[field];
|
76 |
-
}
|
77 |
-
|
78 |
ngOnInit() {
|
79 |
this.initializeForm();
|
80 |
this.loadIntentParameters();
|
81 |
-
|
82 |
// Aktif tab'ı ayarla
|
83 |
if (this.data.activeTab !== undefined) {
|
84 |
this.activeTabIndex = this.data.activeTab;
|
85 |
}
|
86 |
-
|
87 |
if ((this.data.mode === 'edit' || this.data.mode === 'test') && this.data.api) {
|
88 |
this.populateForm(this.data.api);
|
89 |
} else if (this.data.mode === 'duplicate' && this.data.api) {
|
@@ -92,110 +82,20 @@ export default class ApiEditDialogComponent implements OnInit {
|
|
92 |
delete duplicateData.last_update_date;
|
93 |
this.populateForm(duplicateData);
|
94 |
}
|
95 |
-
|
96 |
// Test modunda açıldıysa test JSON'ını hazırla
|
97 |
if (this.data.mode === 'test') {
|
98 |
setTimeout(() => {
|
99 |
this.updateTestRequestJson();
|
100 |
}, 100);
|
101 |
}
|
102 |
-
|
103 |
// Watch response mappings changes
|
104 |
this.form.get('response_mappings')?.valueChanges.subscribe(() => {
|
105 |
this.updateResponseMappingVariables();
|
106 |
});
|
107 |
-
|
108 |
-
// JSON alanlarını izle (validation göstergesi için)
|
109 |
-
this.form.get('body_template')?.valueChanges.subscribe(() => {
|
110 |
-
this.form.get('body_template')?.updateValueAndValidity({ emitEvent: false });
|
111 |
-
});
|
112 |
-
|
113 |
-
this.form.get('auth.token_request_body')?.valueChanges.subscribe(() => {
|
114 |
-
this.form.get('auth.token_request_body')?.updateValueAndValidity({ emitEvent: false });
|
115 |
-
});
|
116 |
-
|
117 |
-
this.form.get('auth.token_refresh_body')?.valueChanges.subscribe(() => {
|
118 |
-
this.form.get('auth.token_refresh_body')?.updateValueAndValidity({ emitEvent: false });
|
119 |
-
});
|
120 |
-
}
|
121 |
-
|
122 |
-
// Tab tuşu handler
|
123 |
-
handleTabKey(event: KeyboardEvent, field: string): void {
|
124 |
-
if (event.key === 'Tab') {
|
125 |
-
event.preventDefault();
|
126 |
-
|
127 |
-
const textarea = event.target as HTMLTextAreaElement;
|
128 |
-
const start = textarea.selectionStart;
|
129 |
-
const end = textarea.selectionEnd;
|
130 |
-
const value = textarea.value;
|
131 |
-
|
132 |
-
// Tab karakteri ekle
|
133 |
-
const newValue = value.substring(0, start) + '\t' + value.substring(end);
|
134 |
-
|
135 |
-
// Form kontrolünü güncelle
|
136 |
-
const control = this.form.get(field);
|
137 |
-
if (control) {
|
138 |
-
control.setValue(newValue);
|
139 |
-
|
140 |
-
// Cursor pozisyonunu ayarla
|
141 |
-
setTimeout(() => {
|
142 |
-
textarea.selectionStart = textarea.selectionEnd = start + 1;
|
143 |
-
textarea.focus();
|
144 |
-
}, 0);
|
145 |
-
}
|
146 |
-
}
|
147 |
-
}
|
148 |
-
|
149 |
-
// Shift+Tab için outdent (opsiyonel)
|
150 |
-
handleShiftTab(event: KeyboardEvent, field: string): void {
|
151 |
-
if (event.key === 'Tab' && event.shiftKey) {
|
152 |
-
event.preventDefault();
|
153 |
-
|
154 |
-
const textarea = event.target as HTMLTextAreaElement;
|
155 |
-
const start = textarea.selectionStart;
|
156 |
-
const value = textarea.value;
|
157 |
-
|
158 |
-
// Cursor'dan önceki en yakın tab'ı bul ve sil
|
159 |
-
const beforeCursor = value.substring(0, start);
|
160 |
-
const lastTabIndex = beforeCursor.lastIndexOf('\t');
|
161 |
-
|
162 |
-
if (lastTabIndex !== -1 && start - lastTabIndex <= 4) { // Tab'a yakınsa
|
163 |
-
const newValue = value.substring(0, lastTabIndex) + value.substring(lastTabIndex + 1);
|
164 |
-
|
165 |
-
const control = this.form.get(field);
|
166 |
-
if (control) {
|
167 |
-
control.setValue(newValue);
|
168 |
-
|
169 |
-
setTimeout(() => {
|
170 |
-
textarea.selectionStart = textarea.selectionEnd = lastTabIndex;
|
171 |
-
textarea.focus();
|
172 |
-
}, 0);
|
173 |
-
}
|
174 |
-
}
|
175 |
-
}
|
176 |
}
|
177 |
|
178 |
-
// Test request için özel tab handler
|
179 |
-
handleTestRequestTab(event: KeyboardEvent): void {
|
180 |
-
if (event.key === 'Tab') {
|
181 |
-
event.preventDefault();
|
182 |
-
|
183 |
-
const textarea = event.target as HTMLTextAreaElement;
|
184 |
-
const start = textarea.selectionStart;
|
185 |
-
const end = textarea.selectionEnd;
|
186 |
-
const value = textarea.value;
|
187 |
-
|
188 |
-
// Tab karakteri ekle
|
189 |
-
this.testRequestJson = value.substring(0, start) + '\t' + value.substring(end);
|
190 |
-
|
191 |
-
// Cursor pozisyonunu ayarla
|
192 |
-
setTimeout(() => {
|
193 |
-
textarea.selectionStart = textarea.selectionEnd = start + 1;
|
194 |
-
textarea.focus();
|
195 |
-
}, 0);
|
196 |
-
}
|
197 |
-
}
|
198 |
-
|
199 |
initializeForm() {
|
200 |
this.form = this.fb.group({
|
201 |
// General Tab
|
@@ -419,21 +319,8 @@ export default class ApiEditDialogComponent implements OnInit {
|
|
419 |
}
|
420 |
}
|
421 |
|
422 |
-
|
423 |
-
|
424 |
-
if (!control || !control.value) return true;
|
425 |
-
|
426 |
-
try {
|
427 |
-
const jsonStr = control.value;
|
428 |
-
const processedJson = this.replaceVariablesForValidation(jsonStr);
|
429 |
-
JSON.parse(processedJson);
|
430 |
-
return true;
|
431 |
-
} catch {
|
432 |
-
return false;
|
433 |
-
}
|
434 |
-
}
|
435 |
-
|
436 |
-
replaceVariablesForValidation(jsonStr: string): string {
|
437 |
let processed = jsonStr;
|
438 |
|
439 |
processed = processed.replace(/\{\{([^}]+)\}\}/g, (match, variablePath) => {
|
@@ -513,15 +400,6 @@ export default class ApiEditDialogComponent implements OnInit {
|
|
513 |
}
|
514 |
}
|
515 |
|
516 |
-
prepareTestRequest(): any {
|
517 |
-
try {
|
518 |
-
const requestData = JSON.parse(this.testRequestJson);
|
519 |
-
return requestData;
|
520 |
-
} catch {
|
521 |
-
return {};
|
522 |
-
}
|
523 |
-
}
|
524 |
-
|
525 |
updateTestRequestJson() {
|
526 |
const formValue = this.form.getRawValue();
|
527 |
let bodyTemplate = {};
|
@@ -644,17 +522,6 @@ export default class ApiEditDialogComponent implements OnInit {
|
|
644 |
Object.keys(this.form.controls).forEach(key => {
|
645 |
this.form.get(key)?.markAsTouched();
|
646 |
});
|
647 |
-
|
648 |
-
const jsonFields = ['body_template', 'auth.token_request_body', 'auth.token_refresh_body'];
|
649 |
-
for (const field of jsonFields) {
|
650 |
-
if (!this.validateJSON(field)) {
|
651 |
-
this.snackBar.open(`Invalid JSON in ${field}`, 'Close', {
|
652 |
-
duration: 3000,
|
653 |
-
panelClass: 'error-snackbar'
|
654 |
-
});
|
655 |
-
return;
|
656 |
-
}
|
657 |
-
}
|
658 |
|
659 |
this.snackBar.open('Please fix validation errors', 'Close', { duration: 3000 });
|
660 |
return;
|
@@ -688,44 +555,4 @@ export default class ApiEditDialogComponent implements OnInit {
|
|
688 |
cancel() {
|
689 |
this.dialogRef.close(false);
|
690 |
}
|
691 |
-
|
692 |
-
saveCursorPosition(field: string, event: any) {
|
693 |
-
const textarea = event.target;
|
694 |
-
this.cursorPositions[field] = textarea.selectionStart;
|
695 |
-
}
|
696 |
-
|
697 |
-
insertTemplateVariable(field: string, variable: string) {
|
698 |
-
const control = field.includes('.')
|
699 |
-
? this.form.get(field)
|
700 |
-
: this.form.get(field);
|
701 |
-
|
702 |
-
if (control) {
|
703 |
-
const currentValue = control.value || '';
|
704 |
-
const variableText = `{{${variable}}}`;
|
705 |
-
const cursorPos = this.cursorPositions[field] || currentValue.length;
|
706 |
-
|
707 |
-
const newValue =
|
708 |
-
currentValue.slice(0, cursorPos) +
|
709 |
-
variableText +
|
710 |
-
currentValue.slice(cursorPos);
|
711 |
-
|
712 |
-
control.setValue(newValue);
|
713 |
-
|
714 |
-
setTimeout(() => {
|
715 |
-
let selector = `textarea[formControlName="${field}"]`;
|
716 |
-
if (field === 'auth.token_request_body') {
|
717 |
-
selector = 'div[formGroupName="auth"] textarea[formControlName="token_request_body"]';
|
718 |
-
} else if (field === 'auth.token_refresh_body') {
|
719 |
-
selector = 'div[formGroupName="auth"] textarea[formControlName="token_refresh_body"]';
|
720 |
-
}
|
721 |
-
|
722 |
-
const textarea = document.querySelector(selector) as HTMLTextAreaElement;
|
723 |
-
if (textarea) {
|
724 |
-
const newPos = cursorPos + variableText.length;
|
725 |
-
textarea.setSelectionRange(newPos, newPos);
|
726 |
-
textarea.focus();
|
727 |
-
}
|
728 |
-
}, 0);
|
729 |
-
}
|
730 |
-
}
|
731 |
}
|
|
|
14 |
import { MatDividerModule } from '@angular/material/divider';
|
15 |
import { MatExpansionModule } from '@angular/material/expansion';
|
16 |
import { MatChipsModule } from '@angular/material/chips';
|
|
|
17 |
import { MatMenuModule } from '@angular/material/menu';
|
18 |
+
import { ApiService } from '../../services/api.service';
|
19 |
+
import { JsonEditorComponent } from '../../shared/json-editor/json-editor.component';
|
20 |
|
21 |
@Component({
|
22 |
selector: 'app-api-edit-dialog',
|
|
|
37 |
MatDividerModule,
|
38 |
MatExpansionModule,
|
39 |
MatChipsModule,
|
40 |
+
MatMenuModule,
|
41 |
+
JsonEditorComponent
|
42 |
],
|
43 |
templateUrl: './api-edit-dialog.component.html',
|
44 |
styleUrls: ['./api-edit-dialog.component.scss']
|
|
|
53 |
responseMappingVariables: string[] = [];
|
54 |
activeTabIndex = 0;
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
httpMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
|
57 |
retryStrategies = ['static', 'exponential'];
|
58 |
variableTypes = ['str', 'int', 'float', 'bool', 'date'];
|
|
|
|
|
59 |
|
60 |
constructor(
|
61 |
private fb: FormBuilder,
|
|
|
65 |
@Inject(MAT_DIALOG_DATA) public data: any
|
66 |
) {}
|
67 |
|
|
|
|
|
|
|
|
|
68 |
ngOnInit() {
|
69 |
this.initializeForm();
|
70 |
this.loadIntentParameters();
|
71 |
+
|
72 |
// Aktif tab'ı ayarla
|
73 |
if (this.data.activeTab !== undefined) {
|
74 |
this.activeTabIndex = this.data.activeTab;
|
75 |
}
|
76 |
+
|
77 |
if ((this.data.mode === 'edit' || this.data.mode === 'test') && this.data.api) {
|
78 |
this.populateForm(this.data.api);
|
79 |
} else if (this.data.mode === 'duplicate' && this.data.api) {
|
|
|
82 |
delete duplicateData.last_update_date;
|
83 |
this.populateForm(duplicateData);
|
84 |
}
|
85 |
+
|
86 |
// Test modunda açıldıysa test JSON'ını hazırla
|
87 |
if (this.data.mode === 'test') {
|
88 |
setTimeout(() => {
|
89 |
this.updateTestRequestJson();
|
90 |
}, 100);
|
91 |
}
|
92 |
+
|
93 |
// Watch response mappings changes
|
94 |
this.form.get('response_mappings')?.valueChanges.subscribe(() => {
|
95 |
this.updateResponseMappingVariables();
|
96 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
}
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
initializeForm() {
|
100 |
this.form = this.fb.group({
|
101 |
// General Tab
|
|
|
319 |
}
|
320 |
}
|
321 |
|
322 |
+
// JSON validation için replacer fonksiyonu
|
323 |
+
replaceVariablesForValidation = (jsonStr: string): string => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
let processed = jsonStr;
|
325 |
|
326 |
processed = processed.replace(/\{\{([^}]+)\}\}/g, (match, variablePath) => {
|
|
|
400 |
}
|
401 |
}
|
402 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
403 |
updateTestRequestJson() {
|
404 |
const formValue = this.form.getRawValue();
|
405 |
let bodyTemplate = {};
|
|
|
522 |
Object.keys(this.form.controls).forEach(key => {
|
523 |
this.form.get(key)?.markAsTouched();
|
524 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
525 |
|
526 |
this.snackBar.open('Please fix validation errors', 'Close', { duration: 3000 });
|
527 |
return;
|
|
|
555 |
cancel() {
|
556 |
this.dialogRef.close(false);
|
557 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
558 |
}
|