generate method

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

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>? passedFields]) async {
  final fields = passedFields ?? 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 ? "topLevelKey: '$topLevelKey'," : '';

  final output = '''
    import 'package:brick_offline_first/brick_offline_first.dart';
    import 'package:brick_offline_first_with_rest/brick_offline_first_with_rest.dart';

    class ${className}RequestTransformer extends RestRequestTransformer {
      final get = const RestRequest(
        url: '/$restEndpoint',
        $fromKey
      );

      const ${className}RequestTransformer(Query? query, RestModel? instance) : super(query, instance);
    }

    @ConnectOfflineFirstWithRest(
      restConfig: RestSerializable(
        fieldRename: FieldRename.snake,
        requestTransformer: ${className}RequestTransformer.new,
      ),
    )
    class $className extends OfflineFirstModel {
    $generatedFields

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

  return _formatter.format(output);
}