From 67af625ccb0248b0586969f0bfea262d636f40a8 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sun, 22 Jun 2025 22:29:11 +0530 Subject: [PATCH] Create pre post script utilities --- lib/providers/collection_providers.dart | 158 ++++-------------------- lib/utils/pre_post_script_utils.dart | 129 +++++++++++++++++++ lib/utils/utils.dart | 3 +- 3 files changed, 157 insertions(+), 133 deletions(-) create mode 100644 lib/utils/pre_post_script_utils.dart diff --git a/lib/providers/collection_providers.dart b/lib/providers/collection_providers.dart index 15a495cc..0dd50592 100644 --- a/lib/providers/collection_providers.dart +++ b/lib/providers/collection_providers.dart @@ -5,8 +5,7 @@ import 'package:apidash/consts.dart'; import 'providers.dart'; import '../models/models.dart'; import '../services/services.dart'; -import '../utils/utils.dart' - show getNewUuid, collectionToHAR, substituteHttpRequestModel; +import '../utils/utils.dart'; final selectedIdStateProvider = StateProvider((ref) => null); @@ -266,133 +265,6 @@ class CollectionStateNotifier unsave(); } - Future 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 newValues = []; - final Map 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 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 newValues = []; - final Map 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 sendRequest() async { final requestId = ref.read(selectedIdStateProvider); ref.read(codePaneVisibleStateProvider.notifier).state = false; @@ -410,8 +282,19 @@ class CollectionStateNotifier } if (requestModel != null && requestModel.preRequestScript.isNotEmpty) { - requestModel = - await handlePreRequestScript(requestModel, originalEnvironmentModel); + requestModel = await handlePreRequestScript( + requestModel, + originalEnvironmentModel, + (envModel, updatedValues) { + ref + .read(environmentsStateNotifierProvider.notifier) + .updateEnvironment( + envModel.id, + name: envModel.name, + values: updatedValues, + ); + }, + ); } APIType apiType = requestModel!.apiType; @@ -472,7 +355,18 @@ class CollectionStateNotifier ); if (requestModel.postRequestScript.isNotEmpty) { 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); } diff --git a/lib/utils/pre_post_script_utils.dart b/lib/utils/pre_post_script_utils.dart new file mode 100644 index 00000000..9ee10595 --- /dev/null +++ b/lib/utils/pre_post_script_utils.dart @@ -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 handlePreRequestScript( + RequestModel requestModel, + EnvironmentModel? originalEnvironmentModel, + void Function(EnvironmentModel, List)? 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 newValues = []; + final Map 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 handlePostResponseScript( + RequestModel requestModel, + EnvironmentModel? originalEnvironmentModel, + void Function(EnvironmentModel, List)? 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 newValues = []; + final Map 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; +} diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 281b8ff9..4ab96e6d 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -4,8 +4,9 @@ export 'file_utils.dart'; export 'har_utils.dart'; export 'header_utils.dart'; export 'history_utils.dart'; -export 'js_utils.dart'; export 'http_utils.dart'; +export 'js_utils.dart'; +export 'pre_post_script_utils.dart'; export 'save_utils.dart'; export 'ui_utils.dart'; export 'window_utils.dart';