Skip to content

Commit

Permalink
feat: add ability to execute raw (untyped) requests (#3)
Browse files Browse the repository at this point in the history
* feat: add ability to make raw request

* run format

* don't generate copyWith for NetworkRequestBody

* default to empty body for raw requests
  • Loading branch information
btrautmann authored Jul 18, 2023
1 parent 12ba3aa commit aa9dd16
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 70 deletions.
30 changes: 29 additions & 1 deletion lib/src/network_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,40 @@ class DeleteRequest extends NetworkRequest {
);
}

/// {@template raw_request}
/// A [NetworkRequest] that allows for passing a [NetworkRequestType] (useful
/// for when this can't be known until runtime). It also defaults to a raw
/// (null) body.
/// {@endtemplate}
class RawRequest extends NetworkRequest {
/// {@macro raw_request}
const RawRequest(
String path, {
required super.type,
super.data = const NetworkRequestBody.empty(),
Map<String, dynamic>? queryParameters,
super.shouldTriggerDataMutation = true,
super.options,
super.cancelToken,
super.onReceiveProgress,
super.onSendProgress,
}) : super(
path: path,
queryParams: queryParameters,
);
}

/// The body of a [NetworkRequest].
@freezed
@Freezed(copyWith: false)
class NetworkRequestBody with _$NetworkRequestBody {
/// An empty body. Results in `null` being passed to `data` of the request.
const factory NetworkRequestBody.empty() = _Empty;

/// A JSON body. Passed directly to `data` of the request.
const factory NetworkRequestBody.json(Json data) = _Json;

/// A raw body. Allows for nullable untyped data that is passed directly
/// to `data` of the request, useful for instances where the data type
/// is not known until runtime.
const factory NetworkRequestBody.raw(Object? data) = _Raw;
}
187 changes: 118 additions & 69 deletions lib/src/network_request.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,48 @@ mixin _$NetworkRequestBody {
TResult when<TResult extends Object?>({
required TResult Function() empty,
required TResult Function(Map<String, dynamic> data) json,
required TResult Function(Object? data) raw,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function()? empty,
TResult? Function(Map<String, dynamic> data)? json,
TResult? Function(Object? data)? raw,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function()? empty,
TResult Function(Map<String, dynamic> data)? json,
TResult Function(Object? data)? raw,
required TResult orElse(),
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult map<TResult extends Object?>({
required TResult Function(_Empty value) empty,
required TResult Function(_Json value) json,
required TResult Function(_Raw value) raw,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
TResult? Function(_Empty value)? empty,
TResult? Function(_Json value)? json,
TResult? Function(_Raw value)? raw,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeMap<TResult extends Object?>({
TResult Function(_Empty value)? empty,
TResult Function(_Json value)? json,
TResult Function(_Raw value)? raw,
required TResult orElse(),
}) =>
throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $NetworkRequestBodyCopyWith<$Res> {
factory $NetworkRequestBodyCopyWith(
NetworkRequestBody value, $Res Function(NetworkRequestBody) then) =
_$NetworkRequestBodyCopyWithImpl<$Res, NetworkRequestBody>;
}

/// @nodoc
class _$NetworkRequestBodyCopyWithImpl<$Res, $Val extends NetworkRequestBody>
implements $NetworkRequestBodyCopyWith<$Res> {
_$NetworkRequestBodyCopyWithImpl(this._value, this._then);

// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
}

/// @nodoc
abstract class _$$_EmptyCopyWith<$Res> {
factory _$$_EmptyCopyWith(_$_Empty value, $Res Function(_$_Empty) then) =
__$$_EmptyCopyWithImpl<$Res>;
}

/// @nodoc
class __$$_EmptyCopyWithImpl<$Res>
extends _$NetworkRequestBodyCopyWithImpl<$Res, _$_Empty>
implements _$$_EmptyCopyWith<$Res> {
__$$_EmptyCopyWithImpl(_$_Empty _value, $Res Function(_$_Empty) _then)
: super(_value, _then);
}

/// @nodoc
class _$_Empty implements _Empty {
Expand All @@ -112,6 +86,7 @@ class _$_Empty implements _Empty {
TResult when<TResult extends Object?>({
required TResult Function() empty,
required TResult Function(Map<String, dynamic> data) json,
required TResult Function(Object? data) raw,
}) {
return empty();
}
Expand All @@ -121,6 +96,7 @@ class _$_Empty implements _Empty {
TResult? whenOrNull<TResult extends Object?>({
TResult? Function()? empty,
TResult? Function(Map<String, dynamic> data)? json,
TResult? Function(Object? data)? raw,
}) {
return empty?.call();
}
Expand All @@ -130,6 +106,7 @@ class _$_Empty implements _Empty {
TResult maybeWhen<TResult extends Object?>({
TResult Function()? empty,
TResult Function(Map<String, dynamic> data)? json,
TResult Function(Object? data)? raw,
required TResult orElse(),
}) {
if (empty != null) {
Expand All @@ -143,6 +120,7 @@ class _$_Empty implements _Empty {
TResult map<TResult extends Object?>({
required TResult Function(_Empty value) empty,
required TResult Function(_Json value) json,
required TResult Function(_Raw value) raw,
}) {
return empty(this);
}
Expand All @@ -152,6 +130,7 @@ class _$_Empty implements _Empty {
TResult? mapOrNull<TResult extends Object?>({
TResult? Function(_Empty value)? empty,
TResult? Function(_Json value)? json,
TResult? Function(_Raw value)? raw,
}) {
return empty?.call(this);
}
Expand All @@ -161,6 +140,7 @@ class _$_Empty implements _Empty {
TResult maybeMap<TResult extends Object?>({
TResult Function(_Empty value)? empty,
TResult Function(_Json value)? json,
TResult Function(_Raw value)? raw,
required TResult orElse(),
}) {
if (empty != null) {
Expand All @@ -174,35 +154,6 @@ abstract class _Empty implements NetworkRequestBody {
const factory _Empty() = _$_Empty;
}

/// @nodoc
abstract class _$$_JsonCopyWith<$Res> {
factory _$$_JsonCopyWith(_$_Json value, $Res Function(_$_Json) then) =
__$$_JsonCopyWithImpl<$Res>;
@useResult
$Res call({Map<String, dynamic> data});
}

/// @nodoc
class __$$_JsonCopyWithImpl<$Res>
extends _$NetworkRequestBodyCopyWithImpl<$Res, _$_Json>
implements _$$_JsonCopyWith<$Res> {
__$$_JsonCopyWithImpl(_$_Json _value, $Res Function(_$_Json) _then)
: super(_value, _then);

@pragma('vm:prefer-inline')
@override
$Res call({
Object? data = null,
}) {
return _then(_$_Json(
null == data
? _value._data
: data // ignore: cast_nullable_to_non_nullable
as Map<String, dynamic>,
));
}
}

/// @nodoc
class _$_Json implements _Json {
Expand Down Expand Up @@ -233,17 +184,12 @@ class _$_Json implements _Json {
int get hashCode =>
Object.hash(runtimeType, const DeepCollectionEquality().hash(_data));

@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$_JsonCopyWith<_$_Json> get copyWith =>
__$$_JsonCopyWithImpl<_$_Json>(this, _$identity);

@override
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function() empty,
required TResult Function(Map<String, dynamic> data) json,
required TResult Function(Object? data) raw,
}) {
return json(data);
}
Expand All @@ -253,6 +199,7 @@ class _$_Json implements _Json {
TResult? whenOrNull<TResult extends Object?>({
TResult? Function()? empty,
TResult? Function(Map<String, dynamic> data)? json,
TResult? Function(Object? data)? raw,
}) {
return json?.call(data);
}
Expand All @@ -262,6 +209,7 @@ class _$_Json implements _Json {
TResult maybeWhen<TResult extends Object?>({
TResult Function()? empty,
TResult Function(Map<String, dynamic> data)? json,
TResult Function(Object? data)? raw,
required TResult orElse(),
}) {
if (json != null) {
Expand All @@ -275,6 +223,7 @@ class _$_Json implements _Json {
TResult map<TResult extends Object?>({
required TResult Function(_Empty value) empty,
required TResult Function(_Json value) json,
required TResult Function(_Raw value) raw,
}) {
return json(this);
}
Expand All @@ -284,6 +233,7 @@ class _$_Json implements _Json {
TResult? mapOrNull<TResult extends Object?>({
TResult? Function(_Empty value)? empty,
TResult? Function(_Json value)? json,
TResult? Function(_Raw value)? raw,
}) {
return json?.call(this);
}
Expand All @@ -293,6 +243,7 @@ class _$_Json implements _Json {
TResult maybeMap<TResult extends Object?>({
TResult Function(_Empty value)? empty,
TResult Function(_Json value)? json,
TResult Function(_Raw value)? raw,
required TResult orElse(),
}) {
if (json != null) {
Expand All @@ -306,6 +257,104 @@ abstract class _Json implements NetworkRequestBody {
const factory _Json(final Map<String, dynamic> data) = _$_Json;

Map<String, dynamic> get data;
@JsonKey(ignore: true)
_$$_JsonCopyWith<_$_Json> get copyWith => throw _privateConstructorUsedError;
}

/// @nodoc
class _$_Raw implements _Raw {
const _$_Raw(this.data);

@override
final Object? data;

@override
String toString() {
return 'NetworkRequestBody.raw(data: $data)';
}

@override
bool operator ==(dynamic other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$_Raw &&
const DeepCollectionEquality().equals(other.data, data));
}

@override
int get hashCode =>
Object.hash(runtimeType, const DeepCollectionEquality().hash(data));

@override
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function() empty,
required TResult Function(Map<String, dynamic> data) json,
required TResult Function(Object? data) raw,
}) {
return raw(data);
}

@override
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function()? empty,
TResult? Function(Map<String, dynamic> data)? json,
TResult? Function(Object? data)? raw,
}) {
return raw?.call(data);
}

@override
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function()? empty,
TResult Function(Map<String, dynamic> data)? json,
TResult Function(Object? data)? raw,
required TResult orElse(),
}) {
if (raw != null) {
return raw(data);
}
return orElse();
}

@override
@optionalTypeArgs
TResult map<TResult extends Object?>({
required TResult Function(_Empty value) empty,
required TResult Function(_Json value) json,
required TResult Function(_Raw value) raw,
}) {
return raw(this);
}

@override
@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
TResult? Function(_Empty value)? empty,
TResult? Function(_Json value)? json,
TResult? Function(_Raw value)? raw,
}) {
return raw?.call(this);
}

@override
@optionalTypeArgs
TResult maybeMap<TResult extends Object?>({
TResult Function(_Empty value)? empty,
TResult Function(_Json value)? json,
TResult Function(_Raw value)? raw,
required TResult orElse(),
}) {
if (raw != null) {
return raw(this);
}
return orElse();
}
}

abstract class _Raw implements NetworkRequestBody {
const factory _Raw(final Object? data) = _$_Raw;

Object? get data;
}
1 change: 1 addition & 0 deletions lib/src/sturdy_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class SturdyHttp {
data: request.data.when(
empty: () => null,
json: (json) => json,
raw: (data) => data,
),
queryParameters: request.queryParams,
options: request.options != null
Expand Down

0 comments on commit aa9dd16

Please sign in to comment.