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
Changes from 2 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
63 changes: 53 additions & 10 deletions packages/edge_runtime/lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,100 @@ 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].
Response(
Object? body, {
int? status,
String? statusText,
int status = 200,
gaetschwartz marked this conversation as resolved.
Show resolved Hide resolved
String statusText = '',
Headers? headers,
}) : _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(
/// Creates a new [Response] object with a JS object as the body.
/// Recursively converts a JSON-like collection to JavaScript compatible representation.
/// It is recommended to use [Response.json] instead as it is more efficient.
///
/// 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