mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
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:
1020
lib/consts.dart
1020
lib/consts.dart
File diff suppressed because it is too large
Load Diff
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user