diff --git a/packages/go_router_builder/lib/src/route_config.dart b/packages/go_router_builder/lib/src/route_config.dart index 0cfa7a3928cb..1676926d6fec 100644 --- a/packages/go_router_builder/lib/src/route_config.dart +++ b/packages/go_router_builder/lib/src/route_config.dart @@ -321,7 +321,7 @@ class GoRouteConfig extends RouteBaseConfig { if (conditions.isNotEmpty) { line = 'if (${conditions.join(' && ')}) '; } - line += '${escapeDartString(parameterName.kebab)}: ' + line += '${formatQueryParams(parameterName)}: ' '${_encodeFor(parameterName)},'; buffer.writeln(line); @@ -361,6 +361,8 @@ class GoRouteConfig extends RouteBaseConfig { Iterable classDeclarations() => [ _extensionDefinition, ..._enumDeclarations(), + if (_ctorQueryParams.isNotEmpty) _queryParamsDefinition, + if (_ctorParams.isNotEmpty) _pathParamsDefinition, ]; String get _extensionDefinition => ''' @@ -383,6 +385,56 @@ extension $_extensionName on $_className { } '''; + /// Returns a [String] representing a class named [_className]QueryParam containing every query param as const + String get _queryParamsDefinition => ''' +class ${_className}QueryParam { + ${_createQueryParamsConst()} +} +'''; + + /// Returns a [String] representing every query param of route as const named with param name + /// and with param generated name as value + /// + /// ex: "static const String shopName = 'shop-name';\nstatic const String shopId = 'shop-id';" + String _createQueryParamsConst() { + String content = ''; + for (final ParameterElement param in _ctorQueryParams) { + content += + 'static const String ${param.name} = ${formatQueryParams(param.name)};\n'; + } + return content; + } + + /// Returns a [String] representing a class named [_className]PathParam containing every path param as const + String get _pathParamsDefinition => ''' +class ${_className}PathParam { + ${_createPathParamsConst()} +} +'''; + + /// Returns a [String] representing every path param of route as const named with param name + /// and with param generated name as value + /// + /// ex: "static const String shopName = 'shopName';\nstatic const String shopId = 'shopId';" + String _createPathParamsConst() { + String content = ''; + for (final ParameterElement param in _ctorParams) { + content += + 'static const String ${param.name} = ${formatPathParams(param.name)};\n'; + } + return content; + } + + /// Returns query param [paramName] formatted in kebab case + static String formatQueryParams(String paramName) { + return escapeDartString(paramName.kebab); + } + + /// Returns path param [paramName] formatted + static String formatPathParams(String paramName) { + return escapeDartString(paramName); + } + /// Returns code representing the constant maps that contain the `enum` to /// [String] mapping for each referenced enum. Iterable _enumDeclarations() { diff --git a/packages/go_router_builder/lib/src/type_helpers.dart b/packages/go_router_builder/lib/src/type_helpers.dart index 94ddcd0a6e7c..8df41d26f4cb 100644 --- a/packages/go_router_builder/lib/src/type_helpers.dart +++ b/packages/go_router_builder/lib/src/type_helpers.dart @@ -7,6 +7,8 @@ import 'package:analyzer/dart/element/type.dart'; import 'package:source_gen/source_gen.dart'; import 'package:source_helper/source_helper.dart'; +import 'route_config.dart'; + /// The name of the generated, private helper for converting [String] to [bool]. const String boolConverterHelperName = r'_$boolConverter'; @@ -97,9 +99,10 @@ String _stateValueAccess(ParameterElement element, Set pathParameters) { late String access; if (pathParameters.contains(element.name)) { - access = 'pathParameters[${escapeDartString(element.name)}]'; + access = 'pathParameters[${GoRouteConfig.formatPathParams(element.name)}]'; } else { - access = 'uri.queryParameters[${escapeDartString(element.name.kebab)}]'; + access = + 'uri.queryParameters[${GoRouteConfig.formatQueryParams(element.name)}]'; } if (pathParameters.contains(element.name) || (!element.type.isNullableType && !element.hasDefaultValue)) { @@ -283,11 +286,11 @@ class _TypeHelperIterable extends _TypeHelper { return ''' state.uri.queryParametersAll[ - ${escapeDartString(parameterElement.name.kebab)}] + ${GoRouteConfig.formatQueryParams(parameterElement.name)}] ?.map($entriesTypeDecoder)$iterableCaster'''; } return ''' -state.uri.queryParametersAll[${escapeDartString(parameterElement.name.kebab)}]'''; +state.uri.queryParametersAll[${GoRouteConfig.formatQueryParams(parameterElement.name)}]'''; } @override @@ -331,7 +334,7 @@ abstract class _TypeHelperWithHelper extends _TypeHelper { if (!pathParameters.contains(parameterName) && (paramType.isNullableType || parameterElement.hasDefaultValue)) { return '$convertMapValueHelperName(' - '${escapeDartString(parameterName.kebab)}, ' + '${GoRouteConfig.formatQueryParams(parameterName)}, ' 'state.uri.queryParameters, ' '${helperName(paramType)})'; }