feat: update pre-request and post-response script handling in CollectionStateNotifier

- Changed return type of handlePreRequestScript and handlePostResponseScript to Future<RequestModel> for better type safety.
- Updated the logic to return the modified request model after executing scripts.
- Ensured that the request model is updated correctly in the sendRequest method after executing the pre-request script.
This commit is contained in:
Udhay-Adithya
2025-05-19 16:54:32 +05:30
parent 464dd9aff8
commit fe4858ecd0
2 changed files with 565 additions and 478 deletions

View File

File diff suppressed because it is too large Load Diff

View File

@@ -267,7 +267,7 @@ class CollectionStateNotifier
unsave(); unsave();
} }
Future<void> handlePreRequestScript( Future<RequestModel> handlePreRequestScript(
RequestModel requestModel, RequestModel requestModel,
EnvironmentModel? originalEnvironmentModel, EnvironmentModel? originalEnvironmentModel,
) async { ) async {
@@ -275,10 +275,8 @@ class CollectionStateNotifier
currentRequestModel: requestModel, currentRequestModel: requestModel,
activeEnvironment: originalEnvironmentModel?.toJson() ?? {}, activeEnvironment: originalEnvironmentModel?.toJson() ?? {},
); );
final newRequestModel =
requestModel =
requestModel.copyWith(httpRequestModel: scriptResult.updatedRequest); requestModel.copyWith(httpRequestModel: scriptResult.updatedRequest);
if (originalEnvironmentModel != null) { if (originalEnvironmentModel != null) {
final updatedEnvironmentMap = scriptResult.updatedEnvironment; final updatedEnvironmentMap = scriptResult.updatedEnvironment;
@@ -322,14 +320,17 @@ class CollectionStateNotifier
} else { } else {
debugPrint( debugPrint(
"Skipped environment update as originalEnvironmentModel was null."); "Skipped environment update as originalEnvironmentModel was null.");
if (scriptResult.updatedEnvironment.isNotEmpty) { if (scriptResult.updatedEnvironment.isNotEmpty) {
debugPrint( debugPrint(
"Warning: Pre-request script updated environment variables, but no active environment was selected to save them to."); "Warning: Pre-request script updated environment variables, but no active environment was selected to save them to.");
} }
return requestModel;
} }
return newRequestModel;
} }
Future<void> handlePostResponseScript( Future<RequestModel> handlePostResponseScript(
RequestModel requestModel, RequestModel requestModel,
EnvironmentModel? originalEnvironmentModel, EnvironmentModel? originalEnvironmentModel,
) async { ) async {
@@ -338,7 +339,7 @@ class CollectionStateNotifier
activeEnvironment: originalEnvironmentModel?.toJson() ?? {}, activeEnvironment: originalEnvironmentModel?.toJson() ?? {},
); );
requestModel = final newRequestModel =
requestModel.copyWith(httpResponseModel: scriptResult.updatedResponse); requestModel.copyWith(httpResponseModel: scriptResult.updatedResponse);
if (originalEnvironmentModel != null) { if (originalEnvironmentModel != null) {
@@ -388,7 +389,9 @@ class CollectionStateNotifier
debugPrint( debugPrint(
"Warning: Post-response script updated environment variables, but no active environment was selected to save them to."); "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 {
@@ -408,7 +411,8 @@ class CollectionStateNotifier
} }
if (requestModel != null && requestModel.preRequestScript.isNotEmpty) { if (requestModel != null && requestModel.preRequestScript.isNotEmpty) {
await handlePreRequestScript(requestModel, originalEnvironmentModel); requestModel =
await handlePreRequestScript(requestModel, originalEnvironmentModel);
} }
APIType apiType = requestModel!.apiType; APIType apiType = requestModel!.apiType;
@@ -432,7 +436,7 @@ class CollectionStateNotifier
noSSL: noSSL, noSSL: noSSL,
); );
late final RequestModel newRequestModel; late RequestModel newRequestModel;
if (responseRec.$1 == null) { if (responseRec.$1 == null) {
newRequestModel = requestModel.copyWith( newRequestModel = requestModel.copyWith(
responseStatus: -1, responseStatus: -1,
@@ -468,7 +472,8 @@ class CollectionStateNotifier
httpResponseModel: httpResponseModel, httpResponseModel: httpResponseModel,
); );
if (requestModel.postRequestScript.isNotEmpty) { if (requestModel.postRequestScript.isNotEmpty) {
handlePostResponseScript(newRequestModel, originalEnvironmentModel); newRequestModel = await handlePostResponseScript(
newRequestModel, originalEnvironmentModel);
} }
ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model); ref.read(historyMetaStateNotifier.notifier).addHistoryRequest(model);
} }