darto_module 0.0.1
darto_module: ^0.0.1 copied to clipboard
Darto Module is an extension for darto that adds support for modular architecture, dependency injection, and automatic route/service organization in a more scalable way
Darto Module
About | Features | Requirements | Getting Started | Project Structure
π― About #
Darto Module is an extension for darto that adds support for modular architecture, dependency injection, and automatic route/service organization in a more scalable way.
It does not replace darto, but works as a complement, providing an abstraction layer for building more structured APIs with independent and reusable modules.
β¨ Features #
- πΉ Modular structure to organize your project into independent modules
- πΉ Simple and flexible dependency injection
- πΉ Automatic route and middleware registration per module
- πΉ Improved scalability for medium and large projects
- πΉ Fully compatible with darto
β Requirements #
Add to your pubspec.yaml:
dependencies:
darto: any
darto_module: any
π Getting Started #
- Create a HealthModule
class HealthModule extends DartoModule {
@override
void onBinds(Injector i) {
i.registerFactory(() => HealthController());
}
@override
void onConfigureRoutes(Router router, Injector i) {
final HealthController controller = i.get();
router.get('/', controller.healthCheck);
}
}
- Create server:
void main() async {
final app = Darto();
app.use('/health-check', HealthModule().init);
app.timeout(30000);
app.listen(3000, () {
print('Server is running on port 3000');
});
}
Now you can access:
http://localhost:3000/health-check
π Project Structure (suggested) #
bin/
main.dart
lib/
modules/
health/
health_module.dart
health_controller.dart
β Extra #
We also provide access to the Result class, allowing you to configure a dynamic and easy-to-use result.
Example usage
return Result.success(data);
or
Future<Result<CompanyModel>> call(String id) async {
try {
final result = await _companyRepository.getById(id);
if (result == null) {
return Result.failure('Company not found');
}
final resultModel = CompanyModel.fromMap(result);
return Result.success(resultModel);
} catch (error) {
return Result.failure('Failed to get company', error);
}
}
ResultHanlder #
In the ResultHandler class, all the most commonly used response types are centralized, making the response method easier to use and map.
Below are some usage examples:
Error example
return ResultHandler.error.forbidden(res, errorMessage);
Success example:
return ResultHandler.success.ok(res, {'status': 'success'});
Or by implementing a handler that checks the type of exception and returns the mapped response:
ResultHandler.errorHanlder(res, exception);
The pre-mapped exceptions are listed below:
class InternalServerErrorApi extends ErrorApi {
InternalServerErrorApi(super.message);
}
class LengthRequiredApi extends ErrorApi {
LengthRequiredApi(super.message);
}
class ExceptionApi extends ErrorApi {
ExceptionApi(super.message);
}
class UpdateRecognitionApi extends ErrorApi {
UpdateRecognitionApi(super.message);
}
class NotFoundApi extends ErrorApi {
NotFoundApi(super.message);
}
class UnAuthorizedApi extends ErrorApi {
UnAuthorizedApi(super.message);
}
class AlreadyExistsApi extends ErrorApi {
AlreadyExistsApi(super.message);
}
class ConflictApi extends ErrorApi {
ConflictApi(super.message);
}
class NotCreatedApi extends ErrorApi {
NotCreatedApi(super.message);
}
class UpdatePasswordNotFoundApi extends ErrorApi {
UpdatePasswordNotFoundApi(super.message);
}
class TooManyRequestsApi extends ErrorApi {
TooManyRequestsApi(super.message);
}
class PreconditionApi extends ErrorApi {
PreconditionApi(super.message);
}
class PreconditionRequiredApi extends ErrorApi {
PreconditionRequiredApi(super.message);
}
Jhonathan Queiroz |