This pure Dart and and dependency-free package provides functionality for working with HTTP status codes in a functional programming style.
Features
- Extends
numtypes with helper functional methods (when, map, maybeWhen, etc.) for handling HTTP status codes. - Provides
StatusCodeenum for easy handling of all IANA registered HTTP status codes (also in functional style). - Provides methods for parsing HTTP status codes from
Stringandnumtypes, and much more.
Getting started
To use this package, add functional_status_codes as a dependency in your pubspec.yaml file.
dependencies:
functional_status_codes: any
Then import the package in your Dart code:
import 'package:functional_status_codes/functional_status_codes.dart';
Usage
Use your favorite HTTP client libraries such as http or dio and take advantage of this package's functional API to easily handle HTTP status codes in your Dart project.
final statusCode = response.statusCode; // int status code of response.
if (!response.statusCode.isStatusCode) return null; // Checks if statusCode >=100 & <600.
final registeredCode = response.statusCode.toRegisteredStatusCode();
// Handle any type of registered status codes via functional `whenOrNull` method:
return registeredCode?.whenOrNull(
okHttp200: () => _parseResponse(response.body),
badRequestHttp400: () => _showError(),
orElse: () => _handleUnknownResponse(response),
);
Additional information
For more information on using this package, check out the API documentation.
If you have any issues or suggestions for the package, please file them in the GitHub repository.
Libraries
- functional_status_codes
- This package provides a functional programming style for working with HTTP status codes.