generate method

Future<String> generate (
  1. [Map<String, dynamic> _fields]
)

Output a usable class annotated by @ConnectOfflineFirstWithRest and extending OfflineFirstModel.

The restEndpoint is generated by removing the domain and protocol from endpoint.

Implementation

Future<String> generate([Map<String, dynamic> _fields]) async {
  final fields = _fields ?? await getRestPayload();
  final generatedFields = generateFields(fields);
  final generatedConstructorFields = generateConstructorFields(fields);
  final splitEndpoint = endpoint.split('/');
  final camelizedClass = toCamelCase(splitEndpoint.last);
  final className = camelizedClass[0].toUpperCase() + camelizedClass.substring(1);
  final restEndpoint = splitEndpoint.sublist(3).join('/');
  final fromKey = topLevelKey != null ? "fromKey: '$topLevelKey'," : '';

  final output = '''
    import 'package:brick_offline_first/offline_first.dart';
    import 'package:brick_offline_first_abstract/annotations.dart';

    @ConnectOfflineFirstWithRest(
      restConfig: RestSerializable(
        fieldRename: FieldRename.snake,
        endpoint: "=> '/$restEndpoint';",$fromKey
      ),
    )
    class $className extends OfflineFirstModel {
    $generatedFields

      $className({
    $generatedConstructorFields,
      });
    }
  ''';

  return _formatter.format(output);
}