From 99c38c4f96b2dbf27b32db50b3ccc0b625107ef9 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Sat, 12 Jul 2025 15:52:10 +0530 Subject: [PATCH] refactor API Key auth --- .../auth/api_key_auth_fields.dart | 40 +++++++++---------- lib/screens/common_widgets/auth/consts.dart | 9 +++++ 2 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 lib/screens/common_widgets/auth/consts.dart diff --git a/lib/screens/common_widgets/auth/api_key_auth_fields.dart b/lib/screens/common_widgets/auth/api_key_auth_fields.dart index 88dd09d9..aca1abaf 100644 --- a/lib/screens/common_widgets/auth/api_key_auth_fields.dart +++ b/lib/screens/common_widgets/auth/api_key_auth_fields.dart @@ -2,6 +2,7 @@ import 'package:apidash/widgets/auth_textfield.dart'; import 'package:apidash_core/apidash_core.dart'; import 'package:apidash_design_system/apidash_design_system.dart'; import 'package:flutter/material.dart'; +import 'consts.dart'; class ApiKeyAuthFields extends StatefulWidget { final AuthModel? authData; @@ -28,8 +29,9 @@ class _ApiKeyAuthFieldsState extends State { super.initState(); final apiAuth = widget.authData?.apikey; _keyController = TextEditingController(text: apiAuth?.key ?? ''); - _nameController = TextEditingController(text: apiAuth?.name ?? 'x-api-key'); - _addKeyTo = apiAuth?.location ?? 'header'; + _nameController = + TextEditingController(text: apiAuth?.name ?? kApiKeyHeaderName); + _addKeyTo = apiAuth?.location ?? kAddToDefaultLocation; } @override @@ -38,7 +40,7 @@ class _ApiKeyAuthFieldsState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - "Add to", + kLabelAddTo, style: TextStyle( fontWeight: FontWeight.normal, fontSize: 14, @@ -48,12 +50,9 @@ class _ApiKeyAuthFieldsState extends State { height: 4, ), ADPopupMenu( - value: _addKeyTo == 'header' ? 'Header' : 'Query Params', - values: const [ - ('header', 'Header'), - ('query', 'Query Params'), - ], - tooltip: "Select where to add API key", + value: kAddToLocationsMap[_addKeyTo], + values: kAddToLocations, + tooltip: kTooltipApiKeyAuth, isOutlined: true, onChanged: widget.readOnly ? null @@ -70,15 +69,15 @@ class _ApiKeyAuthFieldsState extends State { AuthTextField( readOnly: widget.readOnly, controller: _nameController, - hintText: "Header/Query Param Name", + hintText: kHintTextFieldName, onChanged: (value) => _updateApiKeyAuth(), ), const SizedBox(height: 16), AuthTextField( readOnly: widget.readOnly, controller: _keyController, - title: "API Key", - hintText: "Key", + title: kLabelApiKey, + hintText: kHintTextKey, isObscureText: true, onChanged: (value) => _updateApiKeyAuth(), ), @@ -87,21 +86,18 @@ class _ApiKeyAuthFieldsState extends State { } void _updateApiKeyAuth() { + final apiKey = AuthApiKeyModel( + key: _keyController.text.trim(), + name: _nameController.text.trim(), + location: _addKeyTo, + ); widget.updateAuth(widget.authData?.copyWith( type: APIAuthType.apiKey, - apikey: AuthApiKeyModel( - key: _keyController.text.trim(), - name: _nameController.text.trim(), - location: _addKeyTo, - ), + apikey: apiKey, ) ?? AuthModel( type: APIAuthType.apiKey, - apikey: AuthApiKeyModel( - key: _keyController.text.trim(), - name: _nameController.text.trim(), - location: _addKeyTo, - ), + apikey: apiKey, )); } } diff --git a/lib/screens/common_widgets/auth/consts.dart b/lib/screens/common_widgets/auth/consts.dart new file mode 100644 index 00000000..139fb40f --- /dev/null +++ b/lib/screens/common_widgets/auth/consts.dart @@ -0,0 +1,9 @@ +const kApiKeyHeaderName = 'x-api-key'; +const kAddToLocations = [ + ('header', 'Header'), + ('query', 'Query Params'), +]; +final kAddToDefaultLocation = kAddToLocations[0].$1; +final kAddToLocationsMap = {for (var v in kAddToLocations) v.$1: v.$2}; +const kLabelAddTo = "Add to"; +const kTooltipApiKeyAuth = "Select where to add API key";