createDataDataSource method

void createDataDataSource(
  1. String pathPage,
  2. String appsName,
  3. String pageName,
  4. String apiName,
  5. String method,
  6. String? pathUrl,
  7. String? pathHeader,
  8. bool bodyList,
  9. bool responseList,
  10. CacheStrategy? cacheStrategy,
  11. int? ttl,
  12. bool? keepExpiredCache,
)

Implementation

void createDataDataSource(
  String pathPage,
  String appsName,
  String pageName,
  String apiName,
  String method,
  String? pathUrl,
  String? pathHeader,
  bool bodyList,
  bool responseList,
  CacheStrategy? cacheStrategy,
  int? ttl,
  bool? keepExpiredCache,
) {
  final paramPath = <String>[];
  parse(pathUrl ?? '', parameters: paramPath);

  final pageClassName = pageName.pascalCase;
  final apiClassName = apiName.pascalCase;
  final apiMethodName = apiName.camelCase;

  final path = join(pathPage, 'data', 'datasources');
  DirectoryHelper.createDir(path);

  String? headers;
  if (pathHeader != null && exists(pathHeader)) {
    try {
      headers = File(pathHeader).readAsStringSync();
    } catch (e) {
      StatusHelper.warning(e.toString());
    }
  }

  if (headers != null) {
    headers =
        'headers: $headers.map((key, value) => MapEntry(key, value.toString()))..addAll(headers ?? {}),';
  }

  final bodyClass = getBodyClass(apiClassName, bodyList);
  final bodyImpl = bodyList
      ? 'body: jsonEncode(body.map((e) => e.toMap()).toList()),'
      : 'body: body.toMap()${isMultipart(method) ? '.map((key, value) => MapEntry(key, value.toString()))' : ''},${isMultipart(method) ? ' files: body.files,' : ''}';

  final responseClass = getResponseClass(apiClassName, responseList);
  final responseImpl = responseList
      ? '''final mapResponse = jsonDecode(response.body);
  return mapResponse is List
      ? List.from(mapResponse.map((e) => ${apiClassName}Response.fromMap(e)))
      : [${apiClassName}Response.fromMap(mapResponse)];'''
      : 'return ${apiClassName}Response.fromJson(response.body);';

  final convert =
      bodyList || responseList ? "import 'dart:convert';\n\n" : '';

  final apiMethod = isMultipart(method)
      ? method == 'multipart'
          ? 'postMultipart'
          : method
      : method;
  final apiEndpoint = paramPath.isEmpty
      ? '${projectName.pascalCase}Endpoints.$apiMethodName${appsName.pascalCase}'
      : '${projectName.pascalCase}Endpoints.$apiMethodName${appsName.pascalCase}(${paramPath.map((e) => 'body.${e.camelCase}').join(',')})';
  final apiCacheStrategy = cacheStrategy == null || isMultipart(method)
      ? ''
      : '${cacheStrategy.toParamCacheStrategy(ttl: ttl, keepExpiredCache: keepExpiredCache)},';

  if (!exists(join(path, '${pageName}_remote_data_source.dart'))) {
    join(path, '${pageName}_remote_data_source.dart')
        .write('''${convert}import 'package:core/core.dart';

import '../models/body/${apiName}_body.dart';
import '../models/response/${apiName}_response.dart';

abstract class ${pageName.pascalCase}RemoteDataSource {
Future<$responseClass> $apiMethodName($bodyClass body,{Map<String, String>? headers,});
}

class ${pageName.pascalCase}RemoteDataSourceImpl implements ${pageName.pascalCase}RemoteDataSource {
${pageName.pascalCase}RemoteDataSourceImpl({required this.http});

final MorphemeHttp http;

@override
Future<$responseClass> $apiMethodName($bodyClass body,{Map<String, String>? headers,}) async {
  final response = await http.$apiMethod($apiEndpoint, $bodyImpl${headers ?? 'headers: headers,'}$apiCacheStrategy);
  $responseImpl
}
}''');
  } else {
    String data = File(join(path, '${pageName}_remote_data_source.dart'))
        .readAsStringSync();

    data = data.replaceAll(
        RegExp(r"import\s?'package:core\/core.dart';\n?\n?", multiLine: true),
        '''import 'package:core/core.dart';

import '../models/body/${apiName}_body.dart';
import '../models/response/${apiName}_response.dart';''');

    data = data.replaceAll(
        RegExp('abstract\\s?class\\s?${pageClassName}RemoteDataSource\\s?{',
            multiLine: true),
        '''abstract class ${pageClassName}RemoteDataSource {
Future<$responseClass> $apiMethodName($bodyClass body,{Map<String, String>? headers,});''');

    final isEmpty =
        RegExp(r'final MorphemeHttp http;(\s+)?}(\s+)?}').hasMatch(data);

    data = data.replaceAll(RegExp(r'}(\s+)?}'), '''${isEmpty ? '' : '}'}

@override
Future<$responseClass> $apiMethodName($bodyClass body,{Map<String, String>? headers,}) async {
  final response = await http.$apiMethod($apiEndpoint, $bodyImpl${headers ?? 'headers: headers,'}$apiCacheStrategy);
  $responseImpl
}
}''');

    join(path, '${pageName}_remote_data_source.dart').write(data);
  }
  StatusHelper.generated(join(path, '${pageName}_remote_data_source.dart'));
}