darto_module 0.0.1 copy "darto_module: ^0.0.1" to clipboard
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

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 #

  1. 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);
  }
}
  1. 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);
}

Made with:

Jhonathan Queiroz

Back to top

1
likes
130
points
12
downloads

Publisher

unverified uploader

Weekly Downloads

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

darto, flutter, injectfy

More

Packages that depend on darto_module