makeApiService static method
dynamic
makeApiService(})
Creates a new API service.
Implementation
static makeApiService(String className, String value,
{String folderPath = networkingFolder,
bool forceCreate = false,
bool addToConfig = true}) async {
String name = className.replaceAll(RegExp(r'(_?api_service)'), "");
String filePath = '$folderPath/${name.snakeCase}_api_service.dart';
await _makeDirectory(folderPath);
await _checkIfFileExists(filePath, shouldForceCreate: forceCreate);
await _createNewFile(filePath, value, onSuccess: () {
MetroConsole.writeInGreen(
'[API Service] ${name.snakeCase}_api_service created 🎉');
});
if (addToConfig == false) return;
String classImport = makeImportPathApiService(name.snakeCase);
await MetroService.addToConfig(
configName: "decoders",
classImport: classImport,
createTemplate: (file) {
String apiServiceName = "${name.pascalCase}ApiService";
if (file.contains("final Map<Type, dynamic> apiDecoders =")) {
RegExp reg =
RegExp(r'final Map<Type, dynamic> apiDecoders = \{([^}]*)\};');
String temp = """
final Map<Type, dynamic> apiDecoders = {${reg.allMatches(file).map((e) => e.group(1)).toList()[0]}
$apiServiceName: $apiServiceName(),
};""";
return file.replaceFirst(
RegExp(r'final Map<Type, dynamic> apiDecoders = \{([^}]*)\};'),
temp,
);
}
if (file.contains("final Map<Type, BaseApiService> apiDecoders =")) {
RegExp reg = RegExp(
r'final Map<Type, BaseApiService> apiDecoders = \{([^}]*)\};');
String temp = """
final Map<Type, BaseApiService> apiDecoders = {${reg.allMatches(file).map((e) => e.group(1)).toList()[0]}
$apiServiceName: $apiServiceName(),
};""";
return file.replaceFirst(
RegExp(
r'final Map<Type, BaseApiService> apiDecoders = \{([^}]*)\};'),
temp,
);
}
if (file.contains("final Map<Type, NyApiService> apiDecoders =")) {
RegExp reg = RegExp(
r'final Map<Type, NyApiService> apiDecoders = \{([^}]*)\};');
String temp = """
final Map<Type, NyApiService> apiDecoders = {${reg.allMatches(file).map((e) => e.group(1)).toList()[0]}
$apiServiceName: $apiServiceName(),
};""";
return file.replaceFirst(
RegExp(
r'final Map<Type, NyApiService> apiDecoders = \{([^}]*)\};'),
temp,
);
}
return file;
});
}