Browse Source

feat(dynamite): Use URI templates for path parameters

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/1171/head
jld3103 1 year ago
parent
commit
d967ec0bb6
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 27
      packages/dynamite/dynamite/lib/src/builder/client.dart
  2. 1
      packages/dynamite/dynamite/lib/src/builder/imports.dart

27
packages/dynamite/dynamite/lib/src/builder/client.dart

@ -215,6 +215,7 @@ Iterable<Method> buildTags(
.join(','); .join(',');
code.writeln(''' code.writeln('''
final _pathParameters = <String, dynamic>{};
final _queryParameters = <String, dynamic>{}; final _queryParameters = <String, dynamic>{};
final _headers = <String, String>{${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}}; final _headers = <String, String>{${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}};
Uint8List? _body; Uint8List? _body;
@ -323,17 +324,12 @@ Iterable<Method> buildTags(
toDartName(identifierBuilder.toString(), uppercaseFirstCharacter: true), toDartName(identifierBuilder.toString(), uppercaseFirstCharacter: true),
); );
var path = pathEntry.key; code.writeln('''
for (final pathParameter in parameters.where((final p) => p.$in == openapi.ParameterType.path)) { var _uri = Uri.parse(UriTemplate('${pathEntry.key}').expand(_pathParameters));
path = path.replaceAll('{${pathParameter.name}}', '\$_${toDartName(pathParameter.name)}'); if (_queryParameters.isNotEmpty) {
} _uri = _uri.replace(queryParameters: _queryParameters);
}
code.writeln( ''');
'''
final _path = '$path';
final _uri = Uri(path: _path, queryParameters: _queryParameters.isNotEmpty ? _queryParameters : null);
''',
);
if (dataType != null) { if (dataType != null) {
returnDataType = dataType.name; returnDataType = dataType.name;
@ -429,12 +425,13 @@ Iterable<String> buildParameterSerialization(
onlyChildren: parameter.$in == openapi.ParameterType.query, onlyChildren: parameter.$in == openapi.ParameterType.query,
); );
final assignment = switch (parameter.$in) { final mapName = switch (parameter.$in) {
openapi.ParameterType.path => 'final _${toDartName(parameter.name)} = Uri.encodeQueryComponent($value);', openapi.ParameterType.path => '_pathParameters',
openapi.ParameterType.query => "_queryParameters['${parameter.name}'] = $value;", openapi.ParameterType.query => '_queryParameters',
openapi.ParameterType.header => "_headers['${parameter.name}'] = $value;", openapi.ParameterType.header => '_headers',
_ => throw UnsupportedError('Can not work with parameter "${parameter.name}" in "${parameter.$in}"'), _ => throw UnsupportedError('Can not work with parameter "${parameter.name}" in "${parameter.$in}"'),
}; };
final assignment = "$mapName['${parameter.name}'] = $value;";
if (!parameter.required && (result.nullable || hasDefault)) { if (!parameter.required && (result.nullable || hasDefault)) {
yield 'if( '; yield 'if( ';

1
packages/dynamite/dynamite/lib/src/builder/imports.dart

@ -27,6 +27,7 @@ Iterable<Spec> generateImports(final AssetId outputId, final State state) sync*
Directive.import('package:dynamite_runtime/utils.dart'), Directive.import('package:dynamite_runtime/utils.dart'),
Directive.import('package:meta/meta.dart'), Directive.import('package:meta/meta.dart'),
Directive.import('package:universal_io/io.dart'), Directive.import('package:universal_io/io.dart'),
Directive.import('package:uri/uri.dart'),
const Code(''), const Code(''),
]; ];

Loading…
Cancel
Save