sim_api 0.0.1
sim_api: ^0.0.1 copied to clipboard
This package is particularly useful for Flutter app development but can be integrated into any Dart project where offline API simulation is required. It provides a robust and easy-to-use toolset for e [...]
SimApi #
SimApi is a powerful and flexible library for simulating API responses in Dart. It allows developers to create mock APIs for testing and development purposes without the need for a real backend server.
Features #
- Simulate HTTP methods: GET, POST, PUT, PATCH, DELETE
- Customizable response delay to mimic network latency
- Support for route parameters and query parameters
- Custom route handlers for complex scenarios
- Easy data seeding and management
- Flexible route registration
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
sim_api: ^0.0.1
Then run:
flutter pub get
Usage #
Basic Setup #
import 'package:sim_api/sim_api.dart';
void main() {
final api = SimApi<int>();
// Register routes
api.registerRoute('/users');
api.registerRoute('/users', method: SimApiHttpMethod.get, haveRouteParameters: true);
// Seed some data
api.seedData('/users', {
1: {'id': 1, 'name': 'Alice'},
2: {'id': 2, 'name': 'Bob'},
});
// Use the API
api.get(Uri.parse('/users/$1')).then((response) {
print(response.body);
});
}
Custom Route Handlers #
api.registerRoute('/custom', handler: (data, headers) {
return SimApiHttpResponse.ok({'message': 'Custom response'});
});
Simulating Network Delay #
// Set a global delay
api.delay = 1000; // 1 second delay
// Or use the constructor
final api = SimApi(defaultDelay: 1000);
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.