Skip to content
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

feat: Refactor Response class and add new initialization methods #28

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
65 changes: 57 additions & 8 deletions packages/edge_runtime/lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import 'blob.dart';
import 'body.dart';
import 'form_data.dart';
import 'headers.dart';
import 'interop/headers.dart' as headers_interop;
import 'interop/readable_stream.dart';
import 'interop/utils_interop.dart';
import 'interop/headers.dart' as headers_interop;

class Response implements Body {
final interop.Response _delegate;

Response._(this._delegate);

/// Creates a new [Response] object.
/// The [body] can be a [String], [ByteBuffer] or [Uint8List].
///
/// [status] is an HTTP status code and defaults to `200`.
/// [statusText] is a status message and defaults to `''`.
/// [headers] can be used to set HTTP headers, defaults to providing no headers.
Response(
Object? body, {
int? status,
Expand All @@ -26,40 +32,83 @@ class Response implements Body {
}) : _delegate = interop.Response(
convertBody(body),
interop.ResponseInit(
status: status ?? 200,
statusText: statusText ?? '',
status: status,
statusText: statusText,
headers: headers?.delegate ?? jsUndefined,
),
);

/// Creates a new [Response] object with an error.
///
/// See also https://developer.mozilla.org/en-US/docs/Web/API/Response/error.
factory Response.error() {
return Response._(interop.Response.error());
}

/// Creates a new [Response] object with a redirect to the given [url].
///
/// See also https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect.
factory Response.redirect(Uri url, [int? status = 302]) {
return Response._(
interop.Response.redirect(url.toString(), status),
);
}

factory Response.json(
/// **Warning:** It is recommended to use [Response.json] instead
/// as this method is less efficient and the response data can become minified
/// if you enable minification in your build.
///
/// Creates a new [Response] object with a JS object as the body.
/// Recursively converts a JSON-like collection to JavaScript compatible representation.
/// The data must be a [Map] or [Iterable], the contents of which are also deeply converted.
/// Maps are converted into JavaScript objects. Iterables are converted into arrays.
/// Strings, numbers, bools, and @JS() annotated objects are passed through unmodified.
/// Dart objects are also passed through unmodified, but their members aren't usable from JavaScript.
///
/// *Copied from [jsify]*.
factory Response.jsify(
Object? data, {
int? status,
String? statusText,
int status = 200,
String statusText = '',
Headers? headers,
}) {
return Response._(
interop.Response.json(
data != null ? jsify(data) : null,
interop.ResponseInit(
status: status ?? 200,
statusText: statusText ?? '',
status: status,
statusText: statusText,
headers: headers?.delegate ?? jsUndefined,
),
),
);
}

/// Creates a new [Response] object with a JSON string as the body.
/// [data] is converted to a JSON string using [jsonEncode].
///
/// If you are using a JS object as the body, use [Response.jsify] instead.
factory Response.json(
Object? data, {
int status = 200,
String statusText = '',
Headers? headers,
}) {
return Response._(
interop.Response(
data != null ? jsonEncode(data) : null,
interop.ResponseInit(
status: status,
statusText: statusText,
headers: headers?.delegate ??
Headers({
'Content-Type': 'application/json; charset=utf-8',
}),
),
),
);
}

interop.ResponseType get type => _delegate.type;
Uri get url => Uri.parse(_delegate.url);
bool get redirected => _delegate.redirected;
Expand Down
29 changes: 8 additions & 21 deletions packages/edge_runtime/lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import 'dart:typed_data' show ByteBuffer, Uint8List;

Object? convertBody(Object? body) {
if (body == null) {
return null;
}

if (body is String) {
return body;
}

if (body is Uint8List) {
return body;
}

if (body is ByteBuffer) {
return body;
}

throw ArgumentError.value(
body,
'body',
'Body must be an nullable instance of [String], [ByteBuffer] or [Uint8List].',
);
return switch (body) {
String || Uint8List || ByteBuffer || null => body,
gaetschwartz marked this conversation as resolved.
Show resolved Hide resolved
_ => throw ArgumentError.value(
body,
'body',
'Body must be a nullable instance of [String], [ByteBuffer] or [Uint8List].',
),
};
}
2 changes: 1 addition & 1 deletion packages/edge_runtime/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repository: https://github.com/invertase/dart_edge/tree/main/packages/edge_runti
version: 0.0.4+1

environment:
sdk: ">=2.18.5 <3.0.0"
sdk: ">=3.0.0 <4.0.0"

dependencies:
freezed_annotation: ^2.2.0
Expand Down