Skip to content

Commit c42e100

Browse files
committed
refactor(dynamite): externalize url path generation
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
1 parent 61fa838 commit c42e100

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed

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

+48-42
Original file line numberDiff line numberDiff line change
@@ -358,48 +358,7 @@ Iterable<Method> buildTags(
358358
toDartName(identifierBuilder.toString(), className: true),
359359
);
360360

361-
if (!hasUriParameters) {
362-
code.writeln("const _path = '${pathEntry.key}';");
363-
} else {
364-
final queryParams = <String>[];
365-
for (final parameter in parameters) {
366-
if (parameter.$in != openapi.ParameterType.query) {
367-
continue;
368-
}
369-
370-
// Default to a plain parameter without exploding.
371-
queryParams.add(parameter.uriTemplate(withPrefix: false) ?? parameter.pctEncodedName);
372-
}
373-
374-
final pathBuilder = StringBuffer()..write(pathEntry.key);
375-
376-
if (queryParams.isNotEmpty) {
377-
pathBuilder
378-
..write('{?')
379-
..writeAll(queryParams, ',')
380-
..write('}');
381-
}
382-
383-
final path = pathBuilder.toString();
384-
// Sanity check the uri at build time.
385-
try {
386-
UriTemplate(path);
387-
} on ParseException catch (e) {
388-
throw Exception('The resulting uri $path is not a valid uri template according to RFC 6570. $e');
389-
}
390-
391-
final pathDeclaration = declareFinal('_path')
392-
.assign(
393-
refer('UriTemplate', 'package:uri/uri.dart')
394-
.newInstance([literalString(path)])
395-
.property('expand')
396-
.call([refer('_parameters')]),
397-
)
398-
.statement
399-
.accept(state.emitter);
400-
401-
code.writeln(pathDeclaration);
402-
}
361+
buildUrlPath(pathEntry.key, parameters, code, state.emitter.allocator.allocate);
403362

404363
if (dataType != null) {
405364
returnDataType = dataType.name;
@@ -483,6 +442,53 @@ return rawResponse.future;
483442
}
484443
}
485444

445+
void buildUrlPath(
446+
String path,
447+
List<openapi.Parameter> parameters,
448+
StringSink code,
449+
String Function(Reference) allocate,
450+
) {
451+
final hasUriParameters = parameters.firstWhereOrNull(
452+
(parameter) => parameter.$in == openapi.ParameterType.path || parameter.$in == openapi.ParameterType.query,
453+
) !=
454+
null;
455+
456+
if (!hasUriParameters) {
457+
code.writeln("const _path = '$path';");
458+
} else {
459+
final queryParams = <String>[];
460+
for (final parameter in parameters) {
461+
if (parameter.$in != openapi.ParameterType.query) {
462+
continue;
463+
}
464+
465+
// Default to a plain parameter without exploding.
466+
queryParams.add(parameter.uriTemplate(withPrefix: false) ?? parameter.pctEncodedName);
467+
}
468+
469+
final pathBuilder = StringBuffer()..write(path);
470+
471+
if (queryParams.isNotEmpty) {
472+
pathBuilder
473+
..write('{?')
474+
..writeAll(queryParams, ',')
475+
..write('}');
476+
}
477+
478+
final pattern = pathBuilder.toString();
479+
// Sanity check the uri at build time.
480+
try {
481+
UriTemplate(pattern);
482+
} on ParseException catch (e) {
483+
throw Exception('The resulting uri $pattern is not a valid uri template according to RFC 6570. $e');
484+
}
485+
486+
code.writeln(
487+
"final _path = ${allocate(refer('UriTemplate', 'package:uri/uri.dart'))}('$pattern').expand(_parameters);",
488+
);
489+
}
490+
}
491+
486492
String buildParameterSerialization(
487493
TypeResult result,
488494
openapi.Parameter parameter,

0 commit comments

Comments
 (0)