dryvPacks

@dryv/dart-client-sdkv0.1.0

A typed Dart client SDK for the application's HTTP API.

Dart Client SDKClientsUnitPublished 7 days ago2 downloads

dryv pack add @dryv/dart-client-sdk
2.4 KBRaw ↗
jinja
{%- macro dart_type(value) -%}
{%- if value.is.schema -%}{{ value.schema.name.pascal }}
{%- elif value.is.property and value.is.enum -%}{{ value.property.enum.name.pascal }}
{%- 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
{%- else -%}Object?
{%- endif -%}
{%- endmacro -%}
import 'package:dryv_client/src/transport.dart';
{%- for dependency in file.dependencies %}{% if dependency.symbol %}
import '{{ dependency.path.relative }}';
{%- endif %}{% endfor %}

/// HTTP client for {{ feature.name.title }}.
class {{ file.symbol }} {
  const {{ file.symbol }}(this._transport);

  final DryvTransport _transport;
{%- for operation in operations %}
{%- set http = operation.facets.http %}

  /// {{ operation.description or operation.name.title }}{% if operation.role %} ({{ operation.role }}){% endif %}.
  Future<{% if operation.output %}{{ dart_type(operation.output.value) }}{% else %}void{% endif %}> {{ operation.name.camel }}({% if http.inputs %}{
{%- for binding in http.inputs %}
    required {{ dart_type(binding.port.value) }} {{ binding.port.name.camel }},
{%- endfor %}
  }{% endif %}) async {
    final path = '{{ http.path.local }}'
{%- for binding in http.inputs %}{% if binding.is.path %}
        .replaceAll('{{ '{' }}{{ binding.name }}{{ '}' }}', Uri.encodeComponent({{ binding.port.name.camel }}.toString()))
{%- endif %}{% endfor %};
    final response = await _transport.send(
      '{{ http.method }}',
      path,
{%- for binding in http.inputs %}{% if binding.is.body %}
      body: {{ binding.port.name.camel }}{% if binding.port.value.is.schema %}.toJson(){% endif %},
{%- endif %}{% endfor %}
      query: <String, String>{
{%- for binding in http.inputs %}{% if binding.is.query %}
        '{{ binding.name }}': {{ binding.port.name.camel }}.toString(),
{%- endif %}{% endfor %}
      },
    );
{%- if operation.output %}
{%- if operation.output.value.is.schema %}
    return {{ operation.output.value.schema.name.pascal }}.fromJson(response! as Map<String, Object?>);
{%- else %}
    return response;
{%- endif %}
{%- else %}
    return;
{%- endif %}
  }
{%- endfor %}
}