From e6fc87277ff451fdaa3b7203c6a1b5add067dd12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Thu, 20 Apr 2023 13:02:24 +0200 Subject: [PATCH 1/6] feat: Refactor Response class and add new initialization methods - Add default response status and status text in the `Response` constructors - Add `Response.jsify` method for initializing `Response` with a JS object as body - Separate `Response.json` method from `Response.jsify` for initializing `Response` with a JSON-encodable object as body - Add documentation for main the constructors of `Response` --- packages/edge_runtime/lib/src/response.dart | 60 +++++++++++++++++---- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/packages/edge_runtime/lib/src/response.dart b/packages/edge_runtime/lib/src/response.dart index 142a69a..ecb5508 100644 --- a/packages/edge_runtime/lib/src/response.dart +++ b/packages/edge_runtime/lib/src/response.dart @@ -9,51 +9,91 @@ 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, + 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 ?? jsUndefined, ), ), From ff897a491985c150f55873247c5694c83f6d4b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Tue, 2 May 2023 10:08:22 +0200 Subject: [PATCH 2/6] change default headers for json constructor to application/json --- packages/edge_runtime/lib/src/response.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/edge_runtime/lib/src/response.dart b/packages/edge_runtime/lib/src/response.dart index ecb5508..f7307fd 100644 --- a/packages/edge_runtime/lib/src/response.dart +++ b/packages/edge_runtime/lib/src/response.dart @@ -94,7 +94,10 @@ class Response implements Body { interop.ResponseInit( status: status, statusText: statusText, - headers: headers?.delegate ?? jsUndefined, + headers: headers?.delegate ?? + Headers({ + 'Content-Type': 'application/json; charset=utf-8', + }), ), ), ); From 7b48e4611672598327fd88fd8276228a75adf647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Tue, 2 May 2023 13:30:48 +0200 Subject: [PATCH 3/6] Update documentation for `jsify` --- packages/edge_runtime/lib/src/response.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/edge_runtime/lib/src/response.dart b/packages/edge_runtime/lib/src/response.dart index f7307fd..f8c7cb5 100644 --- a/packages/edge_runtime/lib/src/response.dart +++ b/packages/edge_runtime/lib/src/response.dart @@ -50,16 +50,18 @@ class Response implements Body { ); } + /// **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. - /// 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]. + /// *Copied from [jsify]*. factory Response.jsify( Object? data, { int status = 200, From f2cac74a625a2bd03858859fae5aeaef7a344028 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gae=CC=88tan?= Date: Tue, 27 Jun 2023 14:19:34 +0200 Subject: [PATCH 4/6] Revert default behavior of Response --- packages/edge_runtime/lib/src/response.dart | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/edge_runtime/lib/src/response.dart b/packages/edge_runtime/lib/src/response.dart index f8c7cb5..e32f159 100644 --- a/packages/edge_runtime/lib/src/response.dart +++ b/packages/edge_runtime/lib/src/response.dart @@ -20,10 +20,14 @@ class Response implements Body { /// 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 = 200, - String statusText = '', + int? status, + String? statusText, Headers? headers, }) : _delegate = interop.Response( convertBody(body), From 701bff66463589640f2927e3a5c0e0f26b735bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gae=CC=88tan?= Date: Tue, 27 Jun 2023 14:32:56 +0200 Subject: [PATCH 5/6] switch on the body in convertBody --- packages/edge_runtime/lib/src/utils.dart | 29 +++++++----------------- packages/edge_runtime/pubspec.yaml | 2 +- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/packages/edge_runtime/lib/src/utils.dart b/packages/edge_runtime/lib/src/utils.dart index 43ed505..84fc5cc 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 From 15d081ac34e73502c0c2c50536621c8a051aa4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Tue, 27 Jun 2023 14:51:23 +0200 Subject: [PATCH 6/6] Update packages/edge_runtime/lib/src/utils.dart Co-authored-by: Remi Rousselet --- packages/edge_runtime/lib/src/utils.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edge_runtime/lib/src/utils.dart b/packages/edge_runtime/lib/src/utils.dart index 84fc5cc..b254c22 100644 --- a/packages/edge_runtime/lib/src/utils.dart +++ b/packages/edge_runtime/lib/src/utils.dart @@ -2,7 +2,7 @@ import 'dart:typed_data' show ByteBuffer, Uint8List; Object? convertBody(Object? body) { return switch (body) { - String || Uint8List || ByteBuffer || null => body, + String() || Uint8List() || ByteBuffer() || null => body, _ => throw ArgumentError.value( body, 'body',