@dryv/flutter-api-bridgev0.1.0
A Dart/Flutter package over flutter_api_bridge with typed features, endpoints and models.
dryv pack add @dryv/flutter-api-bridge{#- The generated symbol of another planned file, found by the subject it represents. -#}
{%- macro symbol_for(link) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro dart_type(value) -%}
{%- if value.is.schema -%}{{ symbol_for(value.schema) or 'Object?' }}
{%- elif value.is.property and value.is.enum -%}{{ symbol_for(value.property.enum) }}
{%- elif value.is.property and value.is.primitive and value.property.primitive.is.boolean -%}bool
{%- elif value.is.property and value.is.primitive and value.property.primitive.is.number and value.property.primitive.is.integer -%}int
{%- elif value.is.property and value.is.primitive and value.property.primitive.is.number -%}double
{%- elif value.is.property and value.is.primitive -%}String
{%- elif value.is.property and value.is.temporal and value.property.temporal.kind in ['instant', 'date', 'date-time', 'datetime'] -%}DateTime
{%- elif value.is.property and value.is.temporal -%}String
{%- elif value.is.property and value.is.record -%}Map<String, dynamic>
{%- else -%}Object?
{%- endif -%}
{%- endmacro -%}
{%- macro response_type(operation) -%}
{%- if operation.output -%}{{ dart_type(operation.output.value) }}{%- else -%}void{%- endif -%}
{%- endmacro -%}
{#- "yes" when the operation's first output is a generated model. -#}
{%- macro returns_model(operation) -%}
{%- if operation.output and operation.output.value.is.schema and symbol_for(operation.output.value.schema) -%}yes{%- endif -%}
{%- endmacro -%}
{%- macro request_class(method) -%}
{%- if method == 'GET' -%}GetRequest{%- elif method == 'POST' -%}PostRequest{%- elif method == 'PUT' -%}PutRequest{%- elif method == 'PATCH' -%}PatchRequest{%- else -%}DeleteRequest{%- endif -%}
{%- endmacro -%}
{%- macro to_json(binding) -%}
{%- if binding.port.value.is.schema and symbol_for(binding.port.value.schema) -%}{{ binding.port.name.camel }}.toJson(){%- else -%}{{ binding.port.name.camel }}{%- endif -%}
{%- endmacro -%}
{%- macro has(http, kind) -%}
{%- for binding in http.inputs -%}{%- if binding.kind == kind -%}yes{%- endif -%}{%- endfor -%}
{%- endmacro -%}
import 'package:flutter_api_bridge/flutter_api_bridge.dart';
{%- for dependency in file.dependencies %}{% if dependency.symbol and dependency.symbol != file.symbol %}
import '{{ dependency.path.relative }}';
{%- endif %}{% endfor %}
/// Typed client for the {{ group.name.title }} group. Every method runs through the package's
/// ApiConnection and tags its request with a stable operation ID.
class {{ file.symbol }} {
const {{ file.symbol }}(this._connection);
final ApiConnection _connection;
static const _routes = {{ group.name.pascal }}Endpoints();
{%- for operation in operations %}
{%- set http = operation.facets.http %}
{%- set options_type = 'ApiGetRequestOptions' if http.method == 'GET' else 'ApiRequestOptions' %}
/// {{ operation.description or operation.name.title }}
///
/// {{ http.method }} {{ http.path.local }}
Future<ApiResult<{{ response_type(operation) }}>> {{ operation.name.camel }}({
{%- for binding in http.inputs %}
{%- if binding.is.path %}
required String {{ binding.port.name.camel }},
{%- elif binding.is.body %}
required {{ dart_type(binding.port.value) }} {{ binding.port.name.camel }},
{%- elif binding.is.query or binding.is.query_object %}
{{ dart_type(binding.port.value) }}{% if dart_type(binding.port.value) != 'Object?' %}?{% endif %} {{ binding.port.name.camel }},
{%- endif %}
{%- endfor %}
{{ options_type }}? options,
}) {
return _connection.execute(
{{ request_class(http.method) }}<{{ response_type(operation) }}>(
endpoint: {% if has(http, 'path') %}_routes.{{ operation.name.camel }}({% for binding in http.inputs %}{% if binding.is.path %}{{ binding.name }}: {{ binding.port.name.camel }}, {% endif %}{% endfor %}){% else %}_routes.{{ operation.name.camel }}{% endif %},
options: (options ?? const {{ options_type }}()).copyWith(operationId: '{{ group.name.snake }}.{{ operation.name.camel }}'),
{%- if has(http, 'query-object') %}
{%- for binding in http.inputs %}{% if binding.is.query_object %}
query: {{ binding.port.name.camel }}{% if binding.port.value.is.schema %}?.toJson(){% endif %},
{%- endif %}{% endfor %}
{%- elif has(http, 'query') %}
query: <String, Object?>{
{%- for binding in http.inputs %}{% if binding.is.query %}
if ({{ binding.port.name.camel }} != null) '{{ binding.name }}': {{ binding.port.name.camel }},
{%- endif %}{% endfor %}
},
{%- endif %}
{%- if http.method != 'GET' and has(http, 'body') %}
{%- for binding in http.inputs %}{% if binding.is.body %}
body: {{ to_json(binding) }},
{%- endif %}{% endfor %}
{%- endif %}
{%- if returns_model(operation) %}
fromJson: (json) => {{ response_type(operation) }}.fromJson(Map<String, dynamic>.from((json as Map?) ?? const {})),
{%- endif %}
),
);
}
{%- endfor %}
}