Skip to content

Commit

Permalink
Export MediaType from http_parser (#2205)
Browse files Browse the repository at this point in the history
Resolves #2212

### Additional context and info (if any)

Exports `DioMediaType` as the alias of `MediaType` from the `http_parser` package so users don't have to import the transitive dependency explicitly.

---------

Signed-off-by: Benjamin <[email protected]>
Co-authored-by: Alex Li <[email protected]>
  • Loading branch information
Reprevise and AlexV525 authored May 22, 2024
1 parent 0eab30a commit f87a095
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ See the [Migration Guide][] for the complete breaking changes list.**

- Raise the min Dart SDK version to 2.18.0.
- Add constructor for `DioExceptionType.badCertificate`.
- Create type alias `DioMediaType` for `http_parser`'s `MediaType`.

## 5.4.3+1

Expand Down
17 changes: 10 additions & 7 deletions dio/lib/src/multipart_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import 'multipart_file/io_multipart_file.dart'
if (dart.library.html) 'multipart_file/browser_multipart_file.dart';
import 'utils.dart';

/// The type (alias) for specifying the content-type of the `MultipartFile`.
typedef DioMediaType = MediaType;

/// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to
/// correspond to a physical file.
///
Expand All @@ -27,7 +30,7 @@ class MultipartFile {
Stream<List<int>> stream,
this.length, {
this.filename,
MediaType? contentType,
DioMediaType? contentType,
Map<String, List<String>>? headers,
}) : _data = (() => stream),
headers = caseInsensitiveKeyMap(headers),
Expand All @@ -43,7 +46,7 @@ class MultipartFile {
Stream<List<int>> Function() data,
this.length, {
this.filename,
MediaType? contentType,
DioMediaType? contentType,
Map<String, List<String>>? headers,
}) : _data = data,
headers = caseInsensitiveKeyMap(headers),
Expand All @@ -56,7 +59,7 @@ class MultipartFile {
factory MultipartFile.fromBytes(
List<int> value, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) {
return MultipartFile.fromStream(
Expand All @@ -77,7 +80,7 @@ class MultipartFile {
factory MultipartFile.fromString(
String value, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) {
contentType ??= MediaType('text', 'plain');
Expand Down Expand Up @@ -106,7 +109,7 @@ class MultipartFile {
final Map<String, List<String>>? headers;

/// The content-type of the file. Defaults to `application/octet-stream`.
final MediaType? contentType;
final DioMediaType? contentType;

/// The stream builder that will emit the file's contents for every call.
final Stream<List<int>> Function() _data;
Expand All @@ -125,7 +128,7 @@ class MultipartFile {
static Future<MultipartFile> fromFile(
String filePath, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) =>
multipartFileFromPath(
Expand All @@ -138,7 +141,7 @@ class MultipartFile {
static MultipartFile fromFileSync(
String filePath, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) =>
multipartFileFromPathSync(
Expand Down
5 changes: 2 additions & 3 deletions dio/lib/src/multipart_file/io_multipart_file.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'dart:async';
import 'dart:io';

import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart' as p;

import '../multipart_file.dart';

Future<MultipartFile> multipartFileFromPath(
String filePath, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) async {
filename ??= p.basename(filePath);
Expand All @@ -27,7 +26,7 @@ Future<MultipartFile> multipartFileFromPath(
MultipartFile multipartFileFromPathSync(
String filePath, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) {
filename ??= p.basename(filePath);
Expand Down
7 changes: 3 additions & 4 deletions dio/test/formdata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';

import 'mock/adapters.dart';
Expand Down Expand Up @@ -36,7 +35,7 @@ void main() async {
headers: {
'test': <String>['c'],
},
contentType: MediaType.parse('text/plain'),
contentType: DioMediaType.parse('text/plain'),
),
],
});
Expand Down Expand Up @@ -88,7 +87,7 @@ void main() async {
headers: {
'test': <String>['c'],
},
contentType: MediaType.parse('text/plain'),
contentType: DioMediaType.parse('text/plain'),
),
),
);
Expand Down Expand Up @@ -124,7 +123,7 @@ void main() async {
headers: {
'test': <String>['c'],
},
contentType: MediaType.parse('text/plain'),
contentType: DioMediaType.parse('text/plain'),
),
],
});
Expand Down
3 changes: 1 addition & 2 deletions dio/test/multipart_file_test.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';

void main() async {
group(MultipartFile, () {
test(
'fromFile sets correct content-type',
() async {
final mediaType = MediaType.parse('text/plain');
final mediaType = DioMediaType.parse('text/plain');
final file = await MultipartFile.fromFile(
'test/mock/_testfile',
filename: '1.txt',
Expand Down

0 comments on commit f87a095

Please sign in to comment.