diff --git a/packages/edge_runtime/lib/src/response.dart b/packages/edge_runtime/lib/src/response.dart index 142a69a..e32f159 100644 --- a/packages/edge_runtime/lib/src/response.dart +++ b/packages/edge_runtime/lib/src/response.dart @@ -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, @@ -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; diff --git a/packages/edge_runtime/lib/src/utils.dart b/packages/edge_runtime/lib/src/utils.dart index 43ed505..b254c22 100644 --- a/packages/edge_runtime/lib/src/utils.dart +++ b/packages/edge_runtime/lib/src/utils.dart @@ -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, + _ => throw ArgumentError.value( + body, + 'body', + 'Body must be a nullable instance of [String], [ByteBuffer] or [Uint8List].', + ), + }; } diff --git a/packages/edge_runtime/pubspec.yaml b/packages/edge_runtime/pubspec.yaml index 3f4153c..9c1ae75 100644 --- a/packages/edge_runtime/pubspec.yaml +++ b/packages/edge_runtime/pubspec.yaml @@ -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