handlePage method

Future<void> handlePage({
  1. required String featureName,
  2. required String featurePath,
  3. required String pageName,
  4. required dynamic pageValue,
  5. required String? appsName,
})

Implementation

Future<void> handlePage({
  required String featureName,
  required String featurePath,
  required String pageName,
  required dynamic pageValue,
  required String? appsName,
}) async {
  final pathPage = join(featurePath, 'lib', pageName);
  final pathTestPage = join(featurePath, 'test', '${pageName}_test');
  if (!exists(pathPage)) {
    StatusHelper.warning(
        'Page with name $pageName not found in feature $pathPage!');
    return;
  }
  if (pageValue is! Map) {
    StatusHelper.warning(
        'Value page is not valid, please check format json2dart.yaml');
    return;
  }

  if (!isOnlyUnitTest && isApi) {
    removeAllRelatedApiPage(pathPage, pageName, pageValue, isReplace);
  }
  if ((isOnlyUnitTest || isUnitTest)) {
    removeAllRelatedApiPageUnitTest(featureName, pageName);
  }

  if (!isOnlyUnitTest) createMapper(pathPage, pageValue);

  List<Map<String, String>> resultModelUnitTest = [];

  for (var element in pageValue.entries) {
    final apiName = element.key;
    final apiValue = element.value;

    if (apiValue is! Map) {
      StatusHelper.warning(
          'Value api is not valid, please check format json2dart.yaml');
      return;
    }

    final pathUrl = apiValue['path'];
    final paramPath = <String>[];
    parse(pathUrl ?? '', parameters: paramPath);

    final pathBody = apiValue['body'];
    bool isBodyList = false;
    dynamic body = getMapFromJson(
      pathBody,
      callbackJsonIsList: () => isBodyList = true,
      warningMessage: 'Format json body $apiName not valid!',
    );

    bool isResponseList = false;
    final pathResponse = apiValue['response'];
    dynamic response = getMapFromJson(
      pathResponse,
      callbackJsonIsList: () => isResponseList = true,
      warningMessage: 'Format json response $apiName not valid!',
    );

    bodyDateFormat =
        apiValue['body_format_date_time'] ?? defaultBodyDateFormat;
    responseDateFormat =
        apiValue['response_format_date_time'] ?? defaultResponseDateFormat;

    dynamic cacheStrategy = apiValue['cache_strategy'];
    String? strategy;
    int? ttl;
    bool? keepExpiredCache;
    if (cacheStrategy is String) {
      strategy = cacheStrategy;
    } else if (cacheStrategy is Map) {
      strategy = cacheStrategy['strategy'];
      ttl = cacheStrategy['ttl'];
      keepExpiredCache = cacheStrategy['keep_expired_cache'];
    }

    if (!isOnlyUnitTest) {
      await handleApi(
        featureName: featureName,
        featurePath: featurePath,
        pageName: pageName,
        pathPage: pathPage,
        apiName: apiName,
        body: body,
        response: response,
        method: apiValue['method'],
        pathUrl: pathUrl ?? '',
        paramPath: paramPath,
        header: apiValue['header'],
        isBodyList: isBodyList,
        isResponseList: isResponseList,
        cacheStrategy: strategy,
        ttl: ttl,
        keepExpiredCache: keepExpiredCache,
        appsName: appsName,
      );
    }

    dynamic bodyUnitTest;
    dynamic responseUnitTest;
    try {
      final jsonBody = apiValue['body'] != null
          ? File(apiValue['body']).readAsStringSync()
          : '{}';
      bodyUnitTest = jsonDecode(jsonBody);
      final jsonResponse = apiValue['response'] != null
          ? File(apiValue['response']).readAsStringSync()
          : '{}';

      Map<String, dynamic> jsonObject = json.decode(jsonResponse);
      responseUnitTest = processJson(jsonObject);
    } catch (e) {
      bodyUnitTest = {};
      responseUnitTest = {};
    }

    if ((isOnlyUnitTest || isUnitTest) && body != null && response != null) {
      final result = createModelUnitTest(
        pathTestPage: pathTestPage,
        appsName: appsName ?? '',
        featureName: featureName,
        pageName: pageName,
        pathPage: pathPage,
        apiName: apiName,
        jsonBody: pathBody != null ? File(pathBody).readAsStringSync() : '{}',
        jsonResponse: pathResponse != null
            ? File(pathResponse).readAsStringSync()
            : '{}',
        body: bodyUnitTest,
        response: responseUnitTest,
        method: apiValue['method'],
        paramPath: paramPath,
        pathHeader: apiValue['header'],
        cacheStrategy: strategy,
        ttl: ttl,
        keepExpiredCache: keepExpiredCache,
      );
      resultModelUnitTest.add(result);
    }
  }

  format.add(pathPage);

  if (isOnlyUnitTest || isUnitTest) {
    format.add(pathTestPage);
    handleUnitTest(
      pathTestPage: pathTestPage,
      featureName: featureName,
      pageName: pageName,
      pageValue: pageValue,
      resultModelUnitTest: resultModelUnitTest,
    );
  }
}