From 180fd59a1fb48471238b353a5f103cef5e1558e5 Mon Sep 17 00:00:00 2001 From: StormGear Date: Thu, 30 Jan 2025 15:39:45 +0000 Subject: [PATCH] add environment when insomia v4 is imported --- lib/importer/import_dialog.dart | 24 +- lib/importer/importer.dart | 12 + lib/screens/envvar/environments_pane.dart | 1 + lib/utils/envvar_utils.dart | 43 ++ .../lib/import_export/insomnia_io.dart | 18 +- .../lib/models/insomnia_collection.dart | 22 +- .../lib/models/insomnia_environment.dart | 60 +++ .../models/insomnia_environment.freezed.dart | 496 ++++++++++++++++++ .../lib/models/insomnia_environment.g.dart | 47 ++ .../lib/utils/insomnia_utils.dart | 18 +- pubspec.lock | 2 +- pubspec.yaml | 2 + 12 files changed, 737 insertions(+), 8 deletions(-) create mode 100644 packages/insomnia_collection/lib/models/insomnia_environment.dart create mode 100644 packages/insomnia_collection/lib/models/insomnia_environment.freezed.dart create mode 100644 packages/insomnia_collection/lib/models/insomnia_environment.g.dart diff --git a/lib/importer/import_dialog.dart b/lib/importer/import_dialog.dart index a8d54366..51a0ed9e 100644 --- a/lib/importer/import_dialog.dart +++ b/lib/importer/import_dialog.dart @@ -1,3 +1,4 @@ +import 'package:apidash/utils/envvar_utils.dart'; import 'package:apidash_design_system/apidash_design_system.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; @@ -10,9 +11,6 @@ void importToCollectionPane( WidgetRef ref, ScaffoldMessengerState sm, ) { - // TODO: The dialog must have a feature to paste contents in a text field - // Also, a mechanism can be added where on importing a file it shows the - // contents in the text field and then the user presses ok to add it to collection showImportDialog( context: context, importFormat: ref.watch(importFormatStateProvider), @@ -26,6 +24,26 @@ void importToCollectionPane( sm.hideCurrentSnackBar(); file.readAsString().then( (content) { + kEnvImporter + .getInsomniaEnvironment(importFormatType, content) + .then((environment) { + debugPrint('Environment: $environment'); + debugPrint('Environment values: ${environment?.resources}'); + + if (environment != null) { + if (environment.resources == null || + environment.resources!.isEmpty) { + sm.showSnackBar(getSnackBar("No environment variables imported", + small: false)); + } else { + var env = createNewEnvironment(ref, environment); + + sm.showSnackBar(getSnackBar( + "Successfully imported ${env.length} environment variables", + small: false)); + } + } + }); kImporter .getHttpRequestModelList(importFormatType, content) .then((importedRequestModels) { diff --git a/lib/importer/importer.dart b/lib/importer/importer.dart index 4d93d9be..3d649947 100644 --- a/lib/importer/importer.dart +++ b/lib/importer/importer.dart @@ -1,5 +1,6 @@ import 'package:apidash/consts.dart'; import 'package:apidash_core/apidash_core.dart'; +import 'package:insomnia_collection/models/insomnia_environment.dart'; class Importer { Future?> getHttpRequestModelList( @@ -17,4 +18,15 @@ class Importer { } } +class EnvImporter { + Future getInsomniaEnvironment( + ImportFormat fileType, String content) async { + return switch (fileType) { + ImportFormat.insomnia => InsomniaIO().getInsomiaEnvironment(content), + _ => null + }; + } +} + final kImporter = Importer(); +final kEnvImporter = EnvImporter(); diff --git a/lib/screens/envvar/environments_pane.dart b/lib/screens/envvar/environments_pane.dart index 18bd2053..74ddf363 100644 --- a/lib/screens/envvar/environments_pane.dart +++ b/lib/screens/envvar/environments_pane.dart @@ -26,6 +26,7 @@ class EnvironmentsPane extends ConsumerWidget { ref .read(environmentsStateNotifierProvider.notifier) .addEnvironment(); + // createNewEnvironment(ref); }, ), kVSpacer10, diff --git a/lib/utils/envvar_utils.dart b/lib/utils/envvar_utils.dart index 620b8f4a..3a884cfe 100644 --- a/lib/utils/envvar_utils.dart +++ b/lib/utils/envvar_utils.dart @@ -1,6 +1,49 @@ +import 'package:apidash/providers/environment_providers.dart'; import 'package:apidash_core/apidash_core.dart'; import 'package:apidash/consts.dart'; import 'package:apidash/models/models.dart'; +import 'package:flutter/material.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; +import 'package:insomnia_collection/models/insomnia_environment.dart'; + +List createNewEnvironment(WidgetRef ref, InsomniaEnvironment environment) { + // Step 1: Add a new environment + ref.read(environmentsStateNotifierProvider.notifier).addEnvironment(); + + // Step 2: Get the ID of the newly created environment + final newEnvironmentId = + ref.read(selectedEnvironmentIdStateProvider.notifier).state; + + debugPrint('New id is $newEnvironmentId'); + + // Step 3: Update the new environment with a name and variables + if (newEnvironmentId != null) { + if (environment.resources == null || environment.resources!.isEmpty) { + debugPrint('No env variables found'); + return []; + } + List variables = []; + for (var env in environment.resources!) { + variables.add(EnvironmentVariableModel( + key: env.key, + value: env.value, + enabled: env.enabled ?? true, + type: env.type == "secret" + ? EnvironmentVariableType.secret + : EnvironmentVariableType.variable, + )); + } + ref.read(environmentsStateNotifierProvider.notifier).updateEnvironment( + newEnvironmentId, + name: environment.name ?? "Untitled", + values: variables, + ); + return variables; + } else { + debugPrint('No env id found'); + return []; + } +} String getEnvironmentTitle(String? name) { if (name == null || name.trim() == "") { diff --git a/packages/apidash_core/lib/import_export/insomnia_io.dart b/packages/apidash_core/lib/import_export/insomnia_io.dart index 6b5e1e48..3d8e9329 100644 --- a/packages/apidash_core/lib/import_export/insomnia_io.dart +++ b/packages/apidash_core/lib/import_export/insomnia_io.dart @@ -1,3 +1,4 @@ +import 'package:insomnia_collection/models/insomnia_environment.dart'; import 'package:seed/seed.dart'; import '../consts.dart'; import '../models/models.dart'; @@ -10,6 +11,10 @@ class InsomniaIO { try { final ic = ins.insomniaCollectionFromJsonStr(content); final requests = ins.getRequestsFromInsomniaCollection(ic); + + /// TODO; Get env from the insomnia collection + // final environmentVariables = ins.getEnvironmentVariablesFromInsomniaEnvironment(env); + return requests .map((req) => (req.$1, insomniaRequestToHttpRequestModel(req.$2))) .toList(); @@ -18,6 +23,17 @@ class InsomniaIO { } } + InsomniaEnvironment? getInsomiaEnvironment(String content) { + content = content.trim(); + try { + final env = ins.insomniaEnvironmentFromJsonStr(content); + + return env; + } catch (e) { + return null; + } + } + HttpRequestModel insomniaRequestToHttpRequestModel(ins.Resource request) { HTTPVerb method; @@ -89,8 +105,6 @@ class InsomniaIO { } body = request.body?.text; } - - /// TODO: Handle formdata and text } return HttpRequestModel( diff --git a/packages/insomnia_collection/lib/models/insomnia_collection.dart b/packages/insomnia_collection/lib/models/insomnia_collection.dart index adc9e706..12f2c0ff 100644 --- a/packages/insomnia_collection/lib/models/insomnia_collection.dart +++ b/packages/insomnia_collection/lib/models/insomnia_collection.dart @@ -1,9 +1,30 @@ import 'dart:convert'; import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:insomnia_collection/models/insomnia_environment.dart'; part 'insomnia_collection.freezed.dart'; part 'insomnia_collection.g.dart'; +InsomniaEnvironment insomniaEnvironmentFromJsonStr(String str) { + var InsomniaEnvjson = json.decode(str); + print(InsomniaEnvjson.toString()); + InsomniaEnvjson['resources'] = (InsomniaEnvjson['resources'] as List) + .where((resource) => resource['_type'] == 'environment') + .toList(); + print(InsomniaEnvjson['resources'].toString()); + + return InsomniaEnvironment.fromJson(InsomniaEnvjson['resources'][0]); +} + +InsomniaEnvironment insomniaEnvironmentFromJson(Map json) { + // Remove all resources which are not requests + json['resources'] = (json['resources'] as List) + .where((resource) => resource['_type'] == 'environment') + .toList(); + + return InsomniaEnvironment.fromJson(json['resources'][0]); +} + InsomniaCollection insomniaCollectionFromJsonStr(String str) { var Insomniajson = json.decode(str); // Remove all resources which are not requests @@ -14,7 +35,6 @@ InsomniaCollection insomniaCollectionFromJsonStr(String str) { return InsomniaCollection.fromJson(Insomniajson); } - InsomniaCollection insomniaCollectionFromJson(Map json) { // Remove all resources which are not requests json['resources'] = (json['resources'] as List) diff --git a/packages/insomnia_collection/lib/models/insomnia_environment.dart b/packages/insomnia_collection/lib/models/insomnia_environment.dart new file mode 100644 index 00000000..ef32a756 --- /dev/null +++ b/packages/insomnia_collection/lib/models/insomnia_environment.dart @@ -0,0 +1,60 @@ +import 'dart:convert'; +import 'package:freezed_annotation/freezed_annotation.dart'; + +part 'insomnia_environment.freezed.dart'; +part 'insomnia_environment.g.dart'; + +@freezed +class InsomniaEnvironment with _$InsomniaEnvironment { + @JsonSerializable( + explicitToJson: true, + anyMap: true, + includeIfNull: false, + ) + const factory InsomniaEnvironment({ + @JsonKey(name: '_id') String? id, + String? name, + @JsonKey(name: 'kvPairData') List? resources , + @JsonKey(name: '_type') String? type, + }) = _InsomniaEnvironment; + + factory InsomniaEnvironment.fromJson(Map json) => + _$InsomniaEnvironmentFromJson(json); +} + +InsomniaEnvironment insomniaEnvironmentFromJsonStr(String str) { + var insomniaJson = json.decode(str); + return InsomniaEnvironment.fromJson(insomniaJson); +} + +String insomniaEnvironmentToJsonStr(InsomniaEnvironment environment) { + return json.encode(environment.toJson()); +} + +@freezed +class EnvironmentVariable with _$EnvironmentVariable { + @JsonSerializable( + explicitToJson: true, + anyMap: true, + includeIfNull: false, + ) + const factory EnvironmentVariable({ + String? id, + @JsonKey(name: 'name') required String key, + @JsonKey(name: 'value') required String value, + String? type, + @JsonKey(name: 'enabled') bool? enabled, + }) = _EnvironmentVariable; + + factory EnvironmentVariable.fromJson(Map json) => + _$EnvironmentVariableFromJson(json); +} + +EnvironmentVariable environmentVariableFromJsonStr(String str) { + var environmentJson = json.decode(str); + return EnvironmentVariable.fromJson(environmentJson); +} + +String environmentVariableToJsonStr(EnvironmentVariable variable) { + return json.encode(variable.toJson()); +} \ No newline at end of file diff --git a/packages/insomnia_collection/lib/models/insomnia_environment.freezed.dart b/packages/insomnia_collection/lib/models/insomnia_environment.freezed.dart new file mode 100644 index 00000000..ab073b42 --- /dev/null +++ b/packages/insomnia_collection/lib/models/insomnia_environment.freezed.dart @@ -0,0 +1,496 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint +// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark + +part of 'insomnia_environment.dart'; + +// ************************************************************************** +// FreezedGenerator +// ************************************************************************** + +T _$identity(T value) => value; + +final _privateConstructorUsedError = UnsupportedError( + 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models'); + +InsomniaEnvironment _$InsomniaEnvironmentFromJson(Map json) { + return _InsomniaEnvironment.fromJson(json); +} + +/// @nodoc +mixin _$InsomniaEnvironment { + @JsonKey(name: '_id') + String? get id => throw _privateConstructorUsedError; + String? get name => throw _privateConstructorUsedError; + @JsonKey(name: 'kvPairData') + List? get resources => + throw _privateConstructorUsedError; + @JsonKey(name: '_type') + String? get type => throw _privateConstructorUsedError; + + /// Serializes this InsomniaEnvironment to a JSON map. + Map toJson() => throw _privateConstructorUsedError; + + /// Create a copy of InsomniaEnvironment + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + $InsomniaEnvironmentCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $InsomniaEnvironmentCopyWith<$Res> { + factory $InsomniaEnvironmentCopyWith( + InsomniaEnvironment value, $Res Function(InsomniaEnvironment) then) = + _$InsomniaEnvironmentCopyWithImpl<$Res, InsomniaEnvironment>; + @useResult + $Res call( + {@JsonKey(name: '_id') String? id, + String? name, + @JsonKey(name: 'kvPairData') List? resources, + @JsonKey(name: '_type') String? type}); +} + +/// @nodoc +class _$InsomniaEnvironmentCopyWithImpl<$Res, $Val extends InsomniaEnvironment> + implements $InsomniaEnvironmentCopyWith<$Res> { + _$InsomniaEnvironmentCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + /// Create a copy of InsomniaEnvironment + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = freezed, + Object? name = freezed, + Object? resources = freezed, + Object? type = freezed, + }) { + return _then(_value.copyWith( + id: freezed == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as String?, + name: freezed == name + ? _value.name + : name // ignore: cast_nullable_to_non_nullable + as String?, + resources: freezed == resources + ? _value.resources + : resources // ignore: cast_nullable_to_non_nullable + as List?, + type: freezed == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as String?, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$InsomniaEnvironmentImplCopyWith<$Res> + implements $InsomniaEnvironmentCopyWith<$Res> { + factory _$$InsomniaEnvironmentImplCopyWith(_$InsomniaEnvironmentImpl value, + $Res Function(_$InsomniaEnvironmentImpl) then) = + __$$InsomniaEnvironmentImplCopyWithImpl<$Res>; + @override + @useResult + $Res call( + {@JsonKey(name: '_id') String? id, + String? name, + @JsonKey(name: 'kvPairData') List? resources, + @JsonKey(name: '_type') String? type}); +} + +/// @nodoc +class __$$InsomniaEnvironmentImplCopyWithImpl<$Res> + extends _$InsomniaEnvironmentCopyWithImpl<$Res, _$InsomniaEnvironmentImpl> + implements _$$InsomniaEnvironmentImplCopyWith<$Res> { + __$$InsomniaEnvironmentImplCopyWithImpl(_$InsomniaEnvironmentImpl _value, + $Res Function(_$InsomniaEnvironmentImpl) _then) + : super(_value, _then); + + /// Create a copy of InsomniaEnvironment + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = freezed, + Object? name = freezed, + Object? resources = freezed, + Object? type = freezed, + }) { + return _then(_$InsomniaEnvironmentImpl( + id: freezed == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as String?, + name: freezed == name + ? _value.name + : name // ignore: cast_nullable_to_non_nullable + as String?, + resources: freezed == resources + ? _value._resources + : resources // ignore: cast_nullable_to_non_nullable + as List?, + type: freezed == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as String?, + )); + } +} + +/// @nodoc + +@JsonSerializable(explicitToJson: true, anyMap: true, includeIfNull: false) +class _$InsomniaEnvironmentImpl implements _InsomniaEnvironment { + const _$InsomniaEnvironmentImpl( + {@JsonKey(name: '_id') this.id, + this.name, + @JsonKey(name: 'kvPairData') final List? resources, + @JsonKey(name: '_type') this.type}) + : _resources = resources; + + factory _$InsomniaEnvironmentImpl.fromJson(Map json) => + _$$InsomniaEnvironmentImplFromJson(json); + + @override + @JsonKey(name: '_id') + final String? id; + @override + final String? name; + final List? _resources; + @override + @JsonKey(name: 'kvPairData') + List? get resources { + final value = _resources; + if (value == null) return null; + if (_resources is EqualUnmodifiableListView) return _resources; + // ignore: implicit_dynamic_type + return EqualUnmodifiableListView(value); + } + + @override + @JsonKey(name: '_type') + final String? type; + + @override + String toString() { + return 'InsomniaEnvironment(id: $id, name: $name, resources: $resources, type: $type)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$InsomniaEnvironmentImpl && + (identical(other.id, id) || other.id == id) && + (identical(other.name, name) || other.name == name) && + const DeepCollectionEquality() + .equals(other._resources, _resources) && + (identical(other.type, type) || other.type == type)); + } + + @JsonKey(includeFromJson: false, includeToJson: false) + @override + int get hashCode => Object.hash(runtimeType, id, name, + const DeepCollectionEquality().hash(_resources), type); + + /// Create a copy of InsomniaEnvironment + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + @override + @pragma('vm:prefer-inline') + _$$InsomniaEnvironmentImplCopyWith<_$InsomniaEnvironmentImpl> get copyWith => + __$$InsomniaEnvironmentImplCopyWithImpl<_$InsomniaEnvironmentImpl>( + this, _$identity); + + @override + Map toJson() { + return _$$InsomniaEnvironmentImplToJson( + this, + ); + } +} + +abstract class _InsomniaEnvironment implements InsomniaEnvironment { + const factory _InsomniaEnvironment( + {@JsonKey(name: '_id') final String? id, + final String? name, + @JsonKey(name: 'kvPairData') final List? resources, + @JsonKey(name: '_type') final String? type}) = _$InsomniaEnvironmentImpl; + + factory _InsomniaEnvironment.fromJson(Map json) = + _$InsomniaEnvironmentImpl.fromJson; + + @override + @JsonKey(name: '_id') + String? get id; + @override + String? get name; + @override + @JsonKey(name: 'kvPairData') + List? get resources; + @override + @JsonKey(name: '_type') + String? get type; + + /// Create a copy of InsomniaEnvironment + /// with the given fields replaced by the non-null parameter values. + @override + @JsonKey(includeFromJson: false, includeToJson: false) + _$$InsomniaEnvironmentImplCopyWith<_$InsomniaEnvironmentImpl> get copyWith => + throw _privateConstructorUsedError; +} + +EnvironmentVariable _$EnvironmentVariableFromJson(Map json) { + return _EnvironmentVariable.fromJson(json); +} + +/// @nodoc +mixin _$EnvironmentVariable { + String? get id => throw _privateConstructorUsedError; + @JsonKey(name: 'name') + String get key => throw _privateConstructorUsedError; + @JsonKey(name: 'value') + String get value => throw _privateConstructorUsedError; + String? get type => throw _privateConstructorUsedError; + @JsonKey(name: 'enabled') + bool? get enabled => throw _privateConstructorUsedError; + + /// Serializes this EnvironmentVariable to a JSON map. + Map toJson() => throw _privateConstructorUsedError; + + /// Create a copy of EnvironmentVariable + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + $EnvironmentVariableCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $EnvironmentVariableCopyWith<$Res> { + factory $EnvironmentVariableCopyWith( + EnvironmentVariable value, $Res Function(EnvironmentVariable) then) = + _$EnvironmentVariableCopyWithImpl<$Res, EnvironmentVariable>; + @useResult + $Res call( + {String? id, + @JsonKey(name: 'name') String key, + @JsonKey(name: 'value') String value, + String? type, + @JsonKey(name: 'enabled') bool? enabled}); +} + +/// @nodoc +class _$EnvironmentVariableCopyWithImpl<$Res, $Val extends EnvironmentVariable> + implements $EnvironmentVariableCopyWith<$Res> { + _$EnvironmentVariableCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + /// Create a copy of EnvironmentVariable + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = freezed, + Object? key = null, + Object? value = null, + Object? type = freezed, + Object? enabled = freezed, + }) { + return _then(_value.copyWith( + id: freezed == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as String?, + key: null == key + ? _value.key + : key // ignore: cast_nullable_to_non_nullable + as String, + value: null == value + ? _value.value + : value // ignore: cast_nullable_to_non_nullable + as String, + type: freezed == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as String?, + enabled: freezed == enabled + ? _value.enabled + : enabled // ignore: cast_nullable_to_non_nullable + as bool?, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$EnvironmentVariableImplCopyWith<$Res> + implements $EnvironmentVariableCopyWith<$Res> { + factory _$$EnvironmentVariableImplCopyWith(_$EnvironmentVariableImpl value, + $Res Function(_$EnvironmentVariableImpl) then) = + __$$EnvironmentVariableImplCopyWithImpl<$Res>; + @override + @useResult + $Res call( + {String? id, + @JsonKey(name: 'name') String key, + @JsonKey(name: 'value') String value, + String? type, + @JsonKey(name: 'enabled') bool? enabled}); +} + +/// @nodoc +class __$$EnvironmentVariableImplCopyWithImpl<$Res> + extends _$EnvironmentVariableCopyWithImpl<$Res, _$EnvironmentVariableImpl> + implements _$$EnvironmentVariableImplCopyWith<$Res> { + __$$EnvironmentVariableImplCopyWithImpl(_$EnvironmentVariableImpl _value, + $Res Function(_$EnvironmentVariableImpl) _then) + : super(_value, _then); + + /// Create a copy of EnvironmentVariable + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = freezed, + Object? key = null, + Object? value = null, + Object? type = freezed, + Object? enabled = freezed, + }) { + return _then(_$EnvironmentVariableImpl( + id: freezed == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as String?, + key: null == key + ? _value.key + : key // ignore: cast_nullable_to_non_nullable + as String, + value: null == value + ? _value.value + : value // ignore: cast_nullable_to_non_nullable + as String, + type: freezed == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as String?, + enabled: freezed == enabled + ? _value.enabled + : enabled // ignore: cast_nullable_to_non_nullable + as bool?, + )); + } +} + +/// @nodoc + +@JsonSerializable(explicitToJson: true, anyMap: true, includeIfNull: false) +class _$EnvironmentVariableImpl implements _EnvironmentVariable { + const _$EnvironmentVariableImpl( + {this.id, + @JsonKey(name: 'name') required this.key, + @JsonKey(name: 'value') required this.value, + this.type, + @JsonKey(name: 'enabled') this.enabled}); + + factory _$EnvironmentVariableImpl.fromJson(Map json) => + _$$EnvironmentVariableImplFromJson(json); + + @override + final String? id; + @override + @JsonKey(name: 'name') + final String key; + @override + @JsonKey(name: 'value') + final String value; + @override + final String? type; + @override + @JsonKey(name: 'enabled') + final bool? enabled; + + @override + String toString() { + return 'EnvironmentVariable(id: $id, key: $key, value: $value, type: $type, enabled: $enabled)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$EnvironmentVariableImpl && + (identical(other.id, id) || other.id == id) && + (identical(other.key, key) || other.key == key) && + (identical(other.value, value) || other.value == value) && + (identical(other.type, type) || other.type == type) && + (identical(other.enabled, enabled) || other.enabled == enabled)); + } + + @JsonKey(includeFromJson: false, includeToJson: false) + @override + int get hashCode => Object.hash(runtimeType, id, key, value, type, enabled); + + /// Create a copy of EnvironmentVariable + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + @override + @pragma('vm:prefer-inline') + _$$EnvironmentVariableImplCopyWith<_$EnvironmentVariableImpl> get copyWith => + __$$EnvironmentVariableImplCopyWithImpl<_$EnvironmentVariableImpl>( + this, _$identity); + + @override + Map toJson() { + return _$$EnvironmentVariableImplToJson( + this, + ); + } +} + +abstract class _EnvironmentVariable implements EnvironmentVariable { + const factory _EnvironmentVariable( + {final String? id, + @JsonKey(name: 'name') required final String key, + @JsonKey(name: 'value') required final String value, + final String? type, + @JsonKey(name: 'enabled') final bool? enabled}) = + _$EnvironmentVariableImpl; + + factory _EnvironmentVariable.fromJson(Map json) = + _$EnvironmentVariableImpl.fromJson; + + @override + String? get id; + @override + @JsonKey(name: 'name') + String get key; + @override + @JsonKey(name: 'value') + String get value; + @override + String? get type; + @override + @JsonKey(name: 'enabled') + bool? get enabled; + + /// Create a copy of EnvironmentVariable + /// with the given fields replaced by the non-null parameter values. + @override + @JsonKey(includeFromJson: false, includeToJson: false) + _$$EnvironmentVariableImplCopyWith<_$EnvironmentVariableImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/packages/insomnia_collection/lib/models/insomnia_environment.g.dart b/packages/insomnia_collection/lib/models/insomnia_environment.g.dart new file mode 100644 index 00000000..95a1c316 --- /dev/null +++ b/packages/insomnia_collection/lib/models/insomnia_environment.g.dart @@ -0,0 +1,47 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'insomnia_environment.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$InsomniaEnvironmentImpl _$$InsomniaEnvironmentImplFromJson(Map json) => + _$InsomniaEnvironmentImpl( + id: json['_id'] as String?, + name: json['name'] as String?, + resources: (json['kvPairData'] as List?) + ?.map((e) => + EnvironmentVariable.fromJson(Map.from(e as Map))) + .toList(), + type: json['_type'] as String?, + ); + +Map _$$InsomniaEnvironmentImplToJson( + _$InsomniaEnvironmentImpl instance) => + { + if (instance.id case final value?) '_id': value, + if (instance.name case final value?) 'name': value, + if (instance.resources?.map((e) => e.toJson()).toList() case final value?) + 'kvPairData': value, + if (instance.type case final value?) '_type': value, + }; + +_$EnvironmentVariableImpl _$$EnvironmentVariableImplFromJson(Map json) => + _$EnvironmentVariableImpl( + id: json['id'] as String?, + key: json['name'] as String, + value: json['value'] as String, + type: json['type'] as String?, + enabled: json['enabled'] as bool?, + ); + +Map _$$EnvironmentVariableImplToJson( + _$EnvironmentVariableImpl instance) => + { + if (instance.id case final value?) 'id': value, + 'name': instance.key, + 'value': instance.value, + if (instance.type case final value?) 'type': value, + if (instance.enabled case final value?) 'enabled': value, + }; diff --git a/packages/insomnia_collection/lib/utils/insomnia_utils.dart b/packages/insomnia_collection/lib/utils/insomnia_utils.dart index ca3d9810..c0ab145e 100644 --- a/packages/insomnia_collection/lib/utils/insomnia_utils.dart +++ b/packages/insomnia_collection/lib/utils/insomnia_utils.dart @@ -2,6 +2,7 @@ import 'package:insomnia_collection/models/insomnia_collection.dart'; +import 'package:insomnia_collection/models/insomnia_environment.dart'; List<(String?, Resource)> getRequestsFromInsomniaCollection( InsomniaCollection? ic) { @@ -17,6 +18,20 @@ List<(String?, Resource)> getRequestsFromInsomniaCollection( return requests; } +List getEnvironmentVariablesFromInsomniaEnvironment( + InsomniaEnvironment? ev) { + if (ev == null || ev.resources == null) { + return []; + } + List envVariables = []; + if (ev.resources!.length > 0) { + for (var envvar in ev.resources!) { + envVariables.add(envvar); + } + } + return envVariables; +} + List<(String?, Resource)> getRequestsFromInsomniaResource(Resource? resource) { if (resource == null) { return []; @@ -28,4 +43,5 @@ List<(String?, Resource)> getRequestsFromInsomniaResource(Resource? resource) { print('Resource type is not request'); } return requests; -} \ No newline at end of file +} + diff --git a/pubspec.lock b/pubspec.lock index fdf2db3c..0826b5d5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -773,7 +773,7 @@ packages: source: hosted version: "4.3.0" insomnia_collection: - dependency: transitive + dependency: "direct main" description: path: "packages/insomnia_collection" relative: true diff --git a/pubspec.yaml b/pubspec.yaml index e2d00bad..89733f2c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,6 +12,8 @@ dependencies: sdk: flutter apidash_core: path: packages/apidash_core + insomnia_collection: + path: packages/insomnia_collection apidash_design_system: path: packages/apidash_design_system code_builder: ^4.10.0