Create pre post script utilities

This commit is contained in:
Ashita Prasad
2025-06-22 22:29:11 +05:30
parent 9e686a8145
commit 67af625ccb
3 changed files with 157 additions and 133 deletions

View File

@ -5,8 +5,7 @@ import 'package:apidash/consts.dart';
import 'providers.dart'; import 'providers.dart';
import '../models/models.dart'; import '../models/models.dart';
import '../services/services.dart'; import '../services/services.dart';
import '../utils/utils.dart' import '../utils/utils.dart';
show getNewUuid, collectionToHAR, substituteHttpRequestModel;
final selectedIdStateProvider = StateProvider<String?>((ref) => null); final selectedIdStateProvider = StateProvider<String?>((ref) => null);
@ -266,133 +265,6 @@ class CollectionStateNotifier
unsave(); unsave();
} }
Future<RequestModel> handlePreRequestScript(
RequestModel requestModel,
EnvironmentModel? originalEnvironmentModel,
) async {
final scriptResult = await executePreRequestScript(
currentRequestModel: requestModel,
activeEnvironment: originalEnvironmentModel?.toJson() ?? {},
);
final newRequestModel =
requestModel.copyWith(httpRequestModel: scriptResult.updatedRequest);
if (originalEnvironmentModel != null) {
final updatedEnvironmentMap = scriptResult.updatedEnvironment;
final List<EnvironmentVariableModel> newValues = [];
final Map<String, dynamic> mutableUpdatedEnv =
Map.from(updatedEnvironmentMap);
for (final originalVariable in originalEnvironmentModel.values) {
if (mutableUpdatedEnv.containsKey(originalVariable.key)) {
final dynamic newValue = mutableUpdatedEnv[originalVariable.key];
newValues.add(
originalVariable.copyWith(
value: newValue == null ? '' : newValue.toString(),
enabled: true,
),
);
mutableUpdatedEnv.remove(originalVariable.key);
} else {
// Variable was removed by the script (unset/clear), don't add it to newValues.
// Alternatively, you could keep it but set enabled = false:
// newValues.add(originalVariable.copyWith(enabled: false));
}
}
for (final entry in mutableUpdatedEnv.entries) {
final dynamic newValue = entry.value;
newValues.add(
EnvironmentVariableModel(
key: entry.key,
value: newValue == null ? '' : newValue.toString(),
enabled: true,
type: EnvironmentVariableType.variable,
),
);
}
ref.read(environmentsStateNotifierProvider.notifier).updateEnvironment(
originalEnvironmentModel.id,
name: originalEnvironmentModel.name,
values: newValues);
} else {
debugPrint(
"Skipped environment update as originalEnvironmentModel was null.");
if (scriptResult.updatedEnvironment.isNotEmpty) {
debugPrint(
"Warning: Pre-request script updated environment variables, but no active environment was selected to save them to.");
}
return requestModel;
}
return newRequestModel;
}
Future<RequestModel> handlePostResponseScript(
RequestModel requestModel,
EnvironmentModel? originalEnvironmentModel,
) async {
final scriptResult = await executePostResponseScript(
currentRequestModel: requestModel,
activeEnvironment: originalEnvironmentModel?.toJson() ?? {},
);
final newRequestModel =
requestModel.copyWith(httpResponseModel: scriptResult.updatedResponse);
if (originalEnvironmentModel != null) {
final updatedEnvironmentMap = scriptResult.updatedEnvironment;
final List<EnvironmentVariableModel> newValues = [];
final Map<String, dynamic> mutableUpdatedEnv =
Map.from(updatedEnvironmentMap);
for (final originalVariable in originalEnvironmentModel.values) {
if (mutableUpdatedEnv.containsKey(originalVariable.key)) {
final dynamic newValue = mutableUpdatedEnv[originalVariable.key];
newValues.add(
originalVariable.copyWith(
value: newValue == null ? '' : newValue.toString(),
enabled: true,
),
);
mutableUpdatedEnv.remove(originalVariable.key);
} else {
// Variable was removed by the script (unset/clear), don't add it to newValues.
// Alternatively, you could keep it but set enabled = false:
// newValues.add(originalVariable.copyWith(enabled: false));
}
}
for (final entry in mutableUpdatedEnv.entries) {
final dynamic newValue = entry.value;
newValues.add(
EnvironmentVariableModel(
key: entry.key,
value: newValue == null ? '' : newValue.toString(),
enabled: true,
type: EnvironmentVariableType.variable,
),
);
}
ref.read(environmentsStateNotifierProvider.notifier).updateEnvironment(
originalEnvironmentModel.id,
name: originalEnvironmentModel.name,
values: newValues);
} else {
debugPrint(
"Skipped environment update as originalEnvironmentModel was null.");
if (scriptResult.updatedEnvironment.isNotEmpty) {
debugPrint(
"Warning: Post-response script updated environment variables, but no active environment was selected to save them to.");
}
return requestModel;
}
return newRequestModel;
}
Future<void> sendRequest() async { Future<void> sendRequest() async {
final requestId = ref.read(selectedIdStateProvider); final requestId = ref.read(selectedIdStateProvider);
ref.read(codePaneVisibleStateProvider.notifier).state = false; ref.read(codePaneVisibleStateProvider.notifier).state = false;
@ -410,8 +282,19 @@ class CollectionStateNotifier
} }
if (requestModel != null && requestModel.preRequestScript.isNotEmpty) { if (requestModel != null && requestModel.preRequestScript.isNotEmpty) {
requestModel = requestModel = await handlePreRequestScript(
await handlePreRequestScript(requestModel, originalEnvironmentModel); requestModel,
originalEnvironmentModel,
(envModel, updatedValues) {
ref
.read(environmentsStateNotifierProvider.notifier)
.updateEnvironment(
envModel.id,
name: envModel.name,
values: updatedValues,
);
},
);
} }
APIType apiType = requestModel!.apiType; APIType apiType = requestModel!.apiType;
@ -472,7 +355,18 @@ class CollectionStateNotifier
); );
if (requestModel.postRequestScript.isNotEmpty) { if (requestModel.postRequestScript.isNotEmpty) {
newRequestModel = await handlePostResponseScript( newRequestModel = await handlePostResponseScript(
newRequestModel, originalEnvironmentModel); newRequestModel,
originalEnvironmentModel,
(envModel, updatedValues) {
ref
.read(environmentsStateNotifierProvider.notifier)
.updateEnvironment(
envModel.id,
name: envModel.name,
values: updatedValues,
);
},
);
} }
ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model); ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model);
} }

View File

@ -0,0 +1,129 @@
import 'dart:nativewrappers/_internal/vm/lib/ffi_allocation_patch.dart';
import 'package:apidash_core/apidash_core.dart';
import 'package:flutter/foundation.dart';
import '../models/models.dart';
import '../services/services.dart';
Future<RequestModel> handlePreRequestScript(
RequestModel requestModel,
EnvironmentModel? originalEnvironmentModel,
void Function(EnvironmentModel, List<EnvironmentVariableModel>)? updateEnv,
) async {
final scriptResult = await executePreRequestScript(
currentRequestModel: requestModel,
activeEnvironment: originalEnvironmentModel?.toJson() ?? {},
);
final newRequestModel =
requestModel.copyWith(httpRequestModel: scriptResult.updatedRequest);
if (originalEnvironmentModel != null) {
final updatedEnvironmentMap = scriptResult.updatedEnvironment;
final List<EnvironmentVariableModel> newValues = [];
final Map<String, dynamic> mutableUpdatedEnv =
Map.from(updatedEnvironmentMap);
for (final originalVariable in originalEnvironmentModel.values) {
if (mutableUpdatedEnv.containsKey(originalVariable.key)) {
final dynamic newValue = mutableUpdatedEnv[originalVariable.key];
newValues.add(
originalVariable.copyWith(
value: newValue == null ? '' : newValue.toString(),
enabled: true,
),
);
mutableUpdatedEnv.remove(originalVariable.key);
} else {
// Variable was removed by the script (unset/clear), don't add it to newValues.
// Alternatively, you could keep it but set enabled = false:
// newValues.add(originalVariable.copyWith(enabled: false));
}
}
for (final entry in mutableUpdatedEnv.entries) {
final dynamic newValue = entry.value;
newValues.add(
EnvironmentVariableModel(
key: entry.key,
value: newValue == null ? '' : newValue.toString(),
enabled: true,
type: EnvironmentVariableType.variable,
),
);
}
updateEnv?.call(originalEnvironmentModel, newValues);
} else {
debugPrint(
"Skipped environment update as originalEnvironmentModel was null.");
if (scriptResult.updatedEnvironment.isNotEmpty) {
debugPrint(
"Warning: Pre-request script updated environment variables, but no active environment was selected to save them to.");
}
return requestModel;
}
return newRequestModel;
}
Future<RequestModel> handlePostResponseScript(
RequestModel requestModel,
EnvironmentModel? originalEnvironmentModel,
void Function(EnvironmentModel, List<EnvironmentVariableModel>)? updateEnv,
) async {
final scriptResult = await executePostResponseScript(
currentRequestModel: requestModel,
activeEnvironment: originalEnvironmentModel?.toJson() ?? {},
);
final newRequestModel =
requestModel.copyWith(httpResponseModel: scriptResult.updatedResponse);
if (originalEnvironmentModel != null) {
final updatedEnvironmentMap = scriptResult.updatedEnvironment;
final List<EnvironmentVariableModel> newValues = [];
final Map<String, dynamic> mutableUpdatedEnv =
Map.from(updatedEnvironmentMap);
for (final originalVariable in originalEnvironmentModel.values) {
if (mutableUpdatedEnv.containsKey(originalVariable.key)) {
final dynamic newValue = mutableUpdatedEnv[originalVariable.key];
newValues.add(
originalVariable.copyWith(
value: newValue == null ? '' : newValue.toString(),
enabled: true,
),
);
mutableUpdatedEnv.remove(originalVariable.key);
} else {
// Variable was removed by the script (unset/clear), don't add it to newValues.
// Alternatively, you could keep it but set enabled = false:
// newValues.add(originalVariable.copyWith(enabled: false));
}
}
for (final entry in mutableUpdatedEnv.entries) {
final dynamic newValue = entry.value;
newValues.add(
EnvironmentVariableModel(
key: entry.key,
value: newValue == null ? '' : newValue.toString(),
enabled: true,
type: EnvironmentVariableType.variable,
),
);
}
updateEnv?.call(originalEnvironmentModel, newValues);
} else {
debugPrint(
"Skipped environment update as originalEnvironmentModel was null.");
if (scriptResult.updatedEnvironment.isNotEmpty) {
debugPrint(
"Warning: Post-response script updated environment variables, but no active environment was selected to save them to.");
}
return requestModel;
}
return newRequestModel;
}

View File

@ -4,8 +4,9 @@ export 'file_utils.dart';
export 'har_utils.dart'; export 'har_utils.dart';
export 'header_utils.dart'; export 'header_utils.dart';
export 'history_utils.dart'; export 'history_utils.dart';
export 'js_utils.dart';
export 'http_utils.dart'; export 'http_utils.dart';
export 'js_utils.dart';
export 'pre_post_script_utils.dart';
export 'save_utils.dart'; export 'save_utils.dart';
export 'ui_utils.dart'; export 'ui_utils.dart';
export 'window_utils.dart'; export 'window_utils.dart';