Skip to content

[http_parser] Add named constructors to MediaType for convenience #1709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pkgs/http_parser/lib/src/media_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,25 @@ class MediaType {
return MediaType(type, subtype, parameters);
});

/// Creates an arbitrary media type.
MediaType(String type, String subtype, [Map<String, String>? parameters])
: type = type.toLowerCase(),
subtype = subtype.toLowerCase(),
parameters = UnmodifiableMapView(
parameters == null ? {} : CaseInsensitiveMap.from(parameters));

/// Creates an `image/*` media type.
MediaType.image({String subtype = '*', Map<String, String>? parameters})
: this('image', subtype, parameters);

/// Creates an `audio/*` media type.
MediaType.audio({String subtype = '*', Map<String, String>? parameters})
: this('audio', subtype, parameters);

/// Creates an `video/*` media type.
MediaType.video({String subtype = '*', Map<String, String>? parameters})
: this('video', subtype, parameters);

/// Returns a copy of this [MediaType] with some fields altered.
///
/// [type] and [subtype] alter the corresponding fields. [mimeType] is parsed
Expand All @@ -90,12 +103,13 @@ class MediaType {
/// [parameters] overwrites and adds to the corresponding field. If
/// [clearParameters] is passed, it replaces the corresponding field entirely
/// instead.
MediaType change(
{String? type,
String? subtype,
String? mimeType,
Map<String, String>? parameters,
bool clearParameters = false}) {
MediaType change({
String? type,
String? subtype,
String? mimeType,
Map<String, String>? parameters,
bool clearParameters = false,
}) {
if (mimeType != null) {
if (type != null) {
throw ArgumentError('You may not pass both [type] and [mimeType].');
Expand Down
Loading